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'
27
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
149
1510var 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
2514} )
2615
2716function invalid ( value ) {
@@ -32,7 +21,7 @@ function unknown(value) {
3221 throw new Error ( 'Cannot transform node of type `' + value . type + '`' )
3322}
3423
35- function toXast ( tree , options ) {
24+ export function toXast ( tree , options ) {
3625 var space = typeof options === 'string' ? options : ( options || { } ) . space
3726 return one ( tree , { schema : space === 'svg' ? svg : html , ns : null } )
3827}
@@ -62,6 +51,7 @@ function doctype(node, config) {
6251 )
6352}
6453
54+ // eslint-disable-next-line complexity
6555function element ( node , parentConfig ) {
6656 var props = node . properties || { }
6757 var schema = parentConfig . schema
@@ -71,31 +61,39 @@ function element(node, parentConfig) {
7161 var key
7262 var info
7363
74- if ( props . xmlns === namespaces . html ) {
64+ if ( props . xmlns === webNamespaces . html ) {
7565 schema = html
76- } else if ( props . xmlns === namespaces . svg ) {
66+ } else if ( props . xmlns === webNamespaces . svg ) {
7767 schema = svg
7868 } else if ( props . xmlns ) {
7969 // We don’t support non-HTML, non-SVG namespaces, so stay in the same.
8070 } else if ( schema === html && node . tagName === 'svg' ) {
8171 schema = svg
8272 }
8373
84- config = xtend ( parentConfig , { schema : schema , ns : namespaces [ schema . space ] } )
74+ config = Object . assign ( { } , parentConfig , {
75+ schema,
76+ ns : webNamespaces [ schema . space ]
77+ } )
8578
8679 if ( parentConfig . ns !== config . ns ) {
8780 attributes . xmlns = config . ns
8881 }
8982
9083 for ( key in props ) {
84+ if ( ! own . call ( props , key ) ) {
85+ continue
86+ }
87+
9188 info = find ( schema , key )
9289 value = props [ key ]
9390
9491 // Ignore nullish, false, and `NaN` values, and falsey known booleans.
9592 if (
96- value == null ||
93+ value === undefined ||
94+ value === null ||
9795 value === false ||
98- value !== value ||
96+ ( typeof value === 'number' && Number . isNaN ( value ) ) ||
9997 ( ! value && info . boolean )
10098 ) {
10199 continue
@@ -108,9 +106,7 @@ function element(node, parentConfig) {
108106 // Accept `array`.
109107 // Most props are space-separated.
110108 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 )
114110 }
115111 // Cast everything else to string.
116112 else if ( typeof value !== 'string' ) {
@@ -120,11 +116,7 @@ function element(node, parentConfig) {
120116 attributes [ info . attribute ] = value
121117 }
122118
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 )
128120}
129121
130122function patch ( origin , node , config ) {
0 commit comments