From f800b0461083e09328da8cc5e2b2443a5874998a Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 25 Oct 2019 17:14:30 -0700 Subject: [PATCH] (BC Break) Change shallow+deep formats to begin with an array of elements Before: In the shallow+deep representations of `layout`, the root array has a `#tag` and `#children`. After: The root array is just list a list of children. Comment: This is a more accufrate representation of how HTML partials are often written. --- ext/afform/core/CRM/Afform/ArrayHtml.php | 27 ++++++++++++++++--- ext/afform/core/Civi/Afform/FormDataModel.php | 12 ++++++--- .../Civi/Api4/Utils/AfformFormatTrait.php | 2 +- .../mock/tests/phpunit/api/v4/AfformTest.php | 2 +- .../phpunit/api/v4/formatExamples/apple.php | 8 ++++-- .../phpunit/api/v4/formatExamples/banana.php | 20 ++++++++------ .../phpunit/api/v4/formatExamples/cherry.php | 15 +++++++++++ .../phpunit/api/v4/formatExamples/empty.php | 7 +++++ .../phpunit/api/v4/formatExamples/string.php | 7 +++++ 9 files changed, 80 insertions(+), 20 deletions(-) create mode 100644 ext/afform/mock/tests/phpunit/api/v4/formatExamples/cherry.php create mode 100644 ext/afform/mock/tests/phpunit/api/v4/formatExamples/empty.php create mode 100644 ext/afform/mock/tests/phpunit/api/v4/formatExamples/string.php diff --git a/ext/afform/core/CRM/Afform/ArrayHtml.php b/ext/afform/core/CRM/Afform/ArrayHtml.php index 1839271f16..a3086c514a 100644 --- a/ext/afform/core/CRM/Afform/ArrayHtml.php +++ b/ext/afform/core/CRM/Afform/ArrayHtml.php @@ -86,6 +86,13 @@ class CRM_Afform_ArrayHtml { } } $buf .= '>'; + $buf .= $this->convertArraysToHtml($children); + $buf .= ''; + return $buf; + } + + public function convertArraysToHtml($children) { + $buf = ''; foreach ($children as $child) { if (is_string($child)) { @@ -96,7 +103,6 @@ class CRM_Afform_ArrayHtml { } } - $buf .= ''; return $buf; } @@ -118,7 +124,7 @@ class CRM_Afform_ArrayHtml { foreach ($doc->childNodes as $htmlNode) { if ($htmlNode instanceof DOMElement && $htmlNode->tagName === 'html') { - return $this->convertNodeToArray($htmlNode->firstChild->firstChild); + return $this->convertNodesToArray($htmlNode->firstChild->childNodes); } } @@ -138,8 +144,8 @@ class CRM_Afform_ArrayHtml { $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; } @@ -154,6 +160,19 @@ class CRM_Afform_ArrayHtml { } } + /** + * @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. * diff --git a/ext/afform/core/Civi/Afform/FormDataModel.php b/ext/afform/core/Civi/Afform/FormDataModel.php index 03a1413a41..8b783f4bfc 100644 --- a/ext/afform/core/Civi/Afform/FormDataModel.php +++ b/ext/afform/core/Civi/Afform/FormDataModel.php @@ -26,11 +26,15 @@ class FormDataModel { * 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; @@ -56,8 +60,8 @@ class FormDataModel { 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); } } } diff --git a/ext/afform/core/Civi/Api4/Utils/AfformFormatTrait.php b/ext/afform/core/Civi/Api4/Utils/AfformFormatTrait.php index 072528608b..7d224a657f 100644 --- a/ext/afform/core/Civi/Api4/Utils/AfformFormatTrait.php +++ b/ext/afform/core/Civi/Api4/Utils/AfformFormatTrait.php @@ -39,7 +39,7 @@ trait AfformFormatTrait { return $mixed; } $converter = new \CRM_Afform_ArrayHtml($this->layoutFormat !== 'shallow'); - return $converter->convertArrayToHtml($mixed); + return $converter->convertArraysToHtml($mixed); } } diff --git a/ext/afform/mock/tests/phpunit/api/v4/AfformTest.php b/ext/afform/mock/tests/phpunit/api/v4/AfformTest.php index 538e977e5e..606ac9c29a 100644 --- a/ext/afform/mock/tests/phpunit/api/v4/AfformTest.php +++ b/ext/afform/mock/tests/phpunit/api/v4/AfformTest.php @@ -65,7 +65,7 @@ class api_v4_AfformTest extends api_v4_AfformTestCase { 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']; diff --git a/ext/afform/mock/tests/phpunit/api/v4/formatExamples/apple.php b/ext/afform/mock/tests/phpunit/api/v4/formatExamples/apple.php index e5e41acd24..dc37c5024b 100644 --- a/ext/afform/mock/tests/phpunit/api/v4/formatExamples/apple.php +++ b/ext/afform/mock/tests/phpunit/api/v4/formatExamples/apple.php @@ -2,6 +2,10 @@ return [ 'html' => 'New text!', - 'shallow' => ['#tag' => 'strong', '#children' => ['New text!']], - 'deep' => ['#tag' => 'strong', '#children' => ['New text!']], + 'shallow' => [ + ['#tag' => 'strong', '#children' => ['New text!']], + ], + 'deep' => [ + ['#tag' => 'strong', '#children' => ['New text!']], + ], ]; diff --git a/ext/afform/mock/tests/phpunit/api/v4/formatExamples/banana.php b/ext/afform/mock/tests/phpunit/api/v4/formatExamples/banana.php index af5c134adf..779b3cd446 100644 --- a/ext/afform/mock/tests/phpunit/api/v4/formatExamples/banana.php +++ b/ext/afform/mock/tests/phpunit/api/v4/formatExamples/banana.php @@ -3,17 +3,21 @@ return [ 'html' => '
New text!
', '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']], + ], ], ], ]; diff --git a/ext/afform/mock/tests/phpunit/api/v4/formatExamples/cherry.php b/ext/afform/mock/tests/phpunit/api/v4/formatExamples/cherry.php new file mode 100644 index 0000000000..879329bce3 --- /dev/null +++ b/ext/afform/mock/tests/phpunit/api/v4/formatExamples/cherry.php @@ -0,0 +1,15 @@ + 'First Second', + 'shallow' => [ + ['#tag' => 'span', '#children' => ['First']], + ' ', + ['#tag' => 'span', '#children' => ['Second']], + ], + 'deep' => [ + ['#tag' => 'span', '#children' => ['First']], + ' ', + ['#tag' => 'span', '#children' => ['Second']], + ], +]; diff --git a/ext/afform/mock/tests/phpunit/api/v4/formatExamples/empty.php b/ext/afform/mock/tests/phpunit/api/v4/formatExamples/empty.php new file mode 100644 index 0000000000..e08233a2d0 --- /dev/null +++ b/ext/afform/mock/tests/phpunit/api/v4/formatExamples/empty.php @@ -0,0 +1,7 @@ + '', + 'shallow' => [], + 'deep' => [], +]; diff --git a/ext/afform/mock/tests/phpunit/api/v4/formatExamples/string.php b/ext/afform/mock/tests/phpunit/api/v4/formatExamples/string.php new file mode 100644 index 0000000000..18ccc7f487 --- /dev/null +++ b/ext/afform/mock/tests/phpunit/api/v4/formatExamples/string.php @@ -0,0 +1,7 @@ + 'hello world', + 'shallow' => ['hello world'], + 'deep' => ['hello world'], +]; -- 2.25.1