diff --git a/code-input.js b/code-input.js index 42b5ae2..0d2081f 100644 --- a/code-input.js +++ b/code-input.js @@ -831,8 +831,12 @@ var codeInput = { if (this.templateObject != undefined) { // Template registered before loading this.classList.add("code-input_registered"); - // Children not yet present - wait until they are - window.addEventListener("DOMContentLoaded", this.setup.bind(this)) + if (document.readyState === 'loading') { + // Children not yet present - wait until they are + window.addEventListener("DOMContentLoaded", this.setup.bind(this)) + } else { + this.setup(); + } } } @@ -859,7 +863,9 @@ var codeInput = { } disconnectedCallback() { - this.mutationObserver.disconnect(); + if (this.mutationObserver) { + this.mutationObserver.disconnect(); + } } /** diff --git a/tests/tester.js b/tests/tester.js index 80b54b3..7430990 100644 --- a/tests/tester.js +++ b/tests/tester.js @@ -217,6 +217,14 @@ console.log("I've got another line!", 2 < 3, "should be true.");`); console.log("I've got another line!", 2 < 3, "should be true."); `); // Extra newline so line numbers visible if enabled + const programmaticCodeInput = document.createElement("code-input"); + document.body.append(programmaticCodeInput); + programmaticCodeInput.focus(); + document.execCommand("insertText", false, "Hello, World!"); + assertEqual("Core", "Programmatically-created element JS-accessible value", programmaticCodeInput.value, "Hello, World!"); + await waitAsync(50); + assertEqual("Core", "Programmatically-created element rendered value", programmaticCodeInput.preElement.textContent, "Hello, World!\n"); + // Event Listener Tests // Function type listeners let numTimesInputCalled = 0;