syntax-tree / hast-util-raw
Showing 1 of 6 files from the diff.
Other files ignored by Codecov
package.json has changed.
types/index.d.ts has changed.
test.js has changed.
readme.md has changed.

@@ -2,6 +2,7 @@
Loading
2 2
3 3
var Parser = require('parse5/lib/parser')
4 4
var pos = require('unist-util-position')
5 +
var visit = require('unist-util-visit')
5 6
var fromParse5 = require('hast-util-from-parse5')
6 7
var toParse5 = require('hast-util-to-parse5')
7 8
var voids = require('html-void-elements')
@@ -19,12 +20,9 @@
Loading
19 20
var commentToken = 'COMMENT_TOKEN'
20 21
var doctypeToken = 'DOCTYPE_TOKEN'
21 22
22 -
var parseOptions = {
23 -
  sourceCodeLocationInfo: true,
24 -
  scriptingEnabled: false
25 -
}
23 +
var parseOptions = {sourceCodeLocationInfo: true, scriptingEnabled: false}
26 24
27 -
function wrap(tree, file) {
25 +
function wrap(tree, file, options) {
28 26
  var parser = new Parser(parseOptions)
29 27
  var one = zwitch('type', {
30 28
    handlers: {
@@ -37,11 +35,32 @@
Loading
37 35
    },
38 36
    unknown: unknown
39 37
  })
38 +
  var stitches
40 39
  var tokenizer
41 40
  var preprocessor
42 41
  var posTracker
43 42
  var locationTracker
44 -
  var result = fromParse5(documentMode(tree) ? document() : fragment(), file)
43 +
  var result
44 +
  var index
45 +
46 +
  if (file && !('contents' in file)) {
47 +
    options = file
48 +
    file = undefined
49 +
  }
50 +
51 +
  if (options && options.passThrough) {
52 +
    index = -1
53 +
54 +
    while (++index < options.passThrough.length) {
55 +
      one.handlers[options.passThrough[index]] = stitch
56 +
    }
57 +
  }
58 +
59 +
  result = fromParse5(documentMode(tree) ? document() : fragment(), file)
60 +
61 +
  if (stitches) {
62 +
    visit(result, 'comment', mend)
63 +
  }
45 64
46 65
  // Unpack if possible and when not given a `root`.
47 66
  if (tree.type !== 'root' && result.children.length === 1) {
@@ -50,6 +69,13 @@
Loading
50 69
51 70
  return result
52 71
72 +
  function mend(node, index, parent) {
73 +
    if (node.value.stitch) {
74 +
      parent.children[index] = node.value.stitch
75 +
      return index
76 +
    }
77 +
  }
78 +
53 79
  function fragment() {
54 80
    var context = {
55 81
      nodeName: 'template',
@@ -208,6 +234,27 @@
Loading
208 234
    }
209 235
  }
210 236
237 +
  function stitch(node) {
238 +
    var clone = Object.assign({}, node)
239 +
240 +
    stitches = true
241 +
242 +
    // Recurse, because to somewhat handle `[<x>]</x>` (where `[]` denotes the
243 +
    // passed through node).
244 +
    if (node.children) {
245 +
      clone.children = wrap(
246 +
        {type: 'root', children: node.children},
247 +
        file,
248 +
        options
249 +
      ).children
250 +
    }
251 +
252 +
    // Hack: `value` is supposed to be a string, but as none of the tools
253 +
    // (`parse5` or `hast-util-from-parse5`) looks at it, we can pass nodes
254 +
    // through.
255 +
    comment({value: {stitch: clone}})
256 +
  }
257 +
211 258
  function resetTokenizer() {
212 259
    // Reset tokenizer:
213 260
    // See: <https://github.com/inikulin/parse5/blob/9c683e1/packages/parse5/lib/tokenizer/index.js#L218-L234>.
Files Coverage
index.js 100.00%
Project Totals (1 files) 100.00%
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading