You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.2 KiB

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