Skip to content

Commit

Permalink
Merge pull request #265 from github/optional-dash
Browse files Browse the repository at this point in the history
allow for customising prefix of @attr names
  • Loading branch information
keithamus authored Jun 27, 2022
2 parents 31ad1a0 + b98b719 commit 629ab22
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/attr.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {CustomElementClass} from './custom-element.js'
import {dasherize} from './dasherize.js'
import {mustDasherize} from './dasherize.js'
import {meta} from './core.js'

const attrKey = 'attr'
Expand Down Expand Up @@ -39,10 +39,12 @@ const initialized = new WeakSet<Element>()
export function initializeAttrs(instance: HTMLElement, names?: Iterable<string>): void {
if (initialized.has(instance)) return
initialized.add(instance)
if (!names) names = meta(Object.getPrototypeOf(instance), attrKey)
const proto = Object.getPrototypeOf(instance)
const prefix = proto?.constructor?.attrPrefix ?? 'data-'
if (!names) names = meta(proto, attrKey)
for (const key of names) {
const value = (<Record<PropertyKey, unknown>>(<unknown>instance))[key]
const name = attrToAttributeName(key)
const name = mustDasherize(`${prefix}${key}`)
let descriptor: PropertyDescriptor = {
configurable: true,
get(this: HTMLElement): string {
Expand Down Expand Up @@ -80,10 +82,12 @@ export function initializeAttrs(instance: HTMLElement, names?: Iterable<string>)
}
}

const attrToAttributeName = (name: string) => `data-${dasherize(name)}`

export function defineObservedAttributes(classObject: CustomElementClass): void {
let observed = classObject.observedAttributes || []

const prefix = classObject.attrPrefix ?? 'data-'
const attrToAttributeName = (name: string) => mustDasherize(`${prefix}${name}`)

Object.defineProperty(classObject, 'observedAttributes', {
configurable: true,
get() {
Expand Down
2 changes: 2 additions & 0 deletions src/custom-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ export interface CustomElementClass {
observedAttributes?: string[]
disabledFeatures?: string[]
formAssociated?: boolean

attrPrefix?: string
}
21 changes: 21 additions & 0 deletions test/attr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,25 @@ describe('Attr', () => {
expect(instance.getAttributeNames()).to.include('data-clip-x')
})
})

describe('prefix', () => {
@controller
class PrefixAttrTest extends HTMLElement {
static attrPrefix = 'foo-'
@attr fooBarBazBing = 'a'
@attr URLBar = 'b'
@attr ClipX = 'c'
}

let instance: PrefixAttrTest
beforeEach(async () => {
instance = await fixture(html`<prefix-attr-test />`)
})

it('respects custom attrPrefix static member', async () => {
expect(instance.getAttributeNames()).to.include('foo-foo-bar-baz-bing')
expect(instance.getAttributeNames()).to.include('foo-url-bar')
expect(instance.getAttributeNames()).to.include('foo-clip-x')
})
})
})

0 comments on commit 629ab22

Please sign in to comment.