Skip to content

Commit

Permalink
Allow non-string values for attributors
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Jan 15, 2024
1 parent 1dde5ed commit 5295b40
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/attributor/attributor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/attributor/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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 : '';
Expand Down
4 changes: 2 additions & 2 deletions src/attributor/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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 : '';
Expand Down
12 changes: 12 additions & 0 deletions tests/types/attributor.test-d.ts
Original file line number Diff line number Diff line change
@@ -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<any>(
new IndentAttributor('indent', 'indent').value(document.createElement('div')),
);

0 comments on commit 5295b40

Please sign in to comment.