1
1
/* Web Payments Community Group common spec JavaScript */
2
- var jsonld = {
2
+ const jsonld = {
3
3
// Add as the respecConfig localBiblio variable
4
4
// Extend or override global respec references
5
5
localBiblio : {
@@ -59,32 +59,33 @@ var jsonld = {
59
59
// the termlist is in a block of class "termlist", so make sure that
60
60
// has an ID and put that ID into the termLists array so we can
61
61
// 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 = [ ] ;
65
65
66
66
function restrictReferences ( utils , content ) {
67
- var base = document . createElement ( "div" ) ;
68
- base . innerHTML = content ;
67
+ const base = document . createElement ( "div" ) ;
68
+ base . innerHTML = content ;
69
69
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
+ }
83
84
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 ) ;
88
89
}
89
90
90
91
// add a handler to come in after all the definitions are resolved
@@ -95,100 +96,92 @@ function restrictReferences(utils, content) {
95
96
// consider it an internal reference and ignore it.
96
97
97
98
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
+ }
119
118
}
120
- } ) ;
119
+ }
120
+ }
121
+ }
121
122
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 ) ;
136
131
}
132
+ }
137
133
} ;
134
+ // make sure this term doesn't get removed
135
+ if ( termNames [ theTerm ] ) {
136
+ delete termNames [ theTerm ] ;
137
+ }
138
+ } ;
138
139
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
+ }
154
151
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 ] ;
167
162
}
168
- } ) ;
163
+ }
164
+ }
165
+ }
169
166
}
170
167
171
168
function _esc ( s ) {
172
- s = s . replace ( / & / g, '&' ) ;
173
- s = s . replace ( / > / g, '>' ) ;
174
- s = s . replace ( / " / g, '"' ) ;
175
- s = s . replace ( / < / g, '<' ) ;
176
- return s ;
169
+ return s . replace ( / & / g, '&' )
170
+ . replace ( / > / g, '>' )
171
+ . replace ( / " / g, '"' )
172
+ . replace ( / < / g, '<' ) ;
177
173
}
178
174
179
175
function updateExample ( doc , content ) {
180
176
// 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>' ) ;
186
180
}
187
181
188
182
189
183
function unComment ( doc , content ) {
190
184
// 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 ( / - - > / , '' ) ;
194
187
}
0 commit comments