Skip to content

Allow custom replace functions to return DocumentFragment nodes#86

Open
hftf wants to merge 1 commit intopadolsey:masterfrom
hftf:master
Open

Allow custom replace functions to return DocumentFragment nodes#86
hftf wants to merge 1 commit intopadolsey:masterfrom
hftf:master

Conversation

@hftf
Copy link
Copy Markdown

@hftf hftf commented Sep 1, 2020

Fixes a bug demonstrated in https://jsfiddle.net/vy3tsq8o/. Code repeated below:

<p id="ex1">11 22</p>
<p id="ex2">11 22</p>
var pattern = /\d+/g;
var replaceFunction = (useDocumentFragment) => function(portion, match) {
  var fragment = (useDocumentFragment) ?
    document.createDocumentFragment() :
    document.createElement('fragment');
  var mark = document.createElement('mark');

  mark.appendChild(document.createTextNode(portion.text))
  fragment.appendChild(document.createTextNode('x'));
  fragment.appendChild(mark);
  fragment.appendChild(document.createTextNode('y'));
  return fragment;
}

findAndReplaceDOMText(ex1, { find: pattern, replace: replaceFunction(false) });
findAndReplaceDOMText(ex2, { find: pattern, replace: replaceFunction(true)  });

@hftf
Copy link
Copy Markdown
Author

hftf commented Apr 21, 2021

Hope you're doing better. Are you still interested in maintaining the project?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant