postcss 8.4.48 → 8.4.49

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.
package/lib/node.js CHANGED
@@ -32,6 +32,36 @@ function cloneNode(obj, parent) {
32
32
  return cloned
33
33
  }
34
34
 
35
+ function sourceOffset(inputCSS, position) {
36
+ // Not all custom syntaxes support `offset` in `source.start` and `source.end`
37
+ if (
38
+ position &&
39
+ typeof position.offset !== 'undefined'
40
+ ) {
41
+ return position.offset;
42
+ }
43
+
44
+ let column = 1
45
+ let line = 1
46
+ let offset = 0
47
+
48
+ for (let i = 0; i < inputCSS.length; i++) {
49
+ if (line === position.line && column === position.column) {
50
+ offset = i
51
+ break
52
+ }
53
+
54
+ if (inputCSS[i] === '\n') {
55
+ column = 1
56
+ line += 1
57
+ } else {
58
+ column += 1
59
+ }
60
+ }
61
+
62
+ return offset
63
+ }
64
+
35
65
  class Node {
36
66
  constructor(defaults = {}) {
37
67
  this.raws = {}
@@ -174,12 +204,15 @@ class Node {
174
204
  return this.parent.nodes[index + 1]
175
205
  }
176
206
 
177
- positionBy(opts, stringRepresentation) {
207
+ positionBy(opts) {
178
208
  let pos = this.source.start
179
209
  if (opts.index) {
180
210
  pos = this.positionInside(opts.index)
181
211
  } else if (opts.word) {
182
- stringRepresentation = this.source.input.css.slice(this.source.start.offset, this.source.end.offset)
212
+ let stringRepresentation = this.source.input.css.slice(
213
+ sourceOffset(this.source.input.css, this.source.start),
214
+ sourceOffset(this.source.input.css, this.source.end)
215
+ )
183
216
  let index = stringRepresentation.indexOf(opts.word)
184
217
  if (index !== -1) pos = this.positionInside(index)
185
218
  }
@@ -189,7 +222,7 @@ class Node {
189
222
  positionInside(index) {
190
223
  let column = this.source.start.column
191
224
  let line = this.source.start.line
192
- let offset = this.source.start.offset
225
+ let offset = sourceOffset(this.source.input.css, this.source.start)
193
226
  let end = offset + index
194
227
 
195
228
  for (let i = offset; i < end; i++) {
@@ -226,13 +259,15 @@ class Node {
226
259
  }
227
260
 
228
261
  if (opts.word) {
229
- let stringRepresentation = this.source.input.css.slice(this.source.start.offset, this.source.end.offset)
262
+ let stringRepresentation = this.source.input.css.slice(
263
+ sourceOffset(this.source.input.css, this.source.start),
264
+ sourceOffset(this.source.input.css, this.source.end)
265
+ )
230
266
  let index = stringRepresentation.indexOf(opts.word)
231
267
  if (index !== -1) {
232
- start = this.positionInside(index, stringRepresentation)
268
+ start = this.positionInside(index)
233
269
  end = this.positionInside(
234
270
  index + opts.word.length,
235
- stringRepresentation
236
271
  )
237
272
  }
238
273
  } else {
package/lib/processor.js CHANGED
@@ -7,7 +7,7 @@ let Root = require('./root')
7
7
 
8
8
  class Processor {
9
9
  constructor(plugins = []) {
10
- this.version = '8.4.48'
10
+ this.version = '8.4.49'
11
11
  this.plugins = this.normalize(plugins)
12
12
  }
13
13
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss",
3
- "version": "8.4.48",
3
+ "version": "8.4.49",
4
4
  "description": "Tool for transforming styles with JS plugins",
5
5
  "engines": {
6
6
  "node": "^10 || ^12 || >=14"