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",
content: {
type: TEST_NOTE.content.type,
data: "<p><br></p>",
data: "<p><br/></p>",
},
pinned: true,
favorite: true,

View File

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

File diff suppressed because it is too large Load Diff

View File

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

View File

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