postcss 8.4.48 → 8.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.
package/lib/input.d.ts CHANGED
@@ -62,6 +62,17 @@ declare class Input_ {
62
62
  */
63
63
  css: string
64
64
 
65
+ /**
66
+ * Input source with support for non-CSS documents.
67
+ *
68
+ * ```js
69
+ * const input = postcss.parse('a{}', { from: file, document: '<style>a {}</style>' }).input
70
+ * input.document //=> "<style>a {}</style>"
71
+ * input.css //=> "a{}"
72
+ * ```
73
+ */
74
+ document: string
75
+
65
76
  /**
66
77
  * The absolute path to the CSS source file defined
67
78
  * with the `from` option.
package/lib/input.js CHANGED
@@ -33,6 +33,9 @@ class Input {
33
33
  this.hasBOM = false
34
34
  }
35
35
 
36
+ this.document = this.css
37
+ if (opts.document) this.document = opts.document.toString()
38
+
36
39
  if (opts.from) {
37
40
  if (
38
41
  !pathAvailable ||
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.document.slice(
213
+ sourceOffset(this.source.input.document, this.source.start),
214
+ sourceOffset(this.source.input.document, this.source.end)
215
+ )
183
216
  let index = stringRepresentation.indexOf(opts.word)
184
217
  if (index !== -1) pos = this.positionInside(index)
185
218
  }
@@ -189,11 +222,11 @@ 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.document, this.source.start)
193
226
  let end = offset + index
194
227
 
195
228
  for (let i = offset; i < end; i++) {
196
- if (this.source.input.css[i] === '\n') {
229
+ if (this.source.input.document[i] === '\n') {
197
230
  column = 1
198
231
  line += 1
199
232
  } else {
@@ -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.document.slice(
263
+ sourceOffset(this.source.input.document, this.source.start),
264
+ sourceOffset(this.source.input.document, 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/postcss.d.ts CHANGED
@@ -229,7 +229,7 @@ declare namespace postcss {
229
229
  export interface Parser<RootNode = Document | Root> {
230
230
  (
231
231
  css: { toString(): string } | string,
232
- opts?: Pick<ProcessOptions, 'from' | 'map'>
232
+ opts?: Pick<ProcessOptions, 'document' | 'from' | 'map'>
233
233
  ): RootNode
234
234
  }
235
235
 
@@ -315,6 +315,11 @@ declare namespace postcss {
315
315
  }
316
316
 
317
317
  export interface ProcessOptions<RootNode = Document | Root> {
318
+ /**
319
+ * Input file if it is not simple CSS file, but HTML with <style> or JS with CSS-in-JS blocks.
320
+ */
321
+ document?: string
322
+
318
323
  /**
319
324
  * The path of the CSS source file. You should always set `from`,
320
325
  * because it is used in source map generation and syntax error messages.
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.5.0'
11
11
  this.plugins = this.normalize(plugins)
12
12
  }
13
13
 
package/lib/rule.d.ts CHANGED
@@ -22,7 +22,7 @@ declare namespace Rule {
22
22
  between?: string
23
23
 
24
24
  /**
25
- * Contains `true` if there is semicolon after rule.
25
+ * Contains the text of the semicolon after this rule.
26
26
  */
27
27
  ownSemicolon?: string
28
28
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss",
3
- "version": "8.4.48",
3
+ "version": "8.5.0",
4
4
  "description": "Tool for transforming styles with JS plugins",
5
5
  "engines": {
6
6
  "node": "^10 || ^12 || >=14"
@@ -74,7 +74,7 @@
74
74
  "url": "https://github.com/postcss/postcss/issues"
75
75
  },
76
76
  "dependencies": {
77
- "nanoid": "^3.3.7",
77
+ "nanoid": "^3.3.8",
78
78
  "picocolors": "^1.1.1",
79
79
  "source-map-js": "^1.2.1"
80
80
  },