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

Add replaceWith method to text node #271

Open
McFizh opened this issue Mar 8, 2024 · 1 comment
Open

Add replaceWith method to text node #271

McFizh opened this issue Mar 8, 2024 · 1 comment

Comments

@McFizh
Copy link

McFizh commented Mar 8, 2024

Would it be possible to have replaceWith method added to next nodes. Example use case:

Text node has content 'word1 word2 word3' and I would like to replace it with for example the following html:

'word1 <b>word2</b> word3'

This would be really easy to do with:

node.replaceWith('word1 <b>word2</b> word3');

@taoqf
Copy link
Owner

taoqf commented Mar 29, 2024

Yes, it should. I guess you want this do :

const root = parse('word1 word2 word3');
root.replaceWith('word1 <b>word2</b> word3');
root.toString().should.eql('word1 <b>word2</b> word3');

But it doen't, actually, the node wont change after calling replaceWith, and the method replaceWith return the node itself. you can try this:

const root = parse('<div>word1 word2 word3</div>');
const div = root.querySelector('div');
div.replaceWith('<div>word1 <b>word2</b> word3</div>');
root.toString().should.eql('<div>word1 <b>word2</b> word3</div>');

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

No branches or pull requests

2 participants