1 | 'use strict'
|
2 |
|
3 | let Container = require('./container')
|
4 |
|
5 | let LazyResult, Processor
|
6 |
|
7 | class Root extends Container {
|
8 | constructor(defaults) {
|
9 | super(defaults)
|
10 | this.type = 'root'
|
11 | if (!this.nodes) this.nodes = []
|
12 | }
|
13 |
|
14 | normalize(child, sample, type) {
|
15 | let nodes = super.normalize(child)
|
16 |
|
17 | if (sample) {
|
18 | if (type === 'prepend') {
|
19 | if (this.nodes.length > 1) {
|
20 | sample.raws.before = this.nodes[1].raws.before
|
21 | } else {
|
22 | delete sample.raws.before
|
23 | }
|
24 | } else if (this.first !== sample) {
|
25 | for (let node of nodes) {
|
26 | node.raws.before = sample.raws.before
|
27 | }
|
28 | }
|
29 | }
|
30 |
|
31 | return nodes
|
32 | }
|
33 |
|
34 | removeChild(child, ignore) {
|
35 | let index = this.index(child)
|
36 |
|
37 | if (!ignore && index === 0 && this.nodes.length > 1) {
|
38 | this.nodes[1].raws.before = this.nodes[index].raws.before
|
39 | }
|
40 |
|
41 | return super.removeChild(child)
|
42 | }
|
43 |
|
44 | toResult(opts = {}) {
|
45 | let lazy = new LazyResult(new Processor(), this, opts)
|
46 | return lazy.stringify()
|
47 | }
|
48 | }
|
49 |
|
50 | Root.registerLazyResult = dependant => {
|
51 | LazyResult = dependant
|
52 | }
|
53 |
|
54 | Root.registerProcessor = dependant => {
|
55 | Processor = dependant
|
56 | }
|
57 |
|
58 | module.exports = Root
|
59 | Root.default = Root
|
60 |
|
61 | Container.registerRoot(Root)
|