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

Support for insertAdjacentHTML #1219

Closed
danawoodman opened this issue Aug 28, 2015 · 9 comments
Closed

Support for insertAdjacentHTML #1219

danawoodman opened this issue Aug 28, 2015 · 9 comments
Labels

Comments

@danawoodman
Copy link

Is there plans to support element.insertAdjacent in jsdom?

document.body.insertAdjacentHTML('afterbegin', '<p>Hello World</p>')

Results in:

TypeError: document.body.insertAdjacentHTML is not a function
@Joris-van-der-Wel
Copy link
Member

Yes, I was planning to add this eventually

@mbrevda
Copy link

mbrevda commented Sep 2, 2015

+1. Fresh jquery convert here: is there another way that I can use in the meantime?

@domenic
Copy link
Member

domenic commented Sep 2, 2015

You can just use document.body.innerHTML = '<p>Hello World</p>' + document.body.innerHTML. It will be slower as it has to re-parse everything, but it will work. You can also use DOM APIs directly.

@mbrevda
Copy link

mbrevda commented Sep 2, 2015

Oh, cool. Thanks! Seems insertAdjacentHTML is much more efficient, but I guess this will do in the mean time

@Joris-van-der-Wel
Copy link
Member

Here is how you can avoid a reparse. This would be similar to how insertAdjacentHTML could be implemented:

function htmlToFragment(ownerDocument, markup) {
    const container = ownerDocument.createElement('div');
    container.innerHTML = markup;
    const fragment = ownerDocument.createDocumentFragment();
    while (container.firstChild) {
        fragment.appendChild(container.firstChild);
    }
    return fragment;
}

(might contain typo's)

Then you can do something like: node.appendChild(htmlToFragment(document, '<p>hello world</p>'))

@Joris-van-der-Wel Joris-van-der-Wel changed the title Support for insertAdjacent Support for insertAdjacentHTML Sep 2, 2015
@domenic
Copy link
Member

domenic commented Sep 2, 2015

Yeah, that will generally work, although it doesn't work for e.g. inserting <tr>s into tables.

@patrickfatrick
Copy link

+1. Trying out jsdom for the first time today and came across this limitation.

@tombh
Copy link

tombh commented Nov 13, 2015

You can write your own with something like window.HTMLElement.prototype.insertAdjacentHTML = function(){}

@inikulin
Copy link
Contributor

@domenic It will work even for <tr> if you'll use <template> as container.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants