wishthis/node_modules/rtlcss/lib/state.js

51 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-01-21 08:28:41 +00:00
'use strict'
2022-06-08 10:36:39 +00:00
const directiveParser = require('./directive-parser.js')
2022-01-21 08:28:41 +00:00
module.exports = {
stack: [],
2022-06-08 10:36:39 +00:00
pop (current) {
const index = this.stack.indexOf(current)
2022-01-21 08:28:41 +00:00
if (index !== -1) {
this.stack.splice(index, 1)
}
2022-06-08 10:36:39 +00:00
2022-01-21 08:28:41 +00:00
if (!current.preserve) {
current.source.remove()
}
},
2022-06-08 10:36:39 +00:00
parse (node, lazyResult, callback) {
let current
const metadata = directiveParser(node)
2022-01-21 08:28:41 +00:00
if (metadata) {
if (!metadata.begin && metadata.end) {
2022-06-08 10:36:39 +00:00
this.walk((item) => {
2022-01-21 08:28:41 +00:00
if (metadata.name === item.metadata.name) {
this.pop(item)
2022-06-08 10:36:39 +00:00
current = { metadata, directive: item.directive, source: node, preserve: item.preserve }
2022-01-21 08:28:41 +00:00
return false
}
2022-06-08 10:36:39 +00:00
})
2022-01-21 08:28:41 +00:00
} else {
2022-06-08 10:36:39 +00:00
current = { metadata, directive: null, source: node, preserve: null }
2022-01-21 08:28:41 +00:00
}
if (current === undefined) {
2022-06-08 10:36:39 +00:00
lazyResult.warn(`found end "${metadata.name}" without a matching begin.`, { node: node })
2022-01-21 08:28:41 +00:00
} else if (callback(current)) {
this.stack.push(current)
} else if (!current.preserve) {
current.source.remove()
}
}
},
2022-06-08 10:36:39 +00:00
walk (callback) {
let len = this.stack.length
2022-01-21 08:28:41 +00:00
while (--len > -1) {
if (!callback(this.stack[len])) {
break
}
}
}
}