Repository: pyscript/pyscript
File: pyscriptjs/src/components/pyscript.ts (TypeScript)

Code before:
            } catch (err) {
                const e = err as Error;
                _createAlertBanner(e.message);
                throw e;
            }
        }
        return fallback();
    };

    class PyScript extends HTMLElement {
        srcCode: string;
        stdout_manager: Stdio | null;
        stderr_manager: Stdio | null;
        _fetchSourceFallback = () => htmlDecode(this.srcCode);

        async connectedCallback() {
            // Save innerHTML information in srcCode so we can access it later
            // once we clean innerHTML (which is required since we don't want
            // source code to be rendered on the screen)
            this.srcCode = this.innerHTML;
            this.innerHTML = '';
            await init(this, this._fetchSourceFallback);
        }

        // TODO: is this useful at all? Can we move it away from the class?
        async getPySrc(): Promise<string> {
            return fetchSource(this, this._fetchSourceFallback);
        }
    }

    // bootstrap the <script> tag fallback only if needed (once per definition)
    if (!customElements.get('py-script')) {
        // allow any HTMLScriptElement to behave like a PyScript custom-elelement
        type PyScriptElement = HTMLScriptElement & PyScript;

        // the <script> tags to look for, acting like a <py-script> one
        const pyScriptCSS = 'script[type="py"],script[type="py-script"]';

        // bootstrap with the same connectedCallback logic any <script>
        const bootstrap = (script: PyScriptElement) => {
            const pyScriptTag = document.createElement('py-script-tag') as PyScript;
            script.after(pyScriptTag);

            // remove the first empty line to preserve line numbers/counting
            init(pyScriptTag, () => ltrim(script.textContent.replace(/^[\r\n]+/, ''))).catch(() =>
                pyScriptTag.remove(),
            );
        };

        // callback used to bootstrap already known <script> tags
        const callback: MutationCallback = records => {

Review comment (suggestion):
I think we don't need this one anymore since your `fetchSource` method will do what `getPySrc` was doing 😄 

Code after:
            } catch (err) {
                const e = err as Error;
                _createAlertBanner(e.message);
                throw e;
            }
        }
        return fallback();
    };

    class PyScript extends HTMLElement {
        srcCode: string;
        stdout_manager: Stdio | null;
        stderr_manager: Stdio | null;
        _fetchSourceFallback = () => htmlDecode(this.srcCode);

        async connectedCallback() {
            // Save innerHTML information in srcCode so we can access it later
            // once we clean innerHTML (which is required since we don't want
            // source code to be rendered on the screen)
            this.srcCode = this.innerHTML;
            this.innerHTML = '';
            await init(this, this._fetchSourceFallback);
        }

        getPySrc(): Promise<string> {
            return fetchSource(this, this._fetchSourceFallback);
        }
    }

    // bootstrap the <script> tag fallback only if needed (once per definition)
    if (!customElements.get('py-script')) {
        // allow any HTMLScriptElement to behave like a PyScript custom-elelement
        type PyScriptElement = HTMLScriptElement & PyScript;

        // the <script> tags to look for, acting like a <py-script> one
        // both py, pyscript, and py-script, are valid types to help reducing typo cases
        const pyScriptCSS = 'script[type="py"],script[type="pyscript"],script[type="py-script"]';

        // bootstrap with the same connectedCallback logic any <script>
        const bootstrap = (script: PyScriptElement) => {
            const pyScriptTag = document.createElement('py-script-tag') as PyScript;

            // move attributes to the live resulting pyScriptTag reference
            for (const name of ['output', 'stderr']) {
                const value = script.getAttribute(name);
                if (value) {
                    pyScriptTag.setAttribute(name, value);
                }
            }

            // insert pyScriptTag companion right after the original script