} } /* * To retrieve all DOMNode in one operation there is the ->asArray() method. */ $chapters_nodes = $chapters->array(); // Returns an array of DOMNode. echo "————————————————————————————————————————————————————————————————————————————————\n"; /*************** * Namespaces. * ***************/ /* * To use a namespace you have to register its identifier together with its uri. */ $xhtml = fluidns('xhtml', 'http://www.w3.org/1999/xhtml'); $book->namespace($xhtml)->namespace('svg', 'http://www.w3.org/2000/svg')->namespace('xsl', 'http://www.w3.org/TR/xsl', FluidNamespace::MODE_IMPLICIT)->add('xhtml:h1')->add(['xsl:template' => ['xsl:variable']])->query('//xhtml:h1')->add('svg:shape'); echo $book->xml(); echo "————————————————————————————————————————————————————————————————————————————————\n"; /******************* * Removing Nodes. * *******************/ $food->remove('//egg'); // Removes all the eggs. // Which is the same of // $food->query('//egg')->remove(); // Removes all the eggs using a query. // $food->query('/doc')->remove('egg'); // Removes all the eggs using a relative XPath. /* ->remove(...$xpath) * accepts the same arguments of * ->query(...$xpath) * Remember that, like `->query()`, even `->remove()` accepts multiple XPath strings. */
$actual = $xml->appendChild('child', true); assert_is_a($actual, FluidContext::class); $actual = $xml->appendChild('child', 'value', true); assert_is_a($actual, FluidContext::class); $actual = $xml->appendChild(['child1', 'child2'], true); assert_is_a($actual, FluidContext::class); $actual = $xml->appendChild(['child1' => 'value1', 'child2' => 'value2'], true); assert_is_a($actual, FluidContext::class); $actual = $xml->appendChild('child', ['attr' => 'value'], true); assert_is_a($actual, FluidContext::class); $actual = $xml->appendChild(['child1', 'child2'], ['attr' => 'value'], true); assert_is_a($actual, FluidContext::class); }); it('should add namespaced children', function () { $xml = new FluidXml(); $xml->namespace(new FluidNamespace('x', 'x.com')); $xml->namespace(fluidns('xx', 'xx.com', FluidNamespace::MODE_IMPLICIT)); $xml->appendChild('x:xTag1', true)->appendChild('x:xTag2'); $xml->appendChild('xx:xxTag1', true)->appendChild('xx:xxTag2')->appendChild('tag3'); $expected = "<doc>\n" . " <x:xTag1 xmlns:x=\"x.com\">\n" . " <x:xTag2/>\n" . " </x:xTag1>\n" . " <xxTag1 xmlns=\"xx.com\">\n" . " <xxTag2/>\n" . " <tag3/>\n" . " </xxTag1>\n" . "</doc>"; assert_equal_xml($xml, $expected); }); $doc = "<doc>\n" . " <parent>content</parent>\n" . "</doc>"; $dom = new \DOMDocument(); $dom->loadXML($doc); it('should fill the document with an XML string', function () { $xml = new FluidXml(['root' => null]); $xml->appendChild('<root/>'); $expected = "<root/>"; assert_equal_xml($xml, $expected); });
\assert($actual === $expected, __($actual, $expected)); }); }); describe('simplexmlToStringWithoutHeaders()', function () { it('should convert a SimpleXMLElement instance to an XML string without the XML headers (declaration and stylesheets)', function () { $xml = \simplexml_import_dom((new FluidXml())->dom()); $actual = FluidHelper::simplexmlToStringWithoutHeaders($xml); $expected = "<doc/>"; \assert($actual === $expected, __($actual, $expected)); }); }); }); describe('CssTranslator', function () { describe('.xpath()', function () { $hml = new FluidXml(['html' => ['body' => ['div' => ['p' => ['@class' => 'a', '@id' => '123', ['span']], 'h1' => ['@class' => 'b'], 'p' => ['@class' => 'a b'], 'p' => ['@class' => 'a']]]]]); $hml->namespace('svg', 'http://svg.org'); $hml->query('//body')->add('svg', true)->add('shape'); it('should support the CSS selector A', function () use($hml) { $actual = $hml->query('p')->array(); $expected = $hml->query('.//p')->array(); \assert($actual === $expected, __($actual, $expected)); $expected = $hml->query('//p')->array(); \assert($actual === $expected, __($actual, $expected)); }); it('should support the CSS selector ns|A', function () use($hml) { $actual = $hml->query('svg|shape')->array(); $expected = $hml->query('//svg:shape')->array(); \assert($actual === $expected, __($actual, $expected)); }); it('should support the CSS selector :root', function () use($hml) { $actual = $hml->query(':root')->array();