44
55var codeInput = {
66 observedAttributes : [ // Doesn't include events, as they are transferred by overriding {add/remove}EventListener
7- "value" ,
8- "name" ,
7+ "value" ,
98 "placeholder" ,
109 "lang" ,
1110 "template"
1211 ] ,
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+ ] ,
1323 // Attributes to monitor - needs to be global and static
1424
1525 /* Templates */
@@ -151,10 +161,13 @@ var codeInput = {
151161 textarea . placeholder = placeholder ;
152162 textarea . value = value ;
153163 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+ } ) ;
158171
159172 textarea . addEventListener ( 'input' , ( evt ) => { textarea . parentElement . update ( textarea . value ) ; textarea . parentElement . sync_scroll ( ) ; } ) ;
160173 textarea . addEventListener ( 'scroll' , ( evt ) => textarea . parentElement . sync_scroll ( ) ) ;
@@ -186,7 +199,7 @@ var codeInput = {
186199 if ( this . template != undefined ) this . setup ( ) ;
187200 }
188201 static get observedAttributes ( ) {
189- return codeInput . observedAttributes ;
202+ return codeInput . observedAttributes . concat ( codeInput . textareaSyncAttributes ) ;
190203 }
191204
192205 attributeChangedCallback ( name , oldValue , newValue ) {
@@ -200,12 +213,6 @@ var codeInput = {
200213 case "value" :
201214 this . update ( newValue ) ;
202215 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 ;
209216
210217 case "placeholder" :
211218 this . querySelector ( "textarea" ) . placeholder = newValue ;
@@ -251,6 +258,11 @@ var codeInput = {
251258
252259 this . update ( this . value ) ;
253260
261+ break ;
262+ default :
263+ if ( codeInput . textareaSyncAttributes . includes ( name ) ) {
264+ this . querySelector ( "textarea" ) . setAttribute ( name , newValue ) ;
265+ }
254266 break ;
255267 }
256268 }
@@ -285,6 +297,18 @@ var codeInput = {
285297 } else {
286298 this . querySelector ( "textarea" ) . addEventListener ( "selectionchange" , boundCallback , thirdParameter ) ;
287299 }
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+ }
288312 }
289313 }
290314
@@ -321,6 +345,37 @@ var codeInput = {
321345 return this . setAttribute ( "placeholder" , val ) ;
322346 }
323347
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+
324379 pluginData = { } ; // For plugins to store element-specific data under their name, e.g. <code-input>.pluginData.specialChars
325380 } ,
326381
0 commit comments