Skip to content

Commit 8fc4a77

Browse files
committed
Refactor code-style
1 parent 93a3049 commit 8fc4a77

File tree

18 files changed

+135
-117
lines changed

18 files changed

+135
-117
lines changed

lib/all.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var one = require('./one')
44

55
module.exports = all
66

7-
/* Stringify all children of `parent`. */
7+
// Stringify all children of `parent`.
88
function all(ctx, parent) {
99
var children = parent && parent.children
1010
var length = children && children.length

lib/comment.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module.exports = comment
44

5-
/* Stringify a comment `node`. */
5+
// Stringify a comment `node`.
66
function comment(ctx, node) {
77
return '<!--' + node.value + '-->'
88
}

lib/constants.js

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,67 @@
11
'use strict'
22

33
// Characters.
4-
var NULL = '\0'
5-
var AMP = '&'
6-
var SP = ' '
7-
var TB = '\t'
8-
var GR = '`'
9-
var DQ = '"'
10-
var SQ = "'"
11-
var EQ = '='
12-
var LT = '<'
13-
var GT = '>'
14-
var SO = '/'
15-
var LF = '\n'
16-
var CR = '\r'
17-
var FF = '\f'
4+
var nil = '\0'
5+
var ampersand = '&'
6+
var space = ' '
7+
var tab = '\t'
8+
var graveAccent = '`'
9+
var quotationMark = '"'
10+
var apostrophe = "'"
11+
var equalsTo = '='
12+
var lessThan = '<'
13+
var greaterThan = '>'
14+
var slash = '/'
15+
var lineFeed = '\n'
16+
var carriageReturn = '\r'
17+
var formFeed = '\f'
18+
19+
var whitespace = [space, tab, lineFeed, carriageReturn, formFeed]
1820

19-
var whitespace = [SP, TB, LF, CR, FF]
2021
// https://html.spec.whatwg.org/#attribute-name-state
21-
var name = whitespace.concat(AMP, SO, GT, EQ)
22+
var name = whitespace.concat(ampersand, slash, greaterThan, equalsTo)
23+
2224
// https://html.spec.whatwg.org/#attribute-value-(unquoted)-state
23-
var unquoted = whitespace.concat(AMP, GT)
24-
var unquotedSafe = unquoted.concat(NULL, DQ, SQ, LT, EQ, GR)
25+
var unquoted = whitespace.concat(ampersand, greaterThan)
26+
var unquotedSafe = unquoted.concat(
27+
nil,
28+
quotationMark,
29+
apostrophe,
30+
lessThan,
31+
equalsTo,
32+
graveAccent
33+
)
34+
2535
// https://html.spec.whatwg.org/#attribute-value-(single-quoted)-state
26-
var singleQuoted = [AMP, SQ]
36+
var singleQuoted = [ampersand, apostrophe]
37+
2738
// https://html.spec.whatwg.org/#attribute-value-(double-quoted)-state
28-
var doubleQuoted = [AMP, DQ]
39+
var doubleQuoted = [ampersand, quotationMark]
2940

3041
// Maps of subsets. Each value is a matrix of tuples.
3142
// The first value causes parse errors, the second is valid.
3243
// Of both values, the first value is unsafe, and the second is safe.
3344
module.exports = {
3445
name: [
35-
[name, name.concat(DQ, SQ, GR)],
36-
[name.concat(NULL, DQ, SQ, LT), name.concat(NULL, DQ, SQ, LT, GR)]
46+
[name, name.concat(quotationMark, apostrophe, graveAccent)],
47+
[
48+
name.concat(nil, quotationMark, apostrophe, lessThan),
49+
name.concat(nil, quotationMark, apostrophe, lessThan, graveAccent)
50+
]
3751
],
3852
unquoted: [[unquoted, unquotedSafe], [unquotedSafe, unquotedSafe]],
3953
single: [
40-
[singleQuoted, singleQuoted.concat(DQ, GR)],
41-
[singleQuoted.concat(NULL), singleQuoted.concat(NULL, DQ, GR)]
54+
[singleQuoted, singleQuoted.concat(quotationMark, graveAccent)],
55+
[
56+
singleQuoted.concat(nil),
57+
singleQuoted.concat(nil, quotationMark, graveAccent)
58+
]
4259
],
4360
double: [
44-
[doubleQuoted, doubleQuoted.concat(SQ, GR)],
45-
[doubleQuoted.concat(NULL), doubleQuoted.concat(NULL, SQ, GR)]
61+
[doubleQuoted, doubleQuoted.concat(apostrophe, graveAccent)],
62+
[
63+
doubleQuoted.concat(nil),
64+
doubleQuoted.concat(nil, apostrophe, graveAccent)
65+
]
4666
]
4767
}

lib/doctype.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module.exports = doctype
44

5-
/* Stringify a doctype `node`. */
5+
// Stringify a doctype `node`.
66
function doctype(ctx, node) {
77
var sep = ctx.tightDoctype ? '' : ' '
88
var name = node.name

lib/element.js

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,15 @@ var constants = require('./constants')
1212

1313
module.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`.
2824
function 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.
9488
function 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.
138132
function 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.
175169
function 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.
184178
function 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

lib/index.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,25 @@ var one = require('./one')
88

99
module.exports = toHTML
1010

11-
/* Characters. */
12-
var DQ = '"'
13-
var SQ = "'"
11+
var quotationMark = '"'
12+
var apostrophe = "'"
1413

15-
/* Stringify the given HAST node. */
14+
// Stringify the given HAST node.
1615
function toHTML(node, options) {
1716
var settings = options || {}
18-
var quote = settings.quote || DQ
19-
var alternative = quote === DQ ? SQ : DQ
17+
var quote = settings.quote || quotationMark
18+
var alternative = quote === quotationMark ? apostrophe : quotationMark
2019
var smart = settings.quoteSmart
2120

22-
if (quote !== DQ && quote !== SQ) {
21+
if (quote !== quotationMark && quote !== apostrophe) {
2322
throw new Error(
24-
'Invalid quote `' + quote + '`, expected `' + SQ + '` or `' + DQ + '`'
23+
'Invalid quote `' +
24+
quote +
25+
'`, expected `' +
26+
apostrophe +
27+
'` or `' +
28+
quotationMark +
29+
'`'
2530
)
2631
}
2732

0 commit comments

Comments
 (0)