Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Tim van der Lippe | ee97fa3 | 2020-04-23 15:20:56 | [diff] [blame] | 5 | // @ts-nocheck |
| 6 | // TODO(crbug.com/1011811): Enable TypeScript compiler checks |
| 7 | |
Tim van der Lippe | 5df64b2 | 2020-09-11 12:04:24 | [diff] [blame] | 8 | import * as Root from '../../root/root.js'; |
Paul Lewis | ca569a5 | 2020-09-09 16:11:51 | [diff] [blame] | 9 | import * as ThemeSupport from '../../theme_support/theme_support.js'; |
| 10 | |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 11 | /** |
| 12 | * @param {!Node} node |
| 13 | * @param {string} cssFile |
Jack Franklin | 71519f8 | 2020-11-03 12:08:59 | [diff] [blame^] | 14 | * @param {!{enableLegacyPatching:boolean}} options |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 15 | * @suppressGlobalPropertiesCheck |
| 16 | */ |
Jack Franklin | 71519f8 | 2020-11-03 12:08:59 | [diff] [blame^] | 17 | export function appendStyle(node, cssFile, options = { |
| 18 | enableLegacyPatching: false |
| 19 | }) { |
Tim van der Lippe | 5df64b2 | 2020-09-11 12:04:24 | [diff] [blame] | 20 | const content = Root.Runtime.cachedResources.get(cssFile) || ''; |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 21 | if (!content) { |
| 22 | console.error(cssFile + ' not preloaded. Check module.json'); |
| 23 | } |
| 24 | let styleElement = createElement('style'); |
| 25 | styleElement.textContent = content; |
| 26 | node.appendChild(styleElement); |
| 27 | |
Jack Franklin | 71519f8 | 2020-11-03 12:08:59 | [diff] [blame^] | 28 | /** |
| 29 | * We are incrementally removing patching support in favour of CSS variables for supporting dark mode. |
| 30 | * See https://docs.google.com/document/d/1QrSSRsJRzaQBY3zz73ZL84bTcFUV60yMtE5cuu6ED14 for details. |
| 31 | */ |
| 32 | if (options.enableLegacyPatching) { |
| 33 | const themeStyleSheet = ThemeSupport.ThemeSupport.instance().themeStyleSheet(cssFile, content); |
| 34 | if (themeStyleSheet) { |
| 35 | styleElement = createElement('style'); |
| 36 | styleElement.textContent = themeStyleSheet + '\n' + Root.Runtime.Runtime.resolveSourceURL(cssFile + '.theme'); |
| 37 | node.appendChild(styleElement); |
| 38 | } |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 39 | } |
| 40 | } |