From fb4a0860dc9033e47d423dc744922d409c62cbb8 Mon Sep 17 00:00:00 2001 From: Daniele Orlando Date: Tue, 12 Jul 2016 04:05:25 +0200 Subject: [PATCH] Fixes a wrong handling of null/empty node value. [#] Fixed: * fixes #13. --- documents/Changelog.txt | 9 +- documents/ReadMe.md | 47 ++-- source/FluidXml/FluidHelper.php | 2 +- source/FluidXml/FluidInsertionHandler.php | 2 +- specs/FluidXml.php | 279 ++++++++++++++++------ 5 files changed, 237 insertions(+), 102 deletions(-) diff --git a/documents/Changelog.txt b/documents/Changelog.txt index 0ec583e..2c1d4ae 100644 --- a/documents/Changelog.txt +++ b/documents/Changelog.txt @@ -1,7 +1,14 @@ [+]: new [~]: changed [-]: removed [#]: fixed [@]: internal -1.20.2: (2016-03-04) +1.20.3: (2016-07-12) +fixes wrong handling of null/empty node value. + + [#] Fixed: + * fixes #13. + + +1.20.2: fixes some leaked PHP notices. [#] Fixed: diff --git a/documents/ReadMe.md b/documents/ReadMe.md index 9c90316..365037b 100644 --- a/documents/ReadMe.md +++ b/documents/ReadMe.md @@ -48,8 +48,8 @@ ## Changelog -**1.20.2** (2016-03-04): -_fixes some leaked PHP notices._ +**1.20.3** (2016-07-12): +_fixes wrong handling of null/empty node value._ **...** @@ -57,8 +57,9 @@ _fixes some leaked PHP notices._
-[![Donate][donate-button]][donate-link]
-**1$ or more**, due to the PayPal fees. + + Buy Me a Coffee at ko-fi.com + # FluidXML @@ -69,8 +70,6 @@ _fixes some leaked PHP notices._ FluidXML is a PHP library designed to manipulate XML documents with a **concise** and **fluent** API.
It leverages the fluent programming pattern to be **fun and effective**. -With FluidXML the DOM manipulation becomes **fast**, **clear** and **expressive**. - ```php $book = fluidxml(); @@ -93,6 +92,8 @@ $book->addChild('title', 'The Theory Of Everything') ->addChild('chapter', 'The Expanding Universe', ['id' => 2]); ``` +With FluidXML the DOM manipulation becomes **fast**, **clear** and **expressive**. + **PHP Arrays** are first class citizens. ```php @@ -131,11 +132,13 @@ $book->query('//title', '//author', '//chapter') ->attr('lang', 'en'); ``` -And **CSS Selectors** rocks. +And **CSS Selectors** rock. ```php -$book->query('title', 'author', 'chapters > chapter') +$book->query('#id', '.class1.class2', 'div p > span') ->attr('lang', 'en'); + +// Many other selectors are available. ``` **XML/CSS Namespaces** are fully covered. @@ -147,7 +150,7 @@ $book->namespace('xhtml', 'http://www.w3.org/1999/xhtml') ->query('xhtml|h1'); // CSS namespace. ``` -And sometimes **string fragments** are the fastest way. +And sometimes **XML Fragments** are the fastest way. ```php $book->add(<<query('//chapter') - ->each(function($i) { + ->each(function ($i) { $this->attr('id', $i); }); ``` @@ -174,7 +177,7 @@ $book->query('//chapter') $book->query('//chapters') ->times(3) ->add('chapter') - ->times(4, function($i) { + ->times(4, function ($i) { $this->add('chapter'); $this->add('illustration'); }); @@ -185,7 +188,7 @@ Whether some queries are too complex to express with XPath/CSS,
```php $book->query('//chapters') - ->filter(function($i, $node) { + ->filter(function ($i, $node) { return $i % 2 === 0; }) ->attr('even'); @@ -293,21 +296,25 @@ and go to the [Wiki Page][wiki] for more reading. ## Donation If you think this code is **awesome** or if you want to demonstrate
-your immense gratitude **[♥][thankyou]**, donate _1cent_. +your immense gratitude **[♥][thankyou]**, _buy me a coffe_. + + + Buy Me a Coffee at ko-fi.com + + +[//]: # ([![Donate][donate-button]][donate-link]
) +[//]: # (**1$ or more**, due to the PayPal fees.) + + + + -[![Donate][donate-button]][donate-link]
-**1$ or more**, due to the PayPal fees. ## Roadmap * [x] PHP 5.6 backport * [ ] Extending the documentation * [ ] Expanding the APIs - - Click here to lend your support to: FluidXML and make a donation at pledgie.com ! - - - ## Author Daniele Orlando [<fluidxml@danieleorlando.com>](mailto:fluidxml@danieleorlando.com) diff --git a/source/FluidXml/FluidHelper.php b/source/FluidXml/FluidHelper.php index b7986ed..53edb69 100644 --- a/source/FluidXml/FluidHelper.php +++ b/source/FluidXml/FluidHelper.php @@ -10,7 +10,7 @@ public static function isAnXmlString($string) // otherwise the first character check may fail. $string = \ltrim($string); - return $string[0] === '<'; + return $string && $string[0] === '<'; } public static function exportNode(\DOMDocument $dom, \DOMNode $node, $html = false) diff --git a/source/FluidXml/FluidInsertionHandler.php b/source/FluidXml/FluidInsertionHandler.php index ec82f77..efffef4 100644 --- a/source/FluidXml/FluidInsertionHandler.php +++ b/source/FluidXml/FluidInsertionHandler.php @@ -109,7 +109,7 @@ protected function recognizeStringMixed($k, $v) return 'insertSpecialAttribute'; } - if (\is_string($v)) { + if (\is_string($v) || $v === null) { if (! FluidHelper::isAnXmlString($v)) { return 'insertStringSimple'; } diff --git a/specs/FluidXml.php b/specs/FluidXml.php index 2f708c5..d558bf3 100644 --- a/specs/FluidXml.php +++ b/specs/FluidXml.php @@ -953,43 +953,229 @@ function addchild($parent, $i) assert_is_fluid('addChild', 'a'); }); - it('should add a child', function () { + it('should add a child using the argument syntax', function () { $xml = new FluidXml(); $xml->addChild('child1') - ->addChild('child2') ->addChild('parent', true) - ->addChild('child3') - ->addChild('child4'); + ->addChild('child2'); + + $expected = "\n" + . " \n" + . " \n" + . " \n" + . " \n" + . ""; + assert_equal_xml($xml, $expected); + }); + + it('should add a child using the array syntax', function () { + $xml = new FluidXml(); + $xml->addChild(['child1']) + ->addChild(['parent'], true) + ->addChild(['child2']); + + $expected = "\n" + . " \n" + . " \n" + . " \n" + . " \n" + . ""; + assert_equal_xml($xml, $expected); + }); + + it('should add a child with a string value using the argument syntax', function () { + $xml = new FluidXml(); + $xml->addChild('child1', 'value1') + ->addChild('parent', true) + ->addChild('child2', 'value2'); + + $expected = "\n" + . " value1\n" + . " \n" + . " value2\n" + . " \n" + . ""; + assert_equal_xml($xml, $expected); + }); + + it('should add a child with a string value using the array syntax', function () { + $xml = new FluidXml(); + $xml->addChild(['child1' => 'value1']) + ->addChild('parent', true) + ->addChild(['child2' => 'value2']); + + $expected = "\n" + . " value1\n" + . " \n" + . " value2\n" + . " \n" + . ""; + assert_equal_xml($xml, $expected); + }); + + it('should add a child with an empty string value using the argument syntax', function () { + $xml = new FluidXml(); + $xml->addChild('child1', '') + ->addChild('parent', true) + ->addChild('child2', ''); + + $expected = "\n" + . " \n" + . " \n" + . " \n" + . " \n" + . ""; + assert_equal_xml($xml, $expected); + }); + + it('should add a child with an empty string value using the array syntax', function () { + $xml = new FluidXml(); + $xml->addChild(['child1' => '']) + ->addChild('parent', true) + ->addChild(['child2' => '']); $expected = "\n" . " \n" - . " \n" . " \n" - . " \n" - . " \n" + . " \n" . " \n" . ""; assert_equal_xml($xml, $expected); }); - it('should add many children', function () { + it('should add a child with a null value using the argument syntax', function () { $xml = new FluidXml(); - $xml->addChild(['child1', 'child2']) + $xml->addChild('child1', null) ->addChild('parent', true) - ->addChild(['child3', 'child4']); + ->addChild('child2', null); + + $expected = "\n" + . " \n" + . " \n" + . " \n" + . " \n" + . ""; + assert_equal_xml($xml, $expected); + }); + + it('should add a child with a null value using the array syntax', function () { + $xml = new FluidXml(); + $xml->addChild(['child1' => null]) + ->addChild('parent', true) + ->addChild(['child2' => null]); + + $expected = "\n" + . " \n" + . " \n" + . " \n" + . " \n" + . ""; + assert_equal_xml($xml, $expected); + }); + + it('should add a child with an integer value using the argument syntax', function () { + $xml = new FluidXml(); + $xml->addChild('child1', 1) + ->addChild('parent', true) + ->addChild('child2', 1); + + $expected = "\n" + . " 1\n" + . " \n" + . " 1\n" + . " \n" + . ""; + assert_equal_xml($xml, $expected); + }); + + it('should add a child with an integer value using the array syntax', function () { + $xml = new FluidXml(); + $xml->addChild(['child1' => 1]) + ->addChild('parent', true) + ->addChild(['child2' => 1]); + + $expected = "\n" + . " 1\n" + . " \n" + . " 1\n" + . " \n" + . ""; + assert_equal_xml($xml, $expected); + }); + + it('should add a child with a 0 value using the argument syntax', function () { + $xml = new FluidXml(); + $xml->addChild('child1', 0) + ->addChild('parent', true) + ->addChild('child2', 0); + + $expected = "\n" + . " 0\n" + . " \n" + . " 0\n" + . " \n" + . ""; + assert_equal_xml($xml, $expected); + }); + + it('should add a child with a 0 value using the array syntax', function () { + $xml = new FluidXml(); + $xml->addChild(['child1' => 0]) + ->addChild('parent', true) + ->addChild(['child2' => 0]); + + $expected = "\n" + . " 0\n" + . " \n" + . " 0\n" + . " \n" + . ""; + assert_equal_xml($xml, $expected); + }); + + it('should add many children with and without a value', function () { + $xml = new FluidXml(); + $xml->addChild(['child1', 'child2', 'child3' => 'value3', 'child4' => 'value4']) + ->addChild('parent', true) + ->addChild(['child5', 'child6', 'child7' => 'value7', 'child8' => 'value8']); $expected = "\n" . " \n" . " \n" + . " value3\n" + . " value4\n" . " \n" - . " \n" - . " \n" + . " \n" + . " \n" + . " value7\n" + . " value8\n" . " \n" . ""; assert_equal_xml($xml, $expected); }); - it('should add many children recursively', function () { + it('should add many children of the same name with and without a value', function () { + $xml = new FluidXml(); + $xml->addChild(['child', ['child'], ['child' => 'value1'], ['child' => 'value2']]) + ->addChild('parent', true) + ->addChild(['child', ['child'], ['child' => 'value3'], ['child' => 'value4']]); + + $expected = "\n" + . " \n" + . " \n" + . " value1\n" + . " value2\n" + . " \n" + . " \n" + . " \n" + . " value3\n" + . " value4\n" + . " \n" + . ""; + assert_equal_xml($xml, $expected); + }); + + it('should add many children with nested arrays', function () { $xml = new FluidXml(); $xml->addChild(['child1'=>['child11'=>['child111', 'child112'=>'value112'], 'child12'=>'value12'], 'child2'=>['child21', 'child22'=>['child221', 'child222']]]) @@ -1031,70 +1217,6 @@ function addchild($parent, $i) EOF; - - assert_equal_xml($xml, $expected); - }); - - it('should add a child with a value, string and number', function () { - $xml = new FluidXml(); - $xml->addChild(['child1' => 'value1']) - ->addChild('child2', 'value2') - ->addChild(['child3' => 3]) - ->addChild('child4', 4) - ->addChild('parent', true) - ->addChild(['child5' => 'value5']) - ->addChild('child6', 'value6') - ->addChild(['child7' => 7]) - ->addChild('child8', 8); - - $expected = "\n" - . " value1\n" - . " value2\n" - . " 3\n" - . " 4\n" - . " \n" - . " value5\n" - . " value6\n" - . " 7\n" - . " 8\n" - . " \n" - . ""; - assert_equal_xml($xml, $expected); - }); - - it('should add many children with a value', function () { - $xml = new FluidXml(); - $xml->addChild(['child1' => 'value1', 'child2' => 'value2']) - ->addChild('parent', true) - ->addChild(['child3' => 'value3', 'child4' => 'value4']); - - $expected = "\n" - . " value1\n" - . " value2\n" - . " \n" - . " value3\n" - . " value4\n" - . " \n" - . ""; - assert_equal_xml($xml, $expected); - - $xml = new FluidXml(); - $xml->addChild([ 'child', ['child'], ['child' => 'value1'], ['child' => 'value2'] ]) - ->addChild('parent', true) - ->addChild([ 'child', ['child'], ['child' => 'value3'], ['child' => 'value4'] ]); - - $expected = "\n" - . " \n" - . " \n" - . " value1\n" - . " value2\n" - . " \n" - . " \n" - . " \n" - . " value3\n" - . " value4\n" - . " \n" - . ""; assert_equal_xml($xml, $expected); }); @@ -2538,8 +2660,8 @@ function addchild($parent, $i) it('should return the namespace mode', function () { $ns_id = 'x'; $ns_uri = 'x.com'; - $ns_mode = FluidNamespace::MODE_EXPLICIT; $ns = new FluidNamespace($ns_id, $ns_uri); + $ns_mode = FluidNamespace::MODE_EXPLICIT; $actual = $ns->mode(); $expected = $ns_mode; @@ -2767,6 +2889,5 @@ function addchild($parent, $i) $expected = $hml->query('//span', '//div', '//p')->array(); \assert($actual === $expected, __($actual, $expected)); }); - }); });