Skip to content

Commit

Permalink
Merge pull request #205 from paed01/tmp/custom-element-cb
Browse files Browse the repository at this point in the history
fix custom element connectedCallback
  • Loading branch information
joelabrahamsson authored Dec 14, 2023
2 parents 55d3ae2 + e235b3a commit f5d79b1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
17 changes: 17 additions & 0 deletions app/assets/public/custom-element.html
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ <h2 id="headline">Test custom element</h2>
<button id="clickme" type="submit">OK</button>
<button id="resetme" type="reset">Abort</button>
</waiting-button>
<x-greeter>
<div class="greeting"></div>
<button class="greet" type="button">Greet me</button>
</x-greeter>
</form>
<script>
class Greeter extends HTMLElement {
connectedCallback() {
const btn = this.querySelector(".greet");
const greeting = this.querySelector(".greeting");
btn.addEventListener("click", () => {
greeting.innerHTML = "<p>Hello</p>";
});
greeting.innerHTML = "<p>No greeting yet</p>";
}
}
window.customElements.define("x-greeter", Greeter);
</script>
</body>
</html>
11 changes: 10 additions & 1 deletion lib/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,24 @@ module.exports = class Document extends Node {
if ($elm.nodeType) $elm = $($elm);
if (!$elm.length) return;

const tag = $elm[0].name;

const loaded = this[loadedSymbol];
let mockElement = loaded.find((mockedElm) => mockedElm.$elm[0] === $elm[0]);

let mockElement = loaded.find((mockedElm) => mockedElm?.$elm[0] === $elm[0]);

if (mockElement) {
return mockElement;
}

mockElement = this[kElementFactory].create($elm);

loaded.push(mockElement);

if (this[kElementFactory].isCustom(tag) && mockElement.connectedCallback) {
mockElement.connectedCallback();
}

return mockElement;
}
};
8 changes: 5 additions & 3 deletions lib/elementFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ module.exports = class ElementFactory {
if (tagName in definitions) return new definitions[tagName](document, $elm);
const custom = this.custom;
if (tagName in custom) {
const elm = new custom[tagName](document, $elm);
if (elm.connectedCallback) elm.connectedCallback();
return elm;
return new custom[tagName](document, $elm);
}

return new HTMLElement(document, $elm);
Expand All @@ -70,4 +68,8 @@ module.exports = class ElementFactory {
get(name) {
return this.custom[name];
}

isCustom(name) {
return !!this.custom[name];
}
};
13 changes: 13 additions & 0 deletions test/custom-elements-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,17 @@ describe("Custom elements", () => {
page.window.customElements.define("me-button", class Me {});
}).to.throw(DOMException, "Failed to execute 'define' on 'CustomElementRegistry': the name \"me-button\" has already been used with this registry");
});

it("allows custom element to set innerHTML on child", async () => {
const page = await browser.navigateTo("/custom-element.html");
page.runScripts();

const greeting = page.document.getElementsByClassName("greeting")[0];
expect(greeting.innerHTML).to.equal("<p>No greeting yet</p>");

const btn = page.document.getElementsByClassName("greet")[0];
btn.click();

expect(greeting.innerHTML).to.equal("<p>Hello</p>");
});
});

0 comments on commit f5d79b1

Please sign in to comment.