* 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)
| 56 | * targeting the `body`. See https://github.com/angular/components/issues/23546. |
| 57 | */ |
| 58 | function 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. */ |
| 90 | function noopMatchMedia(query: string): MediaQueryList { |
no test coverage detected