-
Notifications
You must be signed in to change notification settings - Fork 380
Expand file tree
/
Copy pathwcag.js
More file actions
150 lines (137 loc) · 5.21 KB
/
wcag.js
File metadata and controls
150 lines (137 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
var version="22";
function titleToPathFrag (title) {
return title.toLowerCase().replace(/[\s,]+/g, "-").replace(/[\(\)]/g, "");
}
function findHeading(el) {
return el.querySelector('h1, h2, h3, h4, h5, h6');
}
function textNoDescendant(el) {
var textContent = "";
el.childNodes.forEach(function(node) {
if (node.nodeType == 3) textContent += node.textContent;
})
return textContent;
}
function linkUnderstanding() {
var understandingBaseURI;
if (respecConfig.specStatus == "ED") understandingBaseURI = "../../understanding/";
else understandingBaseURI = "https://www.w3.org/WAI/WCAG" + version + "/Understanding/";
document.querySelectorAll('.sc,.guideline').forEach(function(node){
var heading = textNoDescendant(findHeading(node));
var pathFrag = titleToPathFrag(heading);
if (node.id == "parsing") pathFrag = "parsing"; // special case parsing
var el = document.createElement("div");
el.setAttribute("class", "doclinks");
el.innerHTML = "<a href=\"" + understandingBaseURI + pathFrag + ".html\">Understanding " + heading +
"</a> <span class=\"screenreader\">|</span> <a href=\"https://www.w3.org/WAI/WCAG" + version + "/quickref/#" + pathFrag + "\">How to Meet " + heading + "</a>";
if (node.className = "sc") node.insertBefore(el, node.children[2]);
if (node.className = "guideline") node.insertBefore(el, node.children[1]);
})
}
function addTextSemantics() {
// put level before and parentheses around the conformance level marker
document.querySelectorAll('p.conformance-level').forEach(function(node){
var level = node.textContent;
node.textContent = "(Level " + level + ")";
})
// put principle in principle headings
document.querySelectorAll('section.sc h2 bdi.secno').forEach(function(node){
var num = node.textContent;
node.textContent = "Principle " + num;
})
// put guideline in GL headings
document.querySelectorAll('section.guideline h3 bdi.secno').forEach(function(node){
var num = node.textContent;
node.textContent = "Guideline " + num;
})
// put success criterion in SC headings
document.querySelectorAll('section.sc h4 bdi.secno').forEach(function(node){
var num = node.textContent;
node.textContent = "Success Criterion " + num;
})
}
function markConformanceLevel() {
}
function swapInDefinitions() {
if (new URLSearchParams(window.location.search).get("defs") != null) document.querySelectorAll('.internalDFN').forEach(function(node){
node.title = node.textContent;
node.textContent = findDef(document.querySelector(node.href.substring(node.href.indexOf('#'))).parentNode.nextElementSibling.firstElementChild).textContent;
})
function findDef(el){
if (el.hasAttribute('class')) return findDef(el.nextElementSibling);
else return el;
}
}
function termTitles() {
// put definitions into title attributes of term references
document.querySelectorAll('.internalDFN').forEach(function(node){
var dfn = document.querySelector(node.href.substring(node.href.indexOf('#')));
if (dfn.parentNode.nodeName == "DT")
node.title = dfn.parentNode.nextElementSibling.querySelector(":not(.change)").textContent.trim().replace(/\s+/g,' ');
else if (dfn.title) node.title=dfn.title;
});
}
// number notes if there are multiple per section
function numberNotes() {
var sectionsWithNotes = new Array();
document.querySelectorAll(".note").forEach(function(note) {
var container = note.closest("dd");
if (container == null) container = note.closest("section");
sectionsWithNotes.push(container);
});
sectionsWithNotes.forEach(function(sec) {
if (sec.noteprocessed) return;
var notes = sec.querySelectorAll('.note');
// no notes, shouldn't happen
if (notes.length == 0) return;
// one note, leave alone
if (notes.length == 1) return;
// more than one note, number them
if (notes.length > 1) {
var count = 1;
sec.querySelectorAll(".note").forEach(function(note) {
var span = note.querySelector(".note-title span");
span.textContent = "Note " + count;
count++;
});
}
sec.noteprocessed = true;
});
}
// change the numbering of examples to remove number from lone examples in a section, and restart numbering for multiple in each section
function renumberExamples() {
var sectionsWithExamples = new Array();
document.querySelectorAll(".example").forEach(function(example) {
var container = example.closest("dd"); // use dd container if present
if (container == null) container = example.closest("section"); // otherwise section
sectionsWithExamples.push(container);
});
sectionsWithExamples.forEach(function(sec) {
if (sec.exprocessed) return;
var examples = sec.querySelectorAll(".example");
// no examples, shouldn't happen
if (examples.length == 0) return;
// one example, remove the numbering
// more than one example, number them
else {
var count = 1;
var rmOrAdd = examples.length == 1 ? "rm" : "add";
sec.querySelectorAll(".example").forEach(function(example) {
var marker = example.querySelector(".marker");
if (rmOrAdd == "rm") marker.textContent = "Example";
else marker.textContent = "Example " + count;
count++;
});
}
sec.exprocessed = true;
});
}
// scripts after Respec has run
function postRespec() {
addTextSemantics();
swapInDefinitions();
termTitles();
linkUnderstanding();
numberNotes();
renumberExamples();
}