forked from w3c/payment-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
117 lines (108 loc) · 4.44 KB
/
utils.js
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
// We should be able to remove terms that are not actually
// referenced from the common definitions
//
// the termlist is in a block of class "termlist", so make sure that
// has an ID and put that ID into the termLists array so we can
// interrogate all of the included termlists later.
var termNames = [] ;
var termLists = [] ;
var termsReferencedByTerms = [] ;
function restrictReferences(utils, content) {
var base = document.createElement("div");
base.innerHTML = content;
// New new logic:
//
// 1. build a list of all term-internal references
// 2. When ready to process, for each reference INTO the terms,
// remove any terms they reference from the termNames array too.
$.each(base.querySelectorAll("dfn"), function(i, item) {
var $t = $(item) ;
var titles = $t.getDfnTitles();
var n = $t.makeID("dfn", titles[0]);
if (n) {
termNames[n] = $t.parent() ;
}
});
var $container = $(".termlist",base) ;
var containerID = $container.makeID("", "terms") ;
termLists.push(containerID) ;
// add a handler to come in after all the definitions are resolved
//
// New logic: If the reference is within a 'dl' element of
// class 'termlist', and if the target of that reference is
// also within a 'dl' element of class 'termlist', then
// consider it an internal reference and ignore it.
return (base.innerHTML);
}
require(["core/pubsubhub"], function(respecEvents) {
"use strict";
respecEvents.sub('end', function(message) {
if (message == 'core/link-to-dfn') {
// all definitions are linked; find any internal references
$(".termlist a.internalDFN").each(function() {
var $r = $(this);
var id = $r.attr('href');
var idref = id.replace(/^#/,"") ;
if (termNames[idref]) {
// this is a reference to another term
// what is the idref of THIS term?
var $def = $r.closest('dd') ;
if ($def.length) {
var $p = $def.prev('dt').find('dfn') ;
var tid = $p.attr('id') ;
if (tid) {
if (termsReferencedByTerms[tid]) {
termsReferencedByTerms[tid].push(idref);
} else {
termsReferencedByTerms[tid] = [] ;
termsReferencedByTerms[tid].push(idref);
}
}
}
}
}) ;
// clearRefs is recursive. Walk down the tree of
// references to ensure that all references are resolved.
var clearRefs = function(theTerm) {
if ( termsReferencedByTerms[theTerm] ) {
$.each(termsReferencedByTerms[theTerm], function(i, item) {
if (termNames[item]) {
delete termNames[item];
clearRefs(item);
}
});
}
// make sure this term doesn't get removed
if (termNames[theTerm]) {
delete termNames[theTerm];
}
};
// now termsReferencedByTerms has ALL terms that
// reference other terms, and a list of the
// terms that they reference
$("a.internalDFN").each(function () {
var $item = $(this) ;
var t = $item.attr('href');
var r = t.replace(/^#/,"") ;
// if the item is outside the term list
if ( ! $item.closest('dl.termlist').length ) {
clearRefs(r);
}
});
// delete any terms that were not referenced.
Object.keys(termNames).forEach(function(term) {
var $p = $("#"+term) ;
if ($p) {
var tList = $p.getDfnTitles();
$p.parent().next().remove();
$p.remove() ;
tList.forEach(function( item ) {
if (respecConfig.definitionMap[item]) {
delete respecConfig.definitionMap[item];
}
});
}
});
}
});
});