Skip to content

Commit 629ab22

Browse files
authored
Merge pull request #265 from github/optional-dash
allow for customising prefix of @attr names
2 parents 31ad1a0 + b98b719 commit 629ab22

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

src/attr.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type {CustomElementClass} from './custom-element.js'
2-
import {dasherize} from './dasherize.js'
2+
import {mustDasherize} from './dasherize.js'
33
import {meta} from './core.js'
44

55
const attrKey = 'attr'
@@ -39,10 +39,12 @@ const initialized = new WeakSet<Element>()
3939
export function initializeAttrs(instance: HTMLElement, names?: Iterable<string>): void {
4040
if (initialized.has(instance)) return
4141
initialized.add(instance)
42-
if (!names) names = meta(Object.getPrototypeOf(instance), attrKey)
42+
const proto = Object.getPrototypeOf(instance)
43+
const prefix = proto?.constructor?.attrPrefix ?? 'data-'
44+
if (!names) names = meta(proto, attrKey)
4345
for (const key of names) {
4446
const value = (<Record<PropertyKey, unknown>>(<unknown>instance))[key]
45-
const name = attrToAttributeName(key)
47+
const name = mustDasherize(`${prefix}${key}`)
4648
let descriptor: PropertyDescriptor = {
4749
configurable: true,
4850
get(this: HTMLElement): string {
@@ -80,10 +82,12 @@ export function initializeAttrs(instance: HTMLElement, names?: Iterable<string>)
8082
}
8183
}
8284

83-
const attrToAttributeName = (name: string) => `data-${dasherize(name)}`
84-
8585
export function defineObservedAttributes(classObject: CustomElementClass): void {
8686
let observed = classObject.observedAttributes || []
87+
88+
const prefix = classObject.attrPrefix ?? 'data-'
89+
const attrToAttributeName = (name: string) => mustDasherize(`${prefix}${name}`)
90+
8791
Object.defineProperty(classObject, 'observedAttributes', {
8892
configurable: true,
8993
get() {

src/custom-element.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ export interface CustomElementClass {
1616
observedAttributes?: string[]
1717
disabledFeatures?: string[]
1818
formAssociated?: boolean
19+
20+
attrPrefix?: string
1921
}

test/attr.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,25 @@ describe('Attr', () => {
156156
expect(instance.getAttributeNames()).to.include('data-clip-x')
157157
})
158158
})
159+
160+
describe('prefix', () => {
161+
@controller
162+
class PrefixAttrTest extends HTMLElement {
163+
static attrPrefix = 'foo-'
164+
@attr fooBarBazBing = 'a'
165+
@attr URLBar = 'b'
166+
@attr ClipX = 'c'
167+
}
168+
169+
let instance: PrefixAttrTest
170+
beforeEach(async () => {
171+
instance = await fixture(html`<prefix-attr-test />`)
172+
})
173+
174+
it('respects custom attrPrefix static member', async () => {
175+
expect(instance.getAttributeNames()).to.include('foo-foo-bar-baz-bing')
176+
expect(instance.getAttributeNames()).to.include('foo-url-bar')
177+
expect(instance.getAttributeNames()).to.include('foo-clip-x')
178+
})
179+
})
159180
})

0 commit comments

Comments
 (0)