-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.js
More file actions
67 lines (58 loc) · 2.1 KB
/
Copy pathutility.js
File metadata and controls
67 lines (58 loc) · 2.1 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
export const isPropertySetInCss = (css, selector, property, value) => {
// convert to string remove spaces and change to lowercase
css = removeSpacesAndLowerCase(css);
property = removeSpacesAndLowerCase(property);
value = removeSpacesAndLowerCase(value);
// generate the properties object for the selector
const properties = {};
const parts = css.split(selector + '{');
const subPart = parts[1].split('}');
const propertiesRaw = subPart[0].split(';');
propertiesRaw.forEach((item) => {
const colonIndex = item.indexOf(':');
const prop = item.slice(0, colonIndex);
const value = item.slice(colonIndex + 1);
properties[prop] = value;
});
//check value
if (properties[property] && properties[property] == value) {
return true;
}
return properties[property];
};
const removeSpacesAndLowerCase = (str) => {
return str
.toString()
.replaceAll(/\/\*[^*]*\*+([^/*][^*]*\*+)*\//g, '')
.replaceAll(/(?:\r\n|\r|\n| )/g, '')
.toLowerCase();
};
export const isMediaRuleCorrect = (css, property, value) => {
// convert to string remove spaces and change to lowercase
css = removeSpacesAndLowerCase(css);
property = removeSpacesAndLowerCase(property);
value = removeSpacesAndLowerCase(value);
const parts = css.split('{');
return parts[0].includes(property + ':' + value);
};
export const getPropertyValue = (css, selector, property) => {
// Normalize selector and property to lowercase for matching
selector = selector.toLowerCase().trim();
property = property.toLowerCase().trim();
// Split the CSS into individual rules
const rules = css.split('}').map((rule) => rule.trim() + '}');
for (let rule of rules) {
if (rule.toLowerCase().includes(selector)) {
// Extract the properties block
const propertiesBlock = rule.split('{')[1].split('}')[0];
const properties = propertiesBlock.split(';').map((prop) => prop.trim());
for (let prop of properties) {
const [key, value] = prop.split(':').map((p) => p.trim());
if (key.toLowerCase() === property) {
return value;
}
}
}
}
return null;
};