From 86f00b9eceec709fac5fc7353d5fbb1dfbeddab5 Mon Sep 17 00:00:00 2001 From: Oliver Geer Date: Fri, 3 Jul 2026 00:56:38 +0100 Subject: [PATCH] Fix bugs and add test for language attribute (Fixes #228) --- code-input.js | 14 ++++++++++---- tests/tester.js | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/code-input.js b/code-input.js index 5eb6ff0..b02c381 100644 --- a/code-input.js +++ b/code-input.js @@ -986,16 +986,16 @@ var codeInput = { /* Check regular attributes */ for(let i = 0; i < codeInput.observedAttributes.length; i++) { if (mutation.attributeName == codeInput.observedAttributes[i]) { - return this.attributeChangedCallback(mutation.attributeName, mutation.oldValue, super.getAttribute(mutation.attributeName)); + this.attributeChangedCallback(mutation.attributeName, mutation.oldValue, super.getAttribute(mutation.attributeName)); } } for(let i = 0; i < codeInput.textareaSyncAttributes.length; i++) { if (mutation.attributeName == codeInput.textareaSyncAttributes[i]) { - return this.attributeChangedCallback(mutation.attributeName, mutation.oldValue, super.getAttribute(mutation.attributeName)); + this.attributeChangedCallback(mutation.attributeName, mutation.oldValue, super.getAttribute(mutation.attributeName)); } } if (mutation.attributeName.substring(0, 5) == "aria-") { - return this.attributeChangedCallback(mutation.attributeName, mutation.oldValue, super.getAttribute(mutation.attributeName)); + this.attributeChangedCallback(mutation.attributeName, mutation.oldValue, super.getAttribute(mutation.attributeName)); } } } @@ -1059,7 +1059,13 @@ var codeInput = { } if (mainTextarea.placeholder == oldValue || oldValue == null && mainTextarea.placeholder == "") { - mainTextarea.placeholder = newValue; + if(newValue === null) { + mainTextarea.removeAttribute("placeholder"); + // If always setAttribute, would set it to the string + // "null" here, which isn't wanted.' + } else { + mainTextarea.setAttribute("placeholder", newValue); + } } this.scheduleHighlight(); diff --git a/tests/tester.js b/tests/tester.js index 96bfdd4..f619b12 100644 --- a/tests/tester.js +++ b/tests/tester.js @@ -491,6 +491,25 @@ console.log("I've got another line!", 2 < 3, "should be true."); codeInputElement.style.removeProperty("caret-color"); + if(!isHLJS) { + // These tests require autodetect plugin to be absent + + codeInputElement.setAttribute("language", "css"); + await waitAsync(50); // Wait for language to propogate to class + testAssertion("Core", "Language Class Propogates", codeInputElement.querySelector("pre").classList.contains("language-css"), `Class name of pre element was "${codeInputElement.querySelector("pre").className}" but code-input element had language="css"`); + + window.requestAnimationFrame(function() { + codeInputElement.setAttribute("placeholder", "Gimme some HTML!"); + codeInputElement.setAttribute("language", "html"); + }); + await waitAsync(500); // Wait for animation frame; language to propogate to class + testAssertion("Core", "Language Class Propogates When Set Immediately After Placeholder Set", codeInputElement.querySelector("pre").classList.contains("language-html"), `Class name of pre element was "${codeInputElement.querySelector("pre").className}" but code-input element had language="css"`); + + codeInputElement.removeAttribute("placeholder"); + codeInputElement.setAttribute("language", "JavaScript"); + await waitAsync(50); // Wait for language to propogate to class + } + /*--- Tests for plugins ---*/ // AutoCloseBrackets testAddingText("AutoCloseBrackets", textarea, function(textarea) {