Passed
Push — main ( faf76d...59f012 )
by Jean-Christophe
02:21
created

JSX   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 98.25%

Importance

Changes 9
Bugs 1 Features 2
Metric Value
eloc 75
c 9
b 1
f 2
dl 0
loc 114
ccs 56
cts 57
cp 0.9825
rs 10
wmc 26

7 Methods

Rating   Name   Duplication   Size   Complexity  
A cleanJSONFunctions() 0 5 1
A toJs() 0 5 1
A nodeToJs() 0 22 6
A getName() 0 2 1
A hasBraces() 0 2 2
A getChildrenStr() 0 21 6
B parseTextNode() 0 17 9
1
<?php
2
namespace PHPMV\utils;
3
4
use PHPMV\js\JavascriptUtils;
5
use PHPMV\react\ReactJS;
6
7
/**
8
 * PHPMV\utils$JSX
9
 * This class is part of Ubiquity
10
 *
11
 * @author jc
12
 * @version 1.0.0
13
 *
14
 */
15
class JSX {
16
17
	public static $reactCreateElement = 'React.createElement';
18
19
	private static $jsDetect = [
20
		'onBlur' => 0,
21
		'onChange' => 0,
22
		'onDblclick' => 0,
23
		'onClick' => 0,
24
		'onSubmit' => 0,
25
		'value' => 0,
26
		'items' => 0,
27
		'defaultValue' => 0,
28
		'dangerouslySetInnerHTML' => 0
29
	];
30
31 3
	private static function getName(string $name, ReactJS $react): string {
32 3
		return $react->components[$name] ?? '"' . $name . '"';
33
	}
34
35
	private static $attributes = [
36
		'classname' => 'className',
37
		'onblur' => 'onBlur',
38
		'onclick' => 'onClick',
39
		'onchange' => 'onChange',
40
		'onsubmit' => 'onSubmit',
41
		'defaultvalue' => 'defaultValue',
42
		'dangerouslysetinnerhtml' => 'dangerouslySetInnerHTML',
43
		'htmlfor' => 'htmlFor'
44
	];
45
46 3
	private static function cleanJSONFunctions(string $json): string {
47 3
		return \str_replace([
48 3
			'"!!%',
49
			'%!!"'
50 3
		], '', $json);
51
	}
52
53 3
	private static function hasBraces(string $str): bool {
54 3
		return (\substr($str, 0, 1) === '{' && \substr($str, - 1) === '}');
55
	}
56
57 3
	private static function nodeToJs(\DOMNode $root, ?ReactJS $react): string {
58 3
		$attributes = [];
59 3
		$name = $root->nodeName;
60
61 3
		if ($root->hasAttributes()) {
62 3
			$attrs = $root->attributes;
63
64 3
			foreach ($attrs as $i => $attr) {
65 3
				$attrName = self::$attributes[$attr->name] ?? $attr->name;
66 3
				$attrValue = $attr->value;
67 3
				if (isset(self::$jsDetect[$attrName])) {
68 2
					if (self::hasBraces($attrValue)) {
69 2
						$attrValue = \substr($attrValue, 1, - 1);
70
					}
71 2
					$attributes[$attrName] = '!!%' . $attrValue . '%!!';
72
				} else {
73 3
					$attributes[$attrName] = $attrValue;
74
				}
75
			}
76
		}
77 3
		$childrenStr = self::getChildrenStr($root, $react);
78 3
		return self::$reactCreateElement . "(" . ((isset($react)) ? self::getName($name, $react) : $name) . "," . self::cleanJSONFunctions(JavascriptUtils::toJSON($attributes)) . "$childrenStr)";
79
	}
80
81 3
	private static function getChildrenStr(\DOMNode $root, ?ReactJS $react): string {
82 3
		$children = [];
83
84 3
		$childNodes = $root->childNodes;
85 3
		$open = null;
86 3
		for ($i = 0; $i < $childNodes->length; $i ++) {
87 3
			$child = $childNodes->item($i);
88 3
			if ($child->nodeType == XML_TEXT_NODE) {
89 3
				$v = \trim($child->nodeValue);
90 3
				if ($v != null) {
91 3
					self::parseTextNode($v, $children, $open);
92
				}
93
			} else {
94 2
				if ($open != '') {
95 1
					$open .= self::nodeToJs($child, $react);
96
				} else {
97 2
					$children[] = self::nodeToJs($child, $react);
98
				}
99
			}
100
		}
101 3
		return (count($children) > 0) ? (',' . implode(',', $children)) : '';
102
	}
103
104 3
	private static function parseTextNode(string $v, array &$children, ?string &$open) {
105 3
		$parts = \preg_split('@(\{.*?\})@', $v, null, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
106 3
		if ($parts && \count($parts) > 0) {
107 3
			foreach ($parts as $ev) {
108 3
				if (self::hasBraces($ev)) {
109 2
					$children[] = \substr($ev, 1, - 1);
110 3
				} elseif (\substr($ev, 0, 1) === '{') {
111 1
					$open = \substr($ev, 1);
112 3
				} elseif ($open != '' && \substr($ev, - 1) === '}') {
113 1
					$children[] = $open . \substr($ev, 0, - 1);
114 1
					$open = '';
115 3
				} elseif (\trim($ev) != null) {
116 3
					$children[] = '"' . $ev . '"';
117
				}
118
			}
119
		} else {
120
			$children[] = "`$v`";
121
		}
122 3
	}
123
124 3
	public static function toJs(string $html, ?ReactJS $react = null): string {
125 3
		\libxml_use_internal_errors(true);
126 3
		$dom = new \DOMDocument('1.0', 'UTF-8');
127 3
		$dom->loadHTML(\mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
128 3
		return self::nodeToJs($dom->documentElement, $react);
129
	}
130
}
131