2019-02-05 04:59:03 +01:00
|
|
|
/* Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
* License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html */
|
|
|
|
|
2021-01-28 17:43:56 +01:00
|
|
|
let currentField: EditingContainer | null = null;
|
2021-01-29 00:43:45 +01:00
|
|
|
let changeTimer: number | null = null;
|
|
|
|
let currentNoteId: number | null = null;
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2019-12-18 04:53:31 +01:00
|
|
|
declare interface String {
|
2021-01-18 17:42:29 +01:00
|
|
|
format(...args: string[]): string;
|
2019-12-18 04:53:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* kept for compatibility with add-ons */
|
2021-01-18 17:42:29 +01:00
|
|
|
String.prototype.format = function (...args: string[]): string {
|
2021-01-19 02:48:41 +01:00
|
|
|
return this.replace(/\{\d+\}/g, (m: string): void => {
|
2021-01-19 03:23:29 +01:00
|
|
|
const match = m.match(/\d+/);
|
2021-01-19 02:48:41 +01:00
|
|
|
|
2021-01-19 03:23:29 +01:00
|
|
|
return match ? args[match[0]] : "";
|
|
|
|
});
|
2017-07-28 08:19:06 +02:00
|
|
|
};
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function setFGButton(col: string): void {
|
2021-01-28 19:52:49 +01:00
|
|
|
document.getElementById("forecolor").style.backgroundColor = col;
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function saveNow(keepFocus: boolean): void {
|
2018-05-28 05:40:35 +02:00
|
|
|
if (!currentField) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-28 08:19:06 +02:00
|
|
|
clearChangeTimer();
|
2018-05-28 05:40:35 +02:00
|
|
|
|
|
|
|
if (keepFocus) {
|
|
|
|
saveField("key");
|
|
|
|
} else {
|
2019-02-05 01:46:47 +01:00
|
|
|
// triggers onBlur, which saves
|
2021-01-28 19:52:49 +01:00
|
|
|
currentField.blurEditingArea();
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function triggerKeyTimer(): void {
|
2018-07-23 04:23:38 +02:00
|
|
|
clearChangeTimer();
|
2020-06-24 13:54:38 +02:00
|
|
|
changeTimer = setTimeout(function () {
|
2018-07-23 04:23:38 +02:00
|
|
|
updateButtonState();
|
|
|
|
saveField("key");
|
|
|
|
}, 600);
|
|
|
|
}
|
|
|
|
|
2020-08-11 02:32:58 +02:00
|
|
|
interface Selection {
|
|
|
|
modify(s: string, t: string, u: string): void;
|
|
|
|
}
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function onKey(evt: KeyboardEvent): void {
|
2017-07-28 08:19:06 +02:00
|
|
|
// esc clears focus, allowing dialog to close
|
2021-01-16 20:23:46 +01:00
|
|
|
if (evt.code === "Escape") {
|
2021-01-28 19:52:49 +01:00
|
|
|
currentField.blurEditingArea();
|
2017-07-28 08:19:06 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-08-11 02:09:03 +02:00
|
|
|
|
2021-01-14 13:32:30 +01:00
|
|
|
// prefer <br> instead of <div></div>
|
2021-01-16 20:23:46 +01:00
|
|
|
if (evt.code === "Enter" && !inListItem()) {
|
2021-01-14 13:32:30 +01:00
|
|
|
evt.preventDefault();
|
|
|
|
document.execCommand("insertLineBreak");
|
|
|
|
}
|
|
|
|
|
2021-01-28 18:37:38 +01:00
|
|
|
// // fix Ctrl+right/left handling in RTL fields
|
|
|
|
if (currentField.isRightToLeft()) {
|
|
|
|
const selection = currentField.getSelection();
|
|
|
|
const granularity = evt.ctrlKey ? "word" : "character";
|
|
|
|
const alter = evt.shiftKey ? "extend" : "move";
|
|
|
|
|
|
|
|
switch (evt.code) {
|
|
|
|
case "ArrowRight":
|
|
|
|
selection.modify(alter, "right", granularity);
|
|
|
|
evt.preventDefault();
|
|
|
|
return;
|
|
|
|
case "ArrowLeft":
|
|
|
|
selection.modify(alter, "left", granularity);
|
|
|
|
evt.preventDefault();
|
|
|
|
return;
|
2020-08-11 02:09:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-23 04:23:38 +02:00
|
|
|
triggerKeyTimer();
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function onKeyUp(evt: KeyboardEvent): void {
|
2021-01-15 18:18:42 +01:00
|
|
|
// Avoid div element on remove
|
2021-01-16 20:23:46 +01:00
|
|
|
if (evt.code === "Enter" || evt.code === "Backspace") {
|
2021-01-28 20:19:04 +01:00
|
|
|
const anchor = currentField.getSelection().anchorNode;
|
2021-01-15 18:18:42 +01:00
|
|
|
|
|
|
|
if (
|
|
|
|
nodeIsElement(anchor) &&
|
|
|
|
anchor.tagName === "DIV" &&
|
2021-01-29 17:41:27 +01:00
|
|
|
!(anchor instanceof EditingContainer) &&
|
2021-01-15 18:18:42 +01:00
|
|
|
anchor.childElementCount === 1 &&
|
|
|
|
anchor.children[0].tagName === "BR"
|
|
|
|
) {
|
|
|
|
anchor.replaceWith(anchor.children[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 18:59:07 +01:00
|
|
|
function nodeIsElement(node: Node): node is Element {
|
2021-01-18 17:05:05 +01:00
|
|
|
return node.nodeType === Node.ELEMENT_NODE;
|
2021-01-14 18:59:07 +01:00
|
|
|
}
|
|
|
|
|
2021-01-26 23:42:04 +01:00
|
|
|
const INLINE_TAGS = [
|
|
|
|
"A",
|
|
|
|
"ABBR",
|
|
|
|
"ACRONYM",
|
|
|
|
"AUDIO",
|
|
|
|
"B",
|
|
|
|
"BDI",
|
|
|
|
"BDO",
|
|
|
|
"BIG",
|
|
|
|
"BR",
|
|
|
|
"BUTTON",
|
|
|
|
"CANVAS",
|
|
|
|
"CITE",
|
|
|
|
"CODE",
|
|
|
|
"DATA",
|
|
|
|
"DATALIST",
|
|
|
|
"DEL",
|
|
|
|
"DFN",
|
|
|
|
"EM",
|
|
|
|
"EMBED",
|
|
|
|
"I",
|
|
|
|
"IFRAME",
|
|
|
|
"IMG",
|
|
|
|
"INPUT",
|
|
|
|
"INS",
|
|
|
|
"KBD",
|
|
|
|
"LABEL",
|
|
|
|
"MAP",
|
|
|
|
"MARK",
|
|
|
|
"METER",
|
|
|
|
"NOSCRIPT",
|
|
|
|
"OBJECT",
|
|
|
|
"OUTPUT",
|
|
|
|
"PICTURE",
|
|
|
|
"PROGRESS",
|
|
|
|
"Q",
|
|
|
|
"RUBY",
|
|
|
|
"S",
|
|
|
|
"SAMP",
|
|
|
|
"SCRIPT",
|
|
|
|
"SELECT",
|
|
|
|
"SLOT",
|
|
|
|
"SMALL",
|
|
|
|
"SPAN",
|
|
|
|
"STRONG",
|
|
|
|
"SUB",
|
|
|
|
"SUP",
|
|
|
|
"SVG",
|
|
|
|
"TEMPLATE",
|
|
|
|
"TEXTAREA",
|
|
|
|
"TIME",
|
|
|
|
"U",
|
|
|
|
"TT",
|
|
|
|
"VAR",
|
|
|
|
"VIDEO",
|
|
|
|
"WBR",
|
|
|
|
];
|
|
|
|
|
2021-01-26 23:00:55 +01:00
|
|
|
function nodeIsInline(node: Node): boolean {
|
2021-01-26 23:49:48 +01:00
|
|
|
return !nodeIsElement(node) || INLINE_TAGS.includes(node.tagName);
|
2021-01-26 23:00:55 +01:00
|
|
|
}
|
|
|
|
|
2021-01-14 18:59:07 +01:00
|
|
|
function inListItem(): boolean {
|
2021-01-28 17:43:56 +01:00
|
|
|
const anchor = currentField.getSelection().anchorNode;
|
2021-01-14 18:59:07 +01:00
|
|
|
|
|
|
|
let inList = false;
|
2021-01-26 23:49:48 +01:00
|
|
|
let n = nodeIsElement(anchor) ? anchor : anchor.parentElement;
|
2021-01-14 18:59:07 +01:00
|
|
|
while (n) {
|
|
|
|
inList = inList || window.getComputedStyle(n).display == "list-item";
|
|
|
|
n = n.parentElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
return inList;
|
|
|
|
}
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function insertNewline(): void {
|
2017-07-28 08:19:06 +02:00
|
|
|
if (!inPreEnvironment()) {
|
2017-07-31 05:22:35 +02:00
|
|
|
setFormat("insertText", "\n");
|
2017-07-28 08:19:06 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// in some cases inserting a newline will not show any changes,
|
|
|
|
// as a trailing newline at the end of a block does not render
|
|
|
|
// differently. so in such cases we note the height has not
|
|
|
|
// changed and insert an extra newline.
|
|
|
|
|
2021-01-28 20:19:04 +01:00
|
|
|
const r = currentField.getSelection().getRangeAt(0);
|
2017-07-28 08:19:06 +02:00
|
|
|
if (!r.collapsed) {
|
|
|
|
// delete any currently selected text first, making
|
|
|
|
// sure the delete is undoable
|
|
|
|
setFormat("delete");
|
|
|
|
}
|
|
|
|
|
2019-12-20 22:33:15 +01:00
|
|
|
const oldHeight = currentField.clientHeight;
|
2017-07-31 05:22:35 +02:00
|
|
|
setFormat("inserthtml", "\n");
|
2017-07-28 08:48:49 +02:00
|
|
|
if (currentField.clientHeight === oldHeight) {
|
2017-07-31 05:22:35 +02:00
|
|
|
setFormat("inserthtml", "\n");
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// is the cursor in an environment that respects whitespace?
|
2021-01-14 18:59:07 +01:00
|
|
|
function inPreEnvironment(): boolean {
|
2021-01-28 20:19:04 +01:00
|
|
|
const anchor = currentField.getSelection().anchorNode;
|
2021-01-14 18:59:07 +01:00
|
|
|
const n = nodeIsElement(anchor) ? anchor : anchor.parentElement;
|
|
|
|
|
2017-07-28 08:19:06 +02:00
|
|
|
return window.getComputedStyle(n).whiteSpace.startsWith("pre");
|
|
|
|
}
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function onInput(): void {
|
2018-07-23 04:23:38 +02:00
|
|
|
// make sure IME changes get saved
|
|
|
|
triggerKeyTimer();
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function updateButtonState(): void {
|
2019-12-20 22:33:15 +01:00
|
|
|
const buts = ["bold", "italic", "underline", "superscript", "subscript"];
|
|
|
|
for (const name of buts) {
|
2021-01-19 03:06:44 +01:00
|
|
|
const elem = document.querySelector(`#${name}`) as HTMLElement;
|
|
|
|
elem.classList.toggle("highlighted", document.queryCommandState(name));
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// fixme: forecolor
|
2019-12-18 07:12:39 +01:00
|
|
|
// 'col': document.queryCommandValue("forecolor")
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function toggleEditorButton(buttonid: string): void {
|
2021-01-19 03:06:44 +01:00
|
|
|
const button = $(buttonid)[0];
|
|
|
|
button.classList.toggle("highlighted");
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function setFormat(cmd: string, arg?: any, nosave: boolean = false): void {
|
2017-07-28 08:19:06 +02:00
|
|
|
document.execCommand(cmd, false, arg);
|
|
|
|
if (!nosave) {
|
2019-12-18 07:12:39 +01:00
|
|
|
saveField("key");
|
2017-07-28 08:19:06 +02:00
|
|
|
updateButtonState();
|
|
|
|
}
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function clearChangeTimer(): void {
|
2017-07-28 08:19:06 +02:00
|
|
|
if (changeTimer) {
|
|
|
|
clearTimeout(changeTimer);
|
|
|
|
changeTimer = null;
|
|
|
|
}
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-26 23:00:55 +01:00
|
|
|
function onFocus(evt: FocusEvent): void {
|
2021-01-28 17:43:56 +01:00
|
|
|
const elem = evt.currentTarget as EditingContainer;
|
2018-04-30 07:26:54 +02:00
|
|
|
if (currentField === elem) {
|
|
|
|
// anki window refocused; current element unchanged
|
|
|
|
return;
|
|
|
|
}
|
2021-01-29 00:43:45 +01:00
|
|
|
elem.focusEditingArea();
|
2017-07-28 08:19:06 +02:00
|
|
|
currentField = elem;
|
2021-01-29 17:41:27 +01:00
|
|
|
pycmd(`focus:${currentField.ord}`);
|
2017-07-28 08:19:06 +02:00
|
|
|
enableButtons();
|
|
|
|
// do this twice so that there's no flicker on newer versions
|
|
|
|
caretToEnd();
|
|
|
|
// scroll if bottom of element off the screen
|
2021-01-18 17:05:05 +01:00
|
|
|
function pos(elem: HTMLElement): number {
|
2019-12-20 22:33:15 +01:00
|
|
|
let cur = 0;
|
2017-07-28 08:19:06 +02:00
|
|
|
do {
|
2021-01-18 17:05:05 +01:00
|
|
|
cur += elem.offsetTop;
|
|
|
|
elem = elem.offsetParent as HTMLElement;
|
|
|
|
} while (elem);
|
2017-07-28 08:19:06 +02:00
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
|
2019-12-20 22:33:15 +01:00
|
|
|
const y = pos(elem);
|
2019-12-18 07:12:39 +01:00
|
|
|
if (
|
|
|
|
window.pageYOffset + window.innerHeight < y + elem.offsetHeight ||
|
|
|
|
window.pageYOffset > y
|
|
|
|
) {
|
2017-07-28 08:19:06 +02:00
|
|
|
window.scroll(0, y + elem.offsetHeight - window.innerHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function focusField(n: number): void {
|
2021-01-28 19:52:49 +01:00
|
|
|
const field = document.getElementById(`f${n}`) as EditingContainer;
|
|
|
|
|
|
|
|
if (field) {
|
|
|
|
field.focusEditingArea();
|
2017-08-05 07:15:19 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function focusIfField(x: number, y: number): boolean {
|
2020-08-25 16:23:34 +02:00
|
|
|
const elements = document.elementsFromPoint(x, y);
|
|
|
|
for (let i = 0; i < elements.length; i++) {
|
2021-01-28 17:43:56 +01:00
|
|
|
let elem = elements[i] as EditingContainer;
|
2021-01-29 17:41:27 +01:00
|
|
|
if (elem instanceof EditingContainer) {
|
2021-01-28 19:52:49 +01:00
|
|
|
elem.focusEditingArea();
|
2020-08-25 16:23:34 +02:00
|
|
|
// the focus event may not fire if the window is not active, so make sure
|
|
|
|
// the current field is set
|
|
|
|
currentField = elem;
|
|
|
|
return true;
|
|
|
|
}
|
2020-08-19 06:37:14 +02:00
|
|
|
}
|
|
|
|
return false;
|
2017-08-01 09:40:51 +02:00
|
|
|
}
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function onPaste(): void {
|
2017-07-28 08:19:06 +02:00
|
|
|
pycmd("paste");
|
|
|
|
window.event.preventDefault();
|
|
|
|
}
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function caretToEnd(): void {
|
2021-01-29 00:43:45 +01:00
|
|
|
const range = document.createRange();
|
|
|
|
range.selectNodeContents(currentField.editingArea);
|
|
|
|
range.collapse(false);
|
|
|
|
const selection = currentField.getSelection();
|
|
|
|
selection.removeAllRanges();
|
|
|
|
selection.addRange(range);
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function onBlur(): void {
|
2019-02-05 01:46:47 +01:00
|
|
|
if (!currentField) {
|
|
|
|
return;
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
2019-01-29 00:51:54 +01:00
|
|
|
|
|
|
|
if (document.activeElement === currentField) {
|
|
|
|
// other widget or window focused; current field unchanged
|
2019-02-05 01:46:47 +01:00
|
|
|
saveField("key");
|
|
|
|
} else {
|
|
|
|
saveField("blur");
|
|
|
|
currentField = null;
|
|
|
|
disableButtons();
|
2019-01-29 00:51:54 +01:00
|
|
|
}
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-28 17:24:07 +01:00
|
|
|
function containsInlineContent(field: Element): boolean {
|
2021-01-26 23:00:55 +01:00
|
|
|
if (field.childNodes.length === 0) {
|
|
|
|
// for now, for all practical purposes, empty fields are in block mode
|
|
|
|
return false;
|
2021-01-26 21:26:04 +01:00
|
|
|
}
|
|
|
|
|
2021-01-26 23:00:55 +01:00
|
|
|
for (const child of field.children) {
|
|
|
|
if (!nodeIsInline(child)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2021-01-26 21:26:04 +01:00
|
|
|
}
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function saveField(type: "blur" | "key"): void {
|
2019-02-05 01:46:47 +01:00
|
|
|
clearChangeTimer();
|
2017-07-28 08:19:06 +02:00
|
|
|
if (!currentField) {
|
|
|
|
// no field has been focused yet
|
|
|
|
return;
|
|
|
|
}
|
2021-01-26 21:26:04 +01:00
|
|
|
|
2021-01-29 19:34:44 +01:00
|
|
|
pycmd(`${type}:${currentField.ord}:${currentNoteId}:${currentField.fieldHTML}`);
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function wrappedExceptForWhitespace(text: string, front: string, back: string): string {
|
2019-12-20 22:33:15 +01:00
|
|
|
const match = text.match(/^(\s*)([^]*?)(\s*)$/);
|
2017-07-28 08:19:06 +02:00
|
|
|
return match[1] + front + match[2] + back + match[3];
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-20 17:01:16 +01:00
|
|
|
function preventButtonFocus(): void {
|
|
|
|
for (const element of document.querySelectorAll("button.linkb")) {
|
|
|
|
element.addEventListener("mousedown", (evt: Event) => {
|
|
|
|
evt.preventDefault();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function disableButtons(): void {
|
2017-12-08 12:06:16 +01:00
|
|
|
$("button.linkb:not(.perm)").prop("disabled", true);
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function enableButtons(): void {
|
2017-07-28 08:19:06 +02:00
|
|
|
$("button.linkb").prop("disabled", false);
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
|
|
|
// disable the buttons if a field is not currently focused
|
2021-01-18 17:05:05 +01:00
|
|
|
function maybeDisableButtons(): void {
|
2021-01-29 17:41:27 +01:00
|
|
|
if (document.activeElement instanceof EditingContainer) {
|
2017-07-28 08:19:06 +02:00
|
|
|
enableButtons();
|
2021-01-29 17:41:27 +01:00
|
|
|
} else {
|
|
|
|
disableButtons();
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function wrap(front: string, back: string): void {
|
2019-12-17 09:27:41 +01:00
|
|
|
wrapInternal(front, back, false);
|
|
|
|
}
|
|
|
|
|
2020-01-16 01:33:36 +01:00
|
|
|
/* currently unused */
|
2021-01-18 17:05:05 +01:00
|
|
|
function wrapIntoText(front: string, back: string): void {
|
2019-12-17 09:27:41 +01:00
|
|
|
wrapInternal(front, back, true);
|
|
|
|
}
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function wrapInternal(front: string, back: string, plainText: boolean): void {
|
2021-01-28 20:19:04 +01:00
|
|
|
const s = currentField.getSelection();
|
2019-12-20 22:33:15 +01:00
|
|
|
let r = s.getRangeAt(0);
|
2019-12-21 11:18:58 +01:00
|
|
|
const content = r.cloneContents();
|
|
|
|
const span = document.createElement("span");
|
2017-07-28 08:19:06 +02:00
|
|
|
span.appendChild(content);
|
2019-12-17 09:27:41 +01:00
|
|
|
if (plainText) {
|
2019-12-20 22:33:15 +01:00
|
|
|
const new_ = wrappedExceptForWhitespace(span.innerText, front, back);
|
2019-12-17 09:27:41 +01:00
|
|
|
setFormat("inserttext", new_);
|
|
|
|
} else {
|
2019-12-20 22:33:15 +01:00
|
|
|
const new_ = wrappedExceptForWhitespace(span.innerHTML, front, back);
|
2019-12-17 09:27:41 +01:00
|
|
|
setFormat("inserthtml", new_);
|
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
if (!span.innerHTML) {
|
|
|
|
// run with an empty selection; move cursor back past postfix
|
|
|
|
r = s.getRangeAt(0);
|
|
|
|
r.setStart(r.startContainer, r.startOffset - back.length);
|
|
|
|
r.collapse(true);
|
|
|
|
s.removeAllRanges();
|
|
|
|
s.addRange(r);
|
|
|
|
}
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function onCutOrCopy(): boolean {
|
2017-09-02 05:48:03 +02:00
|
|
|
pycmd("cutOrCopy");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-28 15:42:37 +01:00
|
|
|
class EditingArea extends HTMLElement {
|
2021-01-28 17:16:10 +01:00
|
|
|
set fieldHTML(content: string) {
|
|
|
|
this.innerHTML = content;
|
|
|
|
|
2021-01-28 17:24:07 +01:00
|
|
|
if (containsInlineContent(this)) {
|
2021-01-28 17:16:10 +01:00
|
|
|
this.appendChild(document.createElement("br"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get fieldHTML(): string {
|
2021-01-28 17:24:07 +01:00
|
|
|
return containsInlineContent(this) && this.innerHTML.endsWith("<br>")
|
2021-01-28 17:16:10 +01:00
|
|
|
? this.innerHTML.slice(0, -4) // trim trailing <br>
|
|
|
|
: this.innerHTML;
|
|
|
|
}
|
2021-01-29 20:13:02 +01:00
|
|
|
|
|
|
|
connectedCallback() {
|
|
|
|
this.setAttribute("contenteditable", "");
|
|
|
|
}
|
2021-01-28 15:42:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
customElements.define("editing-area", EditingArea);
|
|
|
|
|
2021-01-28 17:16:10 +01:00
|
|
|
class EditingContainer extends HTMLDivElement {
|
|
|
|
editingArea: EditingArea;
|
2021-01-29 15:50:34 +01:00
|
|
|
baseStyle: HTMLStyleElement;
|
2021-01-28 15:42:37 +01:00
|
|
|
|
2021-01-29 15:50:34 +01:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.attachShadow({ mode: "open" });
|
2021-01-28 17:16:10 +01:00
|
|
|
this.className = "field";
|
|
|
|
|
2021-01-29 15:50:34 +01:00
|
|
|
const rootStyle = document.createElement("link");
|
|
|
|
rootStyle.setAttribute("rel", "stylesheet");
|
|
|
|
rootStyle.setAttribute("href", "./_anki/css/editing-area.css");
|
2021-01-29 19:34:44 +01:00
|
|
|
this.shadowRoot.appendChild(rootStyle);
|
2021-01-29 15:50:34 +01:00
|
|
|
|
|
|
|
this.baseStyle = document.createElement("style");
|
|
|
|
this.baseStyle.setAttribute("rel", "stylesheet");
|
2021-01-29 19:34:44 +01:00
|
|
|
this.shadowRoot.appendChild(this.baseStyle);
|
2021-01-29 15:50:34 +01:00
|
|
|
|
|
|
|
this.editingArea = document.createElement("editing-area") as EditingArea;
|
|
|
|
this.shadowRoot.appendChild(this.editingArea);
|
|
|
|
}
|
|
|
|
|
2021-01-29 17:41:27 +01:00
|
|
|
get ord(): number {
|
|
|
|
return Number(this.getAttribute("ord"));
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:13:02 +01:00
|
|
|
set fieldHTML(content: string) {
|
|
|
|
this.editingArea.fieldHTML = content;
|
|
|
|
}
|
|
|
|
|
|
|
|
get fieldHTML(): string {
|
|
|
|
return this.editingArea.fieldHTML;
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:50:34 +01:00
|
|
|
connectedCallback(): void {
|
2021-01-28 17:16:10 +01:00
|
|
|
this.addEventListener("keydown", onKey);
|
|
|
|
this.addEventListener("keyup", onKeyUp);
|
|
|
|
this.addEventListener("input", onInput);
|
|
|
|
this.addEventListener("focus", onFocus);
|
|
|
|
this.addEventListener("blur", onBlur);
|
|
|
|
this.addEventListener("paste", onPaste);
|
|
|
|
this.addEventListener("copy", onCutOrCopy);
|
|
|
|
this.addEventListener("oncut", onCutOrCopy);
|
|
|
|
|
2021-01-29 15:50:34 +01:00
|
|
|
const baseStyleSheet = this.baseStyle.sheet as CSSStyleSheet;
|
2021-01-29 17:51:26 +01:00
|
|
|
baseStyleSheet.insertRule("editing-area {}", 0);
|
2021-01-29 15:50:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
disconnectedCallback(): void {
|
|
|
|
this.removeEventListener("keydown", onKey);
|
|
|
|
this.removeEventListener("keyup", onKeyUp);
|
|
|
|
this.removeEventListener("input", onInput);
|
|
|
|
this.removeEventListener("focus", onFocus);
|
|
|
|
this.removeEventListener("blur", onBlur);
|
|
|
|
this.removeEventListener("paste", onPaste);
|
|
|
|
this.removeEventListener("copy", onCutOrCopy);
|
|
|
|
this.removeEventListener("oncut", onCutOrCopy);
|
2021-01-28 17:16:10 +01:00
|
|
|
}
|
|
|
|
|
2021-01-29 14:00:33 +01:00
|
|
|
initialize(color: string, content: string): void {
|
2021-01-28 22:01:34 +01:00
|
|
|
this.setBaseColor(color);
|
2021-01-28 17:16:10 +01:00
|
|
|
this.editingArea.fieldHTML = content;
|
|
|
|
}
|
|
|
|
|
2021-01-28 22:01:34 +01:00
|
|
|
setBaseColor(color: string): void {
|
2021-01-29 15:50:34 +01:00
|
|
|
const styleSheet = this.baseStyle.sheet as CSSStyleSheet;
|
|
|
|
const firstRule = styleSheet.cssRules[0] as CSSStyleRule;
|
2021-01-28 22:01:34 +01:00
|
|
|
firstRule.style.color = color;
|
|
|
|
}
|
|
|
|
|
2021-01-28 18:37:38 +01:00
|
|
|
setBaseStyling(fontFamily: string, fontSize: string, direction: string): void {
|
2021-01-29 15:50:34 +01:00
|
|
|
const styleSheet = this.baseStyle.sheet as CSSStyleSheet;
|
|
|
|
const firstRule = styleSheet.cssRules[0] as CSSStyleRule;
|
2021-01-28 21:38:55 +01:00
|
|
|
firstRule.style.fontFamily = fontFamily;
|
|
|
|
firstRule.style.fontSize = fontSize;
|
|
|
|
firstRule.style.direction = direction;
|
|
|
|
}
|
|
|
|
|
2021-01-28 18:37:38 +01:00
|
|
|
isRightToLeft(): boolean {
|
|
|
|
return this.editingArea.style.direction === "rtl";
|
|
|
|
}
|
|
|
|
|
2021-01-28 17:43:56 +01:00
|
|
|
getSelection(): Selection {
|
2021-01-29 15:50:34 +01:00
|
|
|
return this.shadowRoot.getSelection();
|
2021-01-28 17:43:56 +01:00
|
|
|
}
|
|
|
|
|
2021-01-28 19:52:49 +01:00
|
|
|
focusEditingArea(): void {
|
|
|
|
this.editingArea.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
blurEditingArea(): void {
|
|
|
|
this.editingArea.blur();
|
|
|
|
}
|
2021-01-28 17:16:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
customElements.define("editing-container", EditingContainer, { extends: "div" });
|
|
|
|
|
|
|
|
class EditorField extends HTMLDivElement {
|
|
|
|
labelContainer: HTMLDivElement;
|
|
|
|
label: HTMLSpanElement;
|
|
|
|
editingContainer: EditingContainer;
|
|
|
|
|
2021-01-29 15:50:34 +01:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.labelContainer = document.createElement("div");
|
2021-01-28 15:42:37 +01:00
|
|
|
this.labelContainer.className = "fname";
|
2021-01-29 19:34:44 +01:00
|
|
|
this.appendChild(this.labelContainer);
|
2021-01-28 15:42:37 +01:00
|
|
|
|
2021-01-29 15:50:34 +01:00
|
|
|
this.label = document.createElement("span");
|
2021-01-28 15:42:37 +01:00
|
|
|
this.label.className = "fieldname";
|
2021-01-29 15:50:34 +01:00
|
|
|
this.labelContainer.appendChild(this.label);
|
2021-01-28 15:42:37 +01:00
|
|
|
|
2021-01-29 14:00:33 +01:00
|
|
|
this.editingContainer = document.createElement("div", {
|
|
|
|
is: "editing-container",
|
|
|
|
}) as EditingContainer;
|
|
|
|
this.appendChild(this.editingContainer);
|
2021-01-28 15:42:37 +01:00
|
|
|
}
|
|
|
|
|
2021-01-29 17:41:27 +01:00
|
|
|
static get observedAttributes(): string[] {
|
2021-01-29 19:34:44 +01:00
|
|
|
return ["ord"];
|
2021-01-29 17:41:27 +01:00
|
|
|
}
|
|
|
|
|
2021-01-29 20:13:02 +01:00
|
|
|
set ord(n: number) {
|
|
|
|
this.setAttribute("ord", String(n));
|
|
|
|
}
|
|
|
|
|
2021-01-29 17:41:27 +01:00
|
|
|
attributeChangedCallback(name: string, _oldValue: string, newValue: string): void {
|
|
|
|
switch (name) {
|
|
|
|
case "ord":
|
|
|
|
this.editingContainer.setAttribute("ord", newValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:00:33 +01:00
|
|
|
initialize(label: string, color: string, content: string): void {
|
2021-01-28 15:42:37 +01:00
|
|
|
this.label.innerText = label;
|
2021-01-29 14:00:33 +01:00
|
|
|
this.editingContainer.initialize(color, content);
|
2021-01-28 15:42:37 +01:00
|
|
|
}
|
2021-01-28 18:37:38 +01:00
|
|
|
|
|
|
|
setBaseStyling(fontFamily: string, fontSize: string, direction: string): void {
|
|
|
|
this.editingContainer.setBaseStyling(fontFamily, fontSize, direction);
|
|
|
|
}
|
2021-01-28 15:42:37 +01:00
|
|
|
}
|
2021-01-26 23:00:55 +01:00
|
|
|
|
2021-01-28 17:16:10 +01:00
|
|
|
customElements.define("editor-field", EditorField, { extends: "div" });
|
2021-01-28 15:42:37 +01:00
|
|
|
|
|
|
|
function adjustFieldAmount(amount: number): void {
|
|
|
|
const fieldsContainer = document.getElementById("fields");
|
|
|
|
|
|
|
|
while (fieldsContainer.childElementCount < amount) {
|
2021-01-29 19:34:44 +01:00
|
|
|
const newField = document.createElement("div", {
|
|
|
|
is: "editor-field",
|
|
|
|
}) as EditorField;
|
2021-01-29 17:41:27 +01:00
|
|
|
newField.ord = fieldsContainer.childElementCount;
|
|
|
|
fieldsContainer.appendChild(newField);
|
2021-01-26 23:00:55 +01:00
|
|
|
}
|
|
|
|
|
2021-01-28 15:42:37 +01:00
|
|
|
while (fieldsContainer.childElementCount > amount) {
|
|
|
|
fieldsContainer.removeChild(fieldsContainer.lastElementChild);
|
|
|
|
}
|
2021-01-26 23:00:55 +01:00
|
|
|
}
|
|
|
|
|
2021-01-29 19:34:44 +01:00
|
|
|
function forEditorField<T>(
|
2021-01-28 19:13:39 +01:00
|
|
|
values: T[],
|
2021-01-29 19:34:44 +01:00
|
|
|
func: (field: EditorField, value: T) => void
|
2021-01-28 19:13:39 +01:00
|
|
|
): void {
|
2021-01-29 19:34:44 +01:00
|
|
|
const fields = document.getElementById("fields").children;
|
|
|
|
for (let i = 0; i < fields.length; i++) {
|
|
|
|
const field = fields[i] as EditorField;
|
|
|
|
func(field, values[i]);
|
2021-01-28 18:37:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function setFields(fields: [string, string][]): void {
|
2020-12-21 08:56:20 +01:00
|
|
|
// webengine will include the variable after enter+backspace
|
|
|
|
// if we don't convert it to a literal colour
|
|
|
|
const color = window
|
|
|
|
.getComputedStyle(document.documentElement)
|
|
|
|
.getPropertyValue("--text-fg");
|
2021-01-26 23:00:55 +01:00
|
|
|
|
2021-01-28 15:42:37 +01:00
|
|
|
adjustFieldAmount(fields.length);
|
2021-01-29 19:34:44 +01:00
|
|
|
forEditorField(fields, (field, [name, fieldContent]) =>
|
2021-01-29 14:00:33 +01:00
|
|
|
field.initialize(name, color, fieldContent)
|
2021-01-28 19:13:39 +01:00
|
|
|
);
|
2021-01-26 23:00:55 +01:00
|
|
|
|
2017-07-28 08:19:06 +02:00
|
|
|
maybeDisableButtons();
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-28 19:13:39 +01:00
|
|
|
function setBackgrounds(cols: ("dupe" | "")[]) {
|
2021-01-29 19:34:44 +01:00
|
|
|
forEditorField(cols, (field, value) =>
|
2021-01-28 19:13:39 +01:00
|
|
|
field.editingContainer.classList.toggle("dupe", value === "dupe")
|
|
|
|
);
|
|
|
|
document
|
|
|
|
.querySelector("#dupes")
|
|
|
|
.classList.toggle("is-inactive", !cols.includes("dupe"));
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function setFonts(fonts: [string, number, boolean][]): void {
|
2021-01-29 19:34:44 +01:00
|
|
|
forEditorField(fonts, (field, [fontFamily, fontSize, isRtl]) => {
|
2021-01-28 18:37:38 +01:00
|
|
|
field.setBaseStyling(fontFamily, `${fontSize}px`, isRtl ? "rtl" : "ltr");
|
|
|
|
});
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function setNoteId(id: number): void {
|
2018-04-30 06:51:49 +02:00
|
|
|
currentNoteId = id;
|
|
|
|
}
|
|
|
|
|
2021-01-19 01:08:15 +01:00
|
|
|
let pasteHTML = function (
|
|
|
|
html: string,
|
|
|
|
internal: boolean,
|
|
|
|
extendedMode: boolean
|
|
|
|
): void {
|
2020-12-21 03:13:31 +01:00
|
|
|
html = filterHTML(html, internal, extendedMode);
|
|
|
|
|
2019-05-17 05:40:23 +02:00
|
|
|
if (html !== "") {
|
|
|
|
setFormat("inserthtml", html);
|
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
};
|
|
|
|
|
2021-01-19 01:08:15 +01:00
|
|
|
let filterHTML = function (
|
|
|
|
html: string,
|
|
|
|
internal: boolean,
|
|
|
|
extendedMode: boolean
|
|
|
|
): string {
|
2017-07-28 08:19:06 +02:00
|
|
|
// wrap it in <top> as we aren't allowed to change top level elements
|
2021-01-19 03:23:29 +01:00
|
|
|
const top = document.createElement("ankitop");
|
2021-01-19 03:06:44 +01:00
|
|
|
top.innerHTML = html;
|
|
|
|
|
2017-10-25 09:51:22 +02:00
|
|
|
if (internal) {
|
|
|
|
filterInternalNode(top);
|
2019-12-18 07:12:39 +01:00
|
|
|
} else {
|
2017-10-25 12:20:28 +02:00
|
|
|
filterNode(top, extendedMode);
|
2017-10-25 09:51:22 +02:00
|
|
|
}
|
2019-12-18 04:53:31 +01:00
|
|
|
let outHtml = top.innerHTML;
|
2020-01-26 10:13:31 +01:00
|
|
|
if (!extendedMode && !internal) {
|
2018-06-15 03:36:12 +02:00
|
|
|
// collapse whitespace
|
|
|
|
outHtml = outHtml.replace(/[\n\t ]+/g, " ");
|
|
|
|
}
|
|
|
|
outHtml = outHtml.trim();
|
2017-07-28 08:19:06 +02:00
|
|
|
return outHtml;
|
|
|
|
};
|
|
|
|
|
2019-12-20 22:33:15 +01:00
|
|
|
let allowedTagsBasic = {};
|
|
|
|
let allowedTagsExtended = {};
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2019-12-20 22:33:15 +01:00
|
|
|
let TAGS_WITHOUT_ATTRS = ["P", "DIV", "BR", "SUB", "SUP"];
|
|
|
|
for (const tag of TAGS_WITHOUT_ATTRS) {
|
|
|
|
allowedTagsBasic[tag] = { attrs: [] };
|
2019-12-18 07:12:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TAGS_WITHOUT_ATTRS = [
|
2020-02-07 08:12:43 +01:00
|
|
|
"B",
|
|
|
|
"BLOCKQUOTE",
|
|
|
|
"CODE",
|
|
|
|
"DD",
|
|
|
|
"DL",
|
|
|
|
"DT",
|
|
|
|
"EM",
|
2019-12-18 07:12:39 +01:00
|
|
|
"H1",
|
|
|
|
"H2",
|
|
|
|
"H3",
|
2020-02-07 08:12:43 +01:00
|
|
|
"I",
|
2019-12-18 07:12:39 +01:00
|
|
|
"LI",
|
|
|
|
"OL",
|
|
|
|
"PRE",
|
2020-02-07 08:12:43 +01:00
|
|
|
"RP",
|
|
|
|
"RT",
|
|
|
|
"RUBY",
|
|
|
|
"STRONG",
|
2019-12-18 07:12:39 +01:00
|
|
|
"TABLE",
|
|
|
|
"U",
|
2020-02-07 08:12:43 +01:00
|
|
|
"UL",
|
2019-12-18 07:12:39 +01:00
|
|
|
];
|
2019-12-20 22:33:15 +01:00
|
|
|
for (const tag of TAGS_WITHOUT_ATTRS) {
|
|
|
|
allowedTagsExtended[tag] = { attrs: [] };
|
2017-10-25 12:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-12-18 07:12:39 +01:00
|
|
|
allowedTagsBasic["IMG"] = { attrs: ["SRC"] };
|
2017-10-25 12:20:28 +02:00
|
|
|
|
2019-12-18 07:12:39 +01:00
|
|
|
allowedTagsExtended["A"] = { attrs: ["HREF"] };
|
|
|
|
allowedTagsExtended["TR"] = { attrs: ["ROWSPAN"] };
|
|
|
|
allowedTagsExtended["TD"] = { attrs: ["COLSPAN", "ROWSPAN"] };
|
|
|
|
allowedTagsExtended["TH"] = { attrs: ["COLSPAN", "ROWSPAN"] };
|
|
|
|
allowedTagsExtended["FONT"] = { attrs: ["COLOR"] };
|
2017-10-25 12:20:28 +02:00
|
|
|
|
2019-12-06 04:39:05 +01:00
|
|
|
const allowedStyling = {
|
2019-12-18 07:12:39 +01:00
|
|
|
color: true,
|
|
|
|
"background-color": true,
|
|
|
|
"font-weight": true,
|
|
|
|
"font-style": true,
|
|
|
|
"text-decoration-line": true,
|
2019-12-06 04:39:05 +01:00
|
|
|
};
|
|
|
|
|
2020-06-24 13:54:38 +02:00
|
|
|
let isNightMode = function (): boolean {
|
2020-01-30 23:03:09 +01:00
|
|
|
return document.body.classList.contains("nightMode");
|
|
|
|
};
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
let filterExternalSpan = function (elem: HTMLElement) {
|
2019-12-06 04:39:05 +01:00
|
|
|
// filter out attributes
|
2021-01-19 02:48:41 +01:00
|
|
|
for (const attr of [...elem.attributes]) {
|
2019-12-20 22:33:15 +01:00
|
|
|
const attrName = attr.name.toUpperCase();
|
2021-01-18 17:05:05 +01:00
|
|
|
|
2019-12-18 07:12:39 +01:00
|
|
|
if (attrName !== "STYLE") {
|
2021-01-18 17:05:05 +01:00
|
|
|
elem.removeAttributeNode(attr);
|
2019-12-06 04:39:05 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-18 17:05:05 +01:00
|
|
|
|
2019-12-06 04:39:05 +01:00
|
|
|
// filter styling
|
2021-01-19 02:48:41 +01:00
|
|
|
for (const name of [...elem.style]) {
|
2021-01-19 01:08:15 +01:00
|
|
|
const value = elem.style.getPropertyValue(name);
|
2021-01-18 17:05:05 +01:00
|
|
|
|
|
|
|
if (
|
|
|
|
!allowedStyling.hasOwnProperty(name) ||
|
2019-12-06 05:08:59 +01:00
|
|
|
// google docs adds this unnecessarily
|
2021-01-19 01:08:15 +01:00
|
|
|
(name === "background-color" && value === "transparent") ||
|
2020-01-30 23:03:09 +01:00
|
|
|
// ignore coloured text in night mode for now
|
2021-01-19 01:08:15 +01:00
|
|
|
(isNightMode() && (name === "background-color" || name === "color"))
|
2021-01-18 17:05:05 +01:00
|
|
|
) {
|
|
|
|
elem.style.removeProperty(name);
|
2020-01-30 23:03:09 +01:00
|
|
|
}
|
2019-12-06 04:39:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
allowedTagsExtended["SPAN"] = filterExternalSpan;
|
|
|
|
|
2017-10-25 12:20:28 +02:00
|
|
|
// add basic tags to extended
|
|
|
|
Object.assign(allowedTagsExtended, allowedTagsBasic);
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
function isHTMLElement(elem: Element): elem is HTMLElement {
|
|
|
|
return elem instanceof HTMLElement;
|
|
|
|
}
|
|
|
|
|
2017-10-25 09:51:22 +02:00
|
|
|
// filtering from another field
|
2021-01-18 17:05:05 +01:00
|
|
|
let filterInternalNode = function (elem: Element) {
|
|
|
|
if (isHTMLElement(elem)) {
|
|
|
|
elem.style.removeProperty("background-color");
|
|
|
|
elem.style.removeProperty("font-size");
|
|
|
|
elem.style.removeProperty("font-family");
|
2017-10-25 09:51:22 +02:00
|
|
|
}
|
|
|
|
// recurse
|
2021-01-18 17:05:05 +01:00
|
|
|
for (let i = 0; i < elem.children.length; i++) {
|
|
|
|
const child = elem.children[i];
|
2019-12-20 22:33:15 +01:00
|
|
|
filterInternalNode(child);
|
2017-10-25 09:51:22 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// filtering from external sources
|
2021-01-18 17:05:05 +01:00
|
|
|
let filterNode = function (node: Node, extendedMode: boolean): void {
|
|
|
|
if (!nodeIsElement(node)) {
|
2017-07-28 08:19:06 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// descend first, and take a copy of the child nodes as the loop will skip
|
|
|
|
// elements due to node modifications otherwise
|
2021-01-19 02:48:41 +01:00
|
|
|
for (const child of [...node.children]) {
|
2019-12-20 22:33:15 +01:00
|
|
|
filterNode(child, extendedMode);
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
|
|
|
|
2017-07-28 08:48:49 +02:00
|
|
|
if (node.tagName === "ANKITOP") {
|
2017-07-28 08:19:06 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-18 17:05:05 +01:00
|
|
|
const tag = extendedMode
|
|
|
|
? allowedTagsExtended[node.tagName]
|
|
|
|
: allowedTagsBasic[node.tagName];
|
|
|
|
|
2017-07-28 08:19:06 +02:00
|
|
|
if (!tag) {
|
2019-12-18 07:12:39 +01:00
|
|
|
if (!node.innerHTML || node.tagName === "TITLE") {
|
2017-07-28 08:19:06 +02:00
|
|
|
node.parentNode.removeChild(node);
|
|
|
|
} else {
|
|
|
|
node.outerHTML = node.innerHTML;
|
|
|
|
}
|
|
|
|
} else {
|
2019-12-18 07:12:39 +01:00
|
|
|
if (typeof tag === "function") {
|
2019-12-06 04:39:05 +01:00
|
|
|
// filtering function provided
|
|
|
|
tag(node);
|
|
|
|
} else {
|
|
|
|
// allowed, filter out attributes
|
2021-01-19 02:48:41 +01:00
|
|
|
for (const attr of [...node.attributes]) {
|
2019-12-20 22:33:15 +01:00
|
|
|
const attrName = attr.name.toUpperCase();
|
2019-12-06 04:39:05 +01:00
|
|
|
if (tag.attrs.indexOf(attrName) === -1) {
|
2021-01-18 17:05:05 +01:00
|
|
|
node.removeAttributeNode(attr);
|
2019-12-06 04:39:05 +01:00
|
|
|
}
|
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|