@@ -16,8 +16,8 @@ var codeInput = {
16
16
*/
17
17
observedAttributes : [
18
18
"value" ,
19
- "placeholder" ,
20
- "lang" ,
19
+ "placeholder" ,
20
+ "lang" ,
21
21
"template"
22
22
] ,
23
23
@@ -37,6 +37,18 @@ var codeInput = {
37
37
"pattern"
38
38
] ,
39
39
40
+ /**
41
+ * A list of events whose listeners will be moved to
42
+ * the textarea after they are added to the
43
+ * code-input element.
44
+ */
45
+ textareaSyncEvents : [
46
+ "change" ,
47
+ "selectionchange" ,
48
+ "invalid" ,
49
+ "input"
50
+ ] ,
51
+
40
52
/* ------------------------------------
41
53
* ------------Templates---------------
42
54
* ------------------------------------ */
@@ -364,7 +376,7 @@ var codeInput = {
364
376
365
377
this . pluginEvt ( "afterHighlight" ) ;
366
378
}
367
-
379
+
368
380
/**
369
381
* Synchronise the scrolling of the textarea to the result element.
370
382
*/
@@ -408,7 +420,7 @@ var codeInput = {
408
420
return undefined ;
409
421
}
410
422
}
411
-
423
+
412
424
/**
413
425
* Set up and initialise the textarea.
414
426
* This will be called once the template has been added.
@@ -434,14 +446,14 @@ var codeInput = {
434
446
435
447
// Synchronise attributes to textarea
436
448
codeInput . textareaSyncAttributes . forEach ( ( attribute ) => {
437
- if ( this . hasAttribute ( attribute ) ) {
449
+ if ( this . hasAttribute ( attribute ) ) {
438
450
textarea . setAttribute ( attribute , this . getAttribute ( attribute ) ) ;
439
451
}
440
452
} ) ;
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
-
453
+
454
+ textarea . addEventListener ( 'input' , ( evt ) => { textarea . parentElement . update ( textarea . value ) ; textarea . parentElement . sync_scroll ( ) ; } ) ;
455
+ textarea . addEventListener ( 'scroll' , ( evt ) => textarea . parentElement . sync_scroll ( ) ) ;
456
+
445
457
this . append ( textarea ) ;
446
458
447
459
// Create result element
@@ -504,7 +516,7 @@ var codeInput = {
504
516
* to `codeInput.CodeInput.attributeChangedCallback` when modified.
505
517
*/
506
518
static get observedAttributes ( ) {
507
- return codeInput . observedAttributes . concat ( codeInput . textareaSyncAttributes ) ;
519
+ return codeInput . observedAttributes . concat ( codeInput . textareaSyncAttributes ) ;
508
520
}
509
521
510
522
/**
@@ -568,47 +580,40 @@ var codeInput = {
568
580
569
581
break ;
570
582
default :
571
- if ( codeInput . textareaSyncAttributes . includes ( name ) ) {
583
+ if ( codeInput . textareaSyncAttributes . includes ( name ) ) {
572
584
this . querySelector ( "textarea" ) . setAttribute ( name , newValue ) ;
573
585
}
574
586
break ;
575
587
}
576
588
}
577
589
578
590
}
579
-
591
+
592
+ /* ------------------------------------
593
+ * -----------Overrides----------------
594
+ * ------------------------------------
595
+ * Override/Implement ordinary HTML textarea functionality so that the <code-input>
596
+ * element acts just like a <textarea>. */
597
+
580
598
/**
581
599
* @override
582
600
*/
583
601
addEventListener ( type , listener , options = undefined ) {
584
602
let boundCallback = listener . bind ( this ) ;
585
603
this . boundEventCallbacks [ listener ] = boundCallback ;
586
- if ( type == "change" ) {
604
+
605
+ if ( codeInput . textareaSyncEvents . includes ( type ) ) {
587
606
if ( options === undefined ) {
588
- this . querySelector ( "textarea" ) . addEventListener ( "change" , boundCallback ) ;
607
+ this . querySelector ( "textarea" ) . addEventListener ( type , boundCallback ) ;
589
608
} else {
590
- this . querySelector ( "textarea" ) . addEventListener ( "change" , boundCallback , options ) ;
609
+ this . querySelector ( "textarea" ) . addEventListener ( type , boundCallback , options ) ;
591
610
}
592
- } else if ( type == "selectionchange" ) {
611
+ } else {
593
612
if ( options === undefined ) {
594
- this . querySelector ( "textarea" ) . addEventListener ( "selectionchange" , boundCallback ) ;
613
+ super . addEventListener ( type , boundCallback ) ;
595
614
} else {
596
- this . querySelector ( "textarea" ) . addEventListener ( "selectionchange" , boundCallback , options ) ;
615
+ super . addEventListener ( type , boundCallback , options ) ;
597
616
}
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
- }
610
- } else {
611
- super . addEventListener ( type , listener , options ) ;
612
617
}
613
618
}
614
619
@@ -647,7 +652,7 @@ var codeInput = {
647
652
set value ( val ) {
648
653
return this . setAttribute ( "value" , val ) ;
649
654
}
650
-
655
+
651
656
/**
652
657
* Get the placeholder of the code-input element that appears
653
658
* when no code has been entered.
@@ -664,40 +669,73 @@ var codeInput = {
664
669
return this . setAttribute ( "placeholder" , val ) ;
665
670
}
666
671
667
- /* Form validation */
672
+ /**
673
+ * Returns a ValidityState object that represents the validity states of an element.
674
+ *
675
+ * See `HTMLTextAreaElement.validity`
676
+ */
668
677
get validity ( ) {
669
678
return this . querySelector ( "textarea" ) . validity ;
670
679
}
680
+
681
+ /**
682
+ * Returns the error message that would be displayed if the user submits the form, or an empty string if no error message.
683
+ * It also triggers the standard error message, such as "this is a required field". The result is that the user sees validation
684
+ * messages without actually submitting.
685
+ *
686
+ * See `HTMLTextAreaElement.validationMessage`
687
+ */
671
688
get validationMessage ( ) {
672
689
return this . querySelector ( "textarea" ) . validationMessage ;
673
690
}
691
+
692
+ /**
693
+ * Sets a custom error message that is displayed when a form is submitted.
694
+ *
695
+ * See `HTMLTextAreaElement.setCustomValidity`
696
+ * @param error Sets a custom error message that is displayed when a form is submitted.
697
+ */
674
698
setCustomValidity ( error ) {
675
699
return this . querySelector ( "textarea" ) . setCustomValidity ( error ) ;
676
700
}
701
+
702
+ /**
703
+ * Returns whether a form will validate when it is submitted,
704
+ * without having to submit it.
705
+ *
706
+ * See `HTMLTextAreaElement.checkValidity`
707
+ */
677
708
checkValidity ( ) {
678
709
return this . querySelector ( "textarea" ) . checkValidity ( ) ;
679
710
}
711
+
712
+ /**
713
+ * See `HTMLTextAreaElement.reportValidity`
714
+ */
680
715
reportValidity ( ) {
681
716
return this . querySelector ( "textarea" ) . reportValidity ( ) ;
682
717
}
683
718
684
719
685
- /* Sync all attributes that can be set on the `code-input` element to the `textarea` element */
720
+ /**
721
+ * @override
722
+ */
686
723
setAttribute ( qualifiedName , value ) {
687
724
super . setAttribute ( qualifiedName , value ) ; // code-input
688
725
this . querySelector ( "textarea" ) . setAttribute ( qualifiedName , value ) ; // textarea
689
726
}
690
727
728
+ /**
729
+ * @override
730
+ */
691
731
getAttribute ( qualifiedName ) {
692
- if ( this . querySelector ( "textarea" ) == null ) {
732
+ if ( this . querySelector ( "textarea" ) == null ) {
693
733
return super . getAttribute ( qualifiedName ) ;
694
734
}
695
735
return this . querySelector ( "textarea" ) . getAttribute ( qualifiedName ) ; // textarea
696
736
}
697
737
698
- pluginData = { } ; // For plugins to store element-specific data under their name, e.g. <code-input>.pluginData.specialChars
699
-
700
- /**
738
+ /**
701
739
* Allows plugins to store data in the scope of a single element.
702
740
* Key - name of the plugin
703
741
* Value - object of data to be stored; different plugins may use this differently.
0 commit comments