11'use strict' ;
22
3- /* Dependencies. */
43var xtend = require ( 'xtend' ) ;
54var toH = require ( 'hast-to-hyperscript' ) ;
65var NS = require ( 'web-namespaces' ) ;
76var has = require ( 'has' ) ;
87var zwitch = require ( 'zwitch' ) ;
98var mapz = require ( 'mapz' ) ;
109
11- /* Expose. */
1210module . exports = transform ;
1311
14- /* Construct. */
1512var one = zwitch ( 'type' ) ;
1613var all = mapz ( one , { key : 'children' , indices : false } ) ;
1714
@@ -43,22 +40,11 @@ var attributeSpaces = {
4340 'xmlns:xlink' : { prefix : 'xmlns' , name : 'xlink' , namespace : NS . xmlns }
4441} ;
4542
46- /**
47- * Transform a tree from HAST to Parse5’s AST.
48- *
49- * @param {HASTNode } tree - HAST tree.
50- * @return {ASTNode } - Parse5 tree.
51- */
43+ /* Transform a tree from HAST to Parse5’s AST. */
5244function transform ( tree ) {
5345 return patch ( one ( tree ) , null , NS . html ) ;
5446}
5547
56- /**
57- * Transform a root from HAST to Parse5.
58- *
59- * @param {HASTRoot } node - HAST root.
60- * @return {ASTNode.<Document> } - Parse5 document.
61- */
6248function root ( node ) {
6349 var data = node . data || { } ;
6450 var qs = has ( data , 'quirksMode' ) ? Boolean ( data . quirksMode ) : false ;
@@ -70,12 +56,6 @@ function root(node) {
7056 } ;
7157}
7258
73- /**
74- * Transform an element from HAST to Parse5.
75- *
76- * @param {HASTElement } node - HAST element.
77- * @return {ASTNode.<Element> } - Parse5 element.
78- */
7959function element ( node ) {
8060 var shallow = xtend ( node ) ;
8161
@@ -108,12 +88,6 @@ function element(node) {
10888 } , shallow ) ;
10989}
11090
111- /**
112- * Transform a doctype from HAST to Parse5.
113- *
114- * @param {HASTDoctype } node - HAST doctype.
115- * @return {ASTNode.<DocumentType> } - Parse5 doctype.
116- */
11791function doctype ( node ) {
11892 return wrap ( node , {
11993 nodeName : '#documentType' ,
@@ -123,39 +97,21 @@ function doctype(node) {
12397 } ) ;
12498}
12599
126- /**
127- * Transform a text from HAST to Parse5.
128- *
129- * @param {HASTText } node - HAST text.
130- * @return {ASTNode.<Text> } - Parse5 text.
131- */
132100function text ( node ) {
133101 return wrap ( node , {
134102 nodeName : '#text' ,
135103 value : node . value
136104 } ) ;
137105}
138106
139- /**
140- * Transform a comment from HAST to Parse5.
141- *
142- * @param {HASTComment } node - HAST comment.
143- * @return {ASTNode.<Comment> } - Parse5 comment.
144- */
145107function comment ( node ) {
146108 return wrap ( node , {
147109 nodeName : '#comment' ,
148110 data : node . value
149111 } ) ;
150112}
151113
152- /**
153- * Patch position.
154- *
155- * @param {HASTNode } node - HAST node.
156- * @param {ASTNode } node - Parse5 node.
157- * @return {ASTNode } - Given Parse5 node.
158- */
114+ /* Patch position. */
159115function wrap ( node , ast ) {
160116 if ( node . position && node . position . start && node . position . end ) {
161117 ast . __location = {
@@ -169,15 +125,8 @@ function wrap(node, ast) {
169125 return ast ;
170126}
171127
172- /**
173- * Patch a tree recursively, by adding namespaces
174- * and parent references where needed.
175- *
176- * @param {ASTNode } node - Parse5 node.
177- * @param {ASTNode } parent - Parent of `node`.
178- * @param {string } ns - Current namespace.
179- * @return {ASTNode } - Patched replacement for `node`.
180- */
128+ /* Patch a tree recursively, by adding namespaces
129+ * and parent references where needed. */
181130function patch ( node , parent , ns ) {
182131 var location = node . __location ;
183132 var children = node . childNodes ;
0 commit comments