-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f7dba0
commit 2e4c1ad
Showing
8 changed files
with
6,017 additions
and
5,511 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { ICustomElement } from "./types"; | ||
|
||
/** Save a reference to the ref for teh CustomStateSet */ | ||
const customStateMap = new WeakMap<CustomStateSet, ICustomElement>(); | ||
|
||
export class CustomStateSet extends Set<string> { | ||
constructor(ref: ICustomElement) { | ||
super(); | ||
|
||
customStateMap.set(this, ref); | ||
} | ||
|
||
add(state: string) { | ||
if (!/^--/.exec(state) || typeof state !== 'string') { | ||
throw new DOMException(`Failed to execute 'add' on 'CustomStateSet': The specified value ${state} must start with '--'.`); | ||
} | ||
const result = super.add(state); | ||
const ref = customStateMap.get(this); | ||
ref.toggleAttribute(`state${state}`, true); | ||
return result; | ||
} | ||
|
||
clear() { | ||
for (let [entry] of this.entries()) { | ||
this.delete(entry); | ||
} | ||
super.clear(); | ||
} | ||
|
||
delete(state: string) { | ||
const result = super.delete(state); | ||
const ref = customStateMap.get(this); | ||
ref.toggleAttribute(`state${state}`, false); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { fixture, html, expect, fixtureCleanup } from '@open-wc/testing'; | ||
import { ICustomElement } from '../dist'; | ||
import { CustomStateSet } from '../src/CustomStateSet'; | ||
|
||
describe('CustomStateSet polyfill', () => { | ||
let el: HTMLElement; | ||
let set: CustomStateSet; | ||
|
||
beforeEach(async () => { | ||
el = await fixture(html`<div></div>`); | ||
set = new CustomStateSet(el as ICustomElement); | ||
}); | ||
|
||
afterEach(() => { | ||
fixtureCleanup(); | ||
}); | ||
|
||
describe('it will add attributes', async () => { | ||
set.add('--foo'); | ||
expect(el.hasAttribute('state--foo')).to.be.true; | ||
}); | ||
|
||
describe('it will remove attributes', async () => { | ||
set.add('--foo'); | ||
expect(el.hasAttribute('state--foo')).to.be.true; | ||
|
||
set.delete('--foo'); | ||
expect(el.hasAttribute('state--foo')).to.be.false; | ||
}); | ||
|
||
describe('it will clear all attributes', async () => { | ||
set.add('--foo'); | ||
set.add('--bar'); | ||
|
||
expect(el.hasAttribute('state--foo')).to.be.true; | ||
expect(el.hasAttribute('state--bar')).to.be.true; | ||
|
||
set.clear(); | ||
expect(el.hasAttribute('state--foo')).to.be.false; | ||
expect(el.hasAttribute('state--bar')).to.be.false; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { esbuildPlugin } from '@web/dev-server-esbuild'; | ||
|
||
export default { | ||
plugins: [esbuildPlugin({ ts: true, target: 'auto' })], | ||
}; |