Skip to content

Implement Element.insertAdjacentHTML()#1355

Closed
kasperisager wants to merge 11 commits intojsdom:masterfrom
kasperisager:insert-adjacent-html
Closed

Implement Element.insertAdjacentHTML()#1355
kasperisager wants to merge 11 commits intojsdom:masterfrom
kasperisager:insert-adjacent-html

Conversation

@kasperisager
Copy link
Contributor

Ref. #1219. Grabbed and slightly tweaked the implementation from https://github.com/assaf/zombie/blob/005b4726c9c8bd02a234cb26cb756d90358bf0a4/src/dom/jsdom_patches.js#L60-L90

A test case has been added, but both this as well as the implementation might need some more work. It's a start though!

Ref. #1219. Grabbed and slightly tweaked the implementation from
https://github.com/assaf/zombie/blob/005b4726c9c8bd02a234cb26cb756d90358bf0a4/src/dom/jsdom_patches.js#L60-L90

A test case has been added, but both this as well as the implementation
might need some more work. It's a start though!
@kasperisager
Copy link
Contributor Author

Also, the WHATWG spec doesn't yet contain a definition of insertAdjacentHTML but I've added a reference to https://dom.spec.whatwg.org/#dom-element-insertadjacenthtml as I wasn't sure what else to put there (https://w3c.github.io/DOM-Parsing/#widl-Element-insertAdjacentHTML-void-DOMString-position-DOMString-text maybe?).

@domenic
Copy link
Member

domenic commented Jan 14, 2016

Ah excellent! Could you enable https://github.com/w3c/web-platform-tests/blob/master/domparsing/insert-adjacent.html as well (and make sure it passes)? And probably move your test into to-upstream/domparsing.

@kasperisager
Copy link
Contributor Author

Ah, I totally missed that one! Should make my test case obsolete then.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use this.ownerDocument.createElement("template") as the container. Also you should add some tests showing how using "_" fails for elements like thead, tr, td.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm reading the spec it seems it does something a bit more complicated, e.g. defaulting to body in certain cases. I think it's OK to just use template as it will be most correct, but be careful and check browsers. If there are some edge cases that neither "template" nor "body" cover, then it's OK, we can just leave a TODO.

@domenic
Copy link
Member

domenic commented Jan 14, 2016

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like "If context is null or a document, throw a DOMException with name "NoModificationAllowedError"." would be good to add a test for this too.

@domenic
Copy link
Member

domenic commented Jan 15, 2016

FYI the existing web platform test is failing; not sure exactly why...

@kasperisager
Copy link
Contributor Author

Read through the spec more thoroughly and re-did what I had copied from zombie. The tests pass on my end now 👍

@kasperisager
Copy link
Contributor Author

I should note that not all conditions of step 2 are checked.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So as-is context is unused. I think the spec is not going to be very useful for a guidance on this since it includes "invoking the fragment parsing algorithm with text as markup, and context as the context element" which I don't think we have the ability to do (yet). I made a note of that in #1316 (comment)

Instead I think you should remove context, and just check this.parentNode in line 442. Lines 542 and 460-462 can go away.

@domenic
Copy link
Member

domenic commented Jan 17, 2016

Thank you very much; lovely work! Merged as 33b9341.

@domenic domenic closed this Jan 17, 2016
@kasperisager
Copy link
Contributor Author

Cheers!

@RReverser
Copy link

Looks like this should be trivial to extend to support insertAdjacentText?

@Zirro
Copy link
Member

Zirro commented Sep 27, 2017

@RReverser We would certainly appreciate a pull request doing so, assuming it passes the relevant tests.

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.

4 participants