Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

node-html-light

stfsy12.9kMIT2.12.0

HTML Parser for NodeJS providing a lightweight object oriented interface

node, document, dom, domutils, elements, html, html parser, htmlparser2, parser, render, render html, serverside

readme

Node HTML Light

Build Status Dependency Status DevDependency Status Npm downloads Npm Version Git tag Github issues License

HTML Parser for NodeJS providing a lightweight object oriented interface

How can I install it?

npm i node-html-light --save

Who is using it?

blauspecht.io uses node-html-light to render their whole page server-side. blauspecht.io enables you to provide rich content to your followers, schedule tweets and threads and will add AI-powered features soon.

How can I use it?

Document

Class methods

Instance methods

  • html() -> Node
  • head() -> Node
  • body() -> Node
  • toHtml() -> String

Node

Class methods

  • static fromPath(path) -> Node | Array<Node>
  • static fromString(string) -> Node | Array<Node>
  • static of(object | name, attrs) -> Node | Array<Node>

Instance properties

  • name
  • type
  • parent
  • children
  • root
  • nextSibling
  • previousSibling
  • attributes

Instance methods

  • appendChild(newChild) -> void
  • appendChildBefore(newChild, oldChild) -> void
  • appendChildAfter(newChild, oldChild) -> void
  • find(element, attrs, limit) -> Array<Node>
  • filter(callback, limit) -> Array<Node>
  • removeChild(child) -> void
  • replaceChild(newChild, oldChild) -> void
  • toHtml() -> String

Attributes

Class methods

  • static of(object) -> Array<Attribute>

Text

Class methods

  • static of(string) -> Text

Examples

Create a document using a file

const Document = require('node-html-light').Document
const resolve = require('path').resolve

Document.fromPath(resolve('./index.html')).then((document) => {
    // head is an instance of Node
    const head = document.head()
    // body is an instance of Node
    const body = document.body()

    // find child elements
    // append child elements
    // remove child elements
    // replace child elements

    return document.toHtml()
}).then((html) => {
    // ..
})

Create a Node using a File

const Node = require('node-html-light').Node
const resolve = require('path').resolve

Node.fromPath(resolve('partial.html')).then((node) => {})

Create a Node using a String

const Node = require('node-html-light').Node

const node = Node.fromString('<div></div>')

Create a Node with raw data

const Node = require('node-html-light').Node
const Attributes = require('node-html-light').Attributes

const node = Node.of('meta', Attributes.of({
        'name': 'viewport',
        'theme-color': '#795548'
    })
)

Find a child Node of an existing Element

const Node = require('node-html-light').Node
const Attributes = require('node-html-light').Attributes
const resolve = require('path').resolve

Node.fromPath(resolve('partial.html')).then((node) => {
    const content = node.find('div', Attributes.of({
        'name': 'viewport',
        'theme-color': '#795548'
    }))
})

License

This project is distributed under the MIT license.

changelog

2.12.0 (2021-09-16)

2.11.0 (2021-08-03)

Features

  • add setter for attributes (6b2a16b)
  • update outdated dependencies (cf6067b)

2.10.0 (2021-07-07)

Bug Fixes

  • self closing tags not recognized (2d5ba56)

Features

  • add filter by type function (c3e11a8)
  • add getter for root element (acb9c31)
  • add method to get children of current node (4c0538e)

2.9.0 (2021-07-06)

Features

2.8.1 (2021-07-05)

Bug Fixes

  • some html entities get encoded (cffd8f1)

Features

  • use parseDocument instead of parseDom and set default options (39808b4)

2.8.0 (2021-06-28)

Features

  • add method to file node and childnodes (6bc7e9d)

2.7.0 (2021-06-26)

Features

  • pass index to forEach callback (ecf7fe9)

2.6.0 (2021-06-25)

Features

2.5.0 (2021-05-13)

Features

  • update outdated dependencies (043d841)

2.4.0 (2021-03-28)

Bug Fixes

  • cannot find script elms if no type is given (b8586a0)

Features

  • update outdated dependencies (062de60)

2.3.0 (2021-02-09)

Features

  • update outdated dependencies (062de60)

2.2.0 (2021-02-03)

Features

  • update html parser to 5.0.1 (a5abbf5)

2.1.0 (2021-02-03)

Features

  • add getter for next sibling (9c3d141)
  • add getter for previous sibling of node (eccde43)

2.0.0 (2021-01-29)

Features

  • remove nodes without previous lookup (bd0ec3a)
  • update dependencies (05d7df9)

1.4.0 (2020-10-15)

Features

1.3.0 (2020-04-10)

Features

1.2.0 (2020-03-22)

Features

1.1.0 (2018-12-22)

Bug Fixes

  • node.js: return null if node has no parent (39856bc)

Code Refactoring

  • node.js: chainable methods no longer return this (42d0548)

Features

  • attribute.js: add static of method (498aee6)
  • attributes.js: rename fromObject to of (6763c53)
  • node.js: add getter for parent node (b834c46)
  • node.js: add type property (e4a7127)
  • node.js: convert attributes to property via get (d960e47)
  • node.js: convert name function to property via get (bd59ea6)
  • node.js: convert parent fn to property via get (b95bf36)
  • node.js: limit max number of deletable elements in removeChild (c626b77)
  • node.js: remove append method (97d4f1b)
  • node.js: remove attribute method (ece5e00)
  • node.js: remove method removeFirst (10b15e2)
  • node.js: rename create to of (a902cd0)
  • node.js: rename method removeAll to removeChild (9181fbb)
  • node.js: return attributes as object instead of an array (7e47992)
  • nodes.js: rename fromString to of (a2f57fa)
  • update dependencies (cfe3155)
  • text.js: rename fromString to of (95a8722)

BREAKING CHANGES

  • node.js: removeChild(), appendChild(), appendChildBefore(), appendChildAfter() and replaceChild no longer return this.
  • nodes.js: Method from String no longer exists. Use Nodes.of instead.
  • node.js: Property attributes is now of type object.
  • node.js: Method attributes does no longer exist. Use property attributes instead.
  • node.js: Method name does no longer exist. Use the property name instead.
  • node.js: Method append does no longer exist. Use parent() and appendChild() instead
  • node.js: Method attribute does no longer exist. Use property attributes instead.
  • node.js: Method removeFirst does no longer exist. Use removeChild instead.
  • node.js: Method create does no longer exist. Use Node.of instead.
  • text.js: Method fromString does no longer exist. Use Text.of instead.
  • attributes.js: Method fromObject no longer exists. Use Attributes.of instead.
  • node.js: Method removeAll does no longer exist. Use removeChild instead.

1.0.0 (2017-05-12)

Bug Fixes

  • node.js: return null if node has no parent (39856bc)

Code Refactoring

  • node.js: chainable methods no longer return this (42d0548)

Features

  • attribute.js: add static of method (498aee6)
  • attributes.js: rename fromObject to of (6763c53)
  • node.js: add getter for parent node (b834c46)
  • node.js: add type property (e4a7127)
  • node.js: convert attributes to property via get (d960e47)
  • node.js: convert name function to property via get (bd59ea6)
  • node.js: convert parent fn to property via get (b95bf36)
  • node.js: limit max number of deletable elements in removeChild (c626b77)
  • node.js: remove append method (97d4f1b)
  • node.js: remove attribute method (ece5e00)
  • node.js: remove method removeFirst (10b15e2)
  • node.js: rename create to of (a902cd0)
  • node.js: rename method removeAll to removeChild (9181fbb)
  • node.js: return attributes as object instead of an array (7e47992)
  • nodes.js: rename fromString to of (a2f57fa)
  • text.js: rename fromString to of (95a8722)

BREAKING CHANGES

  • node.js: removeChild(), appendChild(), appendChildBefore(), appendChildAfter() and replaceChild no longer return this.
  • nodes.js: Method from String no longer exists. Use Nodes.of instead.
  • node.js: Property attributes is now of type object.
  • node.js: Method attributes does no longer exist. Use property attributes instead.
  • node.js: Method name does no longer exist. Use the property name instead.
  • node.js: Method append does no longer exist. Use parent() and appendChild() instead
  • node.js: Method attribute does no longer exist. Use property attributes instead.
  • node.js: Method removeFirst does no longer exist. Use removeChild instead.
  • node.js: Method create does no longer exist. Use Node.of instead.
  • text.js: Method fromString does no longer exist. Use Text.of instead.
  • attributes.js: Method fromObject no longer exists. Use Attributes.of instead.
  • node.js: Method removeAll does no longer exist. Use removeChild instead.

0.8.1 (2017-03-26)

0.8.0 (2017-03-26)

Bug Fixes

  • node.js: fix creating nodes with html parser pojos (7833336)
  • package.json: fit git push command (0def570)
  • package.json: fix git push command (13524f2)

Features

  • attributes.js: add attributes class (89ef6eb)
  • nodes.js: add forEach method to allow iterating over nodes (6d89c1c)

0.7.0 (2017-02-11)

Bug Fixes

  • node.js: fix creating nodes with html parser pojos (7833336)
  • package.json: fix git push command (13524f2)

Features

  • nodes.js: add forEach method to allow iterating over nodes (6d89c1c)

0.6.0 (2017-01-11)

Features

  • node.js: add append method (1850902)
  • node.js: add appendChildBefore and appendChildAfter methods (4a5dd83)
  • nodes.js: add nodes class and proxy node's find method (948998f)

0.5.0 (2016-09-26)

Features

  • node.js: add appendChildBefore and appendChildAfter methods (4a5dd83)

0.4.0 (2016-09-18)

Features

  • node.js: allow finding nodes of type text and comment (4dc5823)
  • node.js: expose node types (c908de6)

0.3.0 (2016-08-06)

Code Refactoring

  • node.js: rename factory method (5fdf701)

Features

  • document.js: add getter for html tag (9996bb6)
  • node.js: read nodes from a given path (45ef182)
  • node.js: set attributes on nodes if param is an attribute (485e241)

BREAKING CHANGES

  • node.js: use Node.create now instead of Node.of

0.2.0 (2016-08-06)

Features

  • index.js: export text class (683ec97)
  • node.js: allow stringifying of node instances (783a18b)
  • text.js: add text node class (4c245d7)

0.1.0 (2016-08-03)