Comparing version 8.1.14 to 8.2.0
# Change Log | ||
This project adheres to [Semantic Versioning](https://semver.org/). | ||
## 8.2 “Prince Orobas” | ||
* Add `Node#toJSON()` and `postcss.fromJSON()` (by Niklas Mischkulnig). | ||
## 8.1.14 | ||
@@ -5,0 +8,0 @@ * Fixed parser performance regression. |
@@ -11,2 +11,4 @@ 'use strict' | ||
let fromOffsetCache = Symbol('fromOffset cache') | ||
class Input { | ||
@@ -53,38 +55,41 @@ constructor (css, opts = {}) { | ||
fromOffset (offset) { | ||
let lines = this.css.split('\n') | ||
let lineToIndex = new Array(lines.length) | ||
let prevIndex = 0 | ||
let lastLine, lineToIndex | ||
if (!this[fromOffsetCache]) { | ||
let lines = this.css.split('\n') | ||
lineToIndex = new Array(lines.length) | ||
let prevIndex = 0 | ||
for (let i = 0, l = lines.length; i < l; i++) { | ||
lineToIndex[i] = prevIndex | ||
prevIndex += lines[i].length + 1 | ||
for (let i = 0, l = lines.length; i < l; i++) { | ||
lineToIndex[i] = prevIndex | ||
prevIndex += lines[i].length + 1 | ||
} | ||
this[fromOffsetCache] = lineToIndex | ||
} else { | ||
lineToIndex = this[fromOffsetCache] | ||
} | ||
lastLine = lineToIndex[lineToIndex.length - 1] | ||
let lastLine = lineToIndex[lineToIndex.length - 1] | ||
this.fromOffset = index => { | ||
let min = 0 | ||
if (index >= lastLine) { | ||
min = lineToIndex.length - 1 | ||
} else { | ||
let max = lineToIndex.length - 2 | ||
let mid | ||
while (min < max) { | ||
mid = min + ((max - min) >> 1) | ||
if (index < lineToIndex[mid]) { | ||
max = mid - 1 | ||
} else if (index >= lineToIndex[mid + 1]) { | ||
min = mid + 1 | ||
} else { | ||
min = mid | ||
break | ||
} | ||
let min = 0 | ||
if (offset >= lastLine) { | ||
min = lineToIndex.length - 1 | ||
} else { | ||
let max = lineToIndex.length - 2 | ||
let mid | ||
while (min < max) { | ||
mid = min + ((max - min) >> 1) | ||
if (offset < lineToIndex[mid]) { | ||
max = mid - 1 | ||
} else if (offset >= lineToIndex[mid + 1]) { | ||
min = mid + 1 | ||
} else { | ||
min = mid | ||
break | ||
} | ||
} | ||
return { | ||
line: min + 1, | ||
col: index - lineToIndex[min] + 1 | ||
} | ||
} | ||
return this.fromOffset(offset) | ||
return { | ||
line: min + 1, | ||
col: offset - lineToIndex[min] + 1 | ||
} | ||
} | ||
@@ -173,2 +178,18 @@ | ||
} | ||
toJSON () { | ||
let json = {} | ||
for (let name of ['hasBOM', 'css', 'file', 'id']) { | ||
if (this[name] != null) { | ||
json[name] = this[name] | ||
} | ||
} | ||
if (this.map) { | ||
json.map = { ...this.map } | ||
if (json.map.consumerCache) { | ||
json.map.consumerCache = undefined | ||
} | ||
} | ||
return json | ||
} | ||
} | ||
@@ -175,0 +196,0 @@ |
@@ -190,2 +190,7 @@ 'use strict' | ||
fixed[name] = value.toJSON() | ||
} else if (this.type === 'root' && name === 'source') { | ||
fixed[name] = { | ||
input: value.input.toJSON(), | ||
start: value.start | ||
} | ||
} else { | ||
@@ -192,0 +197,0 @@ fixed[name] = value |
@@ -216,2 +216,6 @@ import { SourceMapGenerator, RawSourceMap } from 'source-map' | ||
export interface JSONHydrator { | ||
(data: object): Node | ||
} | ||
export interface Syntax { | ||
@@ -357,2 +361,13 @@ /** | ||
/** | ||
* Rehydrate a JSON AST (from `Node#toJSON`) back into the AST classes. | ||
* | ||
* ```js | ||
* const json = root.toJSON() | ||
* // save to file, send by network, etc | ||
* const root2 = postcss.fromJSON(json) | ||
* ``` | ||
*/ | ||
fromJSON: JSONHydrator | ||
/** | ||
* Contains the `list` module. | ||
@@ -417,2 +432,3 @@ */ | ||
export const parse: Parser | ||
export const fromJSON: JSONHydrator | ||
@@ -419,0 +435,0 @@ export const comment: Postcss['comment'] |
@@ -9,2 +9,3 @@ 'use strict' | ||
let stringify = require('./stringify') | ||
let fromJSON = require('./fromJSON') | ||
let Warning = require('./warning') | ||
@@ -66,2 +67,3 @@ let Comment = require('./comment') | ||
postcss.parse = parse | ||
postcss.fromJSON = fromJSON | ||
postcss.list = list | ||
@@ -68,0 +70,0 @@ |
@@ -8,3 +8,3 @@ 'use strict' | ||
constructor (plugins = []) { | ||
this.version = '8.1.14' | ||
this.version = '8.2.0' | ||
this.plugins = this.normalize(plugins) | ||
@@ -11,0 +11,0 @@ } |
{ | ||
"name": "postcss", | ||
"version": "8.1.14", | ||
"version": "8.2.0", | ||
"description": "Tool for transforming styles with JS plugins", | ||
@@ -5,0 +5,0 @@ "engines": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
202405
50
5754