diff --git a/ts/editor/BUILD.bazel b/ts/editor/BUILD.bazel index d6fa1f88d..83770ab61 100644 --- a/ts/editor/BUILD.bazel +++ b/ts/editor/BUILD.bazel @@ -21,7 +21,7 @@ ts_library( srcs = glob(["*.ts"]), tsconfig = "//qt/aqt/data/web/js:tsconfig.json", deps = [ - "@npm//@types/jquery", + "//ts/html-filter", ], ) diff --git a/ts/editor/helpers.ts b/ts/editor/helpers.ts index 7493f6698..9281af818 100644 --- a/ts/editor/helpers.ts +++ b/ts/editor/helpers.ts @@ -7,10 +7,6 @@ export function nodeIsElement(node: Node): node is Element { return node.nodeType === Node.ELEMENT_NODE; } -export function isHTMLElement(elem: Element): elem is HTMLElement { - return elem instanceof HTMLElement; -} - const INLINE_TAGS = [ "A", "ABBR", @@ -81,7 +77,3 @@ export function caretToEnd(currentField: EditingArea): void { selection.removeAllRanges(); selection.addRange(range); } - -export function isNightMode(): boolean { - return document.body.classList.contains("nightMode"); -} diff --git a/ts/editor/index.ts b/ts/editor/index.ts index 0f0c87f2e..b6488e9b6 100644 --- a/ts/editor/index.ts +++ b/ts/editor/index.ts @@ -1,9 +1,10 @@ /* Copyright: Ankitects Pty Ltd and contributors * License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html */ +import { filterHTML } from "html-filter/index"; + import { caretToEnd } from "./helpers"; import { saveField } from "./changeTimer"; -import { filterHTML } from "./htmlFilter"; import { updateButtonState, disableButtons } from "./toolbar"; import { EditorField } from "./editorField"; diff --git a/ts/html-filter/BUILD.bazel b/ts/html-filter/BUILD.bazel new file mode 100644 index 000000000..d2d5c49ef --- /dev/null +++ b/ts/html-filter/BUILD.bazel @@ -0,0 +1,31 @@ +load("@npm//@bazel/typescript:index.bzl", "ts_library") +load("//ts:prettier.bzl", "prettier_test") +load("//ts:eslint.bzl", "eslint_test") + +ts_library( + name = "html-filter", + srcs = glob(["*.ts"]), + module_name = "html-filter", + tsconfig = "//qt/aqt/data/web/js:tsconfig.json", + visibility = ["//visibility:public"], + deps = [], +) + +# Tests +################ + +prettier_test( + name = "format_check", + srcs = glob([ + "*.ts", + ]), +) + +eslint_test( + name = "eslint", + srcs = glob( + [ + "*.ts", + ], + ), +) diff --git a/ts/editor/htmlFilterElement.ts b/ts/html-filter/element.ts similarity index 85% rename from ts/editor/htmlFilterElement.ts rename to ts/html-filter/element.ts index 90b314de1..ff454959b 100644 --- a/ts/editor/htmlFilterElement.ts +++ b/ts/html-filter/element.ts @@ -1,10 +1,10 @@ -import { isNightMode, isHTMLElement } from "./helpers"; -import { removeNode as removeElement } from "./htmlFilterNode"; +import { isHTMLElement, isNightMode } from "./helpers"; +import { removeNode as removeElement } from "./node"; import { filterStylingNightMode, filterStylingLightMode, filterStylingInternal, -} from "./htmlFilterStyling"; +} from "./styling"; interface TagsAllowed { [tagName: string]: FilterMethod; @@ -29,13 +29,11 @@ function blockAll(element: Element): void { filterOutAttributes(() => true, element); } -function blockExcept(attrs: string[]): FilterMethod { - return (element: Element) => - filterOutAttributes( - (attributeName: string) => !attrs.includes(attributeName), - element - ); -} +const blockExcept = (attrs: string[]): FilterMethod => (element: Element): void => + filterOutAttributes( + (attributeName: string) => !attrs.includes(attributeName), + element + ); function unwrapElement(element: Element): void { element.outerHTML = element.innerHTML; @@ -95,7 +93,7 @@ const filterElementTagsAllowed = (tagsAllowed: TagsAllowed) => ( ): void => { const tagName = element.tagName; - if (tagsAllowed.hasOwnProperty(tagName)) { + if (Object.prototype.hasOwnProperty.call(tagsAllowed, tagName)) { tagsAllowed[tagName](element); } else if (element.innerHTML) { removeElement(element); diff --git a/ts/html-filter/helpers.ts b/ts/html-filter/helpers.ts new file mode 100644 index 000000000..7945d8942 --- /dev/null +++ b/ts/html-filter/helpers.ts @@ -0,0 +1,7 @@ +export function isHTMLElement(elem: Element): elem is HTMLElement { + return elem instanceof HTMLElement; +} + +export function isNightMode(): boolean { + return document.body.classList.contains("nightMode"); +} diff --git a/ts/editor/htmlFilter.ts b/ts/html-filter/index.ts similarity index 94% rename from ts/editor/htmlFilter.ts rename to ts/html-filter/index.ts index 60528c70e..cec3835e2 100644 --- a/ts/editor/htmlFilter.ts +++ b/ts/html-filter/index.ts @@ -5,8 +5,8 @@ import { filterElementBasic, filterElementExtended, filterElementInternal, -} from "./htmlFilterElement"; -import { filterNode } from "./htmlFilterNode"; +} from "./element"; +import { filterNode } from "./node"; enum FilterMode { Basic, diff --git a/ts/editor/htmlFilterNode.ts b/ts/html-filter/node.ts similarity index 97% rename from ts/editor/htmlFilterNode.ts rename to ts/html-filter/node.ts index ee32dcd4c..735e52fff 100644 --- a/ts/editor/htmlFilterNode.ts +++ b/ts/html-filter/node.ts @@ -29,6 +29,6 @@ export const filterNode = (elementFilter: (element: Element) => void) => ( break; default: - // do nothing + // do nothing } }; diff --git a/ts/editor/htmlFilterStyling.ts b/ts/html-filter/styling.ts similarity index 93% rename from ts/editor/htmlFilterStyling.ts rename to ts/html-filter/styling.ts index 818e266df..4295f5015 100644 --- a/ts/editor/htmlFilterStyling.ts +++ b/ts/html-filter/styling.ts @@ -27,7 +27,8 @@ const stylingInternal: BlockProperties = [ const allowPropertiesBlockValues = ( allowBlock: AllowPropertiesBlockValues ): StylingPredicate => (property: string, value: string): boolean => - allowBlock.hasOwnProperty(property) && !allowBlock[property].includes(value); + Object.prototype.hasOwnProperty.call(allowBlock, property) && + !allowBlock[property].includes(value); const blockProperties = (block: BlockProperties): StylingPredicate => ( property: string diff --git a/ts/tsconfig.json b/ts/tsconfig.json index eb5ce06d4..9189b9acc 100644 --- a/ts/tsconfig.json +++ b/ts/tsconfig.json @@ -7,7 +7,8 @@ "baseUrl": ".", "paths": { "anki/*": ["../bazel-bin/ts/lib/*"], - "sveltelib/*": ["../bazel-bin/ts/sveltelib/*"] + "sveltelib/*": ["../bazel-bin/ts/sveltelib/*"], + "html-filter/*": ["../bazel-bin/ts/html-filter/*"] }, "importsNotUsedAsValues": "error", "outDir": "dist",