MCPcopy Create free account
hub / github.com/angular/components / createEmptyStyleRule

Function createEmptyStyleRule

src/cdk/layout/media-matcher.ts:58–87  ·  view source on GitHub ↗

* Creates an empty stylesheet that is used to work around browser inconsistencies related to * `matchMedia`. At the time of writing, it handles the following cases: * 1. On WebKit browsers, a media query has to have at least one rule in order for `matchMedia` * to fire. We work around it by decla

(query: string, nonce: string | undefined | null)

Source from the content-addressed store, hash-verified

56 * targeting the `body`. See https://github.com/angular/components/issues/23546.
57 */
58function createEmptyStyleRule(query: string, nonce: string | undefined | null) {
59 if (mediaQueriesForWebkitCompatibility.has(query)) {
60 return;
61 }
62
63 try {
64 if (!mediaQueryStyleNode) {
65 mediaQueryStyleNode = document.createElement('style');
66
67 if (nonce) {
68 mediaQueryStyleNode.setAttribute('nonce', nonce);
69 }
70
71 mediaQueryStyleNode.setAttribute('type', 'text/css');
72 document.head!.appendChild(mediaQueryStyleNode);
73 }
74
75 if (mediaQueryStyleNode.sheet) {
76 mediaQueryStyleNode.sheet.insertRule(
77 // Drop the curly braces to avoid injection attacks. Curly braces aren't
78 // valid media query syntax so this should be a no-op in valid cases.
79 `@media ${query.replace(/[{}]/g, '')} {body{ }}`,
80 0,
81 );
82 mediaQueriesForWebkitCompatibility.add(query);
83 }
84 } catch (e) {
85 console.error(e);
86 }
87}
88
89/** No-op matchMedia replacement for non-browser platforms. */
90function noopMatchMedia(query: string): MediaQueryList {

Callers 1

matchMediaMethod · 0.85

Calls 2

errorMethod · 0.80
addMethod · 0.45

Tested by

no test coverage detected