Skip to content

Commit

Permalink
fix: DOMParse bug
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangyu committed Sep 30, 2022
1 parent 735f417 commit 4cdc4d4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 31 deletions.
7 changes: 0 additions & 7 deletions src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ import AddonBase from "./module";
class AddonEvents extends AddonBase {
notifierCallback: any;
notifierCbkDict: any;
// Important!
// Due to unknown reasons, the DOMParser constructor fails after the tab is opened.
// We restore it from the preserved object constructor.
// We init this object when the addon is initialized for later use.
// See src/knowledge.ts
_DOMParser: DOMParser;
constructor(parent: Knowledge4Zotero) {
super(parent);
this.notifierCallback = {
Expand Down Expand Up @@ -177,7 +171,6 @@ class AddonEvents extends AddonBase {
},
};
this.notifierCbkDict = {};
this._DOMParser = new DOMParser();
}

public async onInit() {
Expand Down
4 changes: 0 additions & 4 deletions src/knowledge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,6 @@ class Knowledge extends AddonBase {
this._Addon.views.switchView(OutlineType.treeView);
this._Addon.views.updateOutline();
}
// Important!
// Due to unknown reasons, the DOMParser constructor fails after the tab is opened.
// We restore it from the preserved object constructor.
DOMParser = this._Addon.events._DOMParser.__proto__.constructor;
}

closeWorkspaceWindow() {
Expand Down
48 changes: 28 additions & 20 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ const asciidoctor = require("asciidoctor")();
const seedrandom = require("seedrandom");

class AddonParse extends AddonBase {
private getDOMParser(): DOMParser {
if (Zotero.platformMajorVersion > 60) {
return new DOMParser();
} else {
return Components.classes[
"@mozilla.org/xmlextras/domparser;1"
].createInstance(Components.interfaces.nsIDOMParser);
}
}

// A seedable version of Zotero.Utilities.randomString
private randomString(len: number, chars: string, seed: string) {
if (!chars) {
Expand All @@ -23,6 +33,7 @@ class AddonParse extends AddonBase {
}
return randomstring;
}

public parseNoteTree(note: Zotero.Item): TreeModel.Node<object> {
const noteLines = this._Addon.knowledge.getLinesInNote(note);
let tree = new TreeModel();
Expand Down Expand Up @@ -297,9 +308,7 @@ class AddonParse extends AddonBase {
.join("\n")}</div>`;
console.log(this.parseHTMLLines(item.getNote()).slice(0, lineCount));

let parser = Components.classes[
"@mozilla.org/xmlextras/domparser;1"
].createInstance(Components.interfaces.nsIDOMParser);
let parser = this.getDOMParser();
let doc = parser.parseFromString(note, "text/html");

// Make sure this is the new note
Expand All @@ -308,12 +317,14 @@ class AddonParse extends AddonBase {
);
if (metadataContainer) {
// Load base64 image data into src
let nodes = doc.querySelectorAll("img[data-attachment-key]");
let nodes = doc.querySelectorAll(
"img[data-attachment-key]"
) as NodeListOf<HTMLElement>;
for (let node of nodes) {
node.remove();
}

nodes = doc.querySelectorAll("span[style]");
nodes = doc.querySelectorAll("span[style]") as NodeListOf<HTMLElement>;
for (let node of nodes) {
// Browser converts #RRGGBBAA hex color to rgba function, and we convert it to rgb function,
// because word processors don't understand colors with alpha channel
Expand Down Expand Up @@ -411,9 +422,7 @@ class AddonParse extends AddonBase {
if (noteText.search(/data-schema-version/g) === -1) {
noteText = `<div data-schema-version="8">${noteText}\n</div>`;
}
let parser = Components.classes[
"@mozilla.org/xmlextras/domparser;1"
].createInstance(Components.interfaces.nsIDOMParser);
let parser = this.getDOMParser();
let doc = parser.parseFromString(noteText, "text/html");

let metadataContainer: Element = doc.querySelector(
Expand All @@ -423,17 +432,16 @@ class AddonParse extends AddonBase {
}

parseLineText(line: string): string {
const parser = Components.classes[
"@mozilla.org/xmlextras/domparser;1"
].createInstance(Components.interfaces.nsIDOMParser);
const parser = this.getDOMParser();
try {
if (line.search(/data-schema-version/g) === -1) {
line = `<div data-schema-version="8">${line}</div>`;
}
return parser
.parseFromString(line, "text/html")
.querySelector("body > div[data-schema-version]")
.innerText.trim();
return (
parser
.parseFromString(line, "text/html")
.querySelector("body > div[data-schema-version]") as HTMLElement
).innerText.trim();
} catch (e) {
return "";
}
Expand All @@ -453,10 +461,8 @@ class AddonParse extends AddonBase {

// A realization of Markdown Note.js translator
async parseNoteToMD(noteItem: Zotero.Item, options: any = {}) {
const doc = new DOMParser().parseFromString(
noteItem.getNote() || "",
"text/html"
);
const parser = this.getDOMParser();
const doc = parser.parseFromString(noteItem.getNote() || "", "text/html");
Components.utils.import("resource://gre/modules/osfile.jsm");
doc.querySelectorAll("span").forEach(function (span) {
if (span.style.textDecoration === "line-through") {
Expand Down Expand Up @@ -714,7 +720,9 @@ class AddonParse extends AddonBase {
return "[" + content + "](" + href + title + ")";
},
});
return turndownService.turndown(doc.body);
const parsedMD = turndownService.turndown(doc.body);
console.log(parsedMD);
return parsedMD;
}
}

Expand Down

0 comments on commit 4cdc4d4

Please sign in to comment.