|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Masterminds\HTML5; |
| 4 | + |
| 5 | +use Masterminds\HTML5\Serializer\OutputRules; |
| 6 | +use Masterminds\HTML5\Serializer\Traverser; |
| 7 | + |
| 8 | +/** |
| 9 | + * Shared serializer logic for template-aware DOMDocument implementations. |
| 10 | + */ |
| 11 | +class HTML5DOMDocumentBase extends \DOMDocument |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var \SplObjectStorage|null |
| 15 | + */ |
| 16 | + protected $templateContents; |
| 17 | + |
| 18 | + /** |
| 19 | + * @param bool $create |
| 20 | + * |
| 21 | + * @return \SplObjectStorage|null |
| 22 | + */ |
| 23 | + public function html5PhpTemplateContentsStorage($create = true) |
| 24 | + { |
| 25 | + if (null === $this->templateContents && $create) { |
| 26 | + $this->templateContents = new \SplObjectStorage(); |
| 27 | + } |
| 28 | + |
| 29 | + return $this->templateContents; |
| 30 | + } |
| 31 | + |
| 32 | + protected function html5PhpSaveHTML($node = null) |
| 33 | + { |
| 34 | + $target = null === $node ? $this : $node; |
| 35 | + if (!$this->containsDetachedTemplateContents($target)) { |
| 36 | + return parent::saveHTML($node); |
| 37 | + } |
| 38 | + |
| 39 | + $stream = fopen('php://temp', 'wb'); |
| 40 | + $rules = new OutputRules($stream); |
| 41 | + $traverser = new Traverser($target, $stream, $rules); |
| 42 | + $traverser->walk(); |
| 43 | + $rules->unsetTraverser(); |
| 44 | + |
| 45 | + $html = stream_get_contents($stream, -1, 0); |
| 46 | + fclose($stream); |
| 47 | + |
| 48 | + return $html; |
| 49 | + } |
| 50 | + |
| 51 | + protected function html5PhpImportNode($node, $deep = false) |
| 52 | + { |
| 53 | + $imported = parent::importNode($node, $deep); |
| 54 | + TemplateContents::copySubtree($node, $imported, $deep); |
| 55 | + |
| 56 | + return $imported; |
| 57 | + } |
| 58 | + |
| 59 | + protected function html5PhpCloneNode($deep = false) |
| 60 | + { |
| 61 | + $class = get_class($this); |
| 62 | + $clone = new $class($this->xmlVersion, $this->encoding); |
| 63 | + TemplateContents::registerNodeClasses($clone); |
| 64 | + |
| 65 | + $clone->encoding = $this->encoding; |
| 66 | + $clone->formatOutput = $this->formatOutput; |
| 67 | + $clone->preserveWhiteSpace = $this->preserveWhiteSpace; |
| 68 | + $clone->recover = $this->recover; |
| 69 | + $clone->resolveExternals = $this->resolveExternals; |
| 70 | + $clone->strictErrorChecking = $this->strictErrorChecking; |
| 71 | + $clone->substituteEntities = $this->substituteEntities; |
| 72 | + $clone->validateOnParse = $this->validateOnParse; |
| 73 | + |
| 74 | + if (!$deep) { |
| 75 | + return $clone; |
| 76 | + } |
| 77 | + |
| 78 | + foreach ($this->childNodes as $child) { |
| 79 | + $clone->appendChild($clone->importNode($child, true)); |
| 80 | + } |
| 81 | + |
| 82 | + return $clone; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @param \DOMNode $node |
| 87 | + * |
| 88 | + * @return bool |
| 89 | + */ |
| 90 | + protected function containsDetachedTemplateContents(\DOMNode $node) |
| 91 | + { |
| 92 | + if ($node instanceof \DOMElement && 'template' === strtolower($node->tagName)) { |
| 93 | + return null !== TemplateContents::find($node); |
| 94 | + } |
| 95 | + |
| 96 | + if ($node->hasChildNodes()) { |
| 97 | + foreach ($node->childNodes as $child) { |
| 98 | + if ($this->containsDetachedTemplateContents($child)) { |
| 99 | + return true; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return false; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +if (PHP_VERSION_ID >= 80100) { |
| 109 | + require __DIR__ . '/HTML5DOMDocument81.php'; |
| 110 | +} else { |
| 111 | + class HTML5DOMDocument extends HTML5DOMDocumentBase |
| 112 | + { |
| 113 | + public function saveHTML($node = null) |
| 114 | + { |
| 115 | + return $this->html5PhpSaveHTML($node); |
| 116 | + } |
| 117 | + |
| 118 | + public function importNode($node, $deep = false) |
| 119 | + { |
| 120 | + return $this->html5PhpImportNode($node, $deep); |
| 121 | + } |
| 122 | + |
| 123 | + public function cloneNode($deep = false) |
| 124 | + { |
| 125 | + return $this->html5PhpCloneNode($deep); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments