4
4
5
5
var codeInput = {
6
6
observedAttributes : [ // Doesn't include events, as they are transferred by overriding {add/remove}EventListener
7
- "value" ,
8
- "name" ,
7
+ "value" ,
9
8
"placeholder" ,
10
9
"lang" ,
11
10
"template"
12
11
] ,
12
+
13
+ textareaSyncAttributes : [ // Attributes to move to the textarea after they are applied on the code-input element.
14
+ "value" ,
15
+ "name" ,
16
+ // Form validation - https://developer.mozilla.org/en-US/docs/Learn/Forms/Form_validation#using_built-in_form_validation
17
+ "required" ,
18
+ "minlength" , "maxlength" ,
19
+ "min" , "max" ,
20
+ "type" ,
21
+ "pattern"
22
+ ] ,
13
23
// Attributes to monitor - needs to be global and static
14
24
15
25
/* Templates */
@@ -151,10 +161,13 @@ var codeInput = {
151
161
textarea . placeholder = placeholder ;
152
162
textarea . value = value ;
153
163
textarea . setAttribute ( "spellcheck" , "false" ) ;
154
-
155
- if ( this . getAttribute ( "name" ) ) {
156
- textarea . setAttribute ( "name" , this . getAttribute ( "name" ) ) ; // for use in forms
157
- }
164
+
165
+ // Synchronise attributes to textarea
166
+ codeInput . textareaSyncAttributes . forEach ( ( attribute ) => {
167
+ if ( this . hasAttribute ( attribute ) ) {
168
+ textarea . setAttribute ( attribute , this . getAttribute ( attribute ) ) ;
169
+ }
170
+ } ) ;
158
171
159
172
textarea . addEventListener ( 'input' , ( evt ) => { textarea . parentElement . update ( textarea . value ) ; textarea . parentElement . sync_scroll ( ) ; } ) ;
160
173
textarea . addEventListener ( 'scroll' , ( evt ) => textarea . parentElement . sync_scroll ( ) ) ;
@@ -186,7 +199,7 @@ var codeInput = {
186
199
if ( this . template != undefined ) this . setup ( ) ;
187
200
}
188
201
static get observedAttributes ( ) {
189
- return codeInput . observedAttributes ;
202
+ return codeInput . observedAttributes . concat ( codeInput . textareaSyncAttributes ) ;
190
203
}
191
204
192
205
attributeChangedCallback ( name , oldValue , newValue ) {
@@ -200,12 +213,6 @@ var codeInput = {
200
213
case "value" :
201
214
this . update ( newValue ) ;
202
215
break ;
203
-
204
- case "name" :
205
- if ( this . querySelector ( "textarea" ) !== null ) {
206
- this . querySelector ( "textarea" ) . setAttribute ( "name" , newValue ) ; // for use in forms
207
- }
208
- break ;
209
216
210
217
case "placeholder" :
211
218
this . querySelector ( "textarea" ) . placeholder = newValue ;
@@ -251,6 +258,11 @@ var codeInput = {
251
258
252
259
this . update ( this . value ) ;
253
260
261
+ break ;
262
+ default :
263
+ if ( codeInput . textareaSyncAttributes . includes ( name ) ) {
264
+ this . querySelector ( "textarea" ) . setAttribute ( name , newValue ) ;
265
+ }
254
266
break ;
255
267
}
256
268
}
@@ -285,6 +297,18 @@ var codeInput = {
285
297
} else {
286
298
this . querySelector ( "textarea" ) . addEventListener ( "selectionchange" , boundCallback , thirdParameter ) ;
287
299
}
300
+ } else if ( evt_name == "invalid" ) {
301
+ if ( thirdParameter === null ) {
302
+ this . querySelector ( "textarea" ) . addEventListener ( "invalid" , boundCallback ) ;
303
+ } else {
304
+ this . querySelector ( "textarea" ) . addEventListener ( "invalid" , boundCallback , thirdParameter ) ;
305
+ }
306
+ } else if ( evt_name == "input" ) {
307
+ if ( thirdParameter === null ) {
308
+ this . querySelector ( "textarea" ) . addEventListener ( "input" , boundCallback ) ;
309
+ } else {
310
+ this . querySelector ( "textarea" ) . addEventListener ( "input" , boundCallback , thirdParameter ) ;
311
+ }
288
312
}
289
313
}
290
314
@@ -321,6 +345,37 @@ var codeInput = {
321
345
return this . setAttribute ( "placeholder" , val ) ;
322
346
}
323
347
348
+ /* Form validation */
349
+ get validity ( ) {
350
+ return this . querySelector ( "textarea" ) . validity ;
351
+ }
352
+ get validationMessage ( ) {
353
+ return this . querySelector ( "textarea" ) . validationMessage ;
354
+ }
355
+ setCustomValidity ( error ) {
356
+ return this . querySelector ( "textarea" ) . setCustomValidity ( error ) ;
357
+ }
358
+ checkValidity ( ) {
359
+ return this . querySelector ( "textarea" ) . checkValidity ( ) ;
360
+ }
361
+ reportValidity ( ) {
362
+ return this . querySelector ( "textarea" ) . reportValidity ( ) ;
363
+ }
364
+
365
+
366
+ /* Sync all attributes that can be set on the `code-input` element to the `textarea` element */
367
+ setAttribute ( qualifiedName , value ) {
368
+ super . setAttribute ( qualifiedName , value ) ; // code-input
369
+ this . querySelector ( "textarea" ) . setAttribute ( qualifiedName , value ) ; // textarea
370
+ }
371
+
372
+ getAttribute ( qualifiedName ) {
373
+ if ( this . querySelector ( "textarea" ) == null ) {
374
+ return super . getAttribute ( qualifiedName ) ;
375
+ }
376
+ return this . querySelector ( "textarea" ) . getAttribute ( qualifiedName ) ; // textarea
377
+ }
378
+
324
379
pluginData = { } ; // For plugins to store element-specific data under their name, e.g. <code-input>.pluginData.specialChars
325
380
} ,
326
381
0 commit comments