anki/ts/editor/plain-text-input/remove-prohibited.ts
Henrik Giesel 7f737b60c6
Fix infinite update loop in editor with invalid input HTML (#1761)
* Use async function in PlainTextInput

* Clean up PlainTextInput

* Refactor logic from {Rich,Plain}TextInput into own files

* Remove prohibited tags on content.subscribe which also parses the html
2022-03-31 11:17:13 +10:00

32 lines
901 B
TypeScript

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import { createDummyDoc } from "../../lib/parsing";
const parser = new DOMParser();
function removeTag(element: HTMLElement, tagName: string): void {
for (const elem of element.getElementsByTagName(tagName)) {
elem.remove();
}
}
const prohibitedTags = ["script", "link", "style"];
/**
* The use cases for using those tags in the field html are slim to none.
* We want to make it easier to possibly display cards in an iframe in the future.
*/
function removeProhibitedTags(html: string): string {
const doc = parser.parseFromString(createDummyDoc(html), "text/html");
const body = doc.body;
for (const tag of prohibitedTags) {
removeTag(body, tag);
}
return doc.body.innerHTML;
}
export default removeProhibitedTags;