From 4de04124bef945aeb0c6b788871b880a7145d307 Mon Sep 17 00:00:00 2001 From: Chang Wang Date: Wed, 19 Aug 2020 14:22:53 -0400 Subject: [PATCH 1/3] add unist-util-is, remove hast-util-is-element --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0c0f417..b097ba8 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ ], "dependencies": { "escape-string-regexp": "^2.0.0", - "hast-util-is-element": "^1.0.0", + "unist-util-is": "4.0.2", "unist-util-visit-parents": "^3.0.0" }, "devDependencies": { From a5b93d34993af227fd75de6b7606802c833a36da Mon Sep 17 00:00:00 2001 From: Chang Wang Date: Wed, 19 Aug 2020 14:23:46 -0400 Subject: [PATCH 2/3] use unist-util-is in main file --- index.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 32ea97c..65f6966 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ 'use strict' var visit = require('unist-util-visit-parents') -var is = require('hast-util-is-element') +var is = require('unist-util-is') var escape = require('escape-string-regexp') var defaultIgnore = ['title', 'script', 'style', 'svg', 'math'] @@ -119,6 +119,14 @@ function findAndReplace(tree, find, replace, options) { function search(tree, options, handler) { var ignore = options.ignore || defaultIgnore var result = [] + if (!Array.isArray(ignore)) { + ignore = [ignore] + } + + ignore = ignore.map(function (x) { + if (typeof x === 'object') return x + return {tagName: x} + }) visit(tree, 'text', visitor) From 1dd0251f49bb40ea82818704f4074840b13d5175 Mon Sep 17 00:00:00 2001 From: Chang Wang Date: Wed, 19 Aug 2020 14:24:16 -0400 Subject: [PATCH 3/3] add tests for passing in functions and objects --- index.js | 2 +- test.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 65f6966..3fc53ad 100644 --- a/index.js +++ b/index.js @@ -124,7 +124,7 @@ function search(tree, options, handler) { } ignore = ignore.map(function (x) { - if (typeof x === 'object') return x + if (typeof x !== 'string') return x return {tagName: x} }) diff --git a/test.js b/test.js index fe8c870..0c80ef2 100644 --- a/test.js +++ b/test.js @@ -166,6 +166,48 @@ test('findAndReplace', function (t) { 'should ignore from options' ) + t.deepEqual( + findAndReplace( + h('p', [ + h('span', 'visible text'), + h('span', { + text: 'hidden text', + style: 'font-size:1px;display:none;line-height:1px;' + }) + ]), + 'text', + 'TEXT', + { + ignore: function (node) { + return /display:\s*none/.test(node.properties.style) + } + } + ), + h('p', [ + h('span', ['visible ', 'TEXT']), + h('span', { + text: 'hidden text', + style: 'font-size:1px;display:none;line-height:1px;' + }) + ]), + 'should ignore using function' + ) + + t.deepEqual( + findAndReplace( + h('p', [h('span', 'text'), h('sup', 'text')]), + 'text', + 'TEXT', + { + ignore: { + tagName: 'sup' + } + } + ), + h('p', [h('span', 'TEXT'), h('sup', 'text')]), + 'should ignore using objects' + ) + t.deepEqual( findAndReplace(h('p', 'Some emphasis, importance, and code.'), { importance: function (match) {