ArrayHtml - Change text node format from string to array
authorColeman Watts <coleman@civicrm.org>
Wed, 30 Oct 2019 20:53:19 +0000 (16:53 -0400)
committerCiviCRM <info@civicrm.org>
Wed, 16 Sep 2020 02:13:19 +0000 (19:13 -0700)
Everything in the arrayHtml is associative exept text nodes were just strings.
That made it difficult to loop through the whole thing in javascript, so
this updates the format to be consistent with everything else.

ext/afform/core/CRM/Afform/ArrayHtml.php
ext/afform/mock/tests/phpunit/api/v4/formatExamples/apple.php
ext/afform/mock/tests/phpunit/api/v4/formatExamples/banana.php
ext/afform/mock/tests/phpunit/api/v4/formatExamples/cherry.php
ext/afform/mock/tests/phpunit/api/v4/formatExamples/comments.php
ext/afform/mock/tests/phpunit/api/v4/formatExamples/string.php

index 6f511792625903477cbac105b23505868f7c4913..99fd3221b7b1b8de3c773e286f6b8d2b7234f31a 100644 (file)
@@ -86,6 +86,10 @@ class CRM_Afform_ArrayHtml {
       return sprintf('<!--%s-->', $array['#comment']);
     }
 
+    if (isset($array['#text'])) {
+      return $array['#text'];
+    }
+
     $tag = empty($array['#tag']) ? self::DEFAULT_TAG : $array['#tag'];
     unset($array['#tag']);
     $children = empty($array['#children']) ? [] : $array['#children'];
@@ -183,7 +187,7 @@ class CRM_Afform_ArrayHtml {
       return $arr;
     }
     elseif ($node instanceof DOMText) {
-      return $node->textContent;
+      return ['#text' => $node->textContent];
     }
     elseif ($node instanceof DOMComment) {
       $arr = ['#comment' => $node->nodeValue];
index dc37c5024bd876dfff32bae3962d2431d025cdad..28d8ba4be2f6a69fa70785806e34a3d72237ac23 100644 (file)
@@ -3,9 +3,9 @@
 return [
   'html' => '<strong>New text!</strong>',
   'shallow' => [
-    ['#tag' => 'strong', '#children' => ['New text!']],
+    ['#tag' => 'strong', '#children' => [['#text' => 'New text!']]],
   ],
   'deep' => [
-    ['#tag' => 'strong', '#children' => ['New text!']],
+    ['#tag' => 'strong', '#children' => [['#text' => 'New text!']]],
   ],
 ];
index bf33a0a21f0c4e75c4ad354d5d8449376f763194..7e0a134caaa0032a3512d5b9ca2e7e619082b847 100644 (file)
@@ -6,7 +6,7 @@ return [
     [
       '#tag' => 'div',
       '#children' => [
-        ['#tag' => 'strong', '#children' => ['New text!']],
+        ['#tag' => 'strong', '#children' => [['#text' => 'New text!']]],
         ['#tag' => 'af-field', 'name' => 'do_not_sms', 'defn' => "{label: 'Do not do any of the emailing'}"],
       ],
     ],
@@ -15,7 +15,7 @@ return [
     [
       '#tag' => 'div',
       '#children' => [
-        ['#tag' => 'strong', '#children' => ['New text!']],
+        ['#tag' => 'strong', '#children' => [['#text' => 'New text!']]],
         ['#tag' => 'af-field', 'name' => 'do_not_sms', 'defn' => ['label' => 'Do not do any of the emailing']],
       ],
     ],
index 879329bce31e3295c6f9d7c6977fa576b631ac51..f47efed0e462f7e7abf6d653cb90ad38cd748ef9 100644 (file)
@@ -3,13 +3,13 @@
 return [
   'html' => '<span>First</span> <span>Second</span>',
   'shallow' => [
-    ['#tag' => 'span', '#children' => ['First']],
-    ' ',
-    ['#tag' => 'span', '#children' => ['Second']],
+    ['#tag' => 'span', '#children' => [['#text' => 'First']]],
+    ['#text' => ' '],
+    ['#tag' => 'span', '#children' => [['#text' => 'Second']]],
   ],
   'deep' => [
-    ['#tag' => 'span', '#children' => ['First']],
-    ' ',
-    ['#tag' => 'span', '#children' => ['Second']],
+    ['#tag' => 'span', '#children' => [['#text' => 'First']]],
+    ['#text' => ' '],
+    ['#tag' => 'span', '#children' => [['#text' => 'Second']]],
   ],
 ];
index 248cffddb34cebef36591aa261e0a31187d67aaf..c311e930e67ffd258b6be64b33136e7bb4737329 100644 (file)
@@ -6,11 +6,11 @@ return [
     [
       '#tag' => 'div',
       '#children' => [
-        'One',
+        ['#text' => 'One'],
         ['#comment' => ' uno '],
-        ' Two ',
+        ['#text' => ' Two '],
         ['#comment' => 'dos & so on '],
-        ' Three',
+        ['#text' => ' Three'],
       ],
     ],
     ['#comment' => 'tres-a--b---c'],
@@ -19,11 +19,11 @@ return [
     [
       '#tag' => 'div',
       '#children' => [
-        'One',
+        ['#text' => 'One'],
         ['#comment' => ' uno '],
-        ' Two ',
+        ['#text' => ' Two '],
         ['#comment' => 'dos & so on '],
-        ' Three',
+        ['#text' => ' Three'],
       ],
     ],
     ['#comment' => 'tres-a--b---c'],
index 18ccc7f48751ca0812b19292c3fb6cd6c862cb69..221e1a36f05864fe56460212c8998dce5b858aee 100644 (file)
@@ -2,6 +2,6 @@
 
 return [
   'html' => 'hello world',
-  'shallow' => ['hello world'],
-  'deep' => ['hello world'],
+  'shallow' => [['#text' => 'hello world']],
+  'deep' => [['#text' => 'hello world']],
 ];