Skip to content

Commit d5356b6

Browse files
authored
Merge pull request #253 from github/rename-tag-observer-exports
rename tag-observer exports
2 parents 4f9f287 + f17ab70 commit d5356b6

2 files changed

Lines changed: 16 additions & 16 deletions

File tree

src/tag-observer.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function closestShadowPiercing(el: Element, tagName: string): Element | null {
1111
return closest
1212
}
1313

14-
export const tags = (el: Element, tag: string, parse: Parse) =>
14+
export const parseElementTags = (el: Element, tag: string, parse: Parse) =>
1515
(el.getAttribute(tag) || '')
1616
.trim()
1717
.split(/\s+/g)
@@ -26,34 +26,34 @@ const observer = new MutationObserver((mutations: MutationRecord[]) => {
2626

2727
if (el instanceof Element && registry.has(tag)) {
2828
const [parse, found] = registry.get(tag)!
29-
for (const [tagName, ...meta] of tags(el, tag, parse)) {
29+
for (const [tagName, ...meta] of parseElementTags(el, tag, parse)) {
3030
const controller = closestShadowPiercing(el, tagName)
3131
if (controller) found(el, controller, tag, ...meta)
3232
}
3333
}
3434
} else if (mutation.addedNodes.length) {
3535
for (const node of mutation.addedNodes) {
36-
if (node instanceof Element) add(node)
36+
if (node instanceof Element) observeElementForTags(node)
3737
}
3838
}
3939
}
4040
})
4141

42-
export const register = (tag: string, parse: Parse, found: Found) => {
42+
export const registerTag = (tag: string, parse: Parse, found: Found) => {
4343
if (registry.has(tag)) throw new Error('duplicate tag')
4444
registry.set(tag, [parse, found])
4545
}
4646

47-
export const add = (root: Element | ShadowRoot) => {
47+
export const observeElementForTags = (root: Element | ShadowRoot) => {
4848
for (const [tag, [parse, found]] of registry) {
4949
for (const el of root.querySelectorAll(`[${tag}]`)) {
50-
for (const [tagName, ...meta] of tags(el, tag, parse)) {
50+
for (const [tagName, ...meta] of parseElementTags(el, tag, parse)) {
5151
const controller = closestShadowPiercing(el, tagName)
5252
if (controller) found(el, controller, tag, ...meta)
5353
}
5454
}
5555
if (root instanceof Element && root.hasAttribute(tag)) {
56-
for (const [tagName, ...meta] of tags(root, tag, parse)) {
56+
for (const [tagName, ...meta] of parseElementTags(root, tag, parse)) {
5757
const controller = closestShadowPiercing(root, tagName)
5858
if (controller) found(root, controller, tag, ...meta)
5959
}

test/tag-observer.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {expect, fixture, html} from '@open-wc/testing'
22
import {fake, match} from 'sinon'
3-
import {register, add} from '../src/tag-observer.js'
3+
import {registerTag, observeElementForTags} from '../src/tag-observer.js'
44

55
describe('tag observer', () => {
66
let instance: HTMLElement
@@ -11,20 +11,20 @@ describe('tag observer', () => {
1111
})
1212

1313
it('can register new tag observers', () => {
14-
register('foo', fake(), fake())
14+
registerTag('foo', fake(), fake())
1515
})
1616

1717
it('throws an error when registering a duplicate', () => {
18-
register('duplicate', fake(), fake())
19-
expect(() => register('duplicate', fake(), fake())).to.throw()
18+
registerTag('duplicate', fake(), fake())
19+
expect(() => registerTag('duplicate', fake(), fake())).to.throw()
2020
})
2121

2222
describe('registered behaviour', () => {
2323
const testParse = fake(v => v.split('.'))
2424
const testFound = fake()
25-
register('data-tagtest', testParse, testFound)
25+
registerTag('data-tagtest', testParse, testFound)
2626
beforeEach(() => {
27-
add(instance)
27+
observeElementForTags(instance)
2828
})
2929

3030
it('uses parse to extract tagged element values', () => {
@@ -43,7 +43,7 @@ describe('tag observer', () => {
4343
it('calls found if added to a node that has tags on itself', () => {
4444
const div = document.createElement('div')
4545
div.setAttribute('data-tagtest', 'div.j.k.l')
46-
add(div)
46+
observeElementForTags(div)
4747
expect(testParse).to.be.calledWithExactly('div.j.k.l')
4848
expect(testFound).to.be.calledWithExactly(div, div, 'data-tagtest', 'j', 'k', 'l')
4949
})
@@ -54,7 +54,7 @@ describe('tag observer', () => {
5454
const span = document.createElement('span')
5555
span.setAttribute('data-tagtest', 'div.m.n.o')
5656
shadow.append(span)
57-
add(span)
57+
observeElementForTags(span)
5858
expect(testParse).to.be.calledWithExactly('div.m.n.o')
5959
expect(testFound).to.be.calledWithExactly(span, div, 'data-tagtest', 'm', 'n', 'o')
6060
})
@@ -65,7 +65,7 @@ describe('tag observer', () => {
6565
const span = document.createElement('span')
6666
span.setAttribute('data-tagtest', 'div.p.q.r')
6767
shadow.append(span)
68-
add(shadow)
68+
observeElementForTags(shadow)
6969
expect(testParse).to.be.calledWithExactly('div.p.q.r')
7070
expect(testFound).to.be.calledWithExactly(span, div, 'data-tagtest', 'p', 'q', 'r')
7171
})

0 commit comments

Comments
 (0)