wishthis/node_modules/postcss/lib/root.js

62 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-08-17 09:47:40 +00:00
'use strict'
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
let Container = require('./container')
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
let LazyResult, Processor
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
class Root extends Container {
constructor(defaults) {
super(defaults)
this.type = 'root'
if (!this.nodes) this.nodes = []
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
normalize(child, sample, type) {
let nodes = super.normalize(child)
2022-01-21 08:28:41 +00:00
if (sample) {
if (type === 'prepend') {
if (this.nodes.length > 1) {
2023-08-17 09:47:40 +00:00
sample.raws.before = this.nodes[1].raws.before
2022-01-21 08:28:41 +00:00
} else {
2023-08-17 09:47:40 +00:00
delete sample.raws.before
2022-01-21 08:28:41 +00:00
}
} else if (this.first !== sample) {
2023-08-17 09:47:40 +00:00
for (let node of nodes) {
node.raws.before = sample.raws.before
2022-01-21 08:28:41 +00:00
}
}
}
2023-08-17 09:47:40 +00:00
return nodes
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
removeChild(child, ignore) {
let index = this.index(child)
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
if (!ignore && index === 0 && this.nodes.length > 1) {
this.nodes[1].raws.before = this.nodes[index].raws.before
}
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
return super.removeChild(child)
}
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
toResult(opts = {}) {
let lazy = new LazyResult(new Processor(), this, opts)
return lazy.stringify()
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
}
Root.registerLazyResult = dependant => {
LazyResult = dependant
}
Root.registerProcessor = dependant => {
Processor = dependant
}
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
module.exports = Root
Root.default = Root
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
Container.registerRoot(Root)