Skip to content

Commit

Permalink
create <summary> if it's absent
Browse files Browse the repository at this point in the history
  • Loading branch information
chemerisuk committed Nov 11, 2014
1 parent d4e2dca commit ba15da6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
4 changes: 4 additions & 0 deletions index.html
Expand Up @@ -40,6 +40,10 @@ <h2>Nested elements</h2>
</ul>
</details>
</details>
<h2>No summary</h2>
<details>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p>
</details>
<script src="bower_components/better-dom/dist/better-dom.js"></script>
<script src="build/better-details-polyfill.js"></script>
</body>
Expand Down
28 changes: 19 additions & 9 deletions src/better-details-polyfill.js
Expand Up @@ -9,16 +9,26 @@
// http://www.w3.org/html/wg/drafts/html/master/interactive-elements.html#the-details-element
this.set("role", "group")
.on("toggle", ["stopPropagation"], (stop) => { stop() })
.children("summary:first-child").forEach(this.doInitSummary);
.defineAttribute("open", {
get: this.doGetOpen,
set: this.doSetOpen
});

this.defineAttribute("open", {
get: this.doGetOpen,
set: this.doSetOpen
});
},
doInitSummary(summary) {
summary
// http://www.w3.org/html/wg/drafts/html/master/interactive-elements.html#the-summary-element
var summaries = this.children("summary");

if (!summaries.length) {
// If there is no child summary element, the user agent
// should provide its own legend (e.g. "Details")
summaries.push(DOM.create("summary>`Details`"));
}

// make sure that the <summary> is the first child
if (this.child(0) !== summaries[0]) {
this.prepend(summaries[0]);
}

// http://www.w3.org/html/wg/drafts/html/master/interactive-elements.html#the-summary-element
summaries[0]
.set({role: "button", tabindex: 0})
.on("keydown", ["which"], this.doToggleOpen)
.on("click", this.doToggleOpen);
Expand Down

0 comments on commit ba15da6

Please sign in to comment.