Masterminds / Masterminds/html5-php

loadHTML drops first TextNode of HTML fragment string

Open
#208 1 comment 0 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

Hi, thanks for the great project. I'm experimenting with parsing html strings. While I probably should use loadHTMLFragment() instead of loadHTML(), but I think this is still a bug.
If I try to loadHTML() a string which does not start with a tag but just with plain text, the first text segment is silently ignored.
In the example below, the starting text Aaa disappears from the regenerated HTML string:

require 'vendor/autoload.php';
$html5 = new Masterminds\HTML5();
$html = 'Aaa<br>Bbb<b>Ccc</b>Ddd';
$dom = $html5->loadHTML( $html );

echo $html5->saveHTML( $dom );
/* Result:
<!DOCTYPE html>
<html><br>Bbb<b>Ccc</b>Ddd</html>
*/

echo $dom->saveXML( $dom );
/* Result:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><br/>Bbb<b>Ccc</b>Ddd</html>
*/

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 by running the PHP reproduction in the issue and compare the output from loadHTML() with the input fragment. Trace the loadHTML() parsing path to find where leading text is handled; done means the regenerated HTML preserves the initial “Aaa” text while retaining the existing document structure.

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
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.