-
Notifications
You must be signed in to change notification settings - Fork 648
/
hint.ts
167 lines (128 loc) · 5.91 KB
/
hint.ts
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
166
167
/**
* @fileoverview Checks if CSS exceeds known stylesheet limits.
*/
import { HintContext } from 'hint/dist/src/lib/hint-context';
import { IHint, CanEvaluateScript } from 'hint/dist/src/lib/types';
import { Severity } from '@hint/utils-types';
import meta from './meta';
import { getMessage } from './i18n.import';
/*
* ------------------------------------------------------------------------------
* Public
* ------------------------------------------------------------------------------
*/
export default class StylesheetLimitsHint implements IHint {
public static readonly meta = meta;
public constructor(context: HintContext) {
// Allow limits to be overridden by hint options.
const options = context.hintOptions;
// Check if browsers with default limits are included.
const includesOldIE = ['ie 6', 'ie 7', 'ie 8', 'ie 9'].some((e) => {
return context.targetedBrowsers.includes(e);
});
if (!options && !includesOldIE) {
// Exit if we don't have any limits to test.
return;
}
let hasImportLimit = includesOldIE;
let hasRuleLimit = includesOldIE;
let hasSheetLimit = includesOldIE;
let maxImports = includesOldIE ? 4 : 0;
let maxRules = includesOldIE ? 4095 : 0;
let maxSheets = includesOldIE ? 31 : 0;
// Min the default/options values to ensure overrides can't "hide" browser limits.
if (options) {
// Always use the options value if no default import limit is specified.
if (options.maxImports && (!hasImportLimit || options.maxImports < maxImports)) {
maxImports = options.maxImports;
hasImportLimit = true;
}
if (options.maxRules && (!hasRuleLimit || options.maxRules < maxRules)) {
maxRules = options.maxRules;
hasRuleLimit = true;
}
if (options.maxSheets && (!hasSheetLimit || options.maxSheets < maxSheets)) {
maxSheets = options.maxSheets;
hasSheetLimit = true;
}
}
/* istanbul ignore next */
// The following function will be evaluated in the context of the page.
const injectedCode = function () {
// Recursively count rules and imports in the passed stylesheet.
const countRules = (styleSheet: CSSStyleSheet) => {
const results = {
imports: 0,
rules: 0,
sheets: 1
};
try {
// Count rules in this stylesheet.
Array.from(styleSheet.cssRules).forEach((rule) => {
if (rule instanceof CSSStyleRule) {
// Count each selector in a style rule separately.
results.rules += rule.selectorText.split(',').length;
} else if (rule instanceof CSSImportRule) {
// Recursively count rules in imported stylesheets.
const subResults = countRules(rule.styleSheet);
results.imports += Math.max(results.imports, subResults.imports + 1);
results.rules += subResults.rules + 1;
results.sheets += subResults.sheets;
} else {
// Other rules count as one each.
results.rules += 1;
}
});
} catch (e) {
/*
* Accessing cssRules of a cross-origin stylesheet can throw.
* If this happens, exclude the stylesheet from the count.
*/
}
return results;
};
const combinedResults = {
imports: 0,
rules: 0,
sheets: 0
};
// Get the recursive count of rules for all stylesheets in the page.
Array.from(document.styleSheets).forEach((sheet) => {
if (sheet instanceof CSSStyleSheet) {
const subResults = countRules(sheet);
combinedResults.imports += Math.max(combinedResults.imports, subResults.imports);
combinedResults.rules += subResults.rules;
combinedResults.sheets += subResults.sheets;
}
});
return combinedResults;
};
const validateScanEnd = async (event: CanEvaluateScript) => {
const results = await context.evaluate(`(${injectedCode})()`);
// Report once we hit a limit to support flagging on platforms which will drop subsequent rules.
// Only check `maxImports` if a limit has been specified
if (hasImportLimit && results.imports >= maxImports) {
context.report(
event.resource,
getMessage('maximumNested', context.language, [maxImports.toString(), results.imports.toString()]),
{ severity: Severity.error }
);
}
if (hasRuleLimit && results.rules >= maxRules) {
context.report(
event.resource,
getMessage('maximumRules', context.language, [maxRules.toString(), results.rules.toString()]),
{ severity: Severity.error }
);
}
if (hasSheetLimit && results.sheets >= maxSheets) {
context.report(
event.resource,
getMessage('maximumStylesheets', context.language, [maxSheets.toString(), results.sheets.toString()]),
{ severity: Severity.error }
);
}
};
context.on('can-evaluate::script', validateScanEnd);
}
}