|
20 | 20 |
|
21 | 21 | const contentVisibilityValues = []; |
22 | 22 |
|
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 | + } |
35 | 37 | } |
| 38 | + } catch (error) { |
| 39 | + // Return empty array if regex processing fails |
| 40 | + return []; |
36 | 41 | } |
37 | 42 |
|
38 | 43 | return contentVisibilityValues; |
|
47 | 52 | const unique = []; |
48 | 53 | const seen = {}; |
49 | 54 |
|
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 | + } |
55 | 62 | } |
| 63 | + } catch (error) { |
| 64 | + // Return original array if deduplication fails |
| 65 | + return values; |
56 | 66 | } |
57 | 67 |
|
58 | 68 | return unique; |
|
64 | 74 | */ |
65 | 75 | function contentVisibility() { |
66 | 76 | const contentVisibilityValues = []; |
| 77 | + let debugInfo = { |
| 78 | + stylesheetsProcessed: 0, |
| 79 | + styleBlocksProcessed: 0, |
| 80 | + inlineStylesProcessed: 0, |
| 81 | + errors: [] |
| 82 | + }; |
67 | 83 |
|
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 | + } |
75 | 97 | } |
76 | | - } |
77 | 98 |
|
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 | + } |
85 | 115 | } |
86 | | - } |
87 | 116 |
|
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); |
98 | 136 | } |
99 | 137 | } |
100 | | - } |
101 | 138 |
|
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 | + } |
108 | 159 | } |
109 | 160 |
|
110 | 161 | return contentVisibility(); |
|
0 commit comments