Skip to content
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

Merged
merged 5 commits into from
Oct 15, 2018

Conversation

muan
Copy link
Collaborator

@muan muan commented Oct 12, 2018

Follow-up for #5.

In Edge, with

const div = document.createElement('div')
div.innerHTML = '<details><summary></summary></details>'
document.body.append(div)

attributes would not be added to <summary> because the code only check to see if the top most node added is DETAILS. Now we query for all child summary and add attributes to them.

cc @josh

@muan
Copy link
Collaborator Author

muan commented Oct 12, 2018

Hmm it's not clear to me why the test is failing. Looks like there's no output?

@javan
Copy link
Owner

javan commented Oct 12, 2018

https://app.saucelabs.com/tests/07da98865afb42499311b4784249c902

Looks like the tests hung in a few browsers. Passed on retry. 🤷‍♂️

@javan
Copy link
Owner

javan commented Oct 12, 2018

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
Copy link
Owner

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) ->

@muan muan force-pushed the children branch 2 times, most recently from 695f520 to d32f24c Compare October 12, 2018 22:30
@muan muan force-pushed the children branch 2 times, most recently from dafa06c to a900e99 Compare October 12, 2018 22:54
@muan
Copy link
Collaborator Author

muan commented Oct 12, 2018

@javan Done & done. Ended up with a bunch of force pushes trying to get github to display the order of commits correctly. 🤦‍♀️ . Should be OK now!

I ended up adding a900e99 to assert the attributes in Edge and IE, otherwise the test would still pass with the new wrapping container.

@@ -25,6 +27,9 @@ test "summary is focusable", (assert) ->
done = assert.async()
summary = getElement("summary")
defer ->
if navigator.userAgent.match(/Trident|Edge/)
Copy link
Owner

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")
Copy link
Owner

@javan javan Oct 13, 2018

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.

muan added 2 commits October 13, 2018 10:46
So that when Edge implements details the test wouldn't break
@muan
Copy link
Collaborator Author

muan commented Oct 13, 2018

❤️ Appreciate the reviews. Updated. 🙆🏻

@javan javan merged commit 69cad52 into javan:master Oct 15, 2018
@javan
Copy link
Owner

javan commented Oct 15, 2018

✌️

@muan muan deleted the children branch October 15, 2018 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants