@@ -150,37 +150,41 @@ var codeInput = {
150150 return [ "value" , "placeholder" , "lang" , "template" ] ; // Attributes to monitor
151151 }
152152 attributeChangedCallback ( name , oldValue , newValue ) {
153+ if ( this . isConnected ) {
154+ // This will sometimes be called before the element has been created, so trying to update an attribute causes an error.
155+ // Thanks to Kevin Loughead for pointing this out.
156+ switch ( name ) {
153157
154- switch ( name ) {
158+ case "value" :
155159
156- case "value" :
157-
158- // Update code
159- this . update ( newValue ) ;
160+ // Update code
161+ this . update ( newValue ) ;
162+
163+ break ;
164+
165+ case "placeholder" :
166+ this . querySelector ( "textarea" ) . placeholder = newValue ;
167+ break ;
168+ case "template" :
169+ this . template = codeInput . usedTemplates [ newValue || codeInput . defaultTemplate ] ;
170+ if ( this . template . preElementStyled ) this . classList . add ( "code-input_pre-element-styled" ) ;
171+ else this . classList . remove ( "code-input_pre-element-styled" ) ;
172+ // Syntax Highlight
173+ this . update ( this . value ) ;
160174
161- break ;
175+ case "lang" :
176+ let code = this . querySelector ( "pre code" ) ;
177+ let textarea = this . querySelector ( "textarea" ) ;
162178
163- case "placeholder" :
164- this . querySelector ( "textarea" ) . placeholder = newValue ;
165- break ;
166- case "template" :
167- this . template = codeInput . usedTemplates [ newValue || codeInput . defaultTemplate ] ;
168- if ( this . template . preElementStyled ) this . classList . add ( "code-input_pre-element-styled" ) ;
169- else this . classList . remove ( "code-input_pre-element-styled" ) ;
170- // Syntax Highlight
171- this . update ( this . value ) ;
172-
173- case "lang" :
174- let code = this . querySelector ( "pre code" ) ;
175- let textarea = this . querySelector ( "textarea" ) ;
176-
177- if ( newValue != null ) code . className = ( "language-" + newValue ) ;
178- else code . className = "" ;
179-
180- if ( textarea . placeholder == oldValue ) textarea . placeholder = newValue
181-
182- this . update ( this . value ) ;
179+ if ( newValue != null ) code . className = ( "language-" + newValue ) ;
180+ else code . className = "" ;
181+
182+ if ( textarea . placeholder == oldValue ) textarea . placeholder = newValue
183+
184+ this . update ( this . value ) ;
185+ }
183186 }
187+
184188 }
185189
186190 /* Value attribute */
0 commit comments