Skip to content

Commit 00f1df8

Browse files
committed
add child JSX in text nodes
1 parent 6fd69f2 commit 00f1df8

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

src/PHPMV/utils/JSX.php

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ class JSX {
2121
'onChange' => 0,
2222
'onDblclick' => 0,
2323
'onClick' => 0,
24-
'value' => 0
24+
'onSubmit' => 0,
25+
'value' => 0,
26+
'items' => 0
2527
];
2628

2729
private static function getName(string $name, ReactJS $react): string {
@@ -32,7 +34,8 @@ private static function getName(string $name, ReactJS $react): string {
3234
'classname' => 'className',
3335
'onblur' => 'onBlur',
3436
'onclick' => 'onClick',
35-
'onchange' => 'onChange'
37+
'onchange' => 'onChange',
38+
'onsubmit' => 'onSubmit'
3639
];
3740

3841
private static function cleanJSONFunctions(string $json): string {
@@ -74,32 +77,45 @@ private static function getChildrenStr(\DOMNode $root, ?ReactJS $react): string
7477
$children = [];
7578

7679
$childNodes = $root->childNodes;
77-
80+
$open = null;
7881
for ($i = 0; $i < $childNodes->length; $i ++) {
7982
$child = $childNodes->item($i);
8083
if ($child->nodeType == XML_TEXT_NODE) {
8184
$v = \trim($child->nodeValue);
8285
if ($v != null) {
83-
$parts = \preg_split('@(\{.*?\})@', $v, null, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
84-
if (\count($parts) > 0) {
85-
foreach ($parts as $ev) {
86-
if (self::hasBraces($ev)) {
87-
$children[] = \substr($ev, 1, - 1);
88-
} elseif (\trim($ev) != null) {
89-
$children[] = '"' . $ev . '"';
90-
}
91-
}
92-
} else {
93-
$children[] = "`$v`";
94-
}
86+
self::parseTextNode($v, $children, $open);
9587
}
9688
} else {
97-
$children[] = self::nodeToJs($child, $react);
89+
if ($open != '') {
90+
$open .= self::nodeToJs($child, $react);
91+
} else {
92+
$children[] = self::nodeToJs($child, $react);
93+
}
9894
}
9995
}
10096
return (count($children) > 0) ? (',' . implode(',', $children)) : '';
10197
}
10298

99+
private static function parseTextNode(string $v, array &$children, ?string &$open) {
100+
$parts = \preg_split('@(\{.*?\})@', $v, null, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
101+
if (\count($parts) > 0) {
102+
foreach ($parts as $ev) {
103+
if (self::hasBraces($ev)) {
104+
$children[] = \substr($ev, 1, - 1);
105+
} elseif (\substr($ev, 0, 1) === '{') {
106+
$open = \substr($ev, 1);
107+
} elseif ($open != '' && \substr($ev, - 1) === '}') {
108+
$children[] = $open . \substr($ev, 0, - 1);
109+
$open = '';
110+
} elseif (\trim($ev) != null) {
111+
$children[] = '"' . $ev . '"';
112+
}
113+
}
114+
} else {
115+
$children[] = "`$v`";
116+
}
117+
}
118+
103119
public static function toJs(string $html, ?ReactJS $react = null): string {
104120
\libxml_use_internal_errors(true);
105121
$dom = new \DOMDocument('1.0', 'UTF-8');

0 commit comments

Comments
 (0)