Skip to content

Commit af8d152

Browse files
authored
Merge pull request #184 from MattiasBuelens/fix-add-after-load
Fix adding element after DOM is already loaded
2 parents f4d4067 + 960fc70 commit af8d152

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

code-input.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -831,8 +831,12 @@ var codeInput = {
831831
if (this.templateObject != undefined) {
832832
// Template registered before loading
833833
this.classList.add("code-input_registered");
834-
// Children not yet present - wait until they are
835-
window.addEventListener("DOMContentLoaded", this.setup.bind(this))
834+
if (document.readyState === 'loading') {
835+
// Children not yet present - wait until they are
836+
window.addEventListener("DOMContentLoaded", this.setup.bind(this))
837+
} else {
838+
this.setup();
839+
}
836840
}
837841
}
838842

@@ -859,7 +863,9 @@ var codeInput = {
859863
}
860864

861865
disconnectedCallback() {
862-
this.mutationObserver.disconnect();
866+
if (this.mutationObserver) {
867+
this.mutationObserver.disconnect();
868+
}
863869
}
864870

865871
/**

tests/tester.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,14 @@ console.log("I've got another line!", 2 < 3, "should be true.");`);
217217
console.log("I've got another line!", 2 &lt; 3, "should be true.");
218218
`); // Extra newline so line numbers visible if enabled
219219

220+
const programmaticCodeInput = document.createElement("code-input");
221+
document.body.append(programmaticCodeInput);
222+
programmaticCodeInput.focus();
223+
document.execCommand("insertText", false, "Hello, World!");
224+
assertEqual("Core", "Programmatically-created element JS-accessible value", programmaticCodeInput.value, "Hello, World!");
225+
await waitAsync(50);
226+
assertEqual("Core", "Programmatically-created element rendered value", programmaticCodeInput.preElement.textContent, "Hello, World!\n");
227+
220228
// Event Listener Tests
221229
// Function type listeners
222230
let numTimesInputCalled = 0;

0 commit comments

Comments
 (0)