question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

innerHTML broken for template elements

See original GitHub issue

Test case:

const t = document.createElement('template');
t.innerHTML = '<div>happy-dom is cool!</div>';
console.log(JSON.stringify(t.innerHTML));

Output in

  • Chrome: "<div>happy-dom is cool!</div>"
  • jsdom: "<div>happy-dom is cool!</div>"
  • linkedom: "<div>happy-dom is cool!</div>"
  • happy-dom: ""

Expectation

happy-dom should behave like every other tested DOM.

Motivation

This breaks the integration into solid-register, which uses a dom on top of node to test solid code fast.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
Mas0nShicommented, Apr 8, 2022

get innerHTML from this.childNodes in the Element.ts

https://github.com/capricorn86/happy-dom/blob/faaaa2c90b923a20ccfce1af7e0a514e3d13357b/packages/happy-dom/src/nodes/element/Element.ts#L170-L177

but the div element not in this.childNodes. it’s in this.content.childNodes.

https://github.com/capricorn86/happy-dom/blob/faaaa2c90b923a20ccfce1af7e0a514e3d13357b/packages/happy-dom/src/nodes/html-template-element/HTMLTemplateElement.ts#L21-L26

so we should overwrite innerHTML like this(maybe error for my understand to sources code):

add code in HTMLTemplateElement.ts

	/**
	 * Returns inner HTML.
	 *
	 * @returns HTML.
	 */
	public get innerHTML(): string {
		const xmlSerializer = new XMLSerializer();
		let xml = '';
		for (const node of this.content.childNodes) {
			xml += xmlSerializer.serializeToString(node);
		}
		return xml;
	}
	/**
	 * Sets inner HTML.
	 *
	 * @param html HTML.
	 */
	public set innerHTML(html: string) {
		super.innerHTML = html;
	}

@capricorn86 Is this a good answer?

2reactions
capricorn86commented, Jun 30, 2022

We finally have a fix in place that should resolve the remaining issues related to the HTMLTemplateElement 🙂

You can read more about the release here: https://github.com/capricorn86/happy-dom/releases/tag/v6.0.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

template tag innerHTML breaks script tags - Stack Overflow
I'm trying to run a string of HTML through the HTML parser which contains <tr> elements. Using the normal HTML parser the <tr>...
Read more >
Element.innerHTML - Web APIs | MDN
The Element property innerHTML gets or sets the HTML or XML markup ... If the element whose contents are being replaced is a...
Read more >
innerHTML on non <template> elements does correctly upgrade ...
The polyfill only patches innerHTML on elements. This means that setting innerHTML on a non-template element that includes a nested element will fail...
Read more >
HTML DOM Element getElementsByClassName() - W3Schools
Definition and Usage. The getElementsByClassName() method returns a collection of child elements with a given class name. The getElementsByClassName() method ...
Read more >
div innerHTML with standard lightning web components
You cannot create/use innerHTML for custom components ( c or lightning namespace) in LWC but you can do so for native elements.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found