@@ -8,28 +8,32 @@ var toH = require('hast-to-hyperscript')
8
8
var ns = require ( 'web-namespaces' )
9
9
var zwitch = require ( 'zwitch' )
10
10
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
+ } )
22
22
23
23
// Transform a tree from hast to Parse5’s AST.
24
- function transform ( tree , space ) {
24
+ function toParse5 ( tree , space ) {
25
25
return one ( tree , space === 'svg' ? svg : html )
26
26
}
27
27
28
28
function 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
+ )
33
37
}
34
38
35
39
function fragment ( node , schema ) {
@@ -58,38 +62,33 @@ function comment(node, schema) {
58
62
}
59
63
60
64
function 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 } )
65
66
66
67
function h ( name , attrs ) {
67
68
var values = [ ]
68
- var p5
69
- var attr
69
+ var info
70
70
var value
71
71
var key
72
- var info
73
- var pos
72
+ var index
73
+ var p5
74
74
75
75
for ( key in attrs ) {
76
76
info = find ( schema , key )
77
- attr = attrs [ key ]
78
77
79
- if ( attr === false || ( info . boolean && ! attr ) ) {
78
+ if ( attrs [ key ] === false || ( info . boolean && ! attrs [ key ] ) ) {
80
79
continue
81
80
}
82
81
83
- value = { name : key , value : attr === true ? '' : String ( attr ) }
82
+ value = { name : key , value : attrs [ key ] === true ? '' : String ( attrs [ key ] ) }
84
83
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 ( ':' )
87
86
88
- if ( pos === - 1 ) {
87
+ if ( index < 0 ) {
89
88
value . prefix = ''
90
89
} 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 )
93
92
}
94
93
95
94
value . namespace = ns [ info . space ]
@@ -100,9 +99,7 @@ function element(node, schema) {
100
99
101
100
p5 = patch ( node , { nodeName : name , tagName : name , attrs : values } , schema )
102
101
103
- if ( name === 'template' ) {
104
- p5 . content = fragment ( shallow . content , schema )
105
- }
102
+ if ( name === 'template' ) p5 . content = fragment ( node . content , schema )
106
103
107
104
return p5
108
105
}
@@ -112,28 +109,25 @@ function element(node, schema) {
112
109
function patch ( node , p5 , parentSchema ) {
113
110
var schema = parentSchema
114
111
var position = node . position
115
- var children = node . children
116
112
var childNodes = [ ]
117
- var length = children ? children . length : 0
118
113
var index = - 1
119
114
var child
120
115
121
116
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
126
118
p5 . namespaceURI = ns [ schema . space ]
127
119
}
128
120
129
- while ( ++ index < length ) {
130
- child = one ( children [ index ] , schema )
131
- child . parentNode = p5
132
- childNodes [ index ] = child
133
- }
134
-
135
121
if ( node . type === 'element' || node . type === 'root' ) {
136
122
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
+ }
137
131
}
138
132
139
133
if ( position && position . start && position . end ) {
0 commit comments