@@ -26,14 +26,31 @@ export default class HTMLCodeBlockElement extends HTMLElement {
2626 return { markup : '' } ;
2727 } ;
2828
29+ /** Observer to monitor the editing of the content of this element. */
30+ #observer = new MutationObserver ( ( ) => {
31+ this . #observer. disconnect ( ) ;
32+
33+ // Remove elements other than element with `code` as `slot` attribute value.
34+ // The content of the `[slot="code"]` element will be passed to next rendering.
35+ const slots = this . querySelectorAll ( '[slot]:not([slot="code"])' ) ;
36+
37+ for ( const slot of slots ) {
38+ slot . remove ( ) ;
39+ }
40+
41+ this . #value = ( this . textContent || this . getAttribute ( 'value' ) || '' ) . replace ( / ^ \n / , '' ) . replace ( / \n $ / , '' ) ;
42+
43+ this . #render( ) ;
44+ } ) ;
45+
2946 /** Slot elements for Shadow DOM content */
3047 #slots = ( ( ) => {
3148 /**
3249 * @param name - The value of name attribute for the slot element
3350 * @param id - The value of id attribute for the slot element
3451 * @return - The slot element
3552 */
36- const mkslot = ( name : string , id ? : string ) => {
53+ const mkslot = ( name : string , id : string = '' ) => {
3754 const slot = document . createElement ( 'slot' ) ;
3855
3956 slot . name = name ;
@@ -149,6 +166,9 @@ export default class HTMLCodeBlockElement extends HTMLElement {
149166 this . append ( this . #a11yName) ;
150167 this . append ( this . #copyButton) ;
151168 this . append ( this . #codeWrap) ;
169+ this . #observer. observe ( this , {
170+ childList : true ,
171+ } ) ;
152172 }
153173
154174 /** @return - Syntax Highlighted Source Code */
0 commit comments