Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion source/FluidXml/FluidInsertionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand Down Expand Up @@ -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 <special> 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:
Expand Down
35 changes: 35 additions & 0 deletions specs/FluidXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<doc>\n"
. " <child1>Hello</child1>\n"
. "</doc>";
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 <The> Universe',
]]);

$expected = "<doc>\n"
. " <chapter id=\"1\"><![CDATA[Ideas About <The> Universe]]></chapter>\n"
. "</doc>";
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 = "<doc>\n"
. " <node>\n"
. " <!--This is a comment-->\n"
. " </node>\n"
. "</doc>";
assert_equal_xml($xml, $expected);
});

it('should switch context', function () {
$xml = new FluidXml();

Expand Down