@@ -12,19 +12,15 @@ var constants = require('./constants')
1212
1313module . exports = element
1414
15- /* Constants. */
16- var EMPTY = ''
17-
18- /* Characters. */
19- var SPACE = ' '
20- var DQ = '"'
21- var SQ = "'"
22- var EQ = '='
23- var LT = '<'
24- var GT = '>'
25- var SO = '/'
26-
27- /* Stringify an element `node`. */
15+ var space = ' '
16+ var quotationMark = '"'
17+ var apostrophe = "'"
18+ var equalsTo = '='
19+ var lessThan = '<'
20+ var greaterThan = '>'
21+ var slash = '/'
22+
23+ // Stringify an element `node`.
2824function element ( ctx , node , index , parent ) {
2925 var parentSchema = ctx . schema
3026 var name = node . tagName
@@ -58,39 +54,37 @@ function element(ctx, node, index, parent) {
5854
5955 content = all ( ctx , root )
6056
61- /* If the node is categorised as void, but it has
62- * children, remove the categorisation. This
63- * enables for example `menuitem`s, which are
64- * void in W3C HTML but not void in WHATWG HTML, to
65- * be stringified properly. */
57+ // If the node is categorised as void, but it has children, remove the
58+ // categorisation. This enables for example `menuitem`s, which are void in
59+ // W3C HTML but not void in WHATWG HTML, to be stringified properly.
6660 selfClosing = content ? false : selfClosing
6761
6862 if ( attrs || ! omit || ! omit . opening ( node , index , parent ) ) {
69- value = LT + name + ( attrs ? SPACE + attrs : EMPTY )
63+ value = lessThan + name + ( attrs ? space + attrs : '' )
7064
7165 if ( selfClosing && close ) {
72- if ( ! ctx . tightClose || attrs . charAt ( attrs . length - 1 ) === SO ) {
73- value += SPACE
66+ if ( ! ctx . tightClose || attrs . charAt ( attrs . length - 1 ) === slash ) {
67+ value += space
7468 }
7569
76- value += SO
70+ value += slash
7771 }
7872
79- value += GT
73+ value += greaterThan
8074 }
8175
8276 value += content
8377
8478 if ( ! selfClosing && ( ! omit || ! omit . closing ( node , index , parent ) ) ) {
85- value += LT + SO + name + GT
79+ value += lessThan + slash + name + greaterThan
8680 }
8781
8882 ctx . schema = parentSchema
8983
9084 return value
9185}
9286
93- /* Stringify all attributes. */
87+ // Stringify all attributes.
9488function attributes ( ctx , props ) {
9589 var values = [ ]
9690 var key
@@ -125,16 +119,16 @@ function attributes(ctx, props) {
125119 last = result . charAt ( result . length - 1 )
126120 }
127121
128- /* In tight mode, don’t add a space after quoted attributes. */
129- if ( index !== length - 1 && last !== DQ && last !== SQ ) {
130- values [ index ] = result + SPACE
122+ // In tight mode, don’t add a space after quoted attributes.
123+ if ( index !== length - 1 && last !== quotationMark && last !== apostrophe ) {
124+ values [ index ] = result + space
131125 }
132126 }
133127
134- return values . join ( EMPTY )
128+ return values . join ( '' )
135129}
136130
137- /* Stringify one attribute. */
131+ // Stringify one attribute.
138132function attribute ( ctx , key , value ) {
139133 var schema = ctx . schema
140134 var space = schema . space
@@ -155,7 +149,7 @@ function attribute(ctx, key, value) {
155149 value === false ||
156150 ( typeof value === 'number' && isNaN ( value ) )
157151 ) {
158- return EMPTY
152+ return ''
159153 }
160154
161155 name = attributeName ( ctx , name )
@@ -171,7 +165,7 @@ function attribute(ctx, key, value) {
171165 return name + attributeValue ( ctx , key , value , info )
172166}
173167
174- /* Stringify the attribute name. */
168+ // Stringify the attribute name.
175169function attributeName ( ctx , name ) {
176170 // Always encode without parse errors in non-HTML.
177171 var valid = ctx . schema . space === 'html' ? ctx . valid : 1
@@ -180,7 +174,7 @@ function attributeName(ctx, name) {
180174 return entities ( name , xtend ( ctx . entities , { subset : subset } ) )
181175}
182176
183- /* Stringify the attribute value. */
177+ // Stringify the attribute value.
184178function attributeValue ( ctx , key , value , info ) {
185179 var options = ctx . entities
186180 var quote = ctx . quote
@@ -190,8 +184,8 @@ function attributeValue(ctx, key, value, info) {
190184 var subset
191185
192186 if ( typeof value === 'object' && 'length' in value ) {
193- /* `spaces` doesn’t accept a second argument, but it’s
194- * given here just to keep the code cleaner. */
187+ // `spaces` doesn’t accept a second argument, but it’s given here just to
188+ // keep the code cleaner.
195189 value = ( info . commaSeparated ? commas : spaces ) ( value , {
196190 padLeft : ! ctx . tightLists
197191 } )
@@ -202,7 +196,7 @@ function attributeValue(ctx, key, value, info) {
202196 if ( space !== 'html' || value || ! ctx . collapseEmpty ) {
203197 unquoted = value
204198
205- /* Check unquoted value. */
199+ // Check unquoted value.
206200 if ( space === 'html' && ctx . unquoted ) {
207201 subset = constants . unquoted [ ctx . valid ] [ ctx . safe ]
208202 unquoted = entities (
@@ -211,14 +205,14 @@ function attributeValue(ctx, key, value, info) {
211205 )
212206 }
213207
214- /* If `value` contains entities when unquoted... */
208+ // If `value` contains entities when unquoted…
215209 if ( space !== 'html' || ! ctx . unquoted || unquoted !== value ) {
216- /* If the alternative is less common than `quote`, switch. */
210+ // If the alternative is less common than `quote`, switch.
217211 if ( alternative && ccount ( value , quote ) > ccount ( value , alternative ) ) {
218212 quote = alternative
219213 }
220214
221- subset = quote === SQ ? constants . single : constants . double
215+ subset = quote === apostrophe ? constants . single : constants . double
222216 // Always encode without parse errors in non-HTML.
223217 subset = subset [ space === 'html' ? ctx . valid : 1 ] [ ctx . safe ]
224218
@@ -227,8 +221,8 @@ function attributeValue(ctx, key, value, info) {
227221 value = quote + value + quote
228222 }
229223
230- /* Don’t add a `=` for unquoted empties. */
231- value = value ? EQ + value : value
224+ // Don’t add a `=` for unquoted empties.
225+ value = value ? equalsTo + value : value
232226 }
233227
234228 return value
0 commit comments