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

#451 fixes missing inner html in html template #506

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -3,6 +3,8 @@ import HTMLElement from '../html-element/HTMLElement';
import IDocumentFragment from '../document-fragment/IDocumentFragment';
import INode from '../node/INode';
import IHTMLTemplateElement from './IHTMLTemplateElement';
import XMLSerializer from '../../xml-serializer/XMLSerializer';
import XMLParser from '../../xml-parser/XMLParser';

/**
* HTML Template Element.
Expand All @@ -13,6 +15,35 @@ import IHTMLTemplateElement from './IHTMLTemplateElement';
export default class HTMLTemplateElement extends HTMLElement implements IHTMLTemplateElement {
private _contentElement: IDocumentFragment = null;

/**
* Sets inner HTML.
*
* @param html HTML.
*/
public set innerHTML(html: string) {
for (const child of this.content.childNodes.slice()) {
this.content.removeChild(child);
}

for (const node of XMLParser.parse(this.ownerDocument, html).childNodes.slice()) {
this.content.appendChild(node);
}
}

/**
* 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;
}

/**
* Returns the content.
*
Expand Down
@@ -0,0 +1,29 @@
import Window from '../../../src/window/Window';
import Document from '../../../src/nodes/document/Document';
import HTMLTemplateElement from '../../../src/nodes/html-template-element/HTMLTemplateElement';

describe('HTMLTemplateElement', () => {
let window: Window;
let document: Document;
let element: HTMLTemplateElement;

beforeEach(() => {
window = new Window();
document = window.document;
element = <HTMLTemplateElement>document.createElement('template');
});

afterEach(() => {
jest.restoreAllMocks();
});

it('InnerHTML', () => {
const div = '<div>happy-dom is cool!</div>';
expect(element.content.childNodes.length).toBe(0);
element.innerHTML = div;
expect(element.innerHTML).toBe(div);
expect(element.content.childNodes.length).toBe(1);
element.innerHTML = '';
expect(element.content.childNodes.length).toBe(0);
});
});