Skip to content

Commit fdd828d

Browse files
committed
Initial commit
0 parents  commit fdd828d

33 files changed

+1017
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
*.log
3+
.nyc_output/
4+
coverage/
5+
node_modules/
6+
yarn.lock

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
coverage/

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- lts/dubnium
4+
- node
5+
after_script: bash <(curl -s https://codecov.io/bash)

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
'use strict'
2+
module.exports = require('./lib')

lib/all.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict'
2+
3+
var one = require('./one')
4+
5+
module.exports = all
6+
7+
// Serialize all children of `parent`.
8+
function all(parent, config) {
9+
var children = parent && parent.children
10+
var length = children && children.length
11+
var index = -1
12+
var results = []
13+
14+
while (++index < length) {
15+
results[index] = one(children[index], config)
16+
}
17+
18+
return results.join('')
19+
}

lib/cdata.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict'
2+
3+
var escape = require('./util-escape')
4+
5+
module.exports = serializeCdataSection
6+
7+
var unsafe = /]]>/g
8+
var subset = ['>']
9+
10+
// Serialize a CDATA section.
11+
function serializeCdataSection(node) {
12+
return '<![CDATA[' + escape(node.value, subset, unsafe) + ']]>'
13+
}

lib/comment.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
var escape = require('./util-escape')
4+
5+
module.exports = serializeComment
6+
7+
// Serialize a comment.
8+
function serializeComment(node) {
9+
return '<!--' + escape(node.value, ['-']) + '-->'
10+
}

lib/doctype.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict'
2+
3+
var name = require('./name')
4+
var value = require('./value')
5+
6+
module.exports = serializeDocumentType
7+
8+
// Serialize a document type.
9+
function serializeDocumentType(node, config) {
10+
var nodeName = name(node.name)
11+
var pub = node.public
12+
var sys = node.system
13+
var val = '<!DOCTYPE'
14+
15+
if (nodeName !== '') {
16+
val += ' ' + nodeName
17+
}
18+
19+
if (pub !== null && pub !== undefined && pub !== '') {
20+
val += ' PUBLIC ' + value(pub, config)
21+
} else if (sys !== null && sys !== undefined && sys !== '') {
22+
val += ' SYSTEM'
23+
}
24+
25+
if (sys !== null && sys !== undefined && sys !== '') {
26+
val += ' ' + value(sys, config)
27+
}
28+
29+
return val + '>'
30+
}

lib/element.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict'
2+
3+
var all = require('./all')
4+
var name = require('./name')
5+
var value = require('./value')
6+
7+
module.exports = serializeElement
8+
9+
// Serialize an element.
10+
function serializeElement(node, config) {
11+
var nodeName = name(node.name)
12+
var content = all(node, config)
13+
var attributes = node.attributes || {}
14+
var close = content ? false : config.close
15+
var attrs = []
16+
var key
17+
var val
18+
19+
for (key in attributes) {
20+
val = attributes[key]
21+
22+
if (val !== null && val !== undefined) {
23+
attrs.push(name(key) + '=' + value(val, config))
24+
}
25+
}
26+
27+
return (
28+
'<' +
29+
nodeName +
30+
(attrs.length === 0 ? '' : ' ' + attrs.join(' ')) +
31+
(close ? (config.tight ? '' : ' ') + '/' : '') +
32+
'>' +
33+
content +
34+
(close ? '' : '</' + nodeName + '>')
35+
)
36+
}

lib/index.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict'
2+
3+
var one = require('./one')
4+
5+
module.exports = toXml
6+
7+
var quotationMark = '"'
8+
var apostrophe = "'"
9+
10+
// Serialize the given node.
11+
function toXml(node, options) {
12+
var opts = options || {}
13+
var quote = opts.quote || quotationMark
14+
var alternative = quote === quotationMark ? apostrophe : quotationMark
15+
var smart = opts.quoteSmart
16+
var value =
17+
node && typeof node === 'object' && 'length' in node
18+
? {type: 'root', children: node}
19+
: node
20+
21+
if (quote !== quotationMark && quote !== apostrophe) {
22+
throw new Error(
23+
'Invalid quote `' +
24+
quote +
25+
'`, expected `' +
26+
apostrophe +
27+
'` or `' +
28+
quotationMark +
29+
'`'
30+
)
31+
}
32+
33+
return one(value, {
34+
dangerous: opts.allowDangerousXml,
35+
close: opts.closeEmptyElements,
36+
tight: opts.tightClose,
37+
quote: quote,
38+
alternative: smart ? alternative : null
39+
})
40+
}

lib/instruction.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict'
2+
3+
var name = require('./name')
4+
var escape = require('./util-escape')
5+
6+
module.exports = serializeProcessingInstruction
7+
8+
var unsafe = /\?>/g
9+
var subset = ['>']
10+
11+
// Serialize a processing instruction.
12+
function serializeProcessingInstruction(node) {
13+
var nodeName = name(node.name) || 'x'
14+
var val = escape(node.value, subset, unsafe)
15+
return '<?' + nodeName + (val ? ' ' + val : '') + '?>'
16+
}

lib/name.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict'
2+
3+
var escape = require('./util-escape')
4+
5+
module.exports = serializeName
6+
7+
var subset = ['\t', '\n', ' ', '"', '&', "'", '/', '<', '=', '>']
8+
9+
function serializeName(value) {
10+
return escape(value, subset)
11+
}

lib/one.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict'
2+
3+
module.exports = one
4+
5+
var own = {}.hasOwnProperty
6+
7+
var handlers = {}
8+
9+
handlers.root = require('./all')
10+
handlers.element = require('./element')
11+
handlers.text = require('./text')
12+
handlers.comment = require('./comment')
13+
handlers.doctype = require('./doctype')
14+
handlers.instruction = require('./instruction')
15+
handlers.cdata = require('./cdata')
16+
handlers.raw = require('./raw')
17+
18+
// Serialize one node.
19+
function one(node, config) {
20+
var type = node && node.type
21+
22+
if (!type) {
23+
throw new Error('Expected node, not `' + node + '`')
24+
}
25+
26+
if (!own.call(handlers, type)) {
27+
throw new Error('Cannot compile unknown node `' + type + '`')
28+
}
29+
30+
return handlers[type](node, config)
31+
}

lib/raw.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
var text = require('./text')
4+
5+
module.exports = serializeRaw
6+
7+
// Serialize raw.
8+
function serializeRaw(node, config) {
9+
return config.dangerous ? node.value : text(node, config)
10+
}

lib/text.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict'
2+
3+
var escape = require('./util-escape')
4+
5+
module.exports = serializeText
6+
7+
var subset = ['&', '<']
8+
9+
// Serialize a text.
10+
function serializeText(node) {
11+
return escape(node.value, subset)
12+
}

lib/util-escape.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict'
2+
3+
var entities = require('stringify-entities')
4+
5+
module.exports = escape
6+
7+
var noncharacter = /[\u0000-\u0008\u000B\u000C\u000E-\u001F]/g
8+
9+
function escape(value, subset, unsafe) {
10+
var val = clean(value)
11+
12+
return unsafe ? val.replace(unsafe, encode) : encode(val)
13+
14+
function encode($0) {
15+
return entities($0, {subset: subset})
16+
}
17+
}
18+
19+
function clean(value) {
20+
return String(value || '').replace(noncharacter, '')
21+
}

lib/value.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict'
2+
3+
var ccount = require('ccount')
4+
var escape = require('./util-escape')
5+
6+
module.exports = serializeValue
7+
8+
function serializeValue(value, config) {
9+
var primary = config.quote
10+
var secondary = config.alternative
11+
var val = String(value)
12+
var quote =
13+
secondary && ccount(val, primary) > ccount(val, secondary)
14+
? secondary
15+
: primary
16+
17+
return quote + escape(val, ['<', '&', quote]) + quote
18+
}

license

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(The MIT License)
2+
3+
Copyright (c) 2020 Titus Wormer <tituswormer@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
'Software'), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0 commit comments

Comments
 (0)