}
}
$buf .= '>';
+ $buf .= $this->convertArraysToHtml($children);
+ $buf .= '</' . $tag . '>';
+ return $buf;
+ }
+
+ public function convertArraysToHtml($children) {
+ $buf = '';
foreach ($children as $child) {
if (is_string($child)) {
}
}
- $buf .= '</' . $tag . '>';
return $buf;
}
foreach ($doc->childNodes as $htmlNode) {
if ($htmlNode instanceof DOMElement && $htmlNode->tagName === 'html') {
- return $this->convertNodeToArray($htmlNode->firstChild->firstChild);
+ return $this->convertNodesToArray($htmlNode->firstChild->childNodes);
}
}
$type = $this->pickAttrType($node->tagName, $attribute->name);
$arr[$attribute->name] = $this->decodeAttrValue($type, $txt);
}
- foreach ($node->childNodes as $childNode) {
- $arr['#children'][] = $this->convertNodeToArray($childNode);
+ if ($node->childNodes->length > 0) {
+ $arr['#children'] = $this->convertNodesToArray($node->childNodes);
}
return $arr;
}
}
}
+ /**
+ * @param array|DOMNodeList $nodes
+ * List of DOMNodes
+ * @return array
+ */
+ protected function convertNodesToArray($nodes) {
+ $children = [];
+ foreach ($nodes as $childNode) {
+ $children[] = $this->convertNodeToArray($childNode);
+ }
+ return $children;
+ }
+
/**
* Determine the type of data that is stored in an attribute.
*
* Parsed summary of the entities used in a given form.
*/
public static function create($layout) {
- $entities = array_column(AHQ::getTags($layout, 'af-entity'), NULL, 'name');
+ $root = [
+ '#tag' => 'root',
+ '#children' => $layout,
+ ];
+ $entities = array_column(AHQ::getTags($root, 'af-entity'), NULL, 'name');
foreach (array_keys($entities) as $entity) {
$entities[$entity]['fields'] = [];
}
- self::parseFields($layout, $entities);
+ self::parseFields($root, $entities);
$self = new static();
$self->entities = $entities;
elseif ($child['#tag'] == 'af-fieldset' && !empty($child['#children'])) {
$entities[$child['model']]['fields'] = array_merge($entities[$child['model']]['fields'] ?? [], AHQ::getTags($child, 'af-field'));
}
- elseif (!empty($child['#children'])) {
- self::parseFields($child['#children'], $entities);
+ else {
+ self::parseFields($child, $entities);
}
}
}
return $mixed;
}
$converter = new \CRM_Afform_ArrayHtml($this->layoutFormat !== 'shallow');
- return $converter->convertArrayToHtml($mixed);
+ return $converter->convertArraysToHtml($mixed);
}
}
public function getFormatExamples() {
$es = [];
- foreach (['apple', 'banana'] as $exampleName) {
+ foreach (['empty', 'string', 'apple', 'banana', 'cherry'] as $exampleName) {
$exampleFile = '/formatExamples/' . $exampleName . '.php';
$example = require __DIR__ . $exampleFile;
$formats = ['html', 'shallow', 'deep'];
return [
'html' => '<strong>New text!</strong>',
- 'shallow' => ['#tag' => 'strong', '#children' => ['New text!']],
- 'deep' => ['#tag' => 'strong', '#children' => ['New text!']],
+ 'shallow' => [
+ ['#tag' => 'strong', '#children' => ['New text!']],
+ ],
+ 'deep' => [
+ ['#tag' => 'strong', '#children' => ['New text!']],
+ ],
];
return [
'html' => '<div><strong>New text!</strong><af-field name="do_not_sms" defn="{label: \'Do not do any of the emailing\'}"></af-field></div>',
'shallow' => [
- '#tag' => 'div',
- '#children' => [
- ['#tag' => 'strong', '#children' => ['New text!']],
- ['#tag' => 'af-field', 'name' => 'do_not_sms', 'defn' => "{label: 'Do not do any of the emailing'}"],
+ [
+ '#tag' => 'div',
+ '#children' => [
+ ['#tag' => 'strong', '#children' => ['New text!']],
+ ['#tag' => 'af-field', 'name' => 'do_not_sms', 'defn' => "{label: 'Do not do any of the emailing'}"],
+ ],
],
],
'deep' => [
- '#tag' => 'div',
- '#children' => [
- ['#tag' => 'strong', '#children' => ['New text!']],
- ['#tag' => 'af-field', 'name' => 'do_not_sms', 'defn' => ['label' => 'Do not do any of the emailing']],
+ [
+ '#tag' => 'div',
+ '#children' => [
+ ['#tag' => 'strong', '#children' => ['New text!']],
+ ['#tag' => 'af-field', 'name' => 'do_not_sms', 'defn' => ['label' => 'Do not do any of the emailing']],
+ ],
],
],
];
--- /dev/null
+<?php
+
+return [
+ 'html' => '<span>First</span> <span>Second</span>',
+ 'shallow' => [
+ ['#tag' => 'span', '#children' => ['First']],
+ ' ',
+ ['#tag' => 'span', '#children' => ['Second']],
+ ],
+ 'deep' => [
+ ['#tag' => 'span', '#children' => ['First']],
+ ' ',
+ ['#tag' => 'span', '#children' => ['Second']],
+ ],
+];
--- /dev/null
+<?php
+
+return [
+ 'html' => '',
+ 'shallow' => [],
+ 'deep' => [],
+];
--- /dev/null
+<?php
+
+return [
+ 'html' => 'hello world',
+ 'shallow' => ['hello world'],
+ 'deep' => ['hello world'],
+];