|
| 1 | +/** |
| 2 | + * @author Titus Wormer |
| 3 | + * @copyright 2016 Titus Wormer |
| 4 | + * @license MIT |
| 5 | + * @module hast-util-from-parse5 |
| 6 | + * @fileoverview Transform Parse5’s AST to HAST. |
| 7 | + */ |
| 8 | + |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +/* Dependencies. */ |
| 12 | +var information = require('property-information'); |
| 13 | +var camelcase = require('camelcase'); |
| 14 | +var vfileLocation = require('vfile-location'); |
| 15 | +var has = require('has'); |
| 16 | +var h = require('hastscript'); |
| 17 | + |
| 18 | +/* Expose. */ |
| 19 | +module.exports = wrapper; |
| 20 | + |
| 21 | +/* Handlers. */ |
| 22 | +var map = { |
| 23 | + '#document': root, |
| 24 | + '#document-fragment': root, |
| 25 | + '#text': text, |
| 26 | + '#comment': comment, |
| 27 | + '#documentType': doctype |
| 28 | +}; |
| 29 | + |
| 30 | +/** |
| 31 | + * Wrapper to normalise options. |
| 32 | + * |
| 33 | + * @param {ASTNode} ast - Parse5 node. |
| 34 | + * @param {VFile|Object?} [options] - Configuration. |
| 35 | + * @return {HASTNode} - HAST node. |
| 36 | + */ |
| 37 | +function wrapper(ast, options) { |
| 38 | + var settings = options || {}; |
| 39 | + var file; |
| 40 | + |
| 41 | + if (settings.messages) { |
| 42 | + file = settings; |
| 43 | + settings = {}; |
| 44 | + } else { |
| 45 | + file = settings.file; |
| 46 | + } |
| 47 | + |
| 48 | + return transform(ast, { |
| 49 | + file: file, |
| 50 | + toPosition: file ? vfileLocation(file).toPosition : null, |
| 51 | + verbose: settings.verbose |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Transform a node. |
| 57 | + * |
| 58 | + * @param {ASTNode} ast - Parse5 node. |
| 59 | + * @param {Object} config - Configuration. |
| 60 | + * @return {HASTNode} - HAST node. |
| 61 | + */ |
| 62 | +function transform(ast, config) { |
| 63 | + var fn = has(map, ast.nodeName) ? map[ast.nodeName] : element; |
| 64 | + var children; |
| 65 | + var node; |
| 66 | + |
| 67 | + if (ast.childNodes) { |
| 68 | + children = nodes(ast.childNodes, config); |
| 69 | + } |
| 70 | + |
| 71 | + node = fn(ast, children, config); |
| 72 | + |
| 73 | + if (ast.__location && config.toPosition) { |
| 74 | + node.position = location(ast.__location, ast, node, config); |
| 75 | + } |
| 76 | + |
| 77 | + return node; |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Transform children. |
| 82 | + * |
| 83 | + * @param {Array.<ASTNode>} children - Parse5 nodes. |
| 84 | + * @param {Object} config - Configuration. |
| 85 | + * @return {Array.<HASTNode>} - HAST nodes. |
| 86 | + */ |
| 87 | +function nodes(children, config) { |
| 88 | + var length = children.length; |
| 89 | + var index = -1; |
| 90 | + var result = []; |
| 91 | + |
| 92 | + while (++index < length) { |
| 93 | + result[index] = transform(children[index], config); |
| 94 | + } |
| 95 | + |
| 96 | + return result; |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Transform a document. |
| 101 | + * |
| 102 | + * Stores `ast.quirksMode` in `node.data.quirksMode`. |
| 103 | + * |
| 104 | + * @param {ASTNode.<Document>} ast - Parse5 document. |
| 105 | + * @param {Array.<ASTNode>} children - Children of `ast`. |
| 106 | + * @param {Object} config - Configuration. |
| 107 | + * @return {HASTRoot} - Root node. |
| 108 | + */ |
| 109 | +function root(ast, children, config) { |
| 110 | + var node = { |
| 111 | + type: 'root', |
| 112 | + children: children, |
| 113 | + data: { |
| 114 | + quirksMode: ast.quirksMode |
| 115 | + } |
| 116 | + }; |
| 117 | + |
| 118 | + if (config.file) { |
| 119 | + node.position = location({ |
| 120 | + startOffset: 0, |
| 121 | + endOffset: String(config.file).length |
| 122 | + }, ast, node, config); |
| 123 | + } |
| 124 | + |
| 125 | + return node; |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Transform a doctype. |
| 130 | + * |
| 131 | + * @param {ASTNode.<DocumentType>} ast - Parse5 doctype. |
| 132 | + * @return {HASTDoctype} - Doctype node. |
| 133 | + */ |
| 134 | +function doctype(ast) { |
| 135 | + return { |
| 136 | + type: 'doctype', |
| 137 | + name: ast.name || '', |
| 138 | + public: ast.publicId || null, |
| 139 | + system: ast.systemId || null |
| 140 | + }; |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | + * Transform a text. |
| 145 | + * |
| 146 | + * @param {ASTNode.<Text>} ast - Parse5 text. |
| 147 | + * @return {HASTText} - Text node. |
| 148 | + */ |
| 149 | +function text(ast) { |
| 150 | + return {type: 'text', value: ast.value}; |
| 151 | +} |
| 152 | + |
| 153 | +/** |
| 154 | + * Transform a comment. |
| 155 | + * |
| 156 | + * @param {ASTNode.<Comment>} ast - Parse5 comment. |
| 157 | + * @return {HASTComment} - Comment node. |
| 158 | + */ |
| 159 | +function comment(ast) { |
| 160 | + return {type: 'comment', value: ast.data}; |
| 161 | +} |
| 162 | + |
| 163 | +/** |
| 164 | + * Transform an element. |
| 165 | + * |
| 166 | + * @param {ASTNode.<Element>} ast - Parse5 element. |
| 167 | + * @param {Array.<ASTNode>} children - Children of `ast`. |
| 168 | + * @return {HASTElement} - Element node. |
| 169 | + */ |
| 170 | +function element(ast, children) { |
| 171 | + var props = {}; |
| 172 | + var values = ast.attrs; |
| 173 | + var length = values.length; |
| 174 | + var index = -1; |
| 175 | + var attr; |
| 176 | + |
| 177 | + while (++index < length) { |
| 178 | + attr = values[index]; |
| 179 | + props[(attr.prefix ? attr.prefix + ':' : '') + attr.name] = attr.value; |
| 180 | + } |
| 181 | + |
| 182 | + return h(ast.tagName, props, children); |
| 183 | +} |
| 184 | + |
| 185 | +/** |
| 186 | + * Create clean positional information. |
| 187 | + * |
| 188 | + * @param {Function} toPosition - Offset to position. |
| 189 | + * @param {Object?} [dirty] - Parse5 location information. |
| 190 | + * @return {Location} - Start and end positions. |
| 191 | + */ |
| 192 | +function loc(toPosition, dirty) { |
| 193 | + return { |
| 194 | + start: toPosition(dirty.startOffset), |
| 195 | + end: toPosition(dirty.endOffset) |
| 196 | + }; |
| 197 | +} |
| 198 | + |
| 199 | +/** |
| 200 | + * Create clean positional information. |
| 201 | + * |
| 202 | + * @param {Object} info - Parse5 location information. |
| 203 | + * @param {HASTNode} node - HAST node. |
| 204 | + * @param {Object} ast - Parse5 node. |
| 205 | + * @param {Object} config - Options. |
| 206 | + * @return {Location} - Start and end positions. |
| 207 | + */ |
| 208 | +function location(info, ast, node, config) { |
| 209 | + var end = info.endOffset; |
| 210 | + var values = info.attrs || {}; |
| 211 | + var propPositions = {}; |
| 212 | + var prop; |
| 213 | + var name; |
| 214 | + var reference; |
| 215 | + |
| 216 | + for (prop in values) { |
| 217 | + name = (information(prop) || {}).propertyName || camelcase(prop); |
| 218 | + propPositions[name] = loc(config.toPosition, values[prop]); |
| 219 | + } |
| 220 | + |
| 221 | + /* Upstream: https://github.com/inikulin/parse5/issues/109 */ |
| 222 | + if (node.type === 'element' && !info.endTag) { |
| 223 | + reference = node.children[node.children.length - 1]; |
| 224 | + |
| 225 | + /* Unclosed with children: */ |
| 226 | + if (reference && reference.position) { |
| 227 | + end = reference.position.end.offset; |
| 228 | + /* Unclosed without children: */ |
| 229 | + } else if (info.startTag) { |
| 230 | + end = info.startTag.endOffset; |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + if (config.verbose && node.type === 'element') { |
| 235 | + node.data = { |
| 236 | + position: { |
| 237 | + opening: loc(config.toPosition, info.startTag || info), |
| 238 | + closing: info.endTag ? loc(config.toPosition, info.endTag) : null, |
| 239 | + properties: propPositions |
| 240 | + } |
| 241 | + }; |
| 242 | + } |
| 243 | + |
| 244 | + return { |
| 245 | + start: config.toPosition(info.startOffset), |
| 246 | + end: config.toPosition(end) |
| 247 | + }; |
| 248 | +} |
0 commit comments