1
1
'use strict'
2
2
3
- var information = require ( 'property-information' )
4
- var camelcase = require ( 'camelcase' )
3
+ var html = require ( 'property-information/html' )
4
+ var svg = require ( 'property-information/svg' )
5
+ var find = require ( 'property-information/find' )
5
6
var h = require ( 'hastscript' )
6
7
var xtend = require ( 'xtend' )
7
8
var count = require ( 'ccount' )
@@ -32,6 +33,7 @@ function wrapper(ast, options) {
32
33
}
33
34
34
35
return transform ( ast , {
36
+ schema : settings . space === 'svg' ? svg : html ,
35
37
file : file ,
36
38
verbose : settings . verbose ,
37
39
location : false
@@ -40,26 +42,33 @@ function wrapper(ast, options) {
40
42
41
43
/* Transform a node. */
42
44
function transform ( ast , config ) {
45
+ var schema = config . schema
43
46
var fn = own . call ( map , ast . nodeName ) ? map [ ast . nodeName ] : element
44
47
var children
45
48
var node
46
49
var pos
47
50
51
+ if ( fn === element && schema . space === 'html' && ast . nodeName === 'svg' ) {
52
+ config . schema = svg
53
+ }
54
+
48
55
if ( ast . childNodes ) {
49
56
children = nodes ( ast . childNodes , config )
50
57
}
51
58
52
59
node = fn ( ast , children , config )
53
60
54
61
if ( ast . sourceCodeLocation && config . file ) {
55
- pos = location ( node , ast . sourceCodeLocation , config . verbose )
62
+ pos = location ( node , ast . sourceCodeLocation , config )
56
63
57
64
if ( pos ) {
58
65
config . location = true
59
66
node . position = pos
60
67
}
61
68
}
62
69
70
+ config . schema = schema
71
+
63
72
return node
64
73
}
65
74
@@ -122,24 +131,27 @@ function comment(ast) {
122
131
123
132
/* Transform an element. */
124
133
function element ( ast , children , config ) {
134
+ var name = ast . tagName
135
+ var attributes = ast . attrs
136
+ var length = attributes . length
125
137
var props = { }
126
- var values = ast . attrs
127
- var length = values . length
128
138
var index = - 1
129
- var attr
139
+ var attribute
140
+ var prop
130
141
var node
131
142
var pos
132
143
var start
133
144
var end
134
145
135
146
while ( ++ index < length ) {
136
- attr = values [ index ]
137
- props [ ( attr . prefix ? attr . prefix + ':' : '' ) + attr . name ] = attr . value
147
+ attribute = attributes [ index ]
148
+ prop = ( attribute . prefix ? attribute . prefix + ':' : '' ) + attribute . name
149
+ props [ prop ] = attribute . value
138
150
}
139
151
140
- node = h ( ast . tagName , props , children )
152
+ node = h ( name , props , children )
141
153
142
- if ( ast . nodeName === 'template' && 'content' in ast ) {
154
+ if ( name === 'template' && 'content' in ast ) {
143
155
pos = ast . sourceCodeLocation
144
156
start = pos && pos . startTag && position ( pos . startTag ) . end
145
157
end = pos && pos . endTag && position ( pos . endTag ) . start
@@ -155,13 +167,15 @@ function element(ast, children, config) {
155
167
}
156
168
157
169
/* Create clean positional information. */
158
- function location ( node , location , verbose ) {
170
+ function location ( node , location , config ) {
171
+ var schema = config . schema
172
+ var verbose = config . verbose
159
173
var pos = position ( location )
160
174
var reference
161
- var values
175
+ var attributes
176
+ var attribute
162
177
var props
163
178
var prop
164
- var name
165
179
166
180
if ( node . type === 'element' ) {
167
181
reference = node . children [ node . children . length - 1 ]
@@ -177,12 +191,12 @@ function location(node, location, verbose) {
177
191
}
178
192
179
193
if ( verbose ) {
180
- values = location . attrs
194
+ attributes = location . attrs
181
195
props = { }
182
196
183
- for ( prop in values ) {
184
- name = ( information ( prop ) || { } ) . propertyName || camelcase ( prop )
185
- props [ name ] = position ( values [ prop ] )
197
+ for ( attribute in attributes ) {
198
+ prop = find ( schema , attribute ) . property
199
+ props [ prop ] = position ( attributes [ attribute ] )
186
200
}
187
201
188
202
node . data = {
0 commit comments