Skip to content

Commit cad76c8

Browse files
committed
Add debugging
1 parent 6d19ce8 commit cad76c8

File tree

1 file changed

+101
-50
lines changed

1 file changed

+101
-50
lines changed

dist/content-visibility.js

Lines changed: 101 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,24 @@
2020

2121
const contentVisibilityValues = [];
2222

23-
// Remove CSS comments first
24-
css = css.replace(/\/\*[\s\S]*?\*\//g, '');
25-
26-
// Regex to match content-visibility property declarations
27-
// Matches: content-visibility: value; or content-visibility: value
28-
const contentVisibilityRegex = /content-visibility\s*:\s*([^;}\s]+(?:\s+[^;}\s]+)*)/gi;
29-
let regexMatch;
30-
31-
while ((regexMatch = contentVisibilityRegex.exec(css)) !== null) {
32-
const value = regexMatch[1].trim();
33-
if (value) {
34-
contentVisibilityValues.push(value);
23+
try {
24+
// Remove CSS comments first
25+
css = css.replace(/\/\*[\s\S]*?\*\//g, '');
26+
27+
// Regex to match content-visibility property declarations
28+
// Matches: content-visibility: value; or content-visibility: value
29+
const contentVisibilityRegex = /content-visibility\s*:\s*([^;}\s]+(?:\s+[^;}\s]+)*)/gi;
30+
let regexMatch;
31+
32+
while ((regexMatch = contentVisibilityRegex.exec(css)) !== null) {
33+
const value = regexMatch[1].trim();
34+
if (value) {
35+
contentVisibilityValues.push(value);
36+
}
3537
}
38+
} catch (error) {
39+
// Return empty array if regex processing fails
40+
return [];
3641
}
3742

3843
return contentVisibilityValues;
@@ -47,12 +52,17 @@
4752
const unique = [];
4853
const seen = {};
4954

50-
for (let valueIndex = 0; valueIndex < values.length; valueIndex++) {
51-
const value = values[valueIndex];
52-
if (!seen[value]) {
53-
seen[value] = true;
54-
unique.push(value);
55+
try {
56+
for (let valueIndex = 0; valueIndex < values.length; valueIndex++) {
57+
const value = values[valueIndex];
58+
if (!seen[value]) {
59+
seen[value] = true;
60+
unique.push(value);
61+
}
5562
}
63+
} catch (error) {
64+
// Return original array if deduplication fails
65+
return values;
5666
}
5767

5868
return unique;
@@ -64,47 +74,88 @@
6474
*/
6575
function contentVisibility() {
6676
const contentVisibilityValues = [];
77+
let debugInfo = {
78+
stylesheetsProcessed: 0,
79+
styleBlocksProcessed: 0,
80+
inlineStylesProcessed: 0,
81+
errors: []
82+
};
6783

68-
// Process stylesheets first (usually largest source)
69-
const stylesheets = $WPT_BODIES.filter(body => body.type === 'Stylesheet');
70-
for (let stylesheetIndex = 0; stylesheetIndex < stylesheets.length; stylesheetIndex++) {
71-
const stylesheet = stylesheets[stylesheetIndex];
72-
if (stylesheet.response_body) {
73-
const values = extractContentVisibilityValues(stylesheet.response_body);
74-
contentVisibilityValues.push(...values);
84+
try {
85+
// Process stylesheets first (usually largest source)
86+
if (typeof $WPT_BODIES !== 'undefined' && Array.isArray($WPT_BODIES)) {
87+
const stylesheets = $WPT_BODIES.filter(body => body.type === 'Stylesheet');
88+
debugInfo.stylesheetsProcessed = stylesheets.length;
89+
90+
for (let stylesheetIndex = 0; stylesheetIndex < stylesheets.length; stylesheetIndex++) {
91+
const stylesheet = stylesheets[stylesheetIndex];
92+
if (stylesheet && stylesheet.response_body) {
93+
const values = extractContentVisibilityValues(stylesheet.response_body);
94+
contentVisibilityValues.push(...values);
95+
}
96+
}
7597
}
76-
}
7798

78-
// Process style blocks (usually fewer elements)
79-
const styleElements = document.querySelectorAll('style');
80-
for (let styleIndex = 0; styleIndex < styleElements.length; styleIndex++) {
81-
const styleElement = styleElements[styleIndex];
82-
if (styleElement.innerHTML) {
83-
const values = extractContentVisibilityValues(styleElement.innerHTML);
84-
contentVisibilityValues.push(...values);
99+
// Process style blocks (usually fewer elements)
100+
if (typeof document !== 'undefined' && document.querySelectorAll) {
101+
try {
102+
const styleElements = document.querySelectorAll('style');
103+
debugInfo.styleBlocksProcessed = styleElements.length;
104+
105+
for (let styleIndex = 0; styleIndex < styleElements.length; styleIndex++) {
106+
const styleElement = styleElements[styleIndex];
107+
if (styleElement && styleElement.innerHTML) {
108+
const values = extractContentVisibilityValues(styleElement.innerHTML);
109+
contentVisibilityValues.push(...values);
110+
}
111+
}
112+
} catch (error) {
113+
debugInfo.errors.push('style_blocks_error: ' + error.message);
114+
}
85115
}
86-
}
87116

88-
// Process inline styles (most expensive - limit scope if possible)
89-
// Only process if we haven't found any content-visibility yet
90-
if (contentVisibilityValues.length === 0) {
91-
const elementsWithStyle = document.querySelectorAll('[style]');
92-
for (let elementIndex = 0; elementIndex < elementsWithStyle.length; elementIndex++) {
93-
const elementWithStyle = elementsWithStyle[elementIndex];
94-
const styleAttr = elementWithStyle.getAttribute('style');
95-
if (styleAttr && styleAttr.includes('content-visibility')) {
96-
const values = extractContentVisibilityValues(styleAttr);
97-
contentVisibilityValues.push(...values);
117+
// Process inline styles (most expensive - limit scope if possible)
118+
// Only process if we haven't found any content-visibility yet
119+
if (contentVisibilityValues.length === 0 && typeof document !== 'undefined' && document.querySelectorAll) {
120+
try {
121+
const elementsWithStyle = document.querySelectorAll('[style]');
122+
debugInfo.inlineStylesProcessed = elementsWithStyle.length;
123+
124+
for (let elementIndex = 0; elementIndex < elementsWithStyle.length; elementIndex++) {
125+
const elementWithStyle = elementsWithStyle[elementIndex];
126+
if (elementWithStyle) {
127+
const styleAttr = elementWithStyle.getAttribute('style');
128+
if (styleAttr && styleAttr.includes('content-visibility')) {
129+
const values = extractContentVisibilityValues(styleAttr);
130+
contentVisibilityValues.push(...values);
131+
}
132+
}
133+
}
134+
} catch (error) {
135+
debugInfo.errors.push('inline_styles_error: ' + error.message);
98136
}
99137
}
100-
}
101138

102-
return {
103-
used: contentVisibilityValues.length > 0,
104-
count: contentVisibilityValues.length,
105-
values: contentVisibilityValues,
106-
uniqueValues: getUniqueValues(contentVisibilityValues)
107-
};
139+
return {
140+
used: contentVisibilityValues.length > 0,
141+
count: contentVisibilityValues.length,
142+
values: contentVisibilityValues,
143+
uniqueValues: getUniqueValues(contentVisibilityValues),
144+
debug: debugInfo
145+
};
146+
} catch (error) {
147+
// Return a safe fallback if the entire function fails
148+
return {
149+
used: false,
150+
count: 0,
151+
values: [],
152+
uniqueValues: [],
153+
debug: {
154+
...debugInfo,
155+
errors: [...debugInfo.errors, 'main_error: ' + error.message]
156+
}
157+
};
158+
}
108159
}
109160

110161
return contentVisibility();

0 commit comments

Comments
 (0)