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
Don't replace text nodes if there's nothing to replace #52
Conversation
uhm ... can you please tell me more about this problem? with a real piece od DOM I can test against? Thank you |
P.S. asking because IIRC there is a test already covering the return false case, thanks for any further info |
If all callbacks for all emoji in a text node return false, the text node is replaced nevertheless. This is a problem if you use a MutationObserver to parse any new text nodes. It creates an infinite loop. The observer detects a change => twemoji parses, detects emoji but does not replace them with images (callbacks return false) => twemoji replaces the node anyway with a new fragment => the observer detects a change. And it starts all over. :) In any case, twemoji should not replace the node if it doesn't have to. |
OK, makes sense ... but ... Moreover, you changed the twitter.js file which means you haven't read much about how to contribute or change. So here the catch: twitter.js is automatically generated, you change that even on your host and you are doomed 'casue that's not what changes here.
I will find some time to avoid the problem you mentioned, eventually improving tests to test against such problem too. Feel free to be faster than me or please stay tuned and wait here, since I'll come back and close this once it's done. Cheers |
P.S. also your img check is wrong since that's inside the loop and won't solve a thing wih multiple scheduled changes, you are assuming the last |
Yes, it's in a loop, but it will never be replaced if src is false, so I believe this works fine. If there are one or more changes it will go through. Sorry about patching the wrong file, saw over that. |
I understand, but line I am not saying this problem you are facing is not real, I am saying this pull request is not that good because it address thw wrong file, it patches in a not so good way, and it does not provide tests against the API that should support. I am sure you understand what I am saying, this project is widely used, I don't want to easily accept PR or rush solutions. I see the problem, this PR does not realy solve it. Still wrapping my head around this gotcha. Cheers |
OK, I've changed the test that was indeed checking that nodes were changed anyway, and I've introduced a simple boolean flag that is set to Can you please confirm latest push solves it? Thank you! |
Yep, does the same thing and works. Thanks! |
It doesn't technically does the same thing, it actually fixed a bug I am glad nobody spotted before. If you look close, the Meaning as soon as one single emoji was found, the My change fixes that, and it resets the I know how you feel about your PR, but now that I've explained why that check wasn't enough I hope things are better for you and whoever uses this library. Thank you |
Hm, there are still nodes replaced that don't need to be replaced. |
P.S. You were right about checking |
Not sure it should ... the replacement happens per fragment and the fragment is used only if at least one node has changes ... what kind of scenario are you running twemoji on? You should not have infinite loops anymore, you might have triggered changes but these should vanish in the next iteraction. |
never mind ... you are right, it should be reset each while loop ... |
.... right ... how's |
Perfect, thanks a lot! :) |
\o/ |
If the callback returns false, no text nodes should be replaced.
This is particularly a problem if you parse with a MutationObserver. The change is detected, and twemoji parses again, creating an infinite loop.