diff --git a/source/FluidXml/FluidInsertionHandler.php b/source/FluidXml/FluidInsertionHandler.php index f47fd46..5b6a3a1 100644 --- a/source/FluidXml/FluidInsertionHandler.php +++ b/source/FluidXml/FluidInsertionHandler.php @@ -110,9 +110,15 @@ protected function recognizeStringMixed($k, $v) } if ($k[0] === '@') { - if ($k === '@') { + if ($k === '@' || $k === '@:text') { return 'insertSpecialContent'; } + if ($k === '@:cdata') { + return 'insertSpecialCdata'; + } + if ($k === '@:comment') { + return 'insertSpecialComment'; + } return 'insertSpecialAttribute'; } @@ -250,6 +256,26 @@ protected function insertSpecialContent($parent, $k, $v): array return []; } + protected function insertSpecialCdata($parent, $k, $v): array + { + // The user has passed CDATA content: + // [ '@:cdata' => 'Content with chars.' ] + + $this->newContext($parent)->addCdata($v); + + return []; + } + + protected function insertSpecialComment($parent, $k, $v): array + { + // The user has passed a comment: + // [ '@:comment' => 'This is a comment.' ] + + $this->newContext($parent)->addComment($v); + + return []; + } + protected function insertSpecialAttribute($parent, $k, $v): array { // The user has passed an attribute name and an attribute value: diff --git a/specs/FluidXml.php b/specs/FluidXml.php index 6cac1ef..ffa2ab3 100644 --- a/specs/FluidXml.php +++ b/specs/FluidXml.php @@ -1319,6 +1319,41 @@ function addchild($parent, $i) assert_equal_xml($xml, $expected); }); + it('should add text content using @:text as alias for @', function () { + $xml = new FluidXml(); + $xml->addChild(['child1' => [ '@:text' => 'Hello' ]]); + + $expected = "\n" + . " Hello\n" + . ""; + assert_equal_xml($xml, $expected); + }); + + it('should add CDATA content using @:cdata syntax', function () { + $xml = new FluidXml(); + $xml->addChild(['chapter' => [ + '@id' => '1', + '@:cdata' => 'Ideas About Universe', + ]]); + + $expected = "\n" + . " Universe]]>\n" + . ""; + assert_equal_xml($xml, $expected); + }); + + it('should add comment content using @:comment syntax', function () { + $xml = new FluidXml(); + $xml->addChild(['node' => [ '@:comment' => 'This is a comment' ]]); + + $expected = "\n" + . " \n" + . " \n" + . " \n" + . ""; + assert_equal_xml($xml, $expected); + }); + it('should switch context', function () { $xml = new FluidXml();