Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom replace functions to return DocumentFragment nodes #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

hftf
Copy link

@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
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