Parse an HTML string into `DocumentFragment` or one `Element`, in a few bytes (in browser)
npm install doma[badge-gzip]: https://img.shields.io/bundlephobia/minzip/doma.svg?label=gzipped
[link-npm]: https://www.npmjs.com/package/doma
> Parse an HTML string into DocumentFragment or one Element, in a few bytes (in browser or jsdom)
```
npm install doma
`js`
// This module is only offered as a ES Module
import doma from 'doma';
`js
doma('Cats
and dogs');
//=> DocumentFragment[, Text(' and dogs')]
doma('the cow');
//=> DocumentFragment[Text('the cow')]
doma.one('beautiful bird');
//=>
doma.one('wild animal');
//=> null
`
#### Example: AJAXed page
Note: script tags are not executed, but other on* handlers will run normally once attached to the document.
`js
const response = await fetch('page.html');
const html = await response.text();
const dom = doma(html);
const ajaxedContent = dom.querySelector('#ajax-container').childNodes;
const ajaxedContainer = document.querySelector('#ajax-container');
ajaxedContainer.append(...ajaxedContent);
`
#### Example: Parse images from HTML
Note: images are not fetched when the HTML is parsed. The elements only become "active" (and start loading) once appended to the document.
`js
const html = 'They say it’s round
but actually it’s banana-shaped ';
const dom = doma(html);
// => DocumentFragment[Text('They say it’s round '), , Text(' but actually it’s banana-shaped ',
]
const images = dom.querySelectorAll('img');
// => NodeList[,
]
`
#### Example: Drop HTML tags from string
`js`
const html = 'Never gonna give you up, never gonna let you down';
const string = doma(html).textContent;
// => 'Never gonna give you up, never gonna let you down'
- select-dom - Lightweight querySelector/All` wrapper that outputs an Array.
- delegate-it - DOM event delegation, in <1KB.
- Refined GitHub - Uses this module.
MIT © Federico Brigante