aboutsummaryrefslogtreecommitdiffstats
path: root/vanilla/node_modules/@acemir/cssom/lib/CSSGroupingRule.js
blob: 31d30106b450308c6f0f7c05622056572e6e6f8e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//.CommonJS
var CSSOM = {
	CSSRule: require("./CSSRule").CSSRule,
	CSSRuleList: require("./CSSRuleList").CSSRuleList,
	parse: require('./parse').parse
};
var errorUtils = require("./errorUtils").errorUtils;
///CommonJS


/**
 * @constructor
 * @see https://drafts.csswg.org/cssom/#the-cssgroupingrule-interface
 */
CSSOM.CSSGroupingRule = function CSSGroupingRule() {
	CSSOM.CSSRule.call(this);
	this.__cssRules = new CSSOM.CSSRuleList();
};

CSSOM.CSSGroupingRule.prototype  = Object.create(CSSOM.CSSRule.prototype);
CSSOM.CSSGroupingRule.prototype.constructor = CSSOM.CSSGroupingRule;

Object.setPrototypeOf(CSSOM.CSSGroupingRule, CSSOM.CSSRule);

Object.defineProperty(CSSOM.CSSGroupingRule.prototype, "cssRules", {
	get: function() {
		return this.__cssRules;
	}
});

/**
 * Used to insert a new CSS rule to a list of CSS rules.
 *
 * @example
 *   cssGroupingRule.cssText
 *   -> "body{margin:0;}"
 *   cssGroupingRule.insertRule("img{border:none;}", 1)
 *   -> 1
 *   cssGroupingRule.cssText
 *   -> "body{margin:0;}img{border:none;}"
 *
 * @param {string} rule
 * @param {number} [index]
 * @see https://www.w3.org/TR/cssom-1/#dom-cssgroupingrule-insertrule
 * @return {number} The index within the grouping rule's collection of the newly inserted rule.
 */
 CSSOM.CSSGroupingRule.prototype.insertRule = function insertRule(rule, index) {
	if (rule === undefined && index === undefined) {
		errorUtils.throwMissingArguments(this, 'insertRule', this.constructor.name);
	}
	if (index === void 0) {
		index = 0;
	}
	index = Number(index);
	if (index < 0) {
		index = 4294967296 + index;
	}
	if (index > this.cssRules.length) {
		errorUtils.throwIndexError(this, 'insertRule', this.constructor.name, index, this.cssRules.length);
	}
	var ruleToParse = processedRuleToParse = String(rule);
	ruleToParse = ruleToParse.trim().replace(/^\/\*[\s\S]*?\*\/\s*/, "");
	var isNestedSelector = this.constructor.name === "CSSStyleRule";
	if (isNestedSelector === false) {
		var currentRule = this;
		while (currentRule.parentRule) {
			currentRule = currentRule.parentRule;
			if (currentRule.constructor.name === "CSSStyleRule") {
				isNestedSelector = true;
				break;
			}
		}
	}
	if (isNestedSelector) {
		processedRuleToParse = 's { n { } ' + ruleToParse + '}';
	}
	var isScopeRule = this.constructor.name === "CSSScopeRule";
	if (isScopeRule) {
		if (isNestedSelector) {
			processedRuleToParse = 's { ' + '@scope {' + ruleToParse + '}}';
		} else {
			processedRuleToParse = '@scope {' + ruleToParse + '}';
		}
	}
	var parsedRules = new CSSOM.CSSRuleList();
	CSSOM.parse(processedRuleToParse, {
		styleSheet: this.parentStyleSheet,
		cssRules: parsedRules
	});
	if (isScopeRule) {
		if (isNestedSelector) {
			parsedRules = parsedRules[0].cssRules[0].cssRules;
		} else {
			parsedRules = parsedRules[0].cssRules
		}
	}
	if (isNestedSelector) {
		parsedRules = parsedRules[0].cssRules.slice(1);
	}
	if (parsedRules.length !== 1) {
		if (isNestedSelector && parsedRules.length === 0 && ruleToParse.indexOf('@font-face') === 0) {
			errorUtils.throwError(this, 'DOMException', 
				"Failed to execute 'insertRule' on '" + this.constructor.name + "': " +
				"Only conditional nested group rules, style rules, @scope rules, @apply rules, and nested declaration rules may be nested.",
				'HierarchyRequestError');
		} else {
			errorUtils.throwParseError(this, 'insertRule', this.constructor.name, ruleToParse, 'SyntaxError');
		}
	}
	var cssRule = parsedRules[0];

	if (cssRule.constructor.name === 'CSSNestedDeclarations' && cssRule.style.length === 0) {
		errorUtils.throwParseError(this, 'insertRule', this.constructor.name, ruleToParse, 'SyntaxError');	
	}
	
	// Check for rules that cannot be inserted inside a CSSGroupingRule
	if (cssRule.constructor.name === 'CSSImportRule' || cssRule.constructor.name === 'CSSNamespaceRule') {
		var ruleKeyword = cssRule.constructor.name === 'CSSImportRule' ? '@import' : '@namespace';
		errorUtils.throwError(this, 'DOMException', 
			"Failed to execute 'insertRule' on '" + this.constructor.name + "': " +
			"'" + ruleKeyword + "' rules cannot be inserted inside a group rule.",
			'HierarchyRequestError');
	}
	
	// Check for CSSLayerStatementRule (@layer statement rules)
	if (cssRule.constructor.name === 'CSSLayerStatementRule') {
		errorUtils.throwParseError(this, 'insertRule', this.constructor.name, ruleToParse, 'SyntaxError');
	}
	
	cssRule.__parentRule = this;
	this.cssRules.splice(index, 0, cssRule);
	return index;
};

/**
 * Used to delete a rule from the grouping rule.
 *
 *   cssGroupingRule.cssText
 *   -> "img{border:none;}body{margin:0;}"
 *   cssGroupingRule.deleteRule(0)
 *   cssGroupingRule.cssText
 *   -> "body{margin:0;}"
 *
 * @param {number} index within the grouping rule's rule list of the rule to remove.
 * @see https://www.w3.org/TR/cssom-1/#dom-cssgroupingrule-deleterule
 */
 CSSOM.CSSGroupingRule.prototype.deleteRule = function deleteRule(index) {
	if (index === undefined) {
		errorUtils.throwMissingArguments(this, 'deleteRule', this.constructor.name);
	}
	index = Number(index);
	if (index < 0) {
		index = 4294967296 + index;
	}
	if (index >= this.cssRules.length) {
		errorUtils.throwIndexError(this, 'deleteRule', this.constructor.name, index, this.cssRules.length);
	}
	this.cssRules[index].__parentRule = null;
	this.cssRules[index].__parentStyleSheet = null;
	this.cssRules.splice(index, 1);
};

//.CommonJS
exports.CSSGroupingRule = CSSOM.CSSGroupingRule;
///CommonJS