deno.land / x / lume@v2.1.4 / core / utils / dom.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { DOMParser } from "../../deps/dom.ts";
const parser = new DOMParser();
/** Convert an Document instance to a string */export function documentToString(document: Document) { const { doctype, documentElement } = document;
if (!doctype) { return `<!DOCTYPE html>\n${documentElement?.outerHTML || ""}`; }
return `<!DOCTYPE ${doctype.name}` + (doctype.publicId ? ` PUBLIC "${doctype.publicId}"` : "") + (!doctype.publicId && doctype.systemId ? " SYSTEM" : "") + (doctype.systemId ? ` "${doctype.systemId}"` : "") + `>\n${documentElement?.outerHTML}`;}
/** Parse a string with HTML code and return a Document */export function stringToDocument(string: string): Document { const document = parser.parseFromString(string, "text/html");
if (!document) { throw new Error("Unable to parse the HTML code"); }
return document as unknown as Document;}
lume

Version Info

Tagged at
7 months ago