wishthis/node_modules/rtlcss/lib/state.js

48 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-01-21 08:28:41 +00:00
'use strict'
2022-05-29 09:24:36 +00:00
var directiveParser = require('./directive-parser.js')
2022-01-21 08:28:41 +00:00
module.exports = {
stack: [],
2022-05-29 09:24:36 +00:00
pop: function (current) {
var index = this.stack.indexOf(current)
2022-01-21 08:28:41 +00:00
if (index !== -1) {
this.stack.splice(index, 1)
}
if (!current.preserve) {
current.source.remove()
}
},
2022-05-29 09:24:36 +00:00
parse: function (node, lazyResult, callback) {
var current
var metadata = directiveParser(node)
2022-01-21 08:28:41 +00:00
if (metadata) {
if (!metadata.begin && metadata.end) {
2022-05-29 09:24:36 +00:00
this.walk(function (item) {
2022-01-21 08:28:41 +00:00
if (metadata.name === item.metadata.name) {
this.pop(item)
2022-05-29 09:24:36 +00:00
current = {'metadata': metadata, 'directive': item.directive, 'source': node, 'preserve': item.preserve}
2022-01-21 08:28:41 +00:00
return false
}
2022-05-29 09:24:36 +00:00
}.bind(this))
2022-01-21 08:28:41 +00:00
} else {
2022-05-29 09:24:36 +00:00
current = {'metadata': metadata, 'directive': null, 'source': node, 'preserve': null}
2022-01-21 08:28:41 +00:00
}
if (current === undefined) {
2022-05-29 09:24:36 +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-05-29 09:24:36 +00:00
walk: function (callback) {
var len = this.stack.length
2022-01-21 08:28:41 +00:00
while (--len > -1) {
if (!callback(this.stack[len])) {
break
}
}
}
}