🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign 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

1.0.3
latest
Source
npm
Version published
Weekly downloads
86
-14.85%
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';

or require for CommonJS:

const parse = require('@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.

Keywords

javascript

FAQs

Package last updated on 12 Jun 2023

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