Skip to content

Commit 422167c

Browse files
committed
Update common files.
1 parent 8530193 commit 422167c

File tree

2 files changed

+422
-190
lines changed

2 files changed

+422
-190
lines changed

common/common.js

Lines changed: 93 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Web Payments Community Group common spec JavaScript */
2-
var jsonld = {
2+
const jsonld = {
33
// Add as the respecConfig localBiblio variable
44
// Extend or override global respec references
55
localBiblio: {
@@ -59,32 +59,33 @@ var jsonld = {
5959
// the termlist is in a block of class "termlist", so make sure that
6060
// has an ID and put that ID into the termLists array so we can
6161
// interrogate all of the included termlists later.
62-
var termNames = [] ;
63-
var termLists = [] ;
64-
var termsReferencedByTerms = [] ;
62+
const termNames = [] ;
63+
const termLists = [] ;
64+
const termsReferencedByTerms = [] ;
6565

6666
function restrictReferences(utils, content) {
67-
var base = document.createElement("div");
68-
base.innerHTML = content;
67+
const base = document.createElement("div");
68+
base.innerHTML = content;
6969

70-
// New new logic:
71-
//
72-
// 1. build a list of all term-internal references
73-
// 2. When ready to process, for each reference INTO the terms,
74-
// remove any terms they reference from the termNames array too.
75-
$.each(base.querySelectorAll("dfn:not(.preserve)"), function(i, item) {
76-
var $t = $(item) ;
77-
var titles = $t.getDfnTitles();
78-
var n = $t.makeID("dfn", titles[0]);
79-
if (n) {
80-
termNames[n] = $t.parent() ;
81-
}
82-
});
70+
// New new logic:
71+
//
72+
// 1. build a list of all term-internal references
73+
// 2. When ready to process, for each reference INTO the terms,
74+
// remove any terms they reference from the termNames array too.
75+
const noPreserve = base.querySelectorAll("dfn:not(.preserve)");
76+
for (const item of noPreserve) {
77+
const $t = $(item) ;
78+
const titles = $t.getDfnTitles();
79+
const n = $t.makeID("dfn", titles[0]);
80+
if (n) {
81+
termNames[n] = $t.parent();
82+
}
83+
}
8384

84-
var $container = $(".termlist", base) ;
85-
var containerID = $container.makeID("", "terms") ;
86-
termLists.push(containerID) ;
87-
return (base.innerHTML);
85+
const $container = $(".termlist", base) ;
86+
const containerID = $container.makeID("", "terms") ;
87+
termLists.push(containerID) ;
88+
return (base.innerHTML);
8889
}
8990

9091
// add a handler to come in after all the definitions are resolved
@@ -95,100 +96,92 @@ function restrictReferences(utils, content) {
9596
// consider it an internal reference and ignore it.
9697

9798
function internalizeTermListReferences() {
98-
// all definitions are linked; find any internal references
99-
$(".termlist a.internalDFN").each(function() {
100-
var $r = $(this);
101-
var id = $r.attr('href');
102-
var idref = id.replace(/^#/,"") ;
103-
if (termNames[idref]) {
104-
// this is a reference to another term
105-
// what is the idref of THIS term?
106-
var $def = $r.closest('dd') ;
107-
if ($def.length) {
108-
var $p = $def.prev('dt').find('dfn') ;
109-
var tid = $p.attr('id') ;
110-
if (tid) {
111-
if (termsReferencedByTerms[tid]) {
112-
termsReferencedByTerms[tid].push(idref);
113-
} else {
114-
termsReferencedByTerms[tid] = [] ;
115-
termsReferencedByTerms[tid].push(idref);
116-
}
117-
}
118-
}
99+
// all definitions are linked; find any internal references
100+
const internalTerms = document.querySelectorAll(".termlist a.internalDFN");
101+
for (const item of internalTerms) {
102+
const idref = item.getAttribute('href').replace(/^#/,"") ;
103+
if (termNames[idref]) {
104+
// this is a reference to another term
105+
// what is the idref of THIS term?
106+
const def = item.closest('dd');
107+
if (def) {
108+
const tid = def.previousElementSibling
109+
.querySelector('dfn')
110+
.getAttribute('id');
111+
if (tid) {
112+
if (termsReferencedByTerms[tid]) {
113+
termsReferencedByTerms[tid].push(idref);
114+
} else {
115+
termsReferencedByTerms[tid] = [] ;
116+
termsReferencedByTerms[tid].push(idref);
117+
}
119118
}
120-
});
119+
}
120+
}
121+
}
121122

122-
// clearRefs is recursive. Walk down the tree of
123-
// references to ensure that all references are resolved.
124-
var clearRefs = function(theTerm) {
125-
if ( termsReferencedByTerms[theTerm] ) {
126-
$.each(termsReferencedByTerms[theTerm], function(i, item) {
127-
if (termNames[item]) {
128-
delete termNames[item];
129-
clearRefs(item);
130-
}
131-
});
132-
};
133-
// make sure this term doesn't get removed
134-
if (termNames[theTerm]) {
135-
delete termNames[theTerm];
123+
// clearRefs is recursive. Walk down the tree of
124+
// references to ensure that all references are resolved.
125+
const clearRefs = function(theTerm) {
126+
if ( termsReferencedByTerms[theTerm] ) {
127+
for (const item of termsReferencedByTerms[theTerm]) {
128+
if (termNames[item]) {
129+
delete termNames[item];
130+
clearRefs(item);
136131
}
132+
}
137133
};
134+
// make sure this term doesn't get removed
135+
if (termNames[theTerm]) {
136+
delete termNames[theTerm];
137+
}
138+
};
138139

139-
// now termsReferencedByTerms has ALL terms that
140-
// reference other terms, and a list of the
141-
// terms that they reference
142-
$("a.internalDFN").each(function () {
143-
var $item = $(this) ;
144-
var t = $item.attr('href');
145-
var r = t.replace(/^#/,"") ;
146-
if (r === 'dictionary') {
147-
var rr = r;
148-
}
149-
// if the item is outside the term list
150-
if ( ! $item.closest('dl.termlist').length ) {
151-
clearRefs(r);
152-
}
153-
});
140+
// now termsReferencedByTerms has ALL terms that
141+
// reference other terms, and a list of the
142+
// terms that they reference
143+
const internalRefs = document.querySelectorAll("a.internalDFN");
144+
for (const item of internalRefs) {
145+
const idref = item.getAttribute('href').replace(/^#/,"") ;
146+
// if the item is outside the term list
147+
if ( !item.closest('dl.termlist') ) {
148+
clearRefs(idref);
149+
}
150+
}
154151

155-
// delete any terms that were not referenced.
156-
Object.keys(termNames).forEach(function(term) {
157-
var $p = $("#"+term) ;
158-
if ($p) {
159-
var tList = $p.getDfnTitles();
160-
$p.parent().next().remove();
161-
$p.remove() ;
162-
tList.forEach(function( item ) {
163-
if (respecConfig.definitionMap[item]) {
164-
delete respecConfig.definitionMap[item];
165-
}
166-
});
152+
// delete any terms that were not referenced.
153+
for (const term of Object.keys(termNames)) {
154+
const $p = $("#"+term) ;
155+
if ($p && !$p.empty()) {
156+
const tList = $p.getDfnTitles();
157+
$p.parent().next().remove();
158+
$p.remove() ;
159+
for (const item of tList) {
160+
if (respecConfig.definitionMap[item]) {
161+
delete respecConfig.definitionMap[item];
167162
}
168-
});
163+
}
164+
}
165+
}
169166
}
170167

171168
function _esc(s) {
172-
s = s.replace(/&/g,'&');
173-
s = s.replace(/>/g,'>');
174-
s = s.replace(/"/g,'"');
175-
s = s.replace(/</g,'&lt;');
176-
return s;
169+
return s.replace(/&/g,'&amp;')
170+
.replace(/>/g,'&gt;')
171+
.replace(/"/g,'&quot;')
172+
.replace(/</g,'&lt;');
177173
}
178174

179175
function updateExample(doc, content) {
180176
// perform transformations to make it render and prettier
181-
content = unComment(doc, content);
182-
content = _esc(content);
183-
content = content.replace(/\*\*\*\*([^*]*)\*\*\*\*/g, '<span class="hl-bold">$1</span>');
184-
content = content.replace(/####([^#]*)####/g, '<span class="comment">$1</span>');
185-
return content ;
177+
return _esc(unComment(doc, content))
178+
.replace(/\*\*\*\*([^*]*)\*\*\*\*/g, '<span class="hl-bold">$1</span>')
179+
.replace(/####([^#]*)####/g, '<span class="comment">$1</span>');
186180
}
187181

188182

189183
function unComment(doc, content) {
190184
// perform transformations to make it render and prettier
191-
content = content.replace(/<!--/, '');
192-
content = content.replace(/-->/, '');
193-
return content ;
185+
return content.replace(/<!--/, '')
186+
.replace(/-->/, '');
194187
}

0 commit comments

Comments
 (0)