feat: migrate to happy-dom from node-html-parser

This commit is contained in:
thecodrr
2022-04-19 00:04:57 +05:00
parent 3f6f5e6f9c
commit c9c57d87ab
6 changed files with 660 additions and 92 deletions

View File

@@ -112,7 +112,7 @@ test("update note", () =>
title: "I am a new title", title: "I am a new title",
content: { content: {
type: TEST_NOTE.content.type, type: TEST_NOTE.content.type,
data: "<p><br></p>", data: "<p><br/></p>",
}, },
pinned: true, pinned: true,
favorite: true, favorite: true,

View File

@@ -30,7 +30,7 @@ const notebookTest = (notebook = TEST_NOTEBOOK) =>
var TEST_NOTE = { var TEST_NOTE = {
content: { content: {
type: "tiny", type: "tiny",
data: `<p>Hello<br><span style="color:#f00">This is colorful</span></p>`, data: `<p>Hello<br/><span style="color:#f00">This is colorful</span></p>`,
}, },
}; };

View File

@@ -1,6 +1,6 @@
import showdown from "showdown"; import showdown from "showdown";
import dataurl from "../utils/dataurl"; import dataurl from "../utils/dataurl";
import { getDummyDocument, getInnerText, parseHTML } from "../utils/html-parser"; import { getDummyDocument, parseHTML } from "../utils/html-parser";
var converter = new showdown.Converter(); var converter = new showdown.Converter();
converter.setFlavor("original"); converter.setFlavor("original");
@@ -18,9 +18,7 @@ class Tiny {
} }
toTXT() { toTXT() {
return this.document.body return this.document.body.innerText;
? this.document.body.innerText || this.document.body.textContent
: this.document.textContent;
} }
toMD() { toMD() {
@@ -31,7 +29,7 @@ class Tiny {
const paragraph = this.document.querySelector("p"); const paragraph = this.document.querySelector("p");
if (!paragraph) return; if (!paragraph) return;
return getInnerText(paragraph); return paragraph.innerText;
} }
isEmpty() { isEmpty() {
@@ -66,7 +64,7 @@ class Tiny {
} }
} }
} }
return this.document.outerHTML || this.document.body.innerHTML; return this.document.body.innerHTML;
} }
removeAttachments(hashes) { removeAttachments(hashes) {
@@ -78,7 +76,7 @@ class Tiny {
attachment.remove(); attachment.remove();
} }
return this.document.outerHTML || this.document.body.innerHTML; return this.document.body.innerHTML;
} }
async extractAttachments(store) { async extractAttachments(store) {
@@ -141,7 +139,7 @@ class Tiny {
} }
} }
return { return {
data: this.document.outerHTML || this.document.body.innerHTML, data: this.document.body.innerHTML,
attachments, attachments,
}; };
} }

File diff suppressed because it is too large Load Diff

View File

@@ -33,10 +33,12 @@
"@stablelib/blake2s": "^1.0.1", "@stablelib/blake2s": "^1.0.1",
"async-mutex": "^0.3.2", "async-mutex": "^0.3.2",
"base64-arraybuffer": "^1.0.1", "base64-arraybuffer": "^1.0.1",
"cheerio": "^1.0.0-rc.10",
"dayjs": "^1.10.6", "dayjs": "^1.10.6",
"entities": "^3.0.1", "entities": "^3.0.1",
"fast-sort": "^2.0.1", "fast-sort": "^2.0.1",
"fflate": "^0.7.1", "fflate": "^0.7.1",
"happy-dom": "^2.55.0",
"liqe": "^1.13.0", "liqe": "^1.13.0",
"node-html-parser": "github:thecodrr/node-html-parser", "node-html-parser": "github:thecodrr/node-html-parser",
"qclone": "^1.0.4", "qclone": "^1.0.4",

View File

@@ -1,14 +1,17 @@
import { parse } from "node-html-parser";
import { decodeHTML5 } from "entities"; import { decodeHTML5 } from "entities";
import { Window } from "happy-dom";
export const parseHTML = const RealDOMParser =
typeof DOMParser === "undefined" "window" in global && "DOMParser" in window
? (input) => parse(input) ? new window.DOMParser()
: (input) => new DOMParser().parseFromString(input, "text/html"); : new new Window().DOMParser();
export const parseHTML = (input) =>
RealDOMParser.parseFromString(input, "text/html");
export function getDummyDocument() { export function getDummyDocument() {
const doc = parseHTML("<div></div>"); const doc = parseHTML("<div></div>");
return typeof DOMParser === "undefined" ? doc : doc; return doc;
} }
export function getInnerText(element) { export function getInnerText(element) {