purgetss 7.4.0 → 7.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -470,6 +470,10 @@ export function compileApplyDirectives(twClasses) {
470
470
  const compoundClasses = []
471
471
  const classesWithOpacityValues = []
472
472
 
473
+ // Extract platform from the target class (e.g., 'Window[platform=ios]' → 'ios')
474
+ const platformMatch = className.match(/\[platform=(\w+)\]/)
475
+ const targetPlatform = platformMatch ? platformMatch[1] : null
476
+
473
477
  _.each([...values], searchClass => {
474
478
  if (searchClass.includes('ios:')) {
475
479
  searchClass = `${searchClass.replace('ios:', '')}[platform=ios]`
@@ -496,14 +500,27 @@ export function compileApplyDirectives(twClasses) {
496
500
 
497
501
  classesWithOpacityValues.push({ decimalValue, transparency, originalClass, classNameWithTransparency })
498
502
  } else {
499
- const className = `'.${searchClass}':`
500
- let foundClass = twClassesArray[findIndexOfClassName(className, twClassesArray)]
503
+ let foundClass = null
504
+
505
+ // If the target has a platform, try platform-specific class first
506
+ if (targetPlatform && !searchClass.includes('[platform=')) {
507
+ const platformClassName = `'.${searchClass}[platform=${targetPlatform}]':`
508
+ foundClass = twClassesArray[findIndexOfClassName(platformClassName, twClassesArray)]
509
+ if (!foundClass && fontsClassesArray) {
510
+ foundClass = fontsClassesArray[findIndexOfClassName(platformClassName, fontsClassesArray)]
511
+ }
512
+ }
501
513
 
502
- if (foundClass) compoundClasses.push(justProperties(foundClass))
503
- else if (fontsClassesArray) {
504
- foundClass = fontsClassesArray[findIndexOfClassName(className, fontsClassesArray)]
505
- if (foundClass) compoundClasses.push(justProperties(foundClass))
514
+ // Fall back to generic class (no platform suffix)
515
+ if (!foundClass) {
516
+ const genericClassName = `'.${searchClass}':`
517
+ foundClass = twClassesArray[findIndexOfClassName(genericClassName, twClassesArray)]
518
+ if (!foundClass && fontsClassesArray) {
519
+ foundClass = fontsClassesArray[findIndexOfClassName(genericClassName, fontsClassesArray)]
520
+ }
506
521
  }
522
+
523
+ if (foundClass) compoundClasses.push(justProperties(foundClass))
507
524
  }
508
525
  })
509
526
 
@@ -522,12 +539,54 @@ export function compileApplyDirectives(twClasses) {
522
539
  }
523
540
 
524
541
  twClassesArray[indexOfModifier] = _.replace(twClassesArray[indexOfModifier], /{_applyProperties_}/, fixDuplicateKeys(compoundClasses).join(', '))
542
+ twClassesArray[indexOfModifier] = deduplicateLineProperties(twClassesArray[indexOfModifier])
525
543
  }
526
544
  })
527
545
 
528
546
  return twClassesArray.join('\n')
529
547
  }
530
548
 
549
+ /**
550
+ * Remove duplicate property keys in a TSS line, keeping the last occurrence.
551
+ * This ensures apply directives override static defaults (e.g. backgroundColor).
552
+ */
553
+ function deduplicateLineProperties(line) {
554
+ const match = line.match(/^(.*?\{)\s*(.*)\s*(\})$/)
555
+ if (!match) return line
556
+
557
+ const prefix = match[1]
558
+ const propsStr = match[2]
559
+ const suffix = match[3]
560
+
561
+ // Split by comma respecting nested braces
562
+ const props = []
563
+ let depth = 0
564
+ let current = ''
565
+ for (const char of propsStr) {
566
+ if (char === '{') depth++
567
+ else if (char === '}') depth--
568
+ else if (char === ',' && depth === 0) {
569
+ if (current.trim()) props.push(current.trim())
570
+ current = ''
571
+ continue
572
+ }
573
+ current += char
574
+ }
575
+ if (current.trim()) props.push(current.trim())
576
+
577
+ // Keep last occurrence of each key
578
+ const seen = new Map()
579
+ props.forEach(prop => {
580
+ const colonIdx = prop.indexOf(':')
581
+ if (colonIdx > -1) {
582
+ const key = prop.substring(0, colonIdx).trim()
583
+ seen.set(key, prop)
584
+ }
585
+ })
586
+
587
+ return prefix + ' ' + [...seen.values()].join(', ') + ' ' + suffix
588
+ }
589
+
531
590
  export function justProperties(_foundClass) {
532
591
  return _foundClass.match(/{(.*)}/)[1].trim()
533
592
  }