diff --git a/source/FluidXml/FluidContext.php b/source/FluidXml/FluidContext.php
index 6f76e41..a3f3790 100644
--- a/source/FluidXml/FluidContext.php
+++ b/source/FluidXml/FluidContext.php
@@ -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
diff --git a/source/FluidXml/FluidHelper.php b/source/FluidXml/FluidHelper.php
index 25b0b71..7f6db6e 100644
--- a/source/FluidXml/FluidHelper.php
+++ b/source/FluidXml/FluidHelper.php
@@ -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);
@@ -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 = [];
@@ -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);
diff --git a/source/FluidXml/FluidInterface.php b/source/FluidXml/FluidInterface.php
index 0bfc6e4..a39ce60 100644
--- a/source/FluidXml/FluidInterface.php
+++ b/source/FluidXml/FluidInterface.php
@@ -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);
diff --git a/source/FluidXml/FluidSaveTrait.php b/source/FluidXml/FluidSaveTrait.php
index c0af9d2..20e9be3 100644
--- a/source/FluidXml/FluidSaveTrait.php
+++ b/source/FluidXml/FluidSaveTrait.php
@@ -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.");
@@ -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);
}
diff --git a/source/FluidXml/FluidXml.php b/source/FluidXml/FluidXml.php
index 1f8b11d..8d71646 100644
--- a/source/FluidXml/FluidXml.php
+++ b/source/FluidXml/FluidXml.php
@@ -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
diff --git a/specs/FluidXml.php b/specs/FluidXml.php
index 6cac1ef..6e10788 100644
--- a/specs/FluidXml.php
+++ b/specs/FluidXml.php
@@ -2247,6 +2247,32 @@ function addchild($parent, $i)
. "content2";
\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 = "\n"
+ . " \n"
+ . " \n"
+ . " \n"
+ . " \n"
+ . "";
+ \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 = "\n"
+ . " \n"
+ . " \n"
+ . "";
+ \assert($actual === $expected, __($actual, $expected));
+ });
});
describe('.__toString()', function () {