Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Set className and textContent in the dynamic style element "monaco-colors" before appended #3776

Closed
2 tasks done
csu-feizao opened this issue Mar 10, 2023 · 1 comment · Fixed by microsoft/vscode#176746
Labels
feature-request Request for new features or functionality

Comments

@csu-feizao
Copy link

csu-feizao commented Mar 10, 2023

Context

  • This issue is not a bug report. (please use a different template for reporting a bug)
  • This issue is not a duplicate of an existing issue. (please use the search to find existing issues)

Description

monaco-editor will dynamically create a monaco-colors style element and append it to DOM. But the code is to append to DOM first, then to set its className and textConent.

That is not a bug for monaco-editor, but may cause some bugs in the user application, especially with the micro-frontends applications like qiankun.

For example, most of the micro-frontends applications rewrote the document.head.appendChild method to handle style isolation. When monaco-editor append the monaco-colors style element, style isolation handle function couldn't get any information from that element which was empty at that time.

In my case, I want to prevent this style from being handled by qiankun, like

const rawHeadAppendChild = HTMLHeadElement.prototype.appendChild
HTMLHeadElement.prototype.appendChild = function (node) {
  if (node.className === 'monaco-colors') {
    return rawHeadAppendChild.call(this, node)
  }
  return this.appendChild(node)
}

So can we change the order of creating the 'monaco-colors' style element? If ok I would like to create a pr

There is the original code:

vscode -> src/vs/editor/standalone/browser/standaloneThemeService.ts -> line 285

	private _registerShadowDomContainer(domNode: HTMLElement): IDisposable {
		const styleElement = dom.createStyleSheet(domNode);
		styleElement.className = 'monaco-colors';
		styleElement.textContent = this._allCSS;
		...
	}

vscode -> src/vs/base/browser/dom.ts -> line 694

export function createStyleSheet(container: HTMLElement = document.getElementsByTagName('head')[0]): HTMLStyleElement {
	const style = document.createElement('style');
	style.type = 'text/css';
	style.media = 'screen';
	container.appendChild(style);
	return style;
}

There is the code after being changed:
vscode -> src/vs/editor/standalone/browser/standaloneThemeService.ts -> line 285

	private _registerShadowDomContainer(domNode: HTMLElement): IDisposable {
		const styleElement = dom.createStyleSheet(domNode, style => {
		    style.className = 'monaco-colors';
		    style.textContent = this._allCSS;
                });
		...
	}

vscode -> src/vs/base/browser/dom.ts -> line 694

export function createStyleSheet(container: HTMLElement = document.getElementsByTagName('head')[0], beforeAppend?: (style: HTMLStyleElement) => void): HTMLStyleElement {
	const style = document.createElement('style');
	style.type = 'text/css';
	style.media = 'screen';
        beforeAppend?.(style)
	container.appendChild(style);
	return style;
}

Monaco Editor Playground Link

No response

Monaco Editor Playground Code

No response

@csu-feizao csu-feizao added the feature-request Request for new features or functionality label Mar 10, 2023
@csu-feizao csu-feizao changed the title [Feature Request] Set className and textontent in the dynamic style element "monaco-colors" before appended [Feature Request] Set className and textContent in the dynamic style element "monaco-colors" before appended Mar 10, 2023
@hediet
Copy link
Member

hediet commented Mar 13, 2023

I think this request makes sense. Thanks for providing a PR!

@github-actions github-actions bot locked and limited conversation to collaborators Apr 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature-request Request for new features or functionality
Projects
None yet
2 participants