Skip to content

Commit b02a30a

Browse files
augoniskeithamus
authored andcommitted
Fixes #38
Replaces usages of `document.getElementById` with `container.querySelector`
1 parent a058977 commit b02a30a

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

src/auto-complete-element.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ const state = new WeakMap()
88
export default class AutocompleteElement extends HTMLElement {
99
connectedCallback(): void {
1010
const listId = this.getAttribute('for')
11+
if (!this.isConnected) return
1112
if (!listId) return
1213

1314
// eslint-disable-next-line custom-elements/no-dom-traversal-in-connectedcallback
1415
const input = this.querySelector('input')
15-
const results = document.getElementById(listId)
16+
const results = (this.getRootNode() as Document).getElementById(listId)
1617
if (!(input instanceof HTMLInputElement) || !results) return
1718
const autoselectEnabled = this.getAttribute('data-autoselect') === 'true'
1819
state.set(this, new Autocomplete(this, input, results, autoselectEnabled))

src/autocomplete.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ export default class Autocomplete {
2828
this.input = input
2929
this.results = results
3030
this.combobox = new Combobox(input, results)
31-
this.feedback = document.getElementById(`${this.results.id}-feedback`)
31+
this.feedback = (container.getRootNode() as Document).getElementById(`${this.results.id}-feedback`)
3232
this.autoselectEnabled = autoselectEnabled
33-
this.clearButton = document.getElementById(`${this.input.id || this.input.name}-clear`)
33+
this.clearButton = (container.getRootNode() as Document).getElementById(`${this.input.id || this.input.name}-clear`)
3434

3535
// check to see if there are any default options provided
3636
this.clientOptions = results.querySelectorAll('[role=option]')

test/test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,34 @@ describe('auto-complete element', function () {
314314
assert.equal(feedback.textContent, '')
315315
})
316316
})
317+
318+
describe('shadowdom', () => {
319+
let shadow = null
320+
beforeEach(function () {
321+
const fixture = document.createElement('div')
322+
fixture.id = 'mocha-fixture'
323+
document.body.append(fixture)
324+
shadow = fixture.attachShadow({mode: 'open'})
325+
shadow.innerHTML = `
326+
<auto-complete src="/search" for="popup">
327+
<input type="text">
328+
<ul id="popup"></ul>
329+
<div id="popup-feedback"></div>
330+
</auto-complete>
331+
</div>
332+
`
333+
})
334+
335+
it('uses rootNode to find idrefs', async function () {
336+
const container = shadow.querySelector('auto-complete')
337+
const input = container.querySelector('input')
338+
const popup = container.querySelector('#popup')
339+
340+
triggerInput(input, 'hub')
341+
await once(container, 'loadend')
342+
assert.equal(5, popup.children.length)
343+
})
344+
})
317345
})
318346

319347
function waitForElementToChange(el) {

0 commit comments

Comments
 (0)