|
| 1 | +'use strict' |
| 2 | + |
| 3 | +module.exports = findAndReplace |
| 4 | + |
| 5 | +var visit = require('unist-util-visit-parents') |
| 6 | +var convert = require('unist-util-is/convert') |
| 7 | +var escape = require('escape-string-regexp') |
| 8 | + |
| 9 | +var splice = [].splice |
| 10 | + |
| 11 | +function findAndReplace(tree, find, replace, options) { |
| 12 | + var settings |
| 13 | + var schema |
| 14 | + |
| 15 | + if ( |
| 16 | + typeof find === 'string' || |
| 17 | + (find && typeof find.lastIndex === 'number') |
| 18 | + ) { |
| 19 | + schema = [[find, replace]] |
| 20 | + } else { |
| 21 | + schema = find |
| 22 | + options = replace |
| 23 | + } |
| 24 | + |
| 25 | + settings = options || {} |
| 26 | + |
| 27 | + search(tree, settings, handlerFactory(toPairs(schema))) |
| 28 | + |
| 29 | + return tree |
| 30 | + |
| 31 | + function handlerFactory(pairs) { |
| 32 | + var pair = pairs[0] |
| 33 | + |
| 34 | + return handler |
| 35 | + |
| 36 | + function handler(node, parent) { |
| 37 | + var find = pair[0] |
| 38 | + var replace = pair[1] |
| 39 | + var nodes = [] |
| 40 | + var start = 0 |
| 41 | + var index = parent.children.indexOf(node) |
| 42 | + var position |
| 43 | + var match |
| 44 | + var subhandler |
| 45 | + var value |
| 46 | + |
| 47 | + find.lastIndex = 0 |
| 48 | + |
| 49 | + match = find.exec(node.value) |
| 50 | + |
| 51 | + while (match) { |
| 52 | + position = match.index |
| 53 | + value = replace.apply( |
| 54 | + null, |
| 55 | + [].concat(match, {index: match.index, input: match.input}) |
| 56 | + ) |
| 57 | + |
| 58 | + if (value !== false) { |
| 59 | + if (start !== position) { |
| 60 | + nodes.push({type: 'text', value: node.value.slice(start, position)}) |
| 61 | + } |
| 62 | + |
| 63 | + if (typeof value === 'string' && value.length !== 0) { |
| 64 | + value = {type: 'text', value: value} |
| 65 | + } |
| 66 | + |
| 67 | + if (value) { |
| 68 | + nodes.push(value) |
| 69 | + } |
| 70 | + |
| 71 | + start = position + match[0].length |
| 72 | + } |
| 73 | + |
| 74 | + if (!find.global) { |
| 75 | + break |
| 76 | + } |
| 77 | + |
| 78 | + match = find.exec(node.value) |
| 79 | + } |
| 80 | + |
| 81 | + if (position === undefined) { |
| 82 | + nodes = [node] |
| 83 | + index-- |
| 84 | + } else { |
| 85 | + if (start < node.value.length) { |
| 86 | + nodes.push({type: 'text', value: node.value.slice(start)}) |
| 87 | + } |
| 88 | + |
| 89 | + nodes.unshift(index, 1) |
| 90 | + splice.apply(parent.children, nodes) |
| 91 | + } |
| 92 | + |
| 93 | + if (pairs.length > 1) { |
| 94 | + subhandler = handlerFactory(pairs.slice(1)) |
| 95 | + position = -1 |
| 96 | + |
| 97 | + while (++position < nodes.length) { |
| 98 | + node = nodes[position] |
| 99 | + |
| 100 | + if (node.type === 'text') { |
| 101 | + subhandler(node, parent) |
| 102 | + } else { |
| 103 | + search(node, settings, subhandler) |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return index + nodes.length + 1 |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +function search(tree, settings, handler) { |
| 114 | + var ignored = convert(settings.ignore || []) |
| 115 | + var result = [] |
| 116 | + |
| 117 | + visit(tree, 'text', visitor) |
| 118 | + |
| 119 | + return result |
| 120 | + |
| 121 | + function visitor(node, parents) { |
| 122 | + var index = -1 |
| 123 | + var parent |
| 124 | + var grandparent |
| 125 | + |
| 126 | + while (++index < parents.length) { |
| 127 | + parent = parents[index] |
| 128 | + |
| 129 | + if ( |
| 130 | + ignored( |
| 131 | + parent, |
| 132 | + grandparent ? grandparent.children.indexOf(parent) : undefined, |
| 133 | + grandparent |
| 134 | + ) |
| 135 | + ) { |
| 136 | + return |
| 137 | + } |
| 138 | + |
| 139 | + grandparent = parent |
| 140 | + } |
| 141 | + |
| 142 | + return handler(node, grandparent) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +function toPairs(schema) { |
| 147 | + var result = [] |
| 148 | + var key |
| 149 | + var index |
| 150 | + |
| 151 | + if (typeof schema !== 'object') { |
| 152 | + throw new Error('Expected array or object as schema') |
| 153 | + } |
| 154 | + |
| 155 | + if ('length' in schema) { |
| 156 | + index = -1 |
| 157 | + |
| 158 | + while (++index < schema.length) { |
| 159 | + result.push([ |
| 160 | + toExpression(schema[index][0]), |
| 161 | + toFunction(schema[index][1]) |
| 162 | + ]) |
| 163 | + } |
| 164 | + } else { |
| 165 | + for (key in schema) { |
| 166 | + result.push([toExpression(key), toFunction(schema[key])]) |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + return result |
| 171 | +} |
| 172 | + |
| 173 | +function toExpression(find) { |
| 174 | + return typeof find === 'string' ? new RegExp(escape(find), 'g') : find |
| 175 | +} |
| 176 | + |
| 177 | +function toFunction(replace) { |
| 178 | + return typeof replace === 'function' ? replace : returner |
| 179 | + |
| 180 | + function returner() { |
| 181 | + return replace |
| 182 | + } |
| 183 | +} |
0 commit comments