Skip to content

Commit 8a62f02

Browse files
committed
refactor: modernize the code
1 parent f25466c commit 8a62f02

File tree

1 file changed

+44
-44
lines changed

1 file changed

+44
-44
lines changed

src/index.js

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ const mediaQueryListeners = new Map(); // MediaQuery -> MediaQueryList
2020
/**
2121
* Log debug messages
2222
*/
23-
function log(...arguments_) {
23+
const log = (...arguments_) => {
2424
if (polyfillOptions.debug) {
2525
console.log('[CSS if() Polyfill]', ...arguments_);
2626
}
27-
}
27+
};
2828

2929
/**
3030
* Check if browser has native CSS if() support
3131
*/
32-
function hasNativeSupport() {
32+
const hasNativeSupport = () => {
3333
if (globalThis.window === undefined || !globalThis.CSS) {
3434
return false;
3535
}
@@ -43,17 +43,17 @@ function hasNativeSupport() {
4343
} catch {
4444
return false;
4545
}
46-
}
46+
};
4747

4848
/**
4949
* Evaluate a condition (style(), media(), supports())
5050
*/
51-
function evaluateCondition(
51+
const evaluateCondition = (
5252
condition,
5353
registerForTracking = false,
5454
element = null,
5555
originalContent = null
56-
) {
56+
) => {
5757
condition = condition.trim();
5858

5959
// Handle style() function
@@ -78,12 +78,12 @@ function evaluateCondition(
7878

7979
// Direct boolean evaluation
8080
return evaluateBooleanCondition(condition);
81-
}
81+
};
8282

8383
/**
8484
* Evaluate style() condition
8585
*/
86-
function evaluateStyleCondition(condition) {
86+
const evaluateStyleCondition = (condition) => {
8787
const match = condition.match(/style\s*\(\s*([^)]+)\s*\)/);
8888
if (!match) {
8989
return false;
@@ -115,17 +115,17 @@ function evaluateStyleCondition(condition) {
115115
} finally {
116116
testElement.remove();
117117
}
118-
}
118+
};
119119

120120
/**
121121
* Evaluate media() condition
122122
*/
123-
function evaluateMediaCondition(
123+
const evaluateMediaCondition = (
124124
condition,
125125
registerForTracking = false,
126126
element = null,
127127
originalContent = null
128-
) {
128+
) => {
129129
const match = condition.match(/media\s*\(\s*([^)]+)\s*\)/);
130130
if (!match) {
131131
return false;
@@ -151,12 +151,12 @@ function evaluateMediaCondition(
151151
} catch {
152152
return false;
153153
}
154-
}
154+
};
155155

156156
/**
157157
* Evaluate supports() condition
158158
*/
159-
function evaluateSupportsCondition(condition) {
159+
const evaluateSupportsCondition = (condition) => {
160160
const match = condition.match(/supports\s*\(\s*([^)]+)\s*\)/);
161161
if (!match) {
162162
return false;
@@ -169,12 +169,12 @@ function evaluateSupportsCondition(condition) {
169169
} catch {
170170
return false;
171171
}
172-
}
172+
};
173173

174174
/**
175175
* Evaluate boolean condition
176176
*/
177-
function evaluateBooleanCondition(condition) {
177+
const evaluateBooleanCondition = (condition) => {
178178
// Simple boolean evaluation
179179
const lowerCondition = condition.toLowerCase();
180180

@@ -187,12 +187,12 @@ function evaluateBooleanCondition(condition) {
187187
}
188188

189189
return false;
190-
}
190+
};
191191

192192
/**
193193
* Parse multiple conditions within a single if() function
194194
*/
195-
function parseMultipleConditions(ifContent) {
195+
const parseMultipleConditions = (ifContent) => {
196196
const conditions = [];
197197
let currentCondition = '';
198198
let depth = 0;
@@ -243,12 +243,12 @@ function parseMultipleConditions(ifContent) {
243243
}
244244

245245
return conditions;
246-
}
246+
};
247247

248248
/**
249249
* Process a single condition within an if() function
250250
*/
251-
function processSingleCondition(condition) {
251+
const processSingleCondition = (condition) => {
252252
// Check if this is an else clause
253253
if (condition.trim().startsWith('else:')) {
254254
return {
@@ -305,17 +305,17 @@ function processSingleCondition(condition) {
305305
condition: conditionPart,
306306
value: valuePart
307307
};
308-
}
308+
};
309309

310310
/**
311311
* Process multiple conditions within a single if() function
312312
*/
313-
function processMultipleConditions(
313+
const processMultipleConditions = (
314314
ifContent,
315315
registerForTracking = false,
316316
element = null,
317317
originalContent = null
318-
) {
318+
) => {
319319
// Handle malformed if() functions that don't contain proper syntax
320320
if (!ifContent || !ifContent.includes(':')) {
321321
log('Malformed if() function - missing colon separator');
@@ -355,12 +355,12 @@ function processMultipleConditions(
355355
// No condition matched, return else value
356356
log(`No condition matched, using else value: ${elseValue}`);
357357
return elseValue;
358-
}
358+
};
359359

360360
/**
361361
* Find and extract if() functions with proper nested parentheses handling
362362
*/
363-
function findIfFunctions(text) {
363+
const findIfFunctions = (text) => {
364364
const functions = [];
365365
let index = 0;
366366

@@ -429,12 +429,12 @@ function findIfFunctions(text) {
429429
}
430430

431431
return functions;
432-
}
432+
};
433433

434434
/**
435435
* Process CSS text manually
436436
*/
437-
function processCSSText(cssText, options = {}, element = null) {
437+
const processCSSText = (cssText, options = {}, element = null) => {
438438
// Set options for this processing session
439439
const originalOptions = { ...polyfillOptions };
440440
polyfillOptions = { ...polyfillOptions, ...options };
@@ -488,12 +488,12 @@ function processCSSText(cssText, options = {}, element = null) {
488488
// Restore original options
489489
polyfillOptions = originalOptions;
490490
}
491-
}
491+
};
492492

493493
/**
494494
* Process a style element by rewriting its content
495495
*/
496-
function processStyleElement(styleElement) {
496+
const processStyleElement = (styleElement) => {
497497
if (styleElement.dataset.cssIfPolyfillProcessed) {
498498
return; // Already processed
499499
}
@@ -510,12 +510,12 @@ function processStyleElement(styleElement) {
510510
styleElement.dataset.cssIfPolyfillProcessed = 'true';
511511
log('Style element processed, new length:', processedContent.length);
512512
}
513-
}
513+
};
514514

515515
/**
516516
* Process all existing style elements
517517
*/
518-
function processExistingStylesheets() {
518+
const processExistingStylesheets = () => {
519519
// Process inline style elements
520520
const styleElements = document.querySelectorAll(
521521
'style:not([data-css-if-polyfill-processed])'
@@ -533,7 +533,7 @@ function processExistingStylesheets() {
533533
// but we can try to fetch and reprocess them if they're same-origin
534534
processLinkStylesheet(linkElement);
535535
}
536-
}
536+
};
537537

538538
/**
539539
* Process external stylesheet (if accessible)
@@ -595,12 +595,12 @@ async function processLinkStylesheet(linkElement) {
595595
/**
596596
* Register a media query for change tracking
597597
*/
598-
function registerMediaQuery(
598+
const registerMediaQuery = (
599599
mediaQuery,
600600
element,
601601
originalContent,
602602
mediaQueryList = null
603-
) {
603+
) => {
604604
if (!mediaQueryRegistry.has(mediaQuery)) {
605605
mediaQueryRegistry.set(mediaQuery, new Set());
606606
}
@@ -632,12 +632,12 @@ function registerMediaQuery(
632632
);
633633
}
634634
}
635-
}
635+
};
636636

637637
/**
638638
* Reprocess elements when a media query changes
639639
*/
640-
function reprocessElementsForMediaQuery(mediaQuery) {
640+
const reprocessElementsForMediaQuery = (mediaQuery) => {
641641
const elements = mediaQueryRegistry.get(mediaQuery);
642642
if (!elements) {
643643
return;
@@ -659,12 +659,12 @@ function reprocessElementsForMediaQuery(mediaQuery) {
659659
);
660660
}
661661
}
662-
}
662+
};
663663

664664
/**
665665
* Clean up media query listeners
666666
*/
667-
function cleanupMediaQueryListeners() {
667+
const cleanupMediaQueryListeners = () => {
668668
for (const [
669669
mediaQuery,
670670
{ mediaQueryList, listener }
@@ -679,12 +679,12 @@ function cleanupMediaQueryListeners() {
679679

680680
mediaQueryListeners.clear();
681681
mediaQueryRegistry.clear();
682-
}
682+
};
683683

684684
/**
685685
* Observe stylesheet changes
686686
*/
687-
function observeStylesheetChanges() {
687+
const observeStylesheetChanges = () => {
688688
// Create a MutationObserver to watch for new stylesheets
689689
const observer = new MutationObserver((mutations) => {
690690
for (const mutation of mutations) {
@@ -729,12 +729,12 @@ function observeStylesheetChanges() {
729729
childList: true,
730730
subtree: true
731731
});
732-
}
732+
};
733733

734734
/**
735735
* Initialize the polyfill
736736
*/
737-
function init(options = {}) {
737+
const init = (options = {}) => {
738738
if (globalThis.window === undefined) {
739739
throw new TypeError('CSS if() polyfill requires a browser environment');
740740
}
@@ -750,14 +750,14 @@ function init(options = {}) {
750750
log('Initializing CSS if() polyfill');
751751
processExistingStylesheets();
752752
observeStylesheetChanges();
753-
}
753+
};
754754

755755
/**
756756
* Public API to manually trigger processing
757757
*/
758-
function refresh() {
758+
const refresh = () => {
759759
processExistingStylesheets();
760-
}
760+
};
761761

762762
// Auto-initialize if in browser and DOMContentLoaded
763763
if (globalThis.window !== undefined && typeof document !== 'undefined') {

0 commit comments

Comments
 (0)