Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow non-string values for attributors #136

Merged
merged 3 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/attributor/attributor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ export default class Attributor {
}
}

public add(node: HTMLElement, value: string): boolean {
public add(node: HTMLElement, value: string | number): boolean {
if (!this.canAdd(node, value)) {
return false;
}
node.setAttribute(this.keyName, value);
node.setAttribute(this.keyName, String(value));
return true;
}

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: string | number): 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: string | number): 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
2 changes: 1 addition & 1 deletion src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Scope from './scope';
export type RegistryDefinition = Attributor | BlotConstructor;

export interface RegistryInterface {
create(sroll: Root, input: Node | string | Scope, value?: any): Blot;
create(scroll: Root, input: Node | string | Scope, value?: any): Blot;
query(query: string | Node | Scope, scope: Scope): RegistryDefinition | null;
register(...definitions: any[]): any;
}
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')),
);