Skip to content

Commit 0116d07

Browse files
committed
Refactor to improve bundle size
1 parent 001e937 commit 0116d07

16 files changed

+295
-464
lines changed

Diff for: lib/all.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ module.exports = all
66

77
// Serialize all children of `parent`.
88
function all(ctx, parent) {
9-
var children = parent && parent.children
10-
var length = children && children.length
11-
var index = -1
129
var results = []
10+
var children = (parent && parent.children) || []
11+
var index = -1
1312

14-
while (++index < length) {
13+
while (++index < children.length) {
1514
results[index] = one(ctx, children[index], index, parent)
1615
}
1716

Diff for: lib/comment.js

+5-15
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,13 @@ var entities = require('stringify-entities')
55

66
module.exports = serializeComment
77

8-
// See: <https://html.spec.whatwg.org/multipage/syntax.html#comments>
9-
var breakout = /^>|^->|<!--|-->|--!>|<!-$/g
10-
var subset = ['<', '>']
11-
var bogusSubset = ['>']
12-
138
function serializeComment(ctx, node) {
14-
var value = node.value
15-
16-
if (ctx.bogusComments) {
17-
return (
18-
'<?' + entities(value, xtend(ctx.entities, {subset: bogusSubset})) + '>'
19-
)
20-
}
21-
22-
return '<!--' + value.replace(breakout, encode) + '-->'
9+
// See: <https://html.spec.whatwg.org/multipage/syntax.html#comments>
10+
return ctx.bogusComments
11+
? '<?' + entities(node.value, xtend(ctx.entities, {subset: ['>']})) + '>'
12+
: '<!--' + node.value.replace(/^>|^->|<!--|-->|--!>|<!-$/g, encode) + '-->'
2313

2414
function encode($0) {
25-
return entities($0, xtend(ctx.entities, {subset: subset}))
15+
return entities($0, xtend(ctx.entities, {subset: ['<', '>']}))
2616
}
2717
}

Diff for: lib/constants.js

+12-55
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,28 @@
11
'use strict'
22

3-
// Characters.
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]
20-
21-
// See: <https://html.spec.whatwg.org/#attribute-name-state>.
22-
var name = whitespace.concat(ampersand, slash, greaterThan, equalsTo)
23-
24-
// See: <https://html.spec.whatwg.org/#attribute-value-(unquoted)-state>.
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-
35-
// See: <https://html.spec.whatwg.org/#attribute-value-(single-quoted)-state>.
36-
var singleQuoted = [ampersand, apostrophe]
37-
38-
// See: <https://html.spec.whatwg.org/#attribute-value-(double-quoted)-state>.
39-
var doubleQuoted = [ampersand, quotationMark]
40-
413
// Maps of subsets.
424
// Each value is a matrix of tuples.
435
// The first value causes parse errors, the second is valid.
446
// Of both values, the first value is unsafe, and the second is safe.
457
module.exports = {
8+
// See: <https://html.spec.whatwg.org/#attribute-name-state>.
469
name: [
47-
[name, name.concat(quotationMark, apostrophe, graveAccent)],
48-
[
49-
name.concat(nil, quotationMark, apostrophe, lessThan),
50-
name.concat(nil, quotationMark, apostrophe, lessThan, graveAccent)
51-
]
10+
['\t\n\f\r &/=>'.split(''), '\t\n\f\r "&\'/=>`'.split('')],
11+
['\0\t\n\f\r "&\'/<=>'.split(''), '\0\t\n\f\r "&\'/<=>`'.split('')]
5212
],
13+
// See: <https://html.spec.whatwg.org/#attribute-value-(unquoted)-state>.
5314
unquoted: [
54-
[unquoted, unquotedSafe],
55-
[unquotedSafe, unquotedSafe]
15+
['\t\n\f\r &>'.split(''), '\0\t\n\f\r "&\'<=>`'.split('')],
16+
['\0\t\n\f\r "&\'<=>`'.split(''), '\0\t\n\f\r "&\'<=>`'.split('')]
5617
],
18+
// See: <https://html.spec.whatwg.org/#attribute-value-(single-quoted)-state>.
5719
single: [
58-
[singleQuoted, singleQuoted.concat(quotationMark, graveAccent)],
59-
[
60-
singleQuoted.concat(nil),
61-
singleQuoted.concat(nil, quotationMark, graveAccent)
62-
]
20+
["&'".split(''), '"&\'`'.split('')],
21+
["\0&'".split(''), '\0"&\'`'.split('')]
6322
],
23+
// See: <https://html.spec.whatwg.org/#attribute-value-(double-quoted)-state>.
6424
double: [
65-
[doubleQuoted, doubleQuoted.concat(apostrophe, graveAccent)],
66-
[
67-
doubleQuoted.concat(nil),
68-
doubleQuoted.concat(nil, apostrophe, graveAccent)
69-
]
25+
['"&'.split(''), '"&\'`'.split('')],
26+
['\0"&'.split(''), '\0"&\'`'.split('')]
7027
]
7128
}

Diff for: lib/doctype.js

+11-19
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,36 @@ var entities = require('stringify-entities')
66

77
module.exports = serializeDoctype
88

9-
var docLower = 'doctype'
10-
var docUpper = docLower.toUpperCase()
11-
129
function serializeDoctype(ctx, node) {
13-
var doc = ctx.upperDoctype ? docUpper : docLower
1410
var sep = ctx.tightDoctype ? '' : ' '
15-
var name = node.name
16-
var pub = node.public
17-
var sys = node.system
18-
var parts = ['<!' + doc]
11+
var parts = ['<!' + (ctx.upperDoctype ? 'DOCTYPE' : 'doctype')]
1912

20-
if (name) {
21-
parts.push(sep, name)
13+
if (node.name) {
14+
parts.push(sep, node.name)
2215

23-
if (pub !== null && pub !== undefined) {
24-
parts.push(' public', sep, quote(ctx, pub))
25-
} else if (sys !== null && sys !== undefined) {
16+
if (node.public != null) {
17+
parts.push(' public', sep, quote(ctx, node.public))
18+
} else if (node.system != null) {
2619
parts.push(' system')
2720
}
2821

29-
if (sys !== null && sys !== undefined) {
30-
parts.push(sep, quote(ctx, sys))
22+
if (node.system != null) {
23+
parts.push(sep, quote(ctx, node.system))
3124
}
3225
}
3326

3427
return parts.join('') + '>'
3528
}
3629

3730
function quote(ctx, value) {
38-
var primary = ctx.quote
39-
var secondary = ctx.alternative
4031
var string = String(value)
4132
var quote =
42-
ccount(string, primary) > ccount(string, secondary) ? secondary : primary
33+
ccount(string, ctx.quote) > ccount(string, ctx.alternative)
34+
? ctx.alternative
35+
: ctx.quote
4336

4437
return (
4538
quote +
46-
// Prevent breaking out of doctype.
4739
entities(string, xtend(ctx.entities, {subset: ['<', '&', quote]})) +
4840
quote
4941
)

0 commit comments

Comments
 (0)