1
- 'use strict' ;
1
+ 'use strict'
2
2
3
- var information = require ( 'property-information' ) ;
4
- var camelcase = require ( 'camelcase' ) ;
5
- var h = require ( 'hastscript' ) ;
6
- var xtend = require ( 'xtend' ) ;
7
- var count = require ( 'ccount' ) ;
3
+ var information = require ( 'property-information' )
4
+ var camelcase = require ( 'camelcase' )
5
+ var h = require ( 'hastscript' )
6
+ var xtend = require ( 'xtend' )
7
+ var count = require ( 'ccount' )
8
8
9
- module . exports = wrapper ;
9
+ module . exports = wrapper
10
10
11
- var own = { } . hasOwnProperty ;
11
+ var own = { } . hasOwnProperty
12
12
13
13
/* Handlers. */
14
14
var map = {
@@ -17,83 +17,87 @@ var map = {
17
17
'#text' : text ,
18
18
'#comment' : comment ,
19
19
'#documentType' : doctype
20
- } ;
20
+ }
21
21
22
22
/* Wrapper to normalise options. */
23
23
function wrapper ( ast , options ) {
24
- var settings = options || { } ;
25
- var file ;
24
+ var settings = options || { }
25
+ var file
26
26
27
27
if ( settings . messages ) {
28
- file = settings ;
29
- settings = { } ;
28
+ file = settings
29
+ settings = { }
30
30
} else {
31
- file = settings . file ;
31
+ file = settings . file
32
32
}
33
33
34
34
return transform ( ast , {
35
35
file : file ,
36
36
verbose : settings . verbose ,
37
37
location : false
38
- } ) ;
38
+ } )
39
39
}
40
40
41
41
/* Transform a node. */
42
42
function transform ( ast , config ) {
43
- var fn = own . call ( map , ast . nodeName ) ? map [ ast . nodeName ] : element ;
44
- var children ;
45
- var node ;
46
- var pos ;
43
+ var fn = own . call ( map , ast . nodeName ) ? map [ ast . nodeName ] : element
44
+ var children
45
+ var node
46
+ var pos
47
47
48
48
if ( ast . childNodes ) {
49
- children = nodes ( ast . childNodes , config ) ;
49
+ children = nodes ( ast . childNodes , config )
50
50
}
51
51
52
- node = fn ( ast , children , config ) ;
52
+ node = fn ( ast , children , config )
53
53
54
54
if ( ast . sourceCodeLocation && config . file ) {
55
- pos = location ( node , ast . sourceCodeLocation , config . verbose ) ;
55
+ pos = location ( node , ast . sourceCodeLocation , config . verbose )
56
56
57
57
if ( pos ) {
58
- config . location = true ;
59
- node . position = pos ;
58
+ config . location = true
59
+ node . position = pos
60
60
}
61
61
}
62
62
63
- return node ;
63
+ return node
64
64
}
65
65
66
66
/* Transform children. */
67
67
function nodes ( children , config ) {
68
- var length = children . length ;
69
- var index = - 1 ;
70
- var result = [ ] ;
68
+ var length = children . length
69
+ var index = - 1
70
+ var result = [ ]
71
71
72
72
while ( ++ index < length ) {
73
- result [ index ] = transform ( children [ index ] , config ) ;
73
+ result [ index ] = transform ( children [ index ] , config )
74
74
}
75
75
76
- return result ;
76
+ return result
77
77
}
78
78
79
79
/* Transform a document.
80
80
* Stores `ast.quirksMode` in `node.data.quirksMode`. */
81
81
function root ( ast , children , config ) {
82
- var node = { type : 'root' , children : children , data : { } } ;
83
- var doc ;
82
+ var node = { type : 'root' , children : children , data : { } }
83
+ var doc
84
84
85
- node . data . quirksMode = ast . mode === 'quirks' || ast . mode === 'limited-quirks' ;
85
+ node . data . quirksMode = ast . mode === 'quirks' || ast . mode === 'limited-quirks'
86
86
87
87
if ( config . file && config . location ) {
88
- doc = String ( config . file ) ;
88
+ doc = String ( config . file )
89
89
90
90
node . position = {
91
91
start : { line : 1 , column : 1 , offset : 0 } ,
92
- end : { line : count ( doc , '\n' ) + 1 , column : doc . length - doc . lastIndexOf ( '\n' ) , offset : doc . length }
93
- } ;
92
+ end : {
93
+ line : count ( doc , '\n' ) + 1 ,
94
+ column : doc . length - doc . lastIndexOf ( '\n' ) ,
95
+ offset : doc . length
96
+ }
97
+ }
94
98
}
95
99
96
- return node ;
100
+ return node
97
101
}
98
102
99
103
/* Transform a doctype. */
@@ -103,77 +107,82 @@ function doctype(ast) {
103
107
name : ast . name || '' ,
104
108
public : ast . publicId || null ,
105
109
system : ast . systemId || null
106
- } ;
110
+ }
107
111
}
108
112
109
113
/* Transform a text. */
110
114
function text ( ast ) {
111
- return { type : 'text' , value : ast . value } ;
115
+ return { type : 'text' , value : ast . value }
112
116
}
113
117
114
118
/* Transform a comment. */
115
119
function comment ( ast ) {
116
- return { type : 'comment' , value : ast . data } ;
120
+ return { type : 'comment' , value : ast . data }
117
121
}
118
122
119
123
/* Transform an element. */
120
124
function element ( ast , children , config ) {
121
- var props = { } ;
122
- var values = ast . attrs ;
123
- var length = values . length ;
124
- var index = - 1 ;
125
- var attr ;
126
- var node ;
127
- var pos ;
128
- var start ;
129
- var end ;
125
+ var props = { }
126
+ var values = ast . attrs
127
+ var length = values . length
128
+ var index = - 1
129
+ var attr
130
+ var node
131
+ var pos
132
+ var start
133
+ var end
130
134
131
135
while ( ++ index < length ) {
132
- attr = values [ index ] ;
133
- props [ ( attr . prefix ? attr . prefix + ':' : '' ) + attr . name ] = attr . value ;
136
+ attr = values [ index ]
137
+ props [ ( attr . prefix ? attr . prefix + ':' : '' ) + attr . name ] = attr . value
134
138
}
135
139
136
- node = h ( ast . tagName , props , children ) ;
140
+ node = h ( ast . tagName , props , children )
137
141
138
142
if ( ast . nodeName === 'template' && 'content' in ast ) {
139
- pos = ast . sourceCodeLocation ;
140
- start = pos && pos . startTag && position ( pos . startTag ) . end ;
141
- end = pos && pos . endTag && position ( pos . endTag ) . start ;
143
+ pos = ast . sourceCodeLocation
144
+ start = pos && pos . startTag && position ( pos . startTag ) . end
145
+ end = pos && pos . endTag && position ( pos . endTag ) . start
142
146
143
- node . content = transform ( ast . content , config ) ;
147
+ node . content = transform ( ast . content , config )
144
148
145
149
if ( ( start || end ) && config . file ) {
146
- node . content . position = { start : start , end : end } ;
150
+ node . content . position = { start : start , end : end }
147
151
}
148
152
}
149
153
150
- return node ;
154
+ return node
151
155
}
152
156
153
157
/* Create clean positional information. */
154
158
function location ( node , location , verbose ) {
155
- var pos = position ( location ) ;
156
- var reference ;
157
- var values ;
158
- var props ;
159
- var prop ;
160
- var name ;
159
+ var pos = position ( location )
160
+ var reference
161
+ var values
162
+ var props
163
+ var prop
164
+ var name
161
165
162
166
if ( node . type === 'element' ) {
163
- reference = node . children [ node . children . length - 1 ] ;
167
+ reference = node . children [ node . children . length - 1 ]
164
168
165
169
/* Unclosed with children (upstream: https://github.com/inikulin/parse5/issues/109) */
166
- if ( ! location . endTag && reference && reference . position && reference . position . end ) {
167
- pos . end = xtend ( reference . position . end ) ;
170
+ if (
171
+ ! location . endTag &&
172
+ reference &&
173
+ reference . position &&
174
+ reference . position . end
175
+ ) {
176
+ pos . end = xtend ( reference . position . end )
168
177
}
169
178
170
179
if ( verbose ) {
171
- values = location . attrs ;
172
- props = { } ;
180
+ values = location . attrs
181
+ props = { }
173
182
174
183
for ( prop in values ) {
175
- name = ( information ( prop ) || { } ) . propertyName || camelcase ( prop ) ;
176
- props [ name ] = position ( values [ prop ] ) ;
184
+ name = ( information ( prop ) || { } ) . propertyName || camelcase ( prop )
185
+ props [ name ] = position ( values [ prop ] )
177
186
}
178
187
179
188
node . data = {
@@ -182,19 +191,27 @@ function location(node, location, verbose) {
182
191
closing : location . endTag ? position ( location . endTag ) : null ,
183
192
properties : props
184
193
}
185
- } ;
194
+ }
186
195
}
187
196
}
188
197
189
- return pos ;
198
+ return pos
190
199
}
191
200
192
201
function position ( loc ) {
193
- var start = point ( { line : loc . startLine , column : loc . startCol , offset : loc . startOffset } ) ;
194
- var end = point ( { line : loc . endLine , column : loc . endCol , offset : loc . endOffset } ) ;
195
- return start || end ? { start : start , end : end } : null ;
202
+ var start = point ( {
203
+ line : loc . startLine ,
204
+ column : loc . startCol ,
205
+ offset : loc . startOffset
206
+ } )
207
+ var end = point ( {
208
+ line : loc . endLine ,
209
+ column : loc . endCol ,
210
+ offset : loc . endOffset
211
+ } )
212
+ return start || end ? { start : start , end : end } : null
196
213
}
197
214
198
215
function point ( point ) {
199
- return point . line && point . column ? point : null ;
216
+ return point . line && point . column ? point : null
200
217
}
0 commit comments