blob: 2f21393f2dbc8620ba705580c861043a8bf07cf4 [file] [log] [blame]
Paul Lewis9950e182019-12-16 16:06:071// 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 Lippeee97fa32020-04-23 15:20:565// @ts-nocheck
6// TODO(crbug.com/1011811): Enable TypeScript compiler checks
7
Tim van der Lippe5df64b22020-09-11 12:04:248import * as Root from '../../root/root.js';
Paul Lewisca569a52020-09-09 16:11:519import * as ThemeSupport from '../../theme_support/theme_support.js';
10
Paul Lewis9950e182019-12-16 16:06:0711/**
12 * @param {!Node} node
13 * @param {string} cssFile
Jack Franklin71519f82020-11-03 12:08:5914 * @param {!{enableLegacyPatching:boolean}} options
Paul Lewis9950e182019-12-16 16:06:0715 * @suppressGlobalPropertiesCheck
16 */
Jack Franklin71519f82020-11-03 12:08:5917export function appendStyle(node, cssFile, options = {
18 enableLegacyPatching: false
19}) {
Tim van der Lippe5df64b22020-09-11 12:04:2420 const content = Root.Runtime.cachedResources.get(cssFile) || '';
Paul Lewis9950e182019-12-16 16:06:0721 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 Franklin71519f82020-11-03 12:08:5928 /**
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 Lewis9950e182019-12-16 16:06:0739 }
40}