@web/parse5-utils
Utils for using parse5
Last updated 6 years ago by modern-web .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @web/parse5-utils 
SYNC missed versions from official npm registry.

parse5-utils

Utils for using parse5.

Usage

Examples:

import { parse } from 'parse5';
import { createElement, getTagName, appendChild, findElement } from '@web/parse5-utils';

const doc = parse(`
  <html>
    <body>
      <my-element></my-element>
      <div id="foo"></div>
    </body>
  </html>`);

const body = findElement(doc, e => getTagName(e) === 'body');
const script = createElement('script', { src: './foo.js' });
appendChild(body, script);
import { parse } from 'parse5';
import { getTagName, getAttribute, findElements } from '@web/parse5-utils';

const doc = parse(`
  <html>
    <body>
      <script src="./a.js"></script>
      <script type="module" src="./b.js"></script>
      <script type="module" src="./c.js"></script>
    </body>
  </html>`);

const allModuleScripts = findElements(
  doc,
  e => getTagName(e) === 'script' && getAttribute(e, 'type') === 'module',
);

appendToDocument and prependToDocument will inject a snippet of HTML into the page, making sure it is executed last or first respectively.

It tries to avoid changing the formatting of the original file, using parse5 to find out the location of body and head tags and string concatenation in the original code to do the actual injection. In case of incomplete or invalid HTML it may fall back parse5 to generate a valid document and inject using AST manipulation.

import { prependToDocument, appendToDocument } from '@web/parse5-utils';

const html = '<html><body></body></html>';
const htmlWithInjectedScript = appendToDocument(
  html,
  '<scrip type="module" src="./injected-script.js"></script>',
);

Available functions

  • createDocument
  • createDocumentFragment
  • createElement
  • createScript
  • createCommentNode
  • appendChild
  • insertBefore
  • setTemplateContent
  • getTemplateContent
  • setDocumentType
  • setDocumentMode
  • getDocumentMode
  • detachNode
  • insertText
  • insertTextBefore
  • adoptAttributes
  • getFirstChild
  • getChildNodes
  • getParentNode
  • getAttrList
  • getTagName
  • getNamespaceURI
  • getTextNodeContent
  • getCommentNodeContent
  • getDocumentTypeNodeName
  • getDocumentTypeNodePublicId
  • getDocumentTypeNodeSystemId
  • isTextNode
  • isCommentNode
  • isDocumentTypeNode
  • isElementNode
  • setNodeSourceCodeLocation
  • getNodeSourceCodeLocation
  • updateNodeSourceCodeLocation
  • isHtmlFragment
  • hasAttribute
  • getAttribute
  • getAttributes
  • setAttribute
  • setAttributes
  • removeAttribute
  • setTextContent
  • getTextContent
  • remove
  • findNode
  • findNodes
  • findElement
  • findElements
  • prepend
  • prependToDocument
  • appendToDocument

Current Tags

  • 0.0.0-canary-20240219104536                                ...           canary (3 years ago)
  • 3.0.1                                ...           latest (17 days ago)

31 Versions

  • 3.0.1                                ...           17 days ago
  • 3.0.0                                ...           5 months ago
  • 2.1.1                                ...           10 months ago
  • 0.0.0-canary-20240219104536                                ...           3 years ago
  • 0.0.0-canary-20240219104126                                ...           3 years ago
  • 0.0.0-canary-20240212171943                                ...           3 years ago
  • 0.0.0-canary-20240105101412                                ...           3 years ago
  • 0.0.0-canary-20240105095248                                ...           3 years ago
  • 0.0.0-canary-20240105093823                                ...           3 years ago
  • 0.0.0-canary-20240105092607                                ...           3 years ago
  • 0.0.0-canary-20240105091725                                ...           3 years ago
  • 0.0.0-canary-20231122093600                                ...           3 years ago
  • 2.1.0                                ...           3 years ago
  • 2.0.2                                ...           3 years ago
  • 2.0.1                                ...           3 years ago
  • 2.0.0                                ...           4 years ago
  • 0.0.0-canary-20230420104136                                ...           4 years ago
  • 0.0.0-canary-20230416222938                                ...           4 years ago
  • 0.0.0-canary-20230405181124                                ...           4 years ago
  • 0.0.0-canary-20230404134439                                ...           4 years ago
  • 1.3.1                                ...           4 years ago
  • 0.0.0-canary-20230404122726                                ...           4 years ago
  • 0.0.0-canary-20230403143511                                ...           4 years ago
  • 1.3.0                                ...           5 years ago
  • 1.2.2                                ...           6 years ago
  • 1.2.1                                ...           6 years ago
  • 1.2.0                                ...           6 years ago
  • 1.1.2                                ...           6 years ago
  • 1.1.1                                ...           6 years ago
  • 1.1.0                                ...           6 years ago
  • 1.0.0                                ...           6 years ago
Downloads
Total 69
Today 3
This Week 5
This Month 69
Last Day 1
Last Week 64
Last Month 0
Dependencies (2)
Dev Dependencies (0)
None

© 2010 - cnpmjs.org x YWFE | Home | YWFE