@@ -530,20 +530,67 @@ var codeInput = {
530530 this . pluginEvt ( "afterHighlight" ) ;
531531 }
532532
533+ getStyledHighlightingElement ( ) {
534+ if ( this . templateObject . preElementStyled ) {
535+ return this . preElement ;
536+ } else {
537+ return this . codeElement ;
538+ }
539+ }
540+
533541 /**
534542 * Set the size of the textarea element to the size of the pre/code element.
535543 */
536544 syncSize ( ) {
537545 // Synchronise the size of the pre/code and textarea elements
538- if ( this . templateObject . preElementStyled ) {
539- this . textareaElement . style . height = getComputedStyle ( this . preElement ) . height ;
540- this . textareaElement . style . width = getComputedStyle ( this . preElement ) . width ;
541- } else {
542- this . textareaElement . style . height = getComputedStyle ( this . codeElement ) . height ;
543- this . textareaElement . style . width = getComputedStyle ( this . codeElement ) . width ;
546+ this . textareaElement . style . height = getComputedStyle ( this . getStyledHighlightingElement ( ) ) . height ;
547+ this . textareaElement . style . width = getComputedStyle ( this . getStyledHighlightingElement ( ) ) . width ;
548+ }
549+
550+ /**
551+ * If the color attribute has been defined on the
552+ * code-input element by external code, return true.
553+ * Otherwise, make the aspects the color affects
554+ * (placeholder and caret colour) be the base colour
555+ * of the highlighted text, for best contrast, and
556+ * return false.
557+ */
558+ isColorOverridenSyncIfNot ( ) {
559+ this . style . setProperty ( "--code-input_no-override-color" , "rgb(0, 0, 0)" ) ;
560+ if ( getComputedStyle ( this ) . color == "rgb(0, 0, 0)" ) {
561+ // May not be overriden
562+ this . style . setProperty ( "--code-input_no-override-color" , "rgb(255, 255, 255)" ) ;
563+ if ( getComputedStyle ( this ) . color == "rgb(255, 255, 255)" ) {
564+ // Definitely not overriden
565+ this . style . removeProperty ( "--code-input_no-override-color" ) ;
566+
567+ this . style . setProperty ( "--code-input_highlight-text-color" , getComputedStyle ( this . getStyledHighlightingElement ( ) ) . color ) ;
568+ this . style . setProperty ( "--code-input_default-caret-color" , getComputedStyle ( this . getStyledHighlightingElement ( ) ) . color ) ;
569+ return false ;
570+ }
571+ }
572+ this . style . removeProperty ( "--code-input_no-override-color" ) ;
573+
574+ return true ;
575+ }
576+
577+ /**
578+ * Update the aspects the color affects
579+ * (placeholder and caret colour) to the correct
580+ * colour: either that defined on the code-input
581+ * element, or if none is defined externally the
582+ * base colour of the highlighted text.
583+ */
584+ syncColorCompletely ( ) {
585+ // color of code-input element
586+ if ( this . isColorOverridenSyncIfNot ( ) ) {
587+ // color overriden
588+ this . style . removeProperty ( "--code-input_highlight-text-color" ) ;
589+ this . style . setProperty ( "--code-input_default-caret-color" , getComputedStyle ( this ) . color ) ;
544590 }
545591 }
546592
593+
547594 /**
548595 * Show some instructions to the user only if they are using keyboard navigation - for example, a prompt on how to navigate with the keyboard if Tab is repurposed.
549596 * @param {string } instructions The instructions to display only if keyboard navigation is being used. If it's blank, no instructions will be shown.
@@ -778,38 +825,23 @@ var codeInput = {
778825 resizeObserver . observe ( this ) ;
779826
780827 // Synchronise colors
781- if ( this . templateObject . preElementStyled ) {
782- this . preElement . addEventListener ( "transitionend" , ( evt ) => {
783- if ( evt . propertyName == "color" ) {
784- // So previous variable value does not affect new value:
785- // (required to deal with color being no longer specified in CSS)
786- this . style . removeProperty ( "--code-input_highlight-text-color" ) ;
787-
788- console . log ( "pre" , evt , getComputedStyle ( this . preElement ) . color ) ;
789- this . style . setProperty ( "--code-input_highlight-text-color" , getComputedStyle ( this . preElement ) . color ) ;
790- }
791- } ) ;
792- } else {
793- this . codeElement . addEventListener ( "transitionend" , ( evt ) => {
794- if ( evt . propertyName == "color" ) {
795- // So previous variable value does not affect new value:
796- // (required to deal with color being no longer specified in CSS)
797- this . style . removeProperty ( "--code-input_highlight-text-color" ) ;
798-
799- console . log ( "code" , evt , getComputedStyle ( this . codeElement ) . color ) ;
800- this . style . setProperty ( "--code-input_highlight-text-color" , getComputedStyle ( this . codeElement ) . color ) ;
801- }
802- } ) ;
803- }
804- // Not on this element so CSS transition property which must be set for
805- // listener to work does not conflict with library-user transition property
806- this . dialogContainerElement . addEventListener ( "transitionend" , ( evt ) => {
828+ const preColorChangeCallback = ( evt ) => {
807829 if ( evt . propertyName == "color" ) {
808- console . log ( "ci" , evt , getComputedStyle ( this ) . color ) ;
809-
810- this . style . setProperty ( "--code-input_default-caret-color" , getComputedStyle ( this ) . color ) ;
830+ this . isColorOverridenSyncIfNot ( ) ;
811831 }
812- } ) ;
832+ } ;
833+ this . preElement . addEventListener ( "transitionend" , preColorChangeCallback ) ;
834+ this . preElement . addEventListener ( "-webkit-transitionend" , preColorChangeCallback ) ;
835+ const thisColorChangeCallback = ( evt ) => {
836+ if ( evt . propertyName == "color" ) {
837+ this . syncColorCompletely ( ) ;
838+ }
839+ } ;
840+ // Not on this element so CSS transition property which must be set for
841+ this . dialogContainerElement . addEventListener ( "transitionend" , thisColorChangeCallback ) ;
842+ this . dialogContainerElement . addEventListener ( "-webkit-transitionend" , thisColorChangeCallback ) ;
843+
844+ this . syncColorCompletely ( ) ;
813845
814846 this . classList . add ( "code-input_loaded" ) ;
815847 }
0 commit comments