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
4 changes: 2 additions & 2 deletions source/FluidXml/FluidContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,9 @@ public function __toString(): string
return (string) $this->xml();
}

public function xml($strip = false): string
public function xml($strip = false, int $options = 0): string
{
return FluidHelper::domnodesToString($this->nodes);
return FluidHelper::domnodesToString($this->nodes, false, $options);
}

public function html($strip = false): string
Expand Down
16 changes: 8 additions & 8 deletions source/FluidXml/FluidHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static function isAnXmlString($string): bool
return $string && $string[0] === '<';
}

public static function exportNode(\DOMDocument $dom, \DOMNode $node, $html = false): array|bool|string|null
public static function exportNode(\DOMDocument $dom, \DOMNode $node, $html = false, int $options = 0): array|bool|string|null
{
// $delegate = $html ? 'saveHTML' : 'saveXML';
// return $dom->$delegate($node);
Expand All @@ -22,15 +22,15 @@ public static function exportNode(\DOMDocument $dom, \DOMNode $node, $html = fal
return static::domnodeToHtml($node);
}

return $dom->saveXML($node);
return $dom->saveXML($node, $options);
}

public static function domdocumentToStringWithoutHeaders(\DOMDocument $dom, $html = false): bool|array|string|null
public static function domdocumentToStringWithoutHeaders(\DOMDocument $dom, $html = false, int $options = 0): bool|array|string|null
{
return static::exportNode($dom, $dom->documentElement, $html);
return static::exportNode($dom, $dom->documentElement, $html, $options);
}

public static function domnodelistToString(\DOMNodeList $nodelist, $html = false): string
public static function domnodelistToString(\DOMNodeList $nodelist, $html = false, int $options = 0): string
{
$nodes = [];

Expand All @@ -44,16 +44,16 @@ public static function domnodelistToString(\DOMNodeList $nodelist, $html = false

// Algorithm 1 is faster than Algorithm 2.

return static::domnodesToString($nodes, $html);
return static::domnodesToString($nodes, $html, $options);
}

public static function domnodesToString(array $nodes, $html = false): string
public static function domnodesToString(array $nodes, $html = false, int $options = 0): string
{
$dom = $nodes[0]->ownerDocument;
$xml = '';

foreach ($nodes as $n) {
$xml .= static::exportNode($dom, $n, $html) . PHP_EOL;
$xml .= static::exportNode($dom, $n, $html, $options) . PHP_EOL;
}

return \rtrim($xml);
Expand Down
4 changes: 2 additions & 2 deletions source/FluidXml/FluidInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public function length();
public function dom();
public function array_();
public function __toString();
public function xml($strip = false);
public function xml($strip = false, int $options = 0);
public function html($strip = false);
public function save($file, $strip = false);
public function save($file, $strip = false, int $options = 0);
public function query(...$query);
public function __invoke(...$query);
public function times($times, callable $fn = null);
Expand Down
6 changes: 3 additions & 3 deletions source/FluidXml/FluidSaveTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ trait FluidSaveTrait
/**
* @throws \Exception
*/
public function save($file, $strip = false): static
public function save($file, $strip = false, int $options = 0): static
{
$status = \file_put_contents($file, $this->xml($strip));
$status = \file_put_contents($file, $this->xml($strip, $options));

if (! $status) {
throw new \Exception("The file '$file' is not writable.");
Expand All @@ -18,5 +18,5 @@ public function save($file, $strip = false): static
return $this;
}

abstract public function xml($strip = false);
abstract public function xml($strip = false, int $options = 0);
}
6 changes: 3 additions & 3 deletions source/FluidXml/FluidXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ public function __toString(): string
return (string) $this->xml();
}

public function xml($strip = false)
public function xml($strip = false, int $options = 0)
{
if ($strip) {
return FluidHelper::domdocumentToStringWithoutHeaders($this->document->dom);
return FluidHelper::domdocumentToStringWithoutHeaders($this->document->dom, false, $options);
}

return $this->document->dom->saveXML();
return $this->document->dom->saveXML(null, $options);
}

public function html($strip = false): string
Expand Down
26 changes: 26 additions & 0 deletions specs/FluidXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -2247,6 +2247,32 @@ function addchild($parent, $i)
. "<child>content2</child>";
\assert($actual === $expected, __($actual, $expected));
});

it('should render empty elements as explicit closing tags with LIBXML_NOEMPTYTAG', function () {
$xml = new FluidXml();
$xml->addChild(['parent' => ['child1', 'child2']]);

$actual = $xml->xml(true, \LIBXML_NOEMPTYTAG);
$expected = "<doc>\n"
. " <parent>\n"
. " <child1></child1>\n"
. " <child2></child2>\n"
. " </parent>\n"
. "</doc>";
\assert($actual === $expected, __($actual, $expected));
});

it('should pass LIBXML_NOEMPTYTAG through when querying a context', function () {
$xml = new FluidXml();
$xml->addChild(['parent' => ['child1', 'child2']]);

$actual = $xml->query('//parent')->xml(false, \LIBXML_NOEMPTYTAG);
$expected = "<parent>\n"
. " <child1></child1>\n"
. " <child2></child2>\n"
. "</parent>";
\assert($actual === $expected, __($actual, $expected));
});
});

describe('.__toString()', function () {
Expand Down