Skip to content

firstChild and its docs seem to not match #285

@olets

Description

@olets

The docs say firstChild is undefined if the node has no children. https://github.com/taoqf/node-html-parser#firstchild

In fact, if I'm understanding correctly, parse wraps the provided markup in a tagless HTMLElement.

That's reasonable. It's a nice way of handling parse('<div></div> <div></div>').

But it means, at least in the possibilities I've tried,

  1. firstChild is never undefined
  2. firstChild is the outermost element (not what I'd think of as a "child"), if the provided HTML has one outermost element.

(2) is sort of implied in the docs (https://github.com/taoqf/node-html-parser#usage)

const root = parse('<ul id="list"><li>Hello World</li></ul>');

console.log(root.firstChild.structure);
// ul#list
//   li
//     #text

but it tripped me up: what I thought would be

const root = parse('<div my-attribute></div>')
root.attributes

has to be

const root = parse('<div my-attribute></div>')
root.firstChild.attributes

Repro: https://stackblitz.com/edit/node-html-parser-285?file=index.js

Don't know if the firstChild docs need more nuance, or if some other parts of the docs could use a warning (or even if this is a bug).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions