1- 'use strict' ;
2-
3- var xtend = require ( 'xtend' ) ;
4- var toH = require ( 'hast-to-hyperscript' ) ;
5- var NS = require ( 'web-namespaces' ) ;
6- var zwitch = require ( 'zwitch' ) ;
7- var mapz = require ( 'mapz' ) ;
8-
9- module . exports = transform ;
10-
11- var own = { } . hasOwnProperty ;
12- var one = zwitch ( 'type' ) ;
13- var all = mapz ( one , { key : 'children' , indices : false } ) ;
14-
15- var customProps = [ '__location' , 'childNodes' , 'content' , 'parentNode' , 'namespaceURI' ] ;
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 ;
1+ 'use strict'
2+
3+ var xtend = require ( 'xtend' )
4+ var toH = require ( 'hast-to-hyperscript' )
5+ var NS = require ( 'web-namespaces' )
6+ var zwitch = require ( 'zwitch' )
7+ var mapz = require ( 'mapz' )
8+
9+ module . exports = transform
10+
11+ var own = { } . hasOwnProperty
12+ var one = zwitch ( 'type' )
13+ var all = mapz ( one , { key : 'children' , indices : false } )
14+
15+ var customProps = [
16+ '__location' ,
17+ 'childNodes' ,
18+ 'content' ,
19+ 'parentNode' ,
20+ 'namespaceURI'
21+ ]
22+
23+ one . handlers . root = root
24+ one . handlers . element = element
25+ one . handlers . text = text
26+ one . handlers . comment = comment
27+ one . handlers . doctype = doctype
2228
2329/* Map of tag-names starting new namespaces. */
2430var namespaces = {
2531 math : NS . mathml ,
2632 svg : NS . svg
27- } ;
33+ }
2834
2935/* Map of attributes with namespaces. */
3036var attributeSpaces = {
@@ -40,58 +46,62 @@ var attributeSpaces = {
4046 'xml:space' : { prefix : 'xml' , name : 'space' , namespace : NS . xml } ,
4147 xmlns : { prefix : '' , name : 'xmlns' , namespace : NS . xmlns } ,
4248 'xmlns:xlink' : { prefix : 'xmlns' , name : 'xlink' , namespace : NS . xmlns }
43- } ;
49+ }
4450
4551/* Transform a tree from HAST to Parse5’s AST. */
4652function transform ( tree ) {
47- return patch ( one ( tree ) , null , NS . html ) ;
53+ return patch ( one ( tree ) , null , NS . html )
4854}
4955
5056function root ( node ) {
51- var data = node . data || { } ;
52- var qs = own . call ( data , 'quirksMode' ) ? Boolean ( data . quirksMode ) : false ;
57+ var data = node . data || { }
58+ var qs = own . call ( data , 'quirksMode' ) ? Boolean ( data . quirksMode ) : false
5359
5460 return {
5561 nodeName : '#document' ,
5662 mode : qs ? 'quirks' : 'no-quirks' ,
5763 childNodes : all ( node )
58- } ;
64+ }
5965}
6066
6167function element ( node ) {
62- var shallow = xtend ( node ) ;
68+ var shallow = xtend ( node )
6369
64- shallow . children = [ ] ;
70+ shallow . children = [ ]
6571
66- return toH ( function ( name , attrs ) {
67- var values = [ ] ;
68- var content ;
69- var value ;
70- var key ;
72+ return toH ( function ( name , attrs ) {
73+ var values = [ ]
74+ var content
75+ var value
76+ var key
7177
7278 for ( key in attrs ) {
73- value = { name : key , value : attrs [ key ] } ;
79+ value = { name : key , value : attrs [ key ] }
7480
7581 if ( own . call ( attributeSpaces , key ) ) {
76- value = xtend ( value , attributeSpaces [ key ] ) ;
82+ value = xtend ( value , attributeSpaces [ key ] )
7783 }
7884
79- values . push ( value ) ;
85+ values . push ( value )
8086 }
8187
8288 if ( name === 'template' ) {
83- content = transform ( shallow . content ) ;
84- delete content . mode ;
85- content . nodeName = '#document-fragment' ;
89+ content = transform ( shallow . content )
90+ delete content . mode
91+ content . nodeName = '#document-fragment'
8692 }
8793
88- return wrap ( node , {
89- nodeName : node . tagName ,
90- tagName : node . tagName ,
91- attrs : values ,
92- childNodes : node . children ? all ( node ) : [ ]
93- } , content ) ;
94- } , shallow ) ;
94+ return wrap (
95+ node ,
96+ {
97+ nodeName : node . tagName ,
98+ tagName : node . tagName ,
99+ attrs : values ,
100+ childNodes : node . children ? all ( node ) : [ ]
101+ } ,
102+ content
103+ )
104+ } , shallow )
95105}
96106
97107function doctype ( node ) {
@@ -100,21 +110,21 @@ function doctype(node) {
100110 name : node . name ,
101111 publicId : node . public || null ,
102112 systemId : node . system || null
103- } ) ;
113+ } )
104114}
105115
106116function text ( node ) {
107117 return wrap ( node , {
108118 nodeName : '#text' ,
109119 value : node . value
110- } ) ;
120+ } )
111121}
112122
113123function comment ( node ) {
114124 return wrap ( node , {
115125 nodeName : '#comment' ,
116126 data : node . value
117- } ) ;
127+ } )
118128}
119129
120130/* Patch position. */
@@ -125,62 +135,62 @@ function wrap(node, ast, content) {
125135 col : node . position . start . column ,
126136 startOffset : node . position . start . offset ,
127137 endOffset : node . position . end . offset
128- } ;
138+ }
129139 }
130140
131141 if ( content ) {
132- ast . content = content ;
142+ ast . content = content
133143 }
134144
135- return ast ;
145+ return ast
136146}
137147
138148/* Patch a tree recursively, by adding namespaces
139149 * and parent references where needed. */
140150function patch ( node , parent , ns ) {
141- var location = node . __location ;
142- var children = node . childNodes ;
143- var name = node . tagName ;
144- var replacement = { } ;
145- var length ;
146- var index ;
147- var key ;
151+ var location = node . __location
152+ var children = node . childNodes
153+ var name = node . tagName
154+ var replacement = { }
155+ var length
156+ var index
157+ var key
148158
149159 for ( key in node ) {
150160 if ( customProps . indexOf ( key ) === - 1 ) {
151- replacement [ key ] = node [ key ] ;
161+ replacement [ key ] = node [ key ]
152162 }
153163 }
154164
155165 if ( own . call ( namespaces , name ) ) {
156- ns = namespaces [ name ] ;
166+ ns = namespaces [ name ]
157167 }
158168
159169 if ( own . call ( replacement , 'tagName' ) ) {
160- replacement . namespaceURI = ns ;
170+ replacement . namespaceURI = ns
161171 }
162172
163173 if ( children ) {
164- replacement . childNodes = children ;
165- length = children . length ;
166- index = - 1 ;
174+ replacement . childNodes = children
175+ length = children . length
176+ index = - 1
167177
168178 while ( ++ index < length ) {
169- children [ index ] = patch ( children [ index ] , replacement , ns ) ;
179+ children [ index ] = patch ( children [ index ] , replacement , ns )
170180 }
171181 }
172182
173183 if ( name === 'template' ) {
174- replacement . content = patch ( node . content , null , ns ) ;
184+ replacement . content = patch ( node . content , null , ns )
175185 }
176186
177187 if ( parent ) {
178- replacement . parentNode = parent ;
188+ replacement . parentNode = parent
179189 }
180190
181191 if ( location ) {
182- replacement . __location = location ;
192+ replacement . __location = location
183193 }
184194
185- return replacement ;
195+ return replacement
186196}
0 commit comments