FluidXml\FluidHelper::domdocumentToHtml PHP Method

domdocumentToHtml() public static method

public static domdocumentToHtml ( $dom, $clone = true )
    public static function domdocumentToHtml($dom, $clone = true)
    {
        if ($clone) {
            $dom = $dom->cloneNode(true);
        }
        $voids = ['area', 'base', 'br', 'col', 'colgroup', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
        // Every empty node. There is no reason to match nodes with content inside.
        $query = '//*[not(node())]';
        $nodes = (new \DOMXPath($dom))->query($query);
        foreach ($nodes as $n) {
            if (!\in_array($n->nodeName, $voids)) {
                // If it is not a void/empty tag,
                // we need to leave the tag open.
                $n->appendChild(new \DOMProcessingInstruction('X-NOT-VOID'));
            }
        }
        $html = static::domdocumentToStringWithoutHeaders($dom);
        // Let's remove the placeholder.
        $html = \preg_replace('/\\s*<\\?X-NOT-VOID\\?>\\s*/', '', $html);
        return $html;
    }

Usage Example

Esempio n. 1
0
         $xml = new FluidXml();
         $actual = FluidHelper::isAnXmlString($xml->xml());
         $expected = true;
         \assert($actual === $expected, __($actual, $expected));
         $actual = FluidHelper::isAnXmlString(" \n \n \t" . $xml->xml());
         $expected = true;
         \assert($actual === $expected, __($actual, $expected));
         $actual = FluidHelper::isAnXmlString('item');
         $expected = false;
         \assert($actual === $expected, __($actual, $expected));
     });
 });
 describe(':domdocumentToHtml()', function () {
     it('should convert a DOMDocument instance to an HTML string without respecting void and not void tags.', function () {
         // This is only to analyze a condition (not used) for the code coverage reporter.
         FluidHelper::domdocumentToHtml((new FluidXml())->dom(), true);
     });
 });
 describe(':domdocumentToStringWithoutHeaders()', function () {
     it('should convert a DOMDocument instance to an XML string without the XML headers (declaration and stylesheets)', function () {
         $xml = new FluidXml();
         $actual = FluidHelper::domdocumentToStringWithoutHeaders($xml->dom());
         $expected = "<doc/>";
         \assert($actual === $expected, __($actual, $expected));
         $xml = new FluidXml('doc', ['stylesheet' => 'x.com/style.xsl']);
         $actual = FluidHelper::domdocumentToStringWithoutHeaders($xml->dom());
         $expected = "<doc/>";
         \assert($actual === $expected, __($actual, $expected));
     });
 });
 describe(':domnodelistToString()', function () {