@@ -29,7 +29,6 @@ var codeInput = {
2929 * code-input element.
3030 */
3131 textareaSyncAttributes : [
32- "aria-*" ,
3332 "value" ,
3433 // Form validation - https://developer.mozilla.org/en-US/docs/Learn/Forms/Form_validation#using_built-in_form_validation
3534 "min" , "max" ,
@@ -124,7 +123,7 @@ var codeInput = {
124123 elem = codeInput . templateNotYetRegisteredQueue [ templateName ] [ i ] ;
125124 elem . template = template ;
126125 codeInput . runOnceWindowLoaded ( ( function ( elem ) { elem . connectedCallback ( ) ; } ) . bind ( null , elem ) , elem ) ;
127- // Bind sets elem in parameter
126+ // Bind sets elem as first parameter of function
128127 // So innerHTML can be read
129128 }
130129 console . log ( `code-input: template: Added existing elements with template ${ templateName } ` ) ;
@@ -138,7 +137,7 @@ var codeInput = {
138137 elem = codeInput . templateNotYetRegisteredQueue [ undefined ] [ i ] ;
139138 elem . template = template ;
140139 codeInput . runOnceWindowLoaded ( ( function ( elem ) { elem . connectedCallback ( ) ; } ) . bind ( null , elem ) , elem ) ;
141- // Bind sets elem in parameter
140+ // Bind sets elem as first parameter of function
142141 // So innerHTML can be read
143142 }
144143 }
@@ -257,9 +256,7 @@ var codeInput = {
257256 } ,
258257
259258 /**
260- * Constructor to create a proof-of-concept template that gives a message if too many characters are typed.
261- * @param {codeInput.Plugin[] } plugins - An array of plugin objects to add extra features - see `codeInput.plugins`
262- * @returns template object
259+ * @deprecated Make your own version of this template if you need it - we think it isn't widely used so will remove it from the next version of code-input.
263260 */
264261 characterLimit ( plugins ) {
265262 return {
@@ -283,11 +280,7 @@ var codeInput = {
283280 } ,
284281
285282 /**
286- * Constructor to create a proof-of-concept template that shows text in a repeating series of colors.
287- * @param {string[] } rainbowColors - An array of CSS colors, in the order each color will be shown
288- * @param {string } delimiter - The character used to split up parts of text where each part is a different colour (e.g. "" = characters, " " = words)
289- * @param {codeInput.Plugin[] } plugins - An array of plugin objects to add extra features - see `codeInput.plugins`
290- * @returns template object
283+ * @deprecated Make your own version of this template if you need it - we think it isn't widely used so will remove it from the next version of code-input.
291284 */
292285 rainbowText ( rainbowColors = [ "red" , "orangered" , "orange" , "goldenrod" , "gold" , "green" , "darkgreen" , "navy" , "blue" , "magenta" ] , delimiter = "" , plugins = [ ] ) {
293286 return {
@@ -311,13 +304,13 @@ var codeInput = {
311304 } ,
312305
313306 /**
314- * @deprecated Please use `codeInput.characterLimit(plugins)`
307+ * @deprecated Make your own version of this template if you need it - we think it isn't widely used so will remove it from the next version of code-input.
315308 */
316309 character_limit ( ) {
317310 return this . characterLimit ( [ ] ) ;
318311 } ,
319312 /**
320- * @deprecated Please use `codeInput.rainbowText`
313+ * @deprecated Make your own version of this template if you need it - we think it isn't widely used so will remove it from the next version of code-input.
321314 */
322315 rainbow_text ( rainbowColors = [ "red" , "orangered" , "orange" , "goldenrod" , "gold" , "green" , "darkgreen" , "navy" , "blue" , "magenta" ] , delimiter = "" , plugins = [ ] ) {
323316 return this . rainbowText ( rainbowColors , delimiter , plugins ) ;
@@ -379,14 +372,7 @@ var codeInput = {
379372 console . log ( "code-input: plugin: Created plugin" ) ;
380373
381374 observedAttributes . forEach ( ( attribute ) => {
382- // Move plugin attribute to codeInput observed attributes
383- let regexFromWildcard = codeInput . wildcard2regex ( attribute ) ;
384- if ( regexFromWildcard == null ) {
385- // Not a wildcard
386- codeInput . observedAttributes . push ( attribute ) ;
387- } else {
388- codeInput . observedAttributes . regexp . push ( regexFromWildcard ) ;
389- }
375+ codeInput . observedAttributes . push ( attribute ) ;
390376 } ) ;
391377 }
392378
@@ -495,7 +481,7 @@ var codeInput = {
495481 needsHighlight = false ; // Just inputted
496482
497483 /**
498- * Highlight the code ASAP
484+ * Highlight the code as soon as possible
499485 */
500486 scheduleHighlight ( ) {
501487 this . needsHighlight = true ;
@@ -505,16 +491,18 @@ var codeInput = {
505491 * Call an animation frame
506492 */
507493 animateFrame ( ) {
508- // Sync size
494+ // Synchronise the size of the pre/code and textarea elements
509495 if ( this . template . preElementStyled ) {
496+ this . style . backgroundColor = getComputedStyle ( this . preElement ) . backgroundColor ;
510497 this . textareaElement . style . height = getComputedStyle ( this . preElement ) . height ;
511498 this . textareaElement . style . width = getComputedStyle ( this . preElement ) . width ;
512499 } else {
500+ this . style . backgroundColor = getComputedStyle ( this . codeElement ) . backgroundColor ;
513501 this . textareaElement . style . height = getComputedStyle ( this . codeElement ) . height ;
514502 this . textareaElement . style . width = getComputedStyle ( this . codeElement ) . width ;
515503 }
516504
517- // Sync content
505+ // Synchronise the contents of the pre/code and textarea elements
518506 if ( this . needsHighlight ) {
519507 this . update ( ) ;
520508 this . needsHighlight = false ;
@@ -600,7 +588,7 @@ var codeInput = {
600588 this . pluginEvt ( "beforeElementsAdded" ) ;
601589
602590 // First-time attribute sync
603- let lang = this . getAttribute ( "lang" ) ;
591+ let lang = this . getAttribute ( "language" ) || this . getAttribute ( " lang") ;
604592 let placeholder = this . getAttribute ( "placeholder" ) || this . getAttribute ( "lang" ) || "" ;
605593 let value = this . unescapeHtml ( this . innerHTML ) || this . getAttribute ( "value" ) || "" ;
606594 // Value attribute deprecated, but included for compatibility
@@ -619,19 +607,12 @@ var codeInput = {
619607 this . innerHTML = "" ; // Clear Content
620608
621609 // Synchronise attributes to textarea
622- codeInput . textareaSyncAttributes . forEach ( ( attribute ) => {
623- if ( this . hasAttribute ( attribute ) ) {
610+ for ( let i = 0 ; i < this . attributes . length ; i ++ ) {
611+ let attribute = this . attributes [ i ] . name ;
612+ if ( codeInput . textareaSyncAttributes . includes ( attribute ) || attribute . substring ( 0 , 5 ) == "aria-" ) {
624613 textarea . setAttribute ( attribute , this . getAttribute ( attribute ) ) ;
625614 }
626- } ) ;
627- codeInput . textareaSyncAttributes . regexp . forEach ( ( reg ) =>
628- {
629- for ( const attr of this . attributes ) {
630- if ( attr . nodeName . match ( reg ) ) {
631- textarea . setAttribute ( attr . nodeName , attr . nodeValue ) ;
632- }
633- }
634- } ) ;
615+ }
635616
636617 textarea . addEventListener ( 'input' , ( evt ) => { this . value = this . textareaElement . value ; } ) ;
637618
@@ -672,7 +653,7 @@ var codeInput = {
672653 }
673654
674655 /**
675- * @deprecated Please use `codeInput.CodeInput.escapeHtml `
656+ * @deprecated Please use `codeInput.CodeInput.getTemplate `
676657 */
677658 get_template ( ) {
678659 return this . getTemplate ( ) ;
@@ -716,13 +697,8 @@ var codeInput = {
716697 return this . attributeChangedCallback ( mutation . attributeName , mutation . oldValue , super . getAttribute ( mutation . attributeName ) ) ;
717698 }
718699 }
719-
720- /* Check wildcard attributes */
721- for ( let i = 0 ; i < codeInput . observedAttributes . regexp . length ; i ++ ) {
722- const reg = codeInput . observedAttributes . regexp [ i ] ;
723- if ( mutation . attributeName . match ( reg ) ) {
724- return this . attributeChangedCallback ( mutation . attributeName , mutation . oldValue , super . getAttribute ( mutation . attributeName ) ) ;
725- }
700+ if ( mutation . attributeName . substring ( 0 , 5 ) == "aria-" ) {
701+ return this . attributeChangedCallback ( mutation . attributeName , mutation . oldValue , super . getAttribute ( mutation . attributeName ) ) ;
726702 }
727703 }
728704 }
@@ -746,9 +722,6 @@ var codeInput = {
746722 case "value" :
747723 this . value = newValue ;
748724 break ;
749- case "placeholder" :
750- this . textareaElement . placeholder = newValue ;
751- break ;
752725 case "template" :
753726 this . template = codeInput . usedTemplates [ newValue || codeInput . defaultTemplate ] ;
754727 if ( this . template . preElementStyled ) this . classList . add ( "code-input_pre-element-styled" ) ;
@@ -759,6 +732,7 @@ var codeInput = {
759732 break ;
760733
761734 case "lang" :
735+ case "language" :
762736
763737 let code = this . codeElement ;
764738 let mainTextarea = this . textareaElement ;
@@ -790,7 +764,7 @@ var codeInput = {
790764
791765 break ;
792766 default :
793- if ( codeInput . textareaSyncAttributes . includes ( name ) ) {
767+ if ( codeInput . textareaSyncAttributes . includes ( name ) || name . substring ( 0 , 5 ) == "aria-" ) {
794768 if ( newValue == null || newValue == undefined ) {
795769 this . textareaElement . removeAttribute ( name ) ;
796770 } else {
@@ -968,30 +942,6 @@ var codeInput = {
968942 } ;
969943 } ,
970944
971- arrayWildcards2regex ( list ) {
972- for ( let i = 0 ; i < list . length ; i ++ ) {
973- const name = list [ i ] ;
974- if ( name . indexOf ( "*" ) < 0 )
975- continue ;
976-
977- list . regexp . push ( new RegExp ( "^" +
978- name . replace ( / [ / \- \\ ^ $ + ? . ( ) | [ \] { } ] / g, '\\$&' )
979- . replace ( "*" , ".*" )
980- + "$" , "i" ) ) ;
981- list . splice ( i -- , 1 ) ;
982- } ;
983- } ,
984-
985- wildcard2regex ( wildcard ) {
986- if ( wildcard . indexOf ( "*" ) < 0 )
987- return null ;
988-
989- return new RegExp ( "^" +
990- wildcard . replace ( / [ / \- \\ ^ $ + ? . ( ) | [ \] { } ] / g, '\\$&' )
991- . replace ( "*" , ".*" )
992- + "$" , "i" ) ;
993- } ,
994-
995945 /**
996946 * To ensure the DOM is ready, run this callback after the window
997947 * has loaded (or now if it has already loaded)
@@ -1009,29 +959,4 @@ window.addEventListener("load", function() {
1009959 codeInput . windowLoaded = true ;
1010960} ) ;
1011961
1012-
1013- /**
1014- * convert wildcards into regex
1015- */
1016-
1017- {
1018- Object . defineProperty ( codeInput . textareaSyncAttributes , 'regexp' , {
1019- value : [ ] ,
1020- writable : false ,
1021- enumerable : false ,
1022- configurable : false
1023- } ) ;
1024- codeInput . observedAttributes = codeInput . observedAttributes . concat ( codeInput . textareaSyncAttributes ) ;
1025-
1026- Object . defineProperty ( codeInput . observedAttributes , 'regexp' , {
1027- value : [ ] ,
1028- writable : false ,
1029- enumerable : false ,
1030- configurable : false
1031- } ) ;
1032-
1033- codeInput . arrayWildcards2regex ( codeInput . textareaSyncAttributes ) ;
1034- codeInput . arrayWildcards2regex ( codeInput . observedAttributes ) ;
1035- }
1036-
1037- customElements . define ( "code-input" , codeInput . CodeInput ) ;
962+ customElements . define ( "code-input" , codeInput . CodeInput ) ;
0 commit comments