-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathatrules.ts
More file actions
108 lines (96 loc) · 2.78 KB
/
Copy pathatrules.ts
File metadata and controls
108 lines (96 loc) · 2.78 KB
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
import {
str_equals,
walk,
BREAK,
is_identifier,
is_media_feature,
is_media_type,
is_supports_query,
is_dimension,
is_number,
type CSSNode,
} from '@projectwallace/css-parser'
/**
* Check if an @supports atRule is a browserhack (Wallace parser version)
* @param prelude - The Atrule CSSNode from Wallace parser
*/
export function isSupportsBrowserhack(prelude: CSSNode, on_hack: (hack: string) => void): void {
walk(prelude, function (node) {
// Check SupportsQuery nodes for browserhack patterns
if (is_supports_query(node)) {
const normalizedPrelude = node.value.toString().toLowerCase().replaceAll(/\s+/g, '')
// Check for known browserhack patterns
if (normalizedPrelude.includes('-webkit-appearance:none')) {
on_hack('-webkit-appearance: none')
return BREAK
}
if (normalizedPrelude.includes('-moz-appearance:meterbar')) {
on_hack('-moz-appearance: meterbar')
return BREAK
}
}
})
}
/**
* Check if a @media atRule is a browserhack (Wallace parser version)
* @param prelude - The Atrule CSSNode from Wallace parser
* @returns true if the atrule is a browserhack
*/
export function isMediaBrowserhack(prelude: CSSNode, on_hack: (hack: string) => void): void {
walk(prelude, function (node) {
// Check MediaType nodes for \0 prefix or \9 suffix
if (is_media_type(node)) {
const text = node.value
if (text.startsWith('\\0')) {
on_hack('\\0')
return BREAK
}
if (text.includes('\\9')) {
on_hack('\\9')
return BREAK
}
}
// Check Feature nodes
if (is_media_feature(node)) {
const name = node.property
if (str_equals('-moz-images-in-menus', name)) {
on_hack('-moz-images-in-menus')
return BREAK
}
if (str_equals('min--moz-device-pixel-ratio', name)) {
on_hack('min--moz-device-pixel-ratio')
return BREAK
}
// Check for vendor-specific feature hacks
if (str_equals('-ms-high-contrast', name)) {
on_hack('-ms-high-contrast')
return BREAK
}
// Check for min-resolution with .001dpcm
if (str_equals('min-resolution', name) && node.value !== null && is_dimension(node.value)) {
const dimension = node.value
if (dimension.value === 0.001 && str_equals('dpcm', dimension.unit || '')) {
on_hack('min-resolution: .001dpcm')
return BREAK
}
}
// Check for -webkit-min-device-pixel-ratio with 0 or 10000
if (
str_equals('-webkit-min-device-pixel-ratio', name) &&
node.value !== null &&
is_number(node.value)
) {
const num = node.value.value
if (num === 0 || num === 10000) {
on_hack('-webkit-min-device-pixel-ratio')
return BREAK
}
}
}
// \0 inside a media feature value (e.g. min-width:0\0) — sibling of the numeric value
if (is_identifier(node) && node.text === '\\0') {
on_hack('\\0')
return BREAK
}
})
}