Skip to content

Commit 7882a8c

Browse files
committed
Use ESM
1 parent 0346398 commit 7882a8c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+791
-871
lines changed

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
.DS_Store
22
*.log
3-
.nyc_output/
43
coverage/
54
node_modules/
6-
hast-util-to-html.js
7-
hast-util-to-html.min.js
85
yarn.lock

.prettierignore

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
1-
.nyc_output/
21
coverage/
3-
hast-util-to-html.js
4-
hast-util-to-html.min.js
5-
*.json
62
*.md

index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
'use strict'
2-
module.exports = require('./lib')
1+
export {toHtml} from './lib/index.js'

lib/all.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
'use strict'
2-
3-
var one = require('./one')
4-
5-
module.exports = all
1+
import {one} from './one.js'
62

73
// Serialize all children of `parent`.
8-
function all(ctx, parent) {
4+
export function all(ctx, parent) {
95
var results = []
106
var children = (parent && parent.children) || []
117
var index = -1

lib/comment.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
'use strict'
1+
import {stringifyEntities} from 'stringify-entities'
22

3-
var xtend = require('xtend')
4-
var entities = require('stringify-entities')
5-
6-
module.exports = serializeComment
7-
8-
function serializeComment(ctx, node) {
3+
export function comment(ctx, node) {
94
// See: <https://html.spec.whatwg.org/multipage/syntax.html#comments>
105
return ctx.bogusComments
11-
? '<?' + entities(node.value, xtend(ctx.entities, {subset: ['>']})) + '>'
6+
? '<?' +
7+
stringifyEntities(
8+
node.value,
9+
Object.assign({}, ctx.entities, {subset: ['>']})
10+
) +
11+
'>'
1212
: '<!--' + node.value.replace(/^>|^->|<!--|-->|--!>|<!-$/g, encode) + '-->'
1313

1414
function encode($0) {
15-
return entities($0, xtend(ctx.entities, {subset: ['<', '>']}))
15+
return stringifyEntities(
16+
$0,
17+
Object.assign({}, ctx.entities, {subset: ['<', '>']})
18+
)
1619
}
1720
}

lib/constants.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict'
2-
31
// Maps of subsets.
42
// Each value is a matrix of tuples.
53
// The first value causes parse errors, the second is valid.
64
// Of both values, the first value is unsafe, and the second is safe.
7-
module.exports = {
5+
export const constants = {
86
// See: <https://html.spec.whatwg.org/#attribute-name-state>.
97
name: [
108
['\t\n\f\r &/=>'.split(''), '\t\n\f\r "&\'/=>`'.split('')],

lib/doctype.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
1-
'use strict'
1+
import {ccount} from 'ccount'
2+
import {stringifyEntities} from 'stringify-entities'
23

3-
var xtend = require('xtend')
4-
var ccount = require('ccount')
5-
var entities = require('stringify-entities')
6-
7-
module.exports = serializeDoctype
8-
9-
function serializeDoctype(ctx, node) {
4+
export function doctype(ctx, node) {
105
var sep = ctx.tightDoctype ? '' : ' '
116
var parts = ['<!' + (ctx.upperDoctype ? 'DOCTYPE' : 'doctype')]
127

138
if (node.name) {
149
parts.push(sep, node.name)
1510

16-
if (node.public != null) {
11+
if (node.public !== undefined && node.public !== null) {
1712
parts.push(' public', sep, quote(ctx, node.public))
18-
} else if (node.system != null) {
13+
} else if (node.system !== undefined && node.system !== null) {
1914
parts.push(' system')
2015
}
2116

22-
if (node.system != null) {
17+
if (node.system !== undefined && node.system !== null) {
2318
parts.push(sep, quote(ctx, node.system))
2419
}
2520
}
@@ -36,7 +31,10 @@ function quote(ctx, value) {
3631

3732
return (
3833
quote +
39-
entities(string, xtend(ctx.entities, {subset: ['<', '&', quote]})) +
34+
stringifyEntities(
35+
string,
36+
Object.assign({}, ctx.entities, {subset: ['<', '&', quote]})
37+
) +
4038
quote
4139
)
4240
}

lib/element.js

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
1-
'use strict'
2-
3-
var xtend = require('xtend')
4-
var svg = require('property-information/svg')
5-
var find = require('property-information/find')
6-
var spaces = require('space-separated-tokens')
7-
var commas = require('comma-separated-tokens')
8-
var entities = require('stringify-entities')
9-
var ccount = require('ccount')
10-
var all = require('./all')
11-
var constants = require('./constants')
12-
13-
module.exports = serializeElement
14-
15-
function serializeElement(ctx, node, index, parent) {
1+
import {svg, find} from 'property-information'
2+
import {stringify as spaces} from 'space-separated-tokens'
3+
import {stringify as commas} from 'comma-separated-tokens'
4+
import {stringifyEntities} from 'stringify-entities'
5+
import {ccount} from 'ccount'
6+
import {all} from './all.js'
7+
import {constants} from './constants.js'
8+
9+
// eslint-disable-next-line complexity
10+
export function element(ctx, node, index, parent) {
1611
var schema = ctx.schema
1712
var omit = schema.space === 'svg' ? false : ctx.omit
1813
var parts = []
1914
var selfClosing =
2015
schema.space === 'svg'
2116
? ctx.closeEmpty
22-
: ctx.voids.indexOf(node.tagName.toLowerCase()) > -1
17+
: ctx.voids.includes(node.tagName.toLowerCase())
2318
var attrs
2419
var content
2520
var last
@@ -79,7 +74,7 @@ function serializeAttributes(ctx, props) {
7974
var last
8075

8176
for (key in props) {
82-
if (props[key] != null) {
77+
if (props[key] !== undefined && props[key] !== null) {
8378
value = serializeAttribute(ctx, key, props[key])
8479
if (value) values.push(value)
8580
}
@@ -97,6 +92,7 @@ function serializeAttributes(ctx, props) {
9792
return values.join('')
9893
}
9994

95+
// eslint-disable-next-line complexity
10096
function serializeAttribute(ctx, key, value) {
10197
var info = find(ctx.schema, key)
10298
var quote = ctx.quote
@@ -113,16 +109,17 @@ function serializeAttribute(ctx, key, value) {
113109
}
114110

115111
if (
116-
value == null ||
112+
value === undefined ||
113+
value === null ||
117114
value === false ||
118-
(typeof value === 'number' && value !== value)
115+
(typeof value === 'number' && Number.isNaN(value))
119116
) {
120117
return ''
121118
}
122119

123-
name = entities(
120+
name = stringifyEntities(
124121
info.attribute,
125-
xtend(ctx.entities, {
122+
Object.assign({}, ctx.entities, {
126123
// Always encode without parse errors in non-HTML.
127124
subset:
128125
constants.name[ctx.schema.space === 'html' ? ctx.valid : 1][ctx.safe]
@@ -152,7 +149,7 @@ function serializeAttribute(ctx, key, value) {
152149
typeof value === 'object' && 'length' in value
153150
? // `spaces` doesn’t accept a second argument, but it’s given here just to
154151
// keep the code cleaner.
155-
(info.commaSeparated ? commas.stringify : spaces.stringify)(value, {
152+
(info.commaSeparated ? commas : spaces)(value, {
156153
padLeft: !ctx.tightLists
157154
})
158155
: String(value)
@@ -161,9 +158,9 @@ function serializeAttribute(ctx, key, value) {
161158

162159
// Check unquoted value.
163160
if (ctx.unquoted) {
164-
result = entities(
161+
result = stringifyEntities(
165162
value,
166-
xtend(ctx.entities, {
163+
Object.assign({}, ctx.entities, {
167164
subset: constants.unquoted[ctx.valid][ctx.safe],
168165
attribute: true
169166
})
@@ -180,9 +177,9 @@ function serializeAttribute(ctx, key, value) {
180177

181178
result =
182179
quote +
183-
entities(
180+
stringifyEntities(
184181
value,
185-
xtend(ctx.entities, {
182+
Object.assign({}, ctx.entities, {
186183
// Always encode without parse errors in non-HTML.
187184
subset: (quote === "'" ? constants.single : constants.double)[
188185
ctx.schema.space === 'html' ? ctx.valid : 1

lib/index.js

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1-
'use strict'
1+
import {html, svg} from 'property-information'
2+
import {htmlVoidElements} from 'html-void-elements'
3+
import {omission} from './omission/index.js'
4+
import {one} from './one.js'
25

3-
var html = require('property-information/html')
4-
var svg = require('property-information/svg')
5-
var voids = require('html-void-elements')
6-
var omission = require('./omission')
7-
var one = require('./one')
8-
9-
module.exports = toHtml
10-
11-
var deprecationWarningIssued
12-
13-
function toHtml(node, options) {
6+
export function toHtml(node, options) {
147
var settings = options || {}
158
var quote = settings.quote || '"'
169
var alternative = quote === '"' ? "'" : '"'
@@ -19,21 +12,14 @@ function toHtml(node, options) {
1912
throw new Error('Invalid quote `' + quote + '`, expected `\'` or `"`')
2013
}
2114

22-
if ('allowDangerousHTML' in settings && !deprecationWarningIssued) {
23-
deprecationWarningIssued = true
24-
console.warn(
25-
'Deprecation warning: `allowDangerousHTML` is a nonstandard option, use `allowDangerousHtml` instead'
26-
)
27-
}
28-
2915
return one(
3016
{
3117
valid: settings.allowParseErrors ? 0 : 1,
3218
safe: settings.allowDangerousCharacters ? 0 : 1,
3319
schema: settings.space === 'svg' ? svg : html,
3420
omit: settings.omitOptionalTags && omission,
35-
quote: quote,
36-
alternative: alternative,
21+
quote,
22+
alternative,
3723
smart: settings.quoteSmart,
3824
unquoted: settings.preferUnquoted,
3925
tight: settings.tightAttributes,
@@ -44,7 +30,7 @@ function toHtml(node, options) {
4430
tightClose: settings.tightSelfClosing,
4531
collapseEmpty: settings.collapseEmptyAttributes,
4632
dangerous: settings.allowDangerousHtml || settings.allowDangerousHTML,
47-
voids: settings.voids || voids.concat(),
33+
voids: settings.voids || htmlVoidElements.concat(),
4834
entities: settings.entities || {},
4935
close: settings.closeSelfClosing,
5036
closeEmpty: settings.closeEmptyElements

0 commit comments

Comments
 (0)