-
Notifications
You must be signed in to change notification settings - Fork 145
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
New Feature: replace text handler #12
Conversation
Added example code and description for using the replaceTextHandler function.
Thanks for this Richard -- nice feature! I'll take a look. This can probably go into 0.4.0 (see #13). |
OK cool, I agree with the API changes. I'll close this one then and wait for the new api. |
Shall we pass the match(es) into the handler so that we can make use of it/them? More like the native Presenting an example with the current API(0.3.0) below,
|
I love the idea of being able to supply either a string or a replacement function. I feel like all this replacement stuff needs to be consolidated a bit, along with the suggested API change in #13. In the meantime I have a question: What is the expected behaviour when replacing just the text if the text you're searching for crosses node boundaries? In @badsyntax's code, the replacement will work like so (AFAICT): (replace "foo" with "baz123")
Before:
f<em>o</em><b>o</b>
After:
b<em>a</em><b>z123</b> I.e. the captured search is substituted with the replacement from left-to-right with remainder characters placed in the last portion of the search ( I'm wondering if this feature is too complex to wrap up in a simple API? Will devs want finer control of how the replacement occurs? (I guess if they do they'll just pass a function, right?) re-opening since discussion is happening |
Some quick thoughts:
|
Regarding new things in the apiCleanup branch which I just pushed: You can now replace plain text like so: findAndReplaceDOMText(nodeToSearchIn, {
find: /something/,
replace: 'something else'
}); And wrapping a node, as before, would be done like so: findAndReplaceDOMText(nodeToSearchIn, {
find: /something/,
wrap: 'em'
}); And of course there is still finer control by passing a custom function: findAndReplaceDOMText(nodeToSearchIn, {
find: /something/,
replace: function() {
// Do what you want
// Return a node or string
}
});
The new API deals with this by having, atm, two modes, 'retain' and 'first'. With the former example: (replace "foo" with "baz123")
Before:
f<em>o</em><b>o</b>
After (Using RETAIN):
b<em>a</em><b>z123</b>
After (Using FIRST):
baz123<em></em><b></b> The |
0.4.0 is out -- and it includes the ability to replace with text or node(s). |
This changes introduces a handler that can be used to replace the matched text with new text (instead of wrapping the matched text in a specified element).