From 648f59bfdc137baf03080fa9cf9e9bf04a63f6c7 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 18 Oct 2019 18:30:14 -0700 Subject: [PATCH] Civi\Afform\Symbols - Add unit test. Define behavior for Angular exprs. --- ext/afform/core/Civi/Afform/Symbols.php | 22 +++- .../tests/phpunit/Civi/Afform/SymbolsTest.php | 103 ++++++++++++++++++ 2 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 ext/afform/core/tests/phpunit/Civi/Afform/SymbolsTest.php diff --git a/ext/afform/core/Civi/Afform/Symbols.php b/ext/afform/core/Civi/Afform/Symbols.php index b933afb11f..f816a3c18a 100644 --- a/ext/afform/core/Civi/Afform/Symbols.php +++ b/ext/afform/core/Civi/Afform/Symbols.php @@ -50,7 +50,7 @@ class Symbols { self::increment($this->attributes, $node->nodeName); if ($node->nodeName === 'class') { - $classes = explode(' ', $node->nodeValue); + $classes = $this->parseClasses($node->nodeValue); foreach ($classes as $class) { self::increment($this->classes, $class); } @@ -58,6 +58,26 @@ class Symbols { } } + /** + * @param string $expr + * Ex: 'crm-icon fa-mail' + * @return array + * Ex: ['crm-icon', 'fa-mail'] + */ + protected function parseClasses($expr) { + if ($expr === '' || $expr === NULL || $expr === FALSE) { + return []; + } + if (strpos($expr, '{{') === FALSE) { + return explode(' ', $expr); + } + if (preg_match_all(';([a-zA-Z\-_]+|\{\{.*\}\}) ;U', "$expr ", $m)) { + return $m[1]; + } + error_log("Failed to parse CSS classes: $expr"); + return []; + } + private static function increment(&$arr, $key) { if (!isset($arr[$key])) { $arr[$key] = 0; diff --git a/ext/afform/core/tests/phpunit/Civi/Afform/SymbolsTest.php b/ext/afform/core/tests/phpunit/Civi/Afform/SymbolsTest.php new file mode 100644 index 0000000000..8074fc41f9 --- /dev/null +++ b/ext/afform/core/tests/phpunit/Civi/Afform/SymbolsTest.php @@ -0,0 +1,103 @@ +installMe(__DIR__)->apply(); + } + + public function getExamples() { + $exs = []; + $exs[] = [ + '
', + [ + 'e' => ['div' => 1, 'body' => 1], + 'a' => [], + 'c' => [], + ], + ]; + $exs[] = [ + 'foobar', + [ + 'e' => ['my-tabset' => 1, 'my-tab' => 3, 'body' => 1], + 'a' => ['id' => 3], + 'c' => [], + ], + ]; + $exs[] = [ + '
', + [ + 'e' => ['div' => 2, 'img' => 1, 'body' => 1], + 'a' => ['class' => 3, 'src' => 1], + 'c' => [ + 'my-parent' => 1, + 'my-child' => 1, + 'special' => 1, + ], + ], + ]; + $exs[] = [ + '
a
b
c
', + [ + 'e' => ['div' => 2, 'body' => 1], + 'a' => ['class' => 2], + 'c' => [ + 'my-parent' => 1, + 'my-child' => 1, + 'foo' => 1, + 'bar' => 1, + 'whiz' => 1, + 'bang' => 1, + '{{ghost + stuff}}' => 1, + 'last' => 1, + ], + ], + ]; + $exs[] = [ + '
', + [ + 'e' => ['div' => 1, 'body' => 1], + 'a' => ['class' => 1], + 'c' => [ + '{{ghost + stuff}}' => 1, + '{{make[\'cheese\']}}' => 1, + '{{a}}_{{b}}' => 1, + ], + ], + ]; + + return $exs; + } + + /** + * @param string $html + * @param array $expect + * List of expected symbol counts, by type. + * Types are (e)lement, (a)ttribute, (c)lass + * @dataProvider getExamples + */ + public function testSymbols($html, $expect) { + $expectDefaults = ['e' => [], 'a' => [], 'c' => []]; + $expect = array_merge($expectDefaults, $expect); + $actual = Symbols::scan($html); + + $this->assertEquals($expect['e'], $actual->elements); + $this->assertEquals($expect['a'], $actual->attributes); + $this->assertEquals($expect['c'], $actual->classes); + } + +} -- 2.25.1