Skip to content

Commit 29a0d7c

Browse files
committed
Refactor internals
1 parent 4570cc4 commit 29a0d7c

23 files changed

+190
-204
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+
// Serialize 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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
'use strict'
22

3-
module.exports = comment
3+
module.exports = serializeComment
44

5-
// Stringify a comment `node`.
6-
function comment(ctx, node) {
5+
function serializeComment(ctx, node) {
76
return '<!--' + node.value + '-->'
87
}

lib/doctype.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
'use strict'
22

3-
module.exports = doctype
3+
module.exports = serializeDoctype
44

55
var docLower = 'doctype'
66
var docUpper = docLower.toUpperCase()
77

8-
// Stringify a doctype `node`.
9-
function doctype(ctx, node) {
8+
function serializeDoctype(ctx, node) {
109
var doc = ctx.upperDoctype ? docUpper : docLower
1110
var sep = ctx.tightDoctype ? '' : ' '
1211
var name = node.name
@@ -17,13 +16,13 @@ function doctype(ctx, node) {
1716
if (name) {
1817
val.push(sep, name)
1918

20-
if (pub != null) {
19+
if (pub !== null && pub !== undefined) {
2120
val.push(' public', sep, smart(pub))
22-
} else if (sys != null) {
21+
} else if (sys !== null && sys !== undefined) {
2322
val.push(' system')
2423
}
2524

26-
if (sys != null) {
25+
if (sys !== null && sys !== undefined) {
2726
val.push(sep, smart(sys))
2827
}
2928
}

lib/element.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var ccount = require('ccount')
1010
var all = require('./all')
1111
var constants = require('./constants')
1212

13-
module.exports = element
13+
module.exports = serializeElement
1414

1515
var space = ' '
1616
var quotationMark = '"'
@@ -20,9 +20,8 @@ var lessThan = '<'
2020
var greaterThan = '>'
2121
var slash = '/'
2222

23-
// Stringify an element `node`.
2423
// eslint-disable-next-line complexity
25-
function element(ctx, node, index, parent) {
24+
function serializeElement(ctx, node, index, parent) {
2625
var parentSchema = ctx.schema
2726
var name = node.tagName
2827
var value = ''
@@ -38,7 +37,7 @@ function element(ctx, node, index, parent) {
3837
ctx.schema = svg
3938
}
4039

41-
attrs = attributes(ctx, node.properties)
40+
attrs = serializeAttributes(ctx, node.properties)
4241

4342
if (ctx.schema.space === 'svg') {
4443
omit = false
@@ -95,8 +94,7 @@ function element(ctx, node, index, parent) {
9594
return value
9695
}
9796

98-
// Stringify all attributes.
99-
function attributes(ctx, props) {
97+
function serializeAttributes(ctx, props) {
10098
var values = []
10199
var key
102100
var value
@@ -108,11 +106,11 @@ function attributes(ctx, props) {
108106
for (key in props) {
109107
value = props[key]
110108

111-
if (value == null) {
109+
if (value === null || value === undefined) {
112110
continue
113111
}
114112

115-
result = attribute(ctx, key, value)
113+
result = serializeAttribute(ctx, key, value)
116114

117115
if (result) {
118116
values.push(result)
@@ -139,8 +137,7 @@ function attributes(ctx, props) {
139137
return values.join('')
140138
}
141139

142-
// Stringify one attribute.
143-
function attribute(ctx, key, value) {
140+
function serializeAttribute(ctx, key, value) {
144141
var schema = ctx.schema
145142
var info = find(schema, key)
146143
var name = info.attribute
@@ -155,14 +152,15 @@ function attribute(ctx, key, value) {
155152
}
156153

157154
if (
158-
value == null ||
155+
value === null ||
156+
value === undefined ||
159157
value === false ||
160158
(typeof value === 'number' && isNaN(value))
161159
) {
162160
return ''
163161
}
164162

165-
name = attributeName(ctx, name)
163+
name = serializeAttributeName(ctx, name)
166164

167165
if (value === true) {
168166
// There is currently only one boolean property in SVG: `[download]` on
@@ -184,20 +182,18 @@ function attribute(ctx, key, value) {
184182
return name
185183
}
186184

187-
return name + attributeValue(ctx, key, value, info)
185+
return name + serializeAttributeValue(ctx, key, value, info)
188186
}
189187

190-
// Stringify the attribute name.
191-
function attributeName(ctx, name) {
188+
function serializeAttributeName(ctx, name) {
192189
// Always encode without parse errors in non-HTML.
193190
var valid = ctx.schema.space === 'html' ? ctx.valid : 1
194191
var subset = constants.name[valid][ctx.safe]
195192

196193
return entities(name, xtend(ctx.entities, {subset: subset}))
197194
}
198195

199-
// Stringify the attribute value.
200-
function attributeValue(ctx, key, value, info) {
196+
function serializeAttributeValue(ctx, key, value, info) {
201197
var options = ctx.entities
202198
var quote = ctx.quote
203199
var alternative = ctx.alternative

lib/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ module.exports = toHtml
1111
var quotationMark = '"'
1212
var apostrophe = "'"
1313

14-
// Stringify the given hast node.
1514
function toHtml(node, options) {
1615
var settings = options || {}
1716
var quote = settings.quote || quotationMark

lib/omission/closing.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
var convert = require('unist-util-is/convert')
44
var element = require('hast-util-is-element')
5-
var whiteSpaceLeft = require('./util/white-space-left')
5+
var whiteSpaceStart = require('./util/white-space-start')
66
var after = require('./util/siblings').after
77
var omission = require('./omission')
88

@@ -87,7 +87,7 @@ module.exports = omission({
8787
// Macro for `</head>`, `</colgroup>`, and `</caption>`.
8888
function headOrColgroupOrCaption(node, index, parent) {
8989
var next = after(parent, index, true)
90-
return !next || (!isComment(next) && !whiteSpaceLeft(next))
90+
return !next || (!isComment(next) && !whiteSpaceStart(next))
9191
}
9292

9393
// Whether to omit `</html>`.

lib/omission/omission.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ function omission(handlers) {
1111
// Check if a given node can have a tag omitted.
1212
function omit(node, index, parent) {
1313
var name = node.tagName
14-
var fn = own.call(handlers, name) ? handlers[name] : false
1514

16-
return fn ? fn(node, index, parent) : false
15+
return own.call(handlers, name)
16+
? handlers[name](node, index, parent)
17+
: false
1718
}
1819
}

lib/omission/opening.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ var element = require('hast-util-is-element')
55
var before = require('./util/siblings').before
66
var first = require('./util/first')
77
var place = require('./util/place')
8-
var whiteSpaceLeft = require('./util/white-space-left')
8+
var whiteSpaceStart = require('./util/white-space-start')
99
var closing = require('./closing')
1010
var omission = require('./omission')
1111

12-
var own = {}.hasOwnProperty
13-
1412
var isComment = convert('comment')
1513

1614
var uniqueHeadMetadata = ['title', 'base']
@@ -36,7 +34,7 @@ function html(node) {
3634
function head(node) {
3735
var children = node.children
3836
var length = children.length
39-
var map = {}
37+
var seen = []
4038
var index = -1
4139
var child
4240
var name
@@ -46,23 +44,24 @@ function head(node) {
4644
name = child.tagName
4745

4846
if (element(child, uniqueHeadMetadata)) {
49-
if (own.call(map, name)) {
47+
if (seen.indexOf(name) !== -1) {
5048
return false
5149
}
5250

53-
map[name] = true
51+
seen.push(name)
5452
}
5553
}
5654

57-
return Boolean(length)
55+
return length !== 0
5856
}
5957

6058
// Whether to omit `<body>`.
6159
function body(node) {
6260
var head = first(node, true)
6361

6462
return (
65-
!head || (!isComment(head) && !whiteSpaceLeft(head) && !element(head, meta))
63+
!head ||
64+
(!isComment(head) && !whiteSpaceStart(head) && !element(head, meta))
6665
)
6766
}
6867

lib/omission/util/siblings.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@ function siblings(increment) {
1212
// Find applicable siblings in a direction.
1313
function sibling(parent, index, includeWhiteSpace) {
1414
var siblings = parent && parent.children
15-
var next
16-
17-
index += increment
18-
next = siblings && siblings[index]
15+
var offset = index + increment
16+
var next = siblings && siblings[offset]
1917

2018
if (!includeWhiteSpace) {
2119
while (next && whiteSpace(next)) {
22-
index += increment
23-
next = siblings[index]
20+
offset += increment
21+
next = siblings[offset]
2422
}
2523
}
2624

lib/omission/util/white-space-left.js renamed to lib/omission/util/white-space-start.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
var convert = require('unist-util-is/convert')
44
var whiteSpace = require('hast-util-whitespace')
55

6-
module.exports = whiteSpaceLeft
6+
module.exports = whiteSpaceStart
77

88
var isText = convert('text')
99

1010
// Check if `node` starts with white-space.
11-
function whiteSpaceLeft(node) {
11+
function whiteSpaceStart(node) {
1212
return isText(node) && whiteSpace(node.value.charAt(0))
1313
}

0 commit comments

Comments
 (0)