Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support render PHP8.4 \Dom\HTMLDocument #257

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions src/HTML5/Parser/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/**
* The scanner scans over a given data input to react appropriately to characters.
*/
#[\AllowDynamicProperties]
class Scanner
{
const CHARS_HEX = 'abcdefABCDEF01234567890';
Expand Down
77 changes: 65 additions & 12 deletions src/HTML5/Serializer/OutputRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@

namespace Masterminds\HTML5\Serializer;

use Dom\Attr;
use Dom\CharacterData;
use Dom\Document;
use Dom\Element;
use Dom\Node;
use Dom\XPath;
use Masterminds\HTML5\Elements;

/**
Expand Down Expand Up @@ -229,9 +235,9 @@ public function element($ele)
$this->openTag($ele);
if (Elements::isA($name, Elements::TEXT_RAW)) {
foreach ($ele->childNodes as $child) {
if ($child instanceof \DOMCharacterData) {
if ($child instanceof \DOMCharacterData || $child instanceof CharacterData) {
$this->wr($child->data);
} elseif ($child instanceof \DOMElement) {
} elseif ($child instanceof \DOMElement || $child instanceof Element) {
$this->element($child);
}
}
Expand Down Expand Up @@ -299,13 +305,21 @@ public function processorInstruction($ele)
*/
protected function namespaceAttrs($ele)
{
if (!$this->xpath || $this->xpath->document !== $ele->ownerDocument) {
$this->xpath = new \DOMXPath($ele->ownerDocument);
}
$isLegacyDocument = static::isLegacyDocument($ele);

// Finding namespace in new \Dom\Document will cause error message:
// DOMException: The namespace axis is not well-defined in the living DOM specification.
// Use Dom\Element::getInScopeNamespaces() or Dom\Element::getDescendantNamespaces() instead.
if ($isLegacyDocument) {
// TODO: Fix the namespace attrs writing.
if (!$this->xpath || $this->xpath->document !== $ele->ownerDocument) {
$this->xpath = new \DOMXPath($ele->ownerDocument);
}

foreach ($this->xpath->query('namespace::*[not(.=../../namespace::*)]', $ele) as $nsNode) {
if (!in_array($nsNode->nodeValue, $this->implicitNamespaces)) {
$this->wr(' ')->wr($nsNode->nodeName)->wr('="')->wr($nsNode->nodeValue)->wr('"');
foreach ($this->xpath->query('namespace::*[not(.=../../namespace::*)]', $ele) as $nsNode) {
if (!in_array($nsNode->nodeValue, $this->implicitNamespaces)) {
$this->wr(' ')->wr($nsNode->nodeName)->wr('="')->wr($nsNode->nodeValue)->wr('"');
}
}
}
}
Expand Down Expand Up @@ -375,8 +389,14 @@ protected function attrs($ele)
}
}

protected function nonBooleanAttribute(\DOMAttr $attr)
protected function nonBooleanAttribute($attr)
{
if (!$attr instanceof \DOMAttr && !$attr instanceof Attr) {
throw new \InvalidArgumentException(
__METHOD__ . '() argument 1 should be \DOMAttr or \Dom\Attr'
);
}

$ele = $attr->ownerElement;
foreach ($this->nonBooleanAttributes as $rule) {
if (isset($rule['nodeNamespace']) && $rule['nodeNamespace'] !== $ele->namespaceURI) {
Expand Down Expand Up @@ -415,10 +435,25 @@ protected function nonBooleanAttribute(\DOMAttr $attr)
return false;
}

private function getXPath(\DOMNode $node)
/**
* @param Node|\DOMNode $node
*
* @return XPath|\DOMXPath
*/
private function getXPath($node)
{
$isLegacyDocument = static::isLegacyDocument($node);

if ($isLegacyDocument) {
if (!$this->xpath) {
$this->xpath = new \DOMXPath($node->ownerDocument);
}

return $this->xpath;
}

if (!$this->xpath) {
$this->xpath = new \DOMXPath($node->ownerDocument);
$this->xpath = new XPath($node->ownerDocument);
}

return $this->xpath;
Expand All @@ -430,7 +465,7 @@ private function getXPath(\DOMNode $node)
* Tags for HTML, MathML, and SVG are in the local name. Otherwise, use the
* qualified name (8.3).
*
* @param \DOMNode $ele The element being written.
* @param Node|\DOMNode $ele The element being written.
*/
protected function closeTag($ele)
{
Expand Down Expand Up @@ -550,4 +585,22 @@ protected function escape($text, $attribute = false)

return strtr($text, $replace);
}

/**
* @param Node|\DOMNode $node
*
* @return bool
*/
protected static function isLegacyDocument($node)
{
if ($node instanceof Document) {
return false;
}

if ($node instanceof \DOMDocument) {
return true;
}

return $node->ownerDocument instanceof \DOMDocument;
}
}
10 changes: 7 additions & 3 deletions src/HTML5/Serializer/Traverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Masterminds\HTML5\Serializer;

use Dom\Document;
use Dom\DocumentFragment;
use Dom\NodeList;

/**
* Traverser for walking a DOM tree.
*
Expand Down Expand Up @@ -60,16 +64,16 @@ public function __construct($dom, $out, RulesInterface $rules, $options = array(
*/
public function walk()
{
if ($this->dom instanceof \DOMDocument) {
if ($this->dom instanceof \DOMDocument || $this->dom instanceof Document) {
$this->rules->document($this->dom);
} elseif ($this->dom instanceof \DOMDocumentFragment) {
} elseif ($this->dom instanceof \DOMDocumentFragment || $this->dom instanceof DocumentFragment) {
// Document fragments are a special case. Only the children need to
// be serialized.
if ($this->dom->hasChildNodes()) {
$this->children($this->dom->childNodes);
}
} // If NodeList, loop
elseif ($this->dom instanceof \DOMNodeList) {
elseif ($this->dom instanceof \DOMNodeList || $this->dom instanceof NodeList) {
// If this is a NodeList of DOMDocuments this will not work.
$this->children($this->dom);
} // Else assume this is a DOMNode-like datastructure.
Expand Down
Loading