@@ -114,36 +114,37 @@ class App extends Component {
114114 var pre = codeBlock . parentNode ;
115115 var prevElem = pre . previousElementSibling ;
116116
117- if ( prevElem . type !== "button" ) {
118- var button = document . createElement ( "button" ) ;
119- button . className = "copy-code-button" ;
120- button . setAttribute ( "id" , "copy-code-button" ) ;
121- button . type = "button" ;
122- button . innerText = "Copy" ;
117+ if ( prevElem && prevElem . type === "button" ) {
118+ return ;
119+ }
120+ var button = document . createElement ( "button" ) ;
121+ button . className = "copy-code-button" ;
122+ button . setAttribute ( "id" , "copy-code-button" ) ;
123+ button . type = "button" ;
124+ button . innerText = "Copy" ;
123125
124- button . addEventListener ( "click" , function ( ) {
125- clipboard . writeText ( codeBlock . innerText ) . then (
126- function ( ) {
127- /* Chrome doesn't seem to blur automatically,
126+ button . addEventListener ( "click" , function ( ) {
127+ clipboard . writeText ( codeBlock . innerText ) . then (
128+ function ( ) {
129+ /* Chrome doesn't seem to blur automatically,
128130 leaving the button in a focused state. */
129- button . blur ( ) ;
130- button . innerText = "Copied!" ;
131- setTimeout ( function ( ) {
132- button . innerText = "Copy" ;
133- } , 2000 ) ;
134- } ,
135- function ( error ) {
136- button . innerText = "Error" ;
137- }
138- ) ;
139- } ) ;
131+ button . blur ( ) ;
132+ button . innerText = "Copied!" ;
133+ setTimeout ( function ( ) {
134+ button . innerText = "Copy" ;
135+ } , 2000 ) ;
136+ } ,
137+ function ( error ) {
138+ button . innerText = "Error" ;
139+ }
140+ ) ;
141+ } ) ;
140142
141- if ( pre . parentNode . classList . contains ( "highlight" ) ) {
142- var highlight = pre . parentNode ;
143- highlight . parentNode . insertBefore ( button , highlight ) ;
144- } else {
145- pre . parentNode . insertBefore ( button , pre ) ;
146- }
143+ if ( pre . parentNode . classList . contains ( "highlight" ) ) {
144+ var highlight = pre . parentNode ;
145+ highlight . parentNode . insertBefore ( button , highlight ) ;
146+ } else {
147+ pre . parentNode . insertBefore ( button , pre ) ;
147148 }
148149 } ) ;
149150 } ;
0 commit comments