🚨 Latest Research:Tanstack npm Packages Compromised in Ongoing Mini Shai-Hulud Supply-Chain Attack.Learn More →
Socket
Book a DemoSign in
Socket

@js-bits/dom-parser

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@js-bits/dom-parser

Cross-environment (nodejs/web) DOM parser for XML and HTML

Source
npmnpm
Version
0.0.5
Version published
Weekly downloads
81
-5.81%
Maintainers
1
Weekly downloads
 
Created
Source

Cross-environment (nodejs/web) DOM parser for XML and HTML

This package uses built-in DOMParser in a browser and jsdom module in a Node.js environment and exports parse function.

Installation

Install with npm:

npm install @js-bits/dom-parser

Install with yarn:

yarn add @js-bits/dom-parser

Import where you need it:

import parse from '@js-bits/dom-parser';

How to use

const xml = `<?xml version="1.0" encoding="utf-8"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>
`;

const doc = parse(xml); // Document
console.log(doc.querySelectorAll('heading')); // NodeList

Optionally accepts second mimeType parameter ("text/xml" by default).

const svg = `<svg xmlns="http://www.w3.org/2000/svg" height="100" width="100" viewBox="0 0 100 100">
  <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
</svg>
`;

const doc = parse(svg, 'image/svg+xml'); // Document
console.log(doc.querySelectorAll('circle')); // NodeList

Notes

  • Minimal size (bytes) in a browser since DOMParser is natively supported. Does not include any polyfills.
  • Requires ECMAScript modules to be enabled in Node.js environment. Otherwise, compile into a CommonJS module.

Keywords

javascript

FAQs

Package last updated on 29 Jun 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts