1
- 'use strict'
1
+ import { stringify as commas } from 'comma-separated-tokens'
2
+ import { stringify as spaces } from 'space-separated-tokens'
3
+ import { html , svg , find } from 'property-information'
4
+ import { position } from 'unist-util-position'
5
+ import { webNamespaces } from 'web-namespaces'
6
+ import { zwitch } from 'zwitch'
2
7
3
- module . exports = toXast
4
-
5
- var comma = require ( 'comma-separated-tokens' )
6
- var html = require ( 'property-information/html' )
7
- var svg = require ( 'property-information/svg' )
8
- var find = require ( 'property-information/find' )
9
- var space = require ( 'space-separated-tokens' )
10
- var position = require ( 'unist-util-position' )
11
- var namespaces = require ( 'web-namespaces' )
12
- var xtend = require ( 'xtend' )
13
- var zwitch = require ( 'zwitch' )
8
+ var own = { } . hasOwnProperty
14
9
15
10
var one = zwitch ( 'type' , {
16
- handlers : {
17
- root : root ,
18
- element : element ,
19
- text : text ,
20
- comment : comment ,
21
- doctype : doctype
22
- } ,
23
- invalid : invalid ,
24
- unknown : unknown
11
+ handlers : { root, element, text, comment, doctype} ,
12
+ invalid,
13
+ unknown
25
14
} )
26
15
27
16
function invalid ( value ) {
@@ -32,7 +21,7 @@ function unknown(value) {
32
21
throw new Error ( 'Cannot transform node of type `' + value . type + '`' )
33
22
}
34
23
35
- function toXast ( tree , options ) {
24
+ export function toXast ( tree , options ) {
36
25
var space = typeof options === 'string' ? options : ( options || { } ) . space
37
26
return one ( tree , { schema : space === 'svg' ? svg : html , ns : null } )
38
27
}
@@ -62,6 +51,7 @@ function doctype(node, config) {
62
51
)
63
52
}
64
53
54
+ // eslint-disable-next-line complexity
65
55
function element ( node , parentConfig ) {
66
56
var props = node . properties || { }
67
57
var schema = parentConfig . schema
@@ -71,31 +61,39 @@ function element(node, parentConfig) {
71
61
var key
72
62
var info
73
63
74
- if ( props . xmlns === namespaces . html ) {
64
+ if ( props . xmlns === webNamespaces . html ) {
75
65
schema = html
76
- } else if ( props . xmlns === namespaces . svg ) {
66
+ } else if ( props . xmlns === webNamespaces . svg ) {
77
67
schema = svg
78
68
} else if ( props . xmlns ) {
79
69
// We don’t support non-HTML, non-SVG namespaces, so stay in the same.
80
70
} else if ( schema === html && node . tagName === 'svg' ) {
81
71
schema = svg
82
72
}
83
73
84
- config = xtend ( parentConfig , { schema : schema , ns : namespaces [ schema . space ] } )
74
+ config = Object . assign ( { } , parentConfig , {
75
+ schema,
76
+ ns : webNamespaces [ schema . space ]
77
+ } )
85
78
86
79
if ( parentConfig . ns !== config . ns ) {
87
80
attributes . xmlns = config . ns
88
81
}
89
82
90
83
for ( key in props ) {
84
+ if ( ! own . call ( props , key ) ) {
85
+ continue
86
+ }
87
+
91
88
info = find ( schema , key )
92
89
value = props [ key ]
93
90
94
91
// Ignore nullish, false, and `NaN` values, and falsey known booleans.
95
92
if (
96
- value == null ||
93
+ value === undefined ||
94
+ value === null ||
97
95
value === false ||
98
- value !== value ||
96
+ ( typeof value === 'number' && Number . isNaN ( value ) ) ||
99
97
( ! value && info . boolean )
100
98
) {
101
99
continue
@@ -108,9 +106,7 @@ function element(node, parentConfig) {
108
106
// Accept `array`.
109
107
// Most props are space-separated.
110
108
else if ( typeof value === 'object' && 'length' in value ) {
111
- value = info . commaSeparated
112
- ? comma . stringify ( value )
113
- : space . stringify ( value )
109
+ value = info . commaSeparated ? commas ( value ) : spaces ( value )
114
110
}
115
111
// Cast everything else to string.
116
112
else if ( typeof value !== 'string' ) {
@@ -120,11 +116,7 @@ function element(node, parentConfig) {
120
116
attributes [ info . attribute ] = value
121
117
}
122
118
123
- return patch (
124
- node ,
125
- { type : 'element' , name : node . tagName , attributes : attributes } ,
126
- config
127
- )
119
+ return patch ( node , { type : 'element' , name : node . tagName , attributes} , config )
128
120
}
129
121
130
122
function patch ( origin , node , config ) {
0 commit comments