1
1
'use strict'
2
2
3
- var html = require ( 'property-information/html' )
4
- var svg = require ( 'property-information/svg' )
5
- var find = require ( 'property-information/find' )
6
- var ns = require ( 'web-namespaces' )
7
3
var s = require ( 'hastscript/svg' )
8
4
var h = require ( 'hastscript' )
5
+ var find = require ( 'property-information/find' )
6
+ var html = require ( 'property-information/html' )
7
+ var svg = require ( 'property-information/svg' )
9
8
var vfileLocation = require ( 'vfile-location' )
9
+ var ns = require ( 'web-namespaces' )
10
10
11
11
module . exports = wrapper
12
12
@@ -45,7 +45,7 @@ function transform(ast, config) {
45
45
var schema = config . schema
46
46
var fn = own . call ( map , ast . nodeName ) ? map [ ast . nodeName ] : element
47
47
var children
48
- var node
48
+ var result
49
49
var position
50
50
51
51
if ( fn === element ) {
@@ -56,20 +56,20 @@ function transform(ast, config) {
56
56
children = nodes ( ast . childNodes , config )
57
57
}
58
58
59
- node = fn ( ast , children , config )
59
+ result = fn ( ast , children , config )
60
60
61
61
if ( ast . sourceCodeLocation && config . file ) {
62
- position = location ( node , ast . sourceCodeLocation , config )
62
+ position = location ( result , ast . sourceCodeLocation , config )
63
63
64
64
if ( position ) {
65
65
config . location = true
66
- node . position = position
66
+ result . position = position
67
67
}
68
68
}
69
69
70
70
config . schema = schema
71
71
72
- return node
72
+ return result
73
73
}
74
74
75
75
// Transform children.
@@ -87,7 +87,7 @@ function nodes(children, config) {
87
87
// Transform a document.
88
88
// Stores `ast.quirksMode` in `node.data.quirksMode`.
89
89
function root ( ast , children , config ) {
90
- var node = {
90
+ var result = {
91
91
type : 'root' ,
92
92
children : children ,
93
93
data : { quirksMode : ast . mode === 'quirks' || ast . mode === 'limited-quirks' }
@@ -98,13 +98,13 @@ function root(ast, children, config) {
98
98
if ( config . file && config . location ) {
99
99
doc = String ( config . file )
100
100
location = vfileLocation ( doc )
101
- node . position = {
101
+ result . position = {
102
102
start : location . toPoint ( 0 ) ,
103
103
end : location . toPoint ( doc . length )
104
104
}
105
105
}
106
106
107
- return node
107
+ return result
108
108
}
109
109
110
110
// Transform a doctype.
@@ -130,73 +130,58 @@ function comment(ast) {
130
130
// Transform an element.
131
131
function element ( ast , children , config ) {
132
132
var fn = config . schema . space === 'svg' ? s : h
133
- var name = ast . tagName
134
- var attributes = ast . attrs
135
- var length = attributes . length
136
133
var props = { }
137
134
var index = - 1
135
+ var result
138
136
var attribute
139
- var prop
140
- var node
141
137
var pos
142
138
var start
143
139
var end
144
140
145
- while ( ++ index < length ) {
146
- attribute = attributes [ index ]
147
- prop = ( attribute . prefix ? attribute . prefix + ':' : '' ) + attribute . name
148
- props [ prop ] = attribute . value
141
+ while ( ++ index < ast . attrs . length ) {
142
+ attribute = ast . attrs [ index ]
143
+ props [ ( attribute . prefix ? attribute . prefix + ':' : '' ) + attribute . name ] =
144
+ attribute . value
149
145
}
150
146
151
- node = fn ( name , props , children )
147
+ result = fn ( ast . tagName , props , children )
152
148
153
- if ( name === 'template' && 'content' in ast ) {
149
+ if ( result . tagName === 'template' && 'content' in ast ) {
154
150
pos = ast . sourceCodeLocation
155
151
start = pos && pos . startTag && position ( pos . startTag ) . end
156
152
end = pos && pos . endTag && position ( pos . endTag ) . start
157
153
158
- node . content = transform ( ast . content , config )
154
+ result . content = transform ( ast . content , config )
159
155
160
156
if ( ( start || end ) && config . file ) {
161
- node . content . position = { start : start , end : end }
157
+ result . content . position = { start : start , end : end }
162
158
}
163
159
}
164
160
165
- return node
161
+ return result
166
162
}
167
163
168
164
// Create clean positional information.
169
165
function location ( node , location , config ) {
170
- var schema = config . schema
171
- var verbose = config . verbose
172
- var pos = position ( location )
173
- var reference
174
- var attributes
175
- var attribute
166
+ var result = position ( location )
167
+ var tail
168
+ var key
176
169
var props
177
- var prop
178
170
179
171
if ( node . type === 'element' ) {
180
- reference = node . children [ node . children . length - 1 ]
172
+ tail = node . children [ node . children . length - 1 ]
181
173
182
174
// Bug for unclosed with children.
183
175
// See: <https://github.com/inikulin/parse5/issues/109>.
184
- if (
185
- ! location . endTag &&
186
- reference &&
187
- reference . position &&
188
- reference . position . end
189
- ) {
190
- pos . end = Object . assign ( { } , reference . position . end )
176
+ if ( ! location . endTag && tail && tail . position && tail . position . end ) {
177
+ result . end = Object . assign ( { } , tail . position . end )
191
178
}
192
179
193
- if ( verbose ) {
194
- attributes = location . attrs
180
+ if ( config . verbose ) {
195
181
props = { }
196
182
197
- for ( attribute in attributes ) {
198
- prop = find ( schema , attribute ) . property
199
- props [ prop ] = position ( attributes [ attribute ] )
183
+ for ( key in location . attrs ) {
184
+ props [ find ( config . schema , key ) . property ] = position ( location . attrs [ key ] )
200
185
}
201
186
202
187
node . data = {
@@ -209,7 +194,7 @@ function location(node, location, config) {
209
194
}
210
195
}
211
196
212
- return pos
197
+ return result
213
198
}
214
199
215
200
function position ( loc ) {
0 commit comments