Skip to content

Commit b7bc4e3

Browse files
committed
Update common files.
1 parent f3edaa5 commit b7bc4e3

File tree

4 files changed

+278
-76
lines changed

4 files changed

+278
-76
lines changed

common/common.js

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ function restrictReferences(utils, content) {
4343
// class 'termlist', and if the target of that reference is
4444
// also within a 'dl' element of class 'termlist', then
4545
// consider it an internal reference and ignore it.
46-
require(["core/pubsubhub"], function(respecEvents) {
46+
require(["core/pubsubhub"], (respecEvents) => {
4747
"use strict";
48-
respecEvents.sub('end', function(message) {
48+
respecEvents.sub('end', (message) => {
4949
if (message === 'core/link-to-dfn') {
5050
// all definitions are linked; find any internal references
5151
const internalTerms = document.querySelectorAll(".termlist a.internalDFN");
@@ -69,7 +69,7 @@ require(["core/pubsubhub"], function(respecEvents) {
6969

7070
// clearRefs is recursive. Walk down the tree of
7171
// references to ensure that all references are resolved.
72-
const clearRefs = function(theTerm) {
72+
const clearRefs = (theTerm) => {
7373
if (termsReferencedByTerms[theTerm] ) {
7474
for (const item of termsReferencedByTerms[theTerm]) {
7575
if (termNames[item]) {
@@ -142,10 +142,10 @@ require(["core/pubsubhub"], function(respecEvents) {
142142
* use include the github.io address (as it should...)
143143
*
144144
*/
145-
require(["core/pubsubhub"], function(respecEvents) {
145+
require(["core/pubsubhub"], (respecEvents) => {
146146
"use strict";
147-
respecEvents.sub('beforesave', function(documentElement) {
148-
$("a[href]", documentElement).each( function(index) {
147+
respecEvents.sub('beforesave', (documentElement) => {
148+
$("a[href]", documentElement).each((index) => {
149149
// Don't rewrite these.
150150
if ($(this, documentElement).closest('dd').prev().text().match(/Latest editor|Test suite|Implementation report/)) return;
151151
if ($(this, documentElement).closest('section.preserve').length > 0) return;
@@ -165,16 +165,45 @@ require(["core/pubsubhub"], function(respecEvents) {
165165
});
166166
});
167167

168+
/*
169+
* Implement tabbed examples.
170+
*/
171+
require(["core/pubsubhub"], (respecEvents) => {
172+
"use strict";
173+
respecEvents.sub('end-all', (documentElement) => {
174+
for (const button of document.querySelectorAll(".ds-selector-tabs .selectors button")) {
175+
button.onclick = () => {
176+
const ex = button.closest(".ds-selector-tabs");
177+
ex.querySelector("button.selected").classList.remove("selected");
178+
ex.querySelector(".selected").classList.remove("selected");
179+
button.classList.add('selected');
180+
ex.querySelector("." + button.dataset.selects).classList.add("selected");
181+
}
182+
}
183+
});
184+
});
185+
168186
function _esc(s) {
169187
return s.replace(/&/g,'&')
170188
.replace(/>/g,'>')
171189
.replace(/"/g,'"')
172190
.replace(/</g,'&lt;');
173191
}
174192

193+
function reindent(text) {
194+
// TODO: use trimEnd when Edge supports it
195+
const lines = text.trimRight().split("\n");
196+
while (lines.length && !lines[0].trim()) {
197+
lines.shift();
198+
}
199+
const indents = lines.filter(s => s.trim()).map(s => s.search(/[^\s]/));
200+
const leastIndent = Math.min(...indents);
201+
return lines.map(s => s.slice(leastIndent)).join("\n");
202+
}
203+
175204
function updateExample(doc, content) {
176205
// perform transformations to make it render and prettier
177-
return _esc(unComment(doc, content))
206+
return _esc(reindent(unComment(doc, content)))
178207
.replace(/\*\*\*\*([^*]*)\*\*\*\*/g, '<span class="hl-bold">$1</span>')
179208
.replace(/####([^#]*)####/g, '<span class="comment">$1</span>');
180209
}

0 commit comments

Comments
 (0)