UNPKG

3.07 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2021 Ericsson and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.DecorationStyle = void 0;
19var DecorationStyle;
20(function (DecorationStyle) {
21 function createStyleElement(styleId, container = document.head) {
22 const style = document.createElement('style');
23 style.id = styleId;
24 style.type = 'text/css';
25 style.media = 'screen';
26 style.appendChild(document.createTextNode('')); // trick for webkit
27 container.appendChild(style);
28 return style;
29 }
30 DecorationStyle.createStyleElement = createStyleElement;
31 function createStyleSheet(styleId, container) {
32 return createStyleElement(styleId, container).sheet;
33 }
34 DecorationStyle.createStyleSheet = createStyleSheet;
35 function getRuleIndex(selector, styleSheet) {
36 return Array.from(styleSheet.cssRules || styleSheet.rules).findIndex(rule => rule.type === CSSRule.STYLE_RULE && rule.selectorText === selector);
37 }
38 function getOrCreateStyleRule(selector, styleSheet) {
39 let index = getRuleIndex(selector, styleSheet);
40 if (index === -1) {
41 // The given selector does not exist in the provided independent style sheet, rule index = 0
42 index = styleSheet.insertRule(selector + '{}', 0);
43 }
44 const rules = styleSheet.cssRules || styleSheet.rules;
45 const rule = rules[index];
46 if (rule && rule.type === CSSRule.STYLE_RULE) {
47 return rule;
48 }
49 styleSheet.deleteRule(index);
50 throw new Error('This function is only for CSS style rules. Other types of CSS rules are not allowed.');
51 }
52 DecorationStyle.getOrCreateStyleRule = getOrCreateStyleRule;
53 function deleteStyleRule(selector, styleSheet) {
54 if (!styleSheet) {
55 return;
56 }
57 // In general, only one rule exists for a given selector in the provided independent style sheet
58 const index = getRuleIndex(selector, styleSheet);
59 if (index !== -1) {
60 styleSheet.deleteRule(index);
61 }
62 }
63 DecorationStyle.deleteStyleRule = deleteStyleRule;
64})(DecorationStyle = exports.DecorationStyle || (exports.DecorationStyle = {}));
65//# sourceMappingURL=decoration-style.js.map
\No newline at end of file