将 HTML 字符串解析为 `DocumentFragment` 或一个 `Element`, 占用很少字节(在浏览器中)
Parse an HTML string into
DocumentFragmentor oneElement, in a few bytes (in browser or jsdom)
npm install doma// This module is only offered as a ES Module
import doma from 'doma';doma('
Cats
and dogs');
//=> DocumentFragment[
, Text(' and dogs')]
doma('the cow');
//=> DocumentFragment[Text('the cow')]
doma.one('beautiful bird');
//=>
doma.one('wild animal');
//=> nullNote: script tags are not executed, but other on* handlers will run normally once attached to the document.
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);Note: images are not fetched when the HTML is parsed. The elements only become "active" (and start loading) once appended to the document.
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[, ]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'querySelector/All wrapper that outputs an Array.MIT © Federico Brigante
暂无开放 Issues,或尚未同步最近议题。