1.0.3 • Published 2 years ago
@js-bits/dom-parser v1.0.3
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-parserInstall with yarn:
yarn add @js-bits/dom-parserImport 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')); // NodeListOptionally 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')); // NodeListNotes
- Minimal size (bytes) in a browser since DOMParser is natively supported. Does not include any polyfills.