taoqf / node-html-parser

A very fast HTML parser, generating a simplified DOM, with basic element query support.
MIT License
1.11k stars 107 forks source link

null root node when parsing the following #272

Open lilezek opened 6 months ago

lilezek commented 6 months ago

I have the following code:

const elements = htmlOrXml.map(s => parse(s.trim())); 
console.log(elements[5].structure, `'${htmlOrXml[5]}'`);

Outputing this:

null
  button#button.style-scope.yt-icon-button
    yt-icon#guide-icon.style-scope.ytd-masthead
      yt-icon-shape.style-scope.yt-icon
        icon-shape.yt-spec-icon-shape
          div
            svg
              path '<button id="button" class="style-scope yt-icon-button" aria-label="Guide">
          <yt-icon id="guide-icon" icon="yt-icons:menu" class="style-scope ytd-masthead"><yt-icon-shape class="style-scope yt-icon"><icon-shape class="yt-spec-icon-shape"><div style="width: 100%; height: 100%; fill: currentcolor;"><svg xml
ns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24" focusable="false" style="pointer-events: none; display: block; width: 100%; height: 100%;"><path d="M21 6H3V5h18v1zm0 5H3v1h18v-1zm0 6H3v1h18v-1z"></path></svg></di
v></icon-shape></yt-icon-shape></yt-icon>
        </button>'

Is the null root expected? In the documentation there is no null root.

taoqf commented 4 months ago

Yes. you should skip the root wrapper.