@tbela99/css-parser 1.3.4 → 1.4.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.
@@ -10,6 +10,9 @@ function dirname(path) {
10
10
  //
11
11
  // return path;
12
12
  // }
13
+ if (path === '') {
14
+ return '';
15
+ }
13
16
  let i = 0;
14
17
  let parts = [''];
15
18
  for (; i < path.length; i++) {
@@ -34,6 +37,12 @@ function dirname(path) {
34
37
  * @private
35
38
  */
36
39
  function splitPath(result) {
40
+ if (result.length == 0) {
41
+ return { parts: [], i: 0 };
42
+ }
43
+ if (result === '/') {
44
+ return { parts: ['/'], i: 0 };
45
+ }
37
46
  const parts = [''];
38
47
  let i = 0;
39
48
  for (; i < result.length; i++) {
@@ -77,6 +86,21 @@ function resolve(url, currentDirectory, cwd) {
77
86
  }
78
87
  cwd ??= '';
79
88
  currentDirectory ??= '';
89
+ if (currentDirectory !== '' && url.startsWith(currentDirectory + '/')) {
90
+ return {
91
+ absolute: url,
92
+ relative: url.slice(currentDirectory.length + 1)
93
+ };
94
+ }
95
+ if (currentDirectory === '' && cwd !== '' && url.startsWith(cwd == '/' ? cwd : cwd + '/')) {
96
+ cwd = normalize(cwd);
97
+ const absolute = normalize(url);
98
+ const prefix = cwd == '/' ? cwd : cwd + '/';
99
+ return {
100
+ absolute,
101
+ relative: absolute.startsWith(prefix) ? absolute.slice(prefix.length) : diff(absolute, cwd)
102
+ };
103
+ }
80
104
  if (matchUrl.test(currentDirectory)) {
81
105
  const path = new URL(url, currentDirectory).href;
82
106
  return {
@@ -91,9 +115,15 @@ function resolve(url, currentDirectory, cwd) {
91
115
  else if (currentDirectory.charAt(0) == '/') {
92
116
  result = dirname(currentDirectory) + '/' + url;
93
117
  }
94
- let { parts, i } = splitPath(result);
95
- const absolute = parts.join('/');
96
- const { parts: dirs } = splitPath(cwd ?? currentDirectory);
118
+ const absolute = normalize(result);
119
+ return {
120
+ absolute,
121
+ relative: absolute === '' ? '' : diff(absolute, cwd ?? currentDirectory),
122
+ };
123
+ }
124
+ function diff(path1, path2) {
125
+ let { parts } = splitPath(path1);
126
+ const { parts: dirs } = splitPath(path2);
97
127
  for (const p of dirs) {
98
128
  if (parts[0] == p) {
99
129
  parts.shift();
@@ -102,10 +132,36 @@ function resolve(url, currentDirectory, cwd) {
102
132
  parts.unshift('..');
103
133
  }
104
134
  }
105
- return {
106
- absolute,
107
- relative: parts.join('/') + (i < result.length ? result.slice(i) : '')
108
- };
135
+ return parts.join('/');
136
+ }
137
+ function normalize(path) {
138
+ let parts = [];
139
+ let i = 0;
140
+ for (; i < path.length; i++) {
141
+ const chr = path.charAt(i);
142
+ if (chr == '/') {
143
+ if (parts.length == 0 || parts[parts.length - 1] !== '') {
144
+ parts.push('');
145
+ }
146
+ }
147
+ else if (chr == '?' || chr == '#') {
148
+ break;
149
+ }
150
+ else {
151
+ parts[parts.length - 1] += chr;
152
+ }
153
+ }
154
+ let k = -1;
155
+ while (++k < parts.length) {
156
+ if (parts[k] == '.') {
157
+ parts.splice(k--, 1);
158
+ }
159
+ else if (parts[k] == '..') {
160
+ parts.splice(k - 1, 2);
161
+ k -= 2;
162
+ }
163
+ }
164
+ return (path.charAt(0) == '/' ? '/' : '') + parts.join('/');
109
165
  }
110
166
 
111
167
  export { dirname, matchUrl, resolve };
@@ -25,9 +25,12 @@ class PropertyList {
25
25
  });
26
26
  }
27
27
  add(...declarations) {
28
+ let name;
28
29
  for (const declaration of declarations) {
30
+ name = declaration.typ != EnumToken.DeclarationNodeType ? null : declaration.nam.toLowerCase();
29
31
  if (declaration.typ != EnumToken.DeclarationNodeType ||
30
- (typeof this.options.removeDuplicateDeclarations === 'string' && this.options.removeDuplicateDeclarations === declaration.nam.toLowerCase()) ||
32
+ 'composes' === name ||
33
+ (typeof this.options.removeDuplicateDeclarations === 'string' && this.options.removeDuplicateDeclarations === name) ||
31
34
  (Array.isArray(this.options.removeDuplicateDeclarations) ? this.options.removeDuplicateDeclarations.includes(declaration.nam) : !this.options.removeDuplicateDeclarations)) {
32
35
  this.declarations.set(Number(Math.random().toString().slice(2)).toString(36), declaration);
33
36
  continue;