@@ -16,12 +16,27 @@ var codeInput = {
16
16
*/
17
17
observedAttributes : [
18
18
"value" ,
19
- "name" ,
20
- "placeholder" ,
21
- "lang" ,
19
+ "placeholder" ,
20
+ "lang" ,
22
21
"template"
23
22
] ,
24
23
24
+ /**
25
+ * A list of attributes that will be moved to
26
+ * the textarea after they are applied on the
27
+ * code-input element.
28
+ */
29
+ textareaSyncAttributes : [
30
+ "value" ,
31
+ "name" ,
32
+ // Form validation - https://developer.mozilla.org/en-US/docs/Learn/Forms/Form_validation#using_built-in_form_validation
33
+ "required" ,
34
+ "minlength" , "maxlength" ,
35
+ "min" , "max" ,
36
+ "type" ,
37
+ "pattern"
38
+ ] ,
39
+
25
40
/* ------------------------------------
26
41
* ------------Templates---------------
27
42
* ------------------------------------ */
@@ -417,12 +432,16 @@ var codeInput = {
417
432
textarea . value = value ;
418
433
textarea . setAttribute ( "spellcheck" , "false" ) ;
419
434
420
- if ( this . getAttribute ( "name" ) ) {
421
- textarea . setAttribute ( "name" , this . getAttribute ( "name" ) ) ; // for use in forms
422
- }
423
-
424
- textarea . addEventListener ( 'input' , ( evt ) => { textarea . parentElement . update ( textarea . value ) ; textarea . parentElement . syncScroll ( ) ; } ) ;
425
- textarea . addEventListener ( 'scroll' , ( evt ) => textarea . parentElement . syncScroll ( ) ) ;
435
+ // Synchronise attributes to textarea
436
+ codeInput . textareaSyncAttributes . forEach ( ( attribute ) => {
437
+ if ( this . hasAttribute ( attribute ) ) {
438
+ textarea . setAttribute ( attribute , this . getAttribute ( attribute ) ) ;
439
+ }
440
+ } ) ;
441
+
442
+ textarea . addEventListener ( 'input' , ( evt ) => { textarea . parentElement . update ( textarea . value ) ; textarea . parentElement . sync_scroll ( ) ; } ) ;
443
+ textarea . addEventListener ( 'scroll' , ( evt ) => textarea . parentElement . sync_scroll ( ) ) ;
444
+
426
445
this . append ( textarea ) ;
427
446
428
447
// Create result element
@@ -485,7 +504,7 @@ var codeInput = {
485
504
* to `codeInput.CodeInput.attributeChangedCallback` when modified.
486
505
*/
487
506
static get observedAttributes ( ) {
488
- return codeInput . observedAttributes ;
507
+ return codeInput . observedAttributes . concat ( codeInput . textareaSyncAttributes ) ;
489
508
}
490
509
491
510
/**
@@ -503,13 +522,6 @@ var codeInput = {
503
522
case "value" :
504
523
this . update ( newValue ) ;
505
524
break ;
506
-
507
- case "name" :
508
- if ( this . querySelector ( "textarea" ) !== null ) {
509
- this . querySelector ( "textarea" ) . setAttribute ( "name" , newValue ) ;
510
- }
511
- break ;
512
-
513
525
case "placeholder" :
514
526
this . querySelector ( "textarea" ) . placeholder = newValue ;
515
527
break ;
@@ -554,6 +566,11 @@ var codeInput = {
554
566
555
567
this . update ( this . value ) ;
556
568
569
+ break ;
570
+ default :
571
+ if ( codeInput . textareaSyncAttributes . includes ( name ) ) {
572
+ this . querySelector ( "textarea" ) . setAttribute ( name , newValue ) ;
573
+ }
557
574
break ;
558
575
}
559
576
}
@@ -578,6 +595,18 @@ var codeInput = {
578
595
} else {
579
596
this . querySelector ( "textarea" ) . addEventListener ( "selectionchange" , boundCallback , options ) ;
580
597
}
598
+ } else if ( evt_name == "invalid" ) {
599
+ if ( thirdParameter === null ) {
600
+ this . querySelector ( "textarea" ) . addEventListener ( "invalid" , boundCallback ) ;
601
+ } else {
602
+ this . querySelector ( "textarea" ) . addEventListener ( "invalid" , boundCallback , thirdParameter ) ;
603
+ }
604
+ } else if ( evt_name == "input" ) {
605
+ if ( thirdParameter === null ) {
606
+ this . querySelector ( "textarea" ) . addEventListener ( "input" , boundCallback ) ;
607
+ } else {
608
+ this . querySelector ( "textarea" ) . addEventListener ( "input" , boundCallback , thirdParameter ) ;
609
+ }
581
610
} else {
582
611
super . addEventListener ( type , listener , options ) ;
583
612
}
@@ -635,7 +664,40 @@ var codeInput = {
635
664
return this . setAttribute ( "placeholder" , val ) ;
636
665
}
637
666
638
- /**
667
+ /* Form validation */
668
+ get validity ( ) {
669
+ return this . querySelector ( "textarea" ) . validity ;
670
+ }
671
+ get validationMessage ( ) {
672
+ return this . querySelector ( "textarea" ) . validationMessage ;
673
+ }
674
+ setCustomValidity ( error ) {
675
+ return this . querySelector ( "textarea" ) . setCustomValidity ( error ) ;
676
+ }
677
+ checkValidity ( ) {
678
+ return this . querySelector ( "textarea" ) . checkValidity ( ) ;
679
+ }
680
+ reportValidity ( ) {
681
+ return this . querySelector ( "textarea" ) . reportValidity ( ) ;
682
+ }
683
+
684
+
685
+ /* Sync all attributes that can be set on the `code-input` element to the `textarea` element */
686
+ setAttribute ( qualifiedName , value ) {
687
+ super . setAttribute ( qualifiedName , value ) ; // code-input
688
+ this . querySelector ( "textarea" ) . setAttribute ( qualifiedName , value ) ; // textarea
689
+ }
690
+
691
+ getAttribute ( qualifiedName ) {
692
+ if ( this . querySelector ( "textarea" ) == null ) {
693
+ return super . getAttribute ( qualifiedName ) ;
694
+ }
695
+ return this . querySelector ( "textarea" ) . getAttribute ( qualifiedName ) ; // textarea
696
+ }
697
+
698
+ pluginData = { } ; // For plugins to store element-specific data under their name, e.g. <code-input>.pluginData.specialChars
699
+
700
+ /**
639
701
* Allows plugins to store data in the scope of a single element.
640
702
* Key - name of the plugin
641
703
* Value - object of data to be stored; different plugins may use this differently.
0 commit comments