Skip to content

Commit

Permalink
Merge pull request #11 from muan/children
Browse files Browse the repository at this point in the history
Ensure dynamically added child summary tags are polyfilled
  • Loading branch information
javan authored Oct 15, 2018
2 parents e3dfe87 + d7a265b commit 69cad52
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
18 changes: 12 additions & 6 deletions src/details-element-polyfill/polyfill.coffee.erb
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,21 @@ polyfillToggle = ->
summary.setAttribute("aria-expanded", true)

polyfillFocusAndARIA = ->
for summary in document.querySelectorAll("summary")
for summary in findElementsWithTagName(document.documentElement, "SUMMARY")
addAttributesForSummary(summary)

if MutationObserver?
observer = new MutationObserver (mutations) ->
for {addedNodes} in mutations
for target in addedNodes
if target.tagName is "DETAILS"
if summary = target.querySelector("summary")
addAttributesForSummary(summary, target)
for summary in findElementsWithTagName(target, "SUMMARY")
addAttributesForSummary(summary)

observer.observe(document.documentElement, subtree: true, childList: true)
else
document.addEventListener "DOMNodeInserted", (event) ->
if event.target.tagName is "SUMMARY"
addAttributesForSummary event.target
for summary in findElementsWithTagName(event.target, "SUMMARY")
addAttributesForSummary(summary)

addAttributesForSummary = (summary, details) ->
details ?= findClosestElementWithTagName(summary, "DETAILS")
Expand Down Expand Up @@ -138,6 +137,13 @@ onTogglingTrigger = (callback) ->
event.preventDefault()
, false

findElementsWithTagName = (root, tagName) ->
elements = []
if root.nodeType is Node.ELEMENT_NODE
elements.push(root) if root.tagName is tagName
elements.push(root.getElementsByTagName(tagName)...)
elements

findClosestElementWithTagName = do ->
if typeof Element::closest is "function"
(element, tagName) ->
Expand Down
15 changes: 10 additions & 5 deletions test/src/test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
module "<details>",
beforeEach: ->
fixtureHTML = """
<details id="details">
<summary id="summary">Summary</summary>
<div id="content">Content</div>
</details>
<div id="container">
<details id="details">
<summary id="summary">Summary</summary>
<div id="content">Content</div>
</details>
</div>
"""
document.body.insertAdjacentHTML("beforeend", fixtureHTML)

afterEach: ->
document.body.removeChild(document.getElementById("details"))
document.body.removeChild(document.getElementById("container"))


test "displays summary and hides content initially", (assert) ->
Expand All @@ -25,6 +27,9 @@ test "summary is focusable", (assert) ->
done = assert.async()
summary = getElement("summary")
defer ->
if (typeof HTMLDetailsElement is "undefined")
assert.ok summary.hasAttribute("tabindex")
assert.ok summary.hasAttribute("role")
summary.focus()
assert.equal document.activeElement, summary
done()
Expand Down

0 comments on commit 69cad52

Please sign in to comment.