Masterminds / Masterminds/html5-php

Traverser::node() does not handle entity references

Open
#244 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
HTML
Stars
1.8k
Forks
122
PR merge metrics
No merged PRs in 30d

Description

If I manually modify a DOM document and add an entity reference, HTML5::saveHTML() does not give the same result as DOMDocument::saveHTML():

$html5 = new Masterminds\HTML5(['disable_html_ns' => TRUE]);
$dom = $html5->loadHTML('<body>');
$node = $dom->getElementsByTagName('body')->item(0);

$node->appendChild($dom->createElement('span', 'Identit&eacute;'));

print $dom->saveHTML() . "\n";
print $html5->saveHTML($dom) . "\n";

outputs

<!DOCTYPE html>
<html><body><span>Identit&eacute;</span></body></html>

<!DOCTYPE html>
<html><body><span>Identit</span></body></html>

This was reported in the Drupal project, which has recently switched to using this library instead of using DOMDocument to parse and serialize HTML: https://www.drupal.org/project/drupal/issues/3416204

This is because Traverser::node() does not handle XML_ENTITY_REF_NODE. Should the switch statement and rules class be extended to support this case?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at Traverser::node() and inspect the rules class, focusing on how XML_ENTITY_REF_NODE is handled in the switch. Reproduce the provided DOM modification and compare HTML5::saveHTML() with DOMDocument::saveHTML(); done means the entity reference is preserved in the serialized output.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
web-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.