fix: trim text extracted from html to get correct headline

This commit is contained in:
thecodrr
2021-02-25 21:30:57 +05:00
parent fa97ce04ff
commit c0e78aa5c9
2 changed files with 14 additions and 2 deletions

View File

@@ -75,6 +75,18 @@ test("note should get headline from content", () =>
expect(note.headline).toBe("This is a very colorful existence.");
}));
test("note should get headline from content containing only lists", () =>
noteTest({
...TEST_NOTE,
content: {
type: TEST_NOTE.content.type,
data: `<ol style="list-style-type: decimal;" data-mce-style="list-style-type: decimal;"><li>Hello I won't be a headline :(</li><li>Me too.</li><li>Gold.</li></ol>`,
},
}).then(async ({ db, id }) => {
let note = db.notes.note(id);
expect(note.headline).toBe("Hello I won't be a headline :(Me too.Gold.");
}));
test("note title should allow trailing space", () =>
noteTest({ title: "Hello ", content: TEST_NOTE.content }).then(
async ({ db, id }) => {

View File

@@ -15,9 +15,9 @@ class Tiny {
}
toTXT() {
if (window.DOMParser) {
if ("DOMParser" in window || "DOMParser" in global) {
let doc = new DOMParser().parseFromString(this.data, "text/html");
return (doc.body || doc.firstElementChild).textContent || "";
return doc.body.textContent.trim();
} else {
return decode(
this.data.replace(/<br[^>]*>/gi, "\n").replace(/<[^>]+>/g, "")