@@ -8,28 +8,32 @@ var toH = require('hast-to-hyperscript')
88var ns = require ( 'web-namespaces' )
99var zwitch = require ( 'zwitch' )
1010
11- module . exports = transform
12-
13- var ignoredSpaces = [ 'svg ', 'html' ]
14-
15- var one = zwitch ( 'type' )
16-
17- one . handlers . root = root
18- one . handlers . element = element
19- one . handlers . text = text
20- one . handlers . comment = comment
21- one . handlers . doctype = doctype
11+ module . exports = toParse5
12+
13+ var one = zwitch ( 'type ', {
14+ handlers : {
15+ root : root ,
16+ element : element ,
17+ text : text ,
18+ comment : comment ,
19+ doctype : doctype
20+ }
21+ } )
2222
2323// Transform a tree from hast to Parse5’s AST.
24- function transform ( tree , space ) {
24+ function toParse5 ( tree , space ) {
2525 return one ( tree , space === 'svg' ? svg : html )
2626}
2727
2828function root ( node , schema ) {
29- var data = node . data || { }
30- var mode = data . quirksMode ? 'quirks' : 'no-quirks'
31-
32- return patch ( node , { nodeName : '#document' , mode : mode } , schema )
29+ return patch (
30+ node ,
31+ {
32+ nodeName : '#document' ,
33+ mode : ( node . data || { } ) . quirksMode ? 'quirks' : 'no-quirks'
34+ } ,
35+ schema
36+ )
3337}
3438
3539function fragment ( node , schema ) {
@@ -58,38 +62,33 @@ function comment(node, schema) {
5862}
5963
6064function element ( node , schema ) {
61- var space = schema . space
62- var shallow = xtend ( node , { children : [ ] } )
63-
64- return toH ( h , shallow , { space : space } )
65+ return toH ( h , xtend ( node , { children : [ ] } ) , { space : schema . space } )
6566
6667 function h ( name , attrs ) {
6768 var values = [ ]
68- var p5
69- var attr
69+ var info
7070 var value
7171 var key
72- var info
73- var pos
72+ var index
73+ var p5
7474
7575 for ( key in attrs ) {
7676 info = find ( schema , key )
77- attr = attrs [ key ]
7877
79- if ( attr === false || ( info . boolean && ! attr ) ) {
78+ if ( attrs [ key ] === false || ( info . boolean && ! attrs [ key ] ) ) {
8079 continue
8180 }
8281
83- value = { name : key , value : attr === true ? '' : String ( attr ) }
82+ value = { name : key , value : attrs [ key ] === true ? '' : String ( attrs [ key ] ) }
8483
85- if ( info . space && ignoredSpaces . indexOf ( info . space ) === - 1 ) {
86- pos = key . indexOf ( ':' )
84+ if ( info . space && info . space !== 'html' && info . space !== 'svg' ) {
85+ index = key . indexOf ( ':' )
8786
88- if ( pos === - 1 ) {
87+ if ( index < 0 ) {
8988 value . prefix = ''
9089 } else {
91- value . name = key . slice ( pos + 1 )
92- value . prefix = key . slice ( 0 , pos )
90+ value . name = key . slice ( index + 1 )
91+ value . prefix = key . slice ( 0 , index )
9392 }
9493
9594 value . namespace = ns [ info . space ]
@@ -100,9 +99,7 @@ function element(node, schema) {
10099
101100 p5 = patch ( node , { nodeName : name , tagName : name , attrs : values } , schema )
102101
103- if ( name === 'template' ) {
104- p5 . content = fragment ( shallow . content , schema )
105- }
102+ if ( name === 'template' ) p5 . content = fragment ( node . content , schema )
106103
107104 return p5
108105 }
@@ -112,28 +109,25 @@ function element(node, schema) {
112109function patch ( node , p5 , parentSchema ) {
113110 var schema = parentSchema
114111 var position = node . position
115- var children = node . children
116112 var childNodes = [ ]
117- var length = children ? children . length : 0
118113 var index = - 1
119114 var child
120115
121116 if ( node . type === 'element' ) {
122- if ( schema . space === 'html' && node . tagName === 'svg' ) {
123- schema = svg
124- }
125-
117+ if ( schema . space === 'html' && node . tagName === 'svg' ) schema = svg
126118 p5 . namespaceURI = ns [ schema . space ]
127119 }
128120
129- while ( ++ index < length ) {
130- child = one ( children [ index ] , schema )
131- child . parentNode = p5
132- childNodes [ index ] = child
133- }
134-
135121 if ( node . type === 'element' || node . type === 'root' ) {
136122 p5 . childNodes = childNodes
123+
124+ if ( node . children ) {
125+ while ( ++ index < node . children . length ) {
126+ child = one ( node . children [ index ] , schema )
127+ child . parentNode = p5
128+ childNodes [ index ] = child
129+ }
130+ }
137131 }
138132
139133 if ( position && position . start && position . end ) {
0 commit comments