Skip to content

Commit bff0cca

Browse files
committed
Change code-input-fallback attribute to data-code-input-fallback for more predictable behaviour; allow value property to be set before registration
1 parent eea9cb3 commit bff0cca

File tree

7 files changed

+48
-31
lines changed

7 files changed

+48
-31
lines changed

code-input.css

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ code-input textarea, code-input pre {
9494

9595
/* Move the textarea in front of the result */
9696

97-
code-input textarea:not([code-input-fallback]) {
97+
code-input textarea:not([data-code-input-fallback]) {
9898
z-index: 1;
9999
}
100100
code-input pre {
@@ -103,7 +103,7 @@ code-input pre {
103103

104104
/* Make textarea almost completely transparent, except for caret and placeholder */
105105

106-
code-input textarea:not([code-input-fallback]) {
106+
code-input textarea:not([data-code-input-fallback]) {
107107
color: transparent;
108108
background: transparent;
109109
caret-color: inherit!important; /* Or choose your favourite color */
@@ -161,7 +161,7 @@ code-input:not(.code-input_registered) textarea {
161161
min-height: calc(100% - var(--padding, 16px) * 2 - 2em);
162162
}
163163

164-
code-input:not(.code-input_loaded) pre, code-input:not(.code-input_loaded) textarea:not([code-input-fallback]) {
164+
code-input:not(.code-input_loaded) pre, code-input:not(.code-input_loaded) textarea:not([data-code-input-fallback]) {
165165
opacity: 0;
166166
}
167167

@@ -218,26 +218,26 @@ code-input:has(pre[dir=rtl]) .code-input_dialog-container .code-input_keyboard-n
218218
right: 0;
219219
}
220220

221-
code-input:not(:has(textarea:not([code-input-fallback]):focus)) .code-input_dialog-container .code-input_keyboard-navigation-instructions,
221+
code-input:not(:has(textarea:not([data-code-input-fallback]):focus)) .code-input_dialog-container .code-input_keyboard-navigation-instructions,
222222
code-input.code-input_mouse-focused .code-input_dialog-container .code-input_keyboard-navigation-instructions,
223223
code-input .code-input_dialog-container .code-input_keyboard-navigation-instructions:empty {
224224
/* When not keyboard-focused / no instructions don't show instructions */
225225
display: none;
226226
}
227227

228228
/* Things with padding when instructions are present */
229-
code-input:not(:has(.code-input_keyboard-navigation-instructions:empty)):has(textarea:not([code-input-fallback]):focus):not(.code-input_mouse-focused) textarea,
230-
code-input:not(:has(.code-input_keyboard-navigation-instructions:empty)):has(textarea:not([code-input-fallback]):focus):not(.code-input_mouse-focused):not(.code-input_pre-element-styled) pre code,
231-
code-input:not(:has(.code-input_keyboard-navigation-instructions:empty)):has(textarea:not([code-input-fallback]):focus):not(.code-input_mouse-focused).code-input_pre-element-styled pre {
229+
code-input:not(:has(.code-input_keyboard-navigation-instructions:empty)):has(textarea:not([data-code-input-fallback]):focus):not(.code-input_mouse-focused) textarea,
230+
code-input:not(:has(.code-input_keyboard-navigation-instructions:empty)):has(textarea:not([data-code-input-fallback]):focus):not(.code-input_mouse-focused):not(.code-input_pre-element-styled) pre code,
231+
code-input:not(:has(.code-input_keyboard-navigation-instructions:empty)):has(textarea:not([data-code-input-fallback]):focus):not(.code-input_mouse-focused).code-input_pre-element-styled pre {
232232
padding-top: calc(var(--padding, 16px) + 3em)!important;
233233
}
234-
code-input:not(:has(.code-input_keyboard-navigation-instructions:empty)):has(textarea:not([code-input-fallback]):focus):not(.code-input_mouse-focused) textarea, code-input:not(:has(.code-input_keyboard-navigation-instructions:empty)):has(textarea:not([code-input-fallback]):focus):not(.code-input_mouse-focused):not(.code-input_pre-element-styled) pre code, code-input:not(:has(.code-input_keyboard-navigation-instructions:empty)):has(textarea:not([code-input-fallback]):focus):not(.code-input_mouse-focused).code-input_pre-element-styled pre {
234+
code-input:not(:has(.code-input_keyboard-navigation-instructions:empty)):has(textarea:not([data-code-input-fallback]):focus):not(.code-input_mouse-focused) textarea, code-input:not(:has(.code-input_keyboard-navigation-instructions:empty)):has(textarea:not([data-code-input-fallback]):focus):not(.code-input_mouse-focused):not(.code-input_pre-element-styled) pre code, code-input:not(:has(.code-input_keyboard-navigation-instructions:empty)):has(textarea:not([data-code-input-fallback]):focus):not(.code-input_mouse-focused).code-input_pre-element-styled pre {
235235
min-height: calc(100% - var(--padding, 16px) * 2 - 3em);
236236
}
237237

238238
/* No JavaScript fallback - styles to override all previous */
239239

240-
code-input textarea[code-input-fallback] {
240+
code-input textarea[data-code-input-fallback] {
241241
overflow: auto;
242242
background-color: inherit;
243243
color: inherit;

code-input.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ var codeInput = {
622622

623623
this.pluginEvt("beforeElementsAdded");
624624

625-
const fallbackTextarea = this.querySelector("textarea[code-input-fallback]");
625+
const fallbackTextarea = this.querySelector("textarea[data-code-input-fallback]");
626626
let value;
627627
if(fallbackTextarea) {
628628
// Fallback textarea exists
@@ -966,8 +966,18 @@ var codeInput = {
966966
* Get the text contents of the code-input element.
967967
*/
968968
get value() {
969-
// Get from editable textarea element
970-
return this.textareaElement.value;
969+
if(this.textareaElement) {
970+
// Get from editable textarea element
971+
return this.textareaElement.value;
972+
} else {
973+
// Unregistered
974+
const fallbackTextarea = this.querySelector("textarea[data-code-input-fallback]");
975+
if(fallbackTextarea) {
976+
return fallbackTextarea.value;
977+
} else {
978+
return this.innerHTML;
979+
}
980+
}
971981
}
972982
/**
973983
* Set the text contents of the code-input element.
@@ -977,13 +987,20 @@ var codeInput = {
977987
if (val === null || val === undefined) {
978988
val = "";
979989
}
980-
981-
// Save in editable textarea element
982-
this.textareaElement.value = val;
983-
// Trigger highlight
984-
this.scheduleHighlight();
985-
986-
return val;
990+
if(this.textareaElement) {
991+
// Save in editable textarea element
992+
this.textareaElement.value = val;
993+
// Trigger highlight
994+
this.scheduleHighlight();
995+
} else {
996+
// Unregistered
997+
const fallbackTextarea = this.querySelector("textarea[data-code-input-fallback]");
998+
if(fallbackTextarea) {
999+
fallbackTextarea.value = val;
1000+
} else {
1001+
this.innerHTML = val;
1002+
}
1003+
}
9871004
}
9881005

9891006
// Synchronise blur and focus

code-input.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/interface/forms/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ title = '`code-input` in HTML5 Forms'
1111
This is a good time to make use of the fallback textarea which is used when JavaScript is disabled; the following code will send data to the HTML5 form correctly whether or not JavaScript is enabled:
1212
```html
1313
<form>
14-
<code-input><textarea code-input-fallback name="code"></textarea></code-input>
14+
<code-input><textarea data-code-input-fallback name="code"></textarea></code-input>
1515
<input type="submit"/>
1616
</form>
1717
```

tests/hljs.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ <h4><a href="prism.html">Test for Prism.js</a></h4>
4242

4343
<details id="collapse-results"><summary>Test Results (Click to Open)</summary><pre id="test-results"></pre></details>
4444
<form method="GET" action="https://google.com/search" target="_blank">
45-
<code-input><textarea code-input-fallback name="q">console.log("Hello, World!");
45+
<code-input><textarea data-code-input-fallback name="q">console.log("Hello, World!");
4646
// A second line
4747
// A third line with &lt;html> tags</textarea></code-input>
4848
<input type="submit" value="Search Google For Code"/>

0 commit comments

Comments
 (0)