-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ensure dynamically added child summary tags are polyfilled #11
Conversation
Hmm it's not clear to me why the test is failing. Looks like there's no output? |
https://app.saucelabs.com/tests/07da98865afb42499311b4784249c902 Looks like the tests hung in a few browsers. Passed on retry. 🤷♂️ |
Can you try applying this patch and pushing it before your changes to make sure we have test coverage, please? --- a/test/src/test.coffee
+++ b/test/src/test.coffee
@@ -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")) |
|
||
observer.observe(document.documentElement, subtree: true, childList: true) | ||
else | ||
document.addEventListener "DOMNodeInserted", (event) -> | ||
if event.target.tagName is "SUMMARY" | ||
addAttributesForSummary event.target | ||
if target instanceof HTMLElement | ||
for target in event.target.querySelectorAll('summary') | ||
addAttributesForSummary target |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's an idea for cleaning up the repetition with a new helper:
--- a/src/details-element-polyfill/polyfill.coffee.erb
+++ b/src/details-element-polyfill/polyfill.coffee.erb
@@ -71,22 +71,21 @@ polyfillToggle = ->
summary.setAttribute("aria-expanded", true)
polyfillFocusAndARIA = ->
- for summary in document.querySelectorAll("summary")
+ for summary in findElementsWithTagName(document.body, "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")
@@ -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) ->
695f520
to
d32f24c
Compare
dafa06c
to
a900e99
Compare
Co-authored-by: Javan Makhmali <[email protected]>
Co-authored-by: Javan Makhmali <[email protected]>
test/src/test.coffee
Outdated
@@ -25,6 +27,9 @@ test "summary is focusable", (assert) -> | |||
done = assert.async() | |||
summary = getElement("summary") | |||
defer -> | |||
if navigator.userAgent.match(/Trident|Edge/) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we test for native support instead of user agent sniffing here? That way the tests won't break if/when Edge implements the <details>
element. Maybe if (typeof HTMLDetailsElement is "undefined")
?
@@ -71,22 +71,21 @@ polyfillToggle = -> | |||
summary.setAttribute("aria-expanded", true) | |||
|
|||
polyfillFocusAndARIA = -> | |||
for summary in document.querySelectorAll("summary") | |||
for summary in findElementsWithTagName(document, "SUMMARY") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to pass document.documentElement
or document.body
here, or update findElementsWithTagName()
because document.nodeType
!= Node.ELEMENT_NODE
.
So that when Edge implements details the test wouldn't break
❤️ Appreciate the reviews. Updated. 🙆🏻 |
Follow-up for #5.
In Edge, with
attributes would not be added to
<summary>
because the code only check to see if the top most node added isDETAILS
. Now we query for all childsummary
and add attributes to them.cc @josh