From 5295b40f682636fc6f53d0b6512869b9a486b8ad Mon Sep 17 00:00:00 2001 From: Zihua Li Date: Mon, 15 Jan 2024 17:02:24 +0800 Subject: [PATCH] Allow non-string values for attributors --- src/attributor/attributor.ts | 4 ++-- src/attributor/class.ts | 4 ++-- src/attributor/style.ts | 4 ++-- tests/types/attributor.test-d.ts | 12 ++++++++++++ 4 files changed, 18 insertions(+), 6 deletions(-) create mode 100644 tests/types/attributor.test-d.ts diff --git a/src/attributor/attributor.ts b/src/attributor/attributor.ts index 3f02f1cb..7dcd9f4f 100644 --- a/src/attributor/attributor.ts +++ b/src/attributor/attributor.ts @@ -29,7 +29,7 @@ export default class Attributor { } } - public add(node: HTMLElement, value: string): boolean { + public add(node: HTMLElement, value: any): boolean { if (!this.canAdd(node, value)) { return false; } @@ -52,7 +52,7 @@ export default class Attributor { node.removeAttribute(this.keyName); } - public value(node: HTMLElement): string { + public value(node: HTMLElement): any { const value = node.getAttribute(this.keyName); if (this.canAdd(node, value) && value) { return value; diff --git a/src/attributor/class.ts b/src/attributor/class.ts index 7210505f..07ba9063 100644 --- a/src/attributor/class.ts +++ b/src/attributor/class.ts @@ -14,7 +14,7 @@ class ClassAttributor extends Attributor { .map((name) => name.split('-').slice(0, -1).join('-')); } - public add(node: HTMLElement, value: string): boolean { + public add(node: HTMLElement, value: any): boolean { if (!this.canAdd(node, value)) { return false; } @@ -33,7 +33,7 @@ class ClassAttributor extends Attributor { } } - public value(node: HTMLElement): string { + public value(node: HTMLElement): any { const result = match(node, this.keyName)[0] || ''; const value = result.slice(this.keyName.length + 1); // +1 for hyphen return this.canAdd(node, value) ? value : ''; diff --git a/src/attributor/style.ts b/src/attributor/style.ts index 8b6ab6cc..e5e83254 100644 --- a/src/attributor/style.ts +++ b/src/attributor/style.ts @@ -17,7 +17,7 @@ class StyleAttributor extends Attributor { }); } - public add(node: HTMLElement, value: string): boolean { + public add(node: HTMLElement, value: any): boolean { if (!this.canAdd(node, value)) { return false; } @@ -34,7 +34,7 @@ class StyleAttributor extends Attributor { } } - public value(node: HTMLElement): string { + public value(node: HTMLElement): any { // @ts-expect-error Fix me later const value = node.style[camelize(this.keyName)]; return this.canAdd(node, value) ? value : ''; diff --git a/tests/types/attributor.test-d.ts b/tests/types/attributor.test-d.ts new file mode 100644 index 00000000..02446b19 --- /dev/null +++ b/tests/types/attributor.test-d.ts @@ -0,0 +1,12 @@ +import { assertType } from 'vitest'; +import { ClassAttributor } from '../../src/parchment'; + +class IndentAttributor extends ClassAttributor { + value(node: HTMLElement) { + return parseInt(super.value(node), 10) || undefined; + } +} + +assertType( + new IndentAttributor('indent', 'indent').value(document.createElement('div')), +);