JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 683116
  • Score
    100M100P100Q204163F
  • License ISC

Transform a DOM tree to HAST

Package Exports

  • hast-util-from-dom

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (hast-util-from-dom) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

hast-util-from-dom Build Status

Transform a DOM tree to HAST

Installation

yarn:

yarn add hast-util-from-dom

npm:

npm install hast-util-from-dom

Usage

This utility is similar to hast-util-from-parse5, but is intended for browser use and therefore relies on the native DOM API instead of an external parsing library.

Say we have the following file, example.html:

<!doctype html><title>Hello!</title><h1 id="world">World!<!--after--><script src="example.js" charset="UTF-8"></script>

Suppose example.js is a bundled version of something like this:

import inspect from 'unist-util-inspect';
import fromDOM from 'hast-util-from-dom';

const hast = fromDOM(document.documentElement.parentNode);

console.log(inspect.noColor(hast));

Viewing example.html should yield the following in the console:

root[2]
├─ doctype [name="html"]
└─ element[2] [tagName="html"]
   ├─ element[1] [tagName="head"]
   │  └─ element[1] [tagName="title"]
   │     └─ text: "Hello!"
   └─ element[1] [tagName="body"]
      └─ element[3] [tagName="h1"][properties={"id":"world"}]
         ├─ text: "World!"
         ├─ comment: "after"
         └─ element[0] [tagName="script"][properties={"src":"example.js","charSet":"UTF-8"}]

API

fromDOM(node)

Transform a DOM Node to a HAST Node.

This works in a similar way to the parse5 version except that it works directly from the DOM rather than a string of HTML. Consequently, it does not maintain location infomation.

License

ISC © Keith McKnight