css到json解析器冻结浏览器窗口
问题描述:
我在几天前发现了一个非常好的方法来解析css
-string(甚至嵌套)到json
。但是,似乎有一个问题在其中。css到json解析器冻结浏览器窗口
https://github.com/csvplot/cml-parse
如果我们试图解析css
-string,它会杀死的浏览器窗口,不知道是怎么回事......我已经运行结束an issue但有没有人来回答问题,因为维护者David Ellis丢失了。
任何意见/建议?
function parse(data) {
var stateStack = [];
var scopeStack = [];
var outObj = {};
while(data) {
// Grab current number of indentation characters
/^(\s*)/.test(data);
// If we've entered any state, and that state is not an explicit block declaration ({}'s) and we have an indent level smaller than the most recent indent level,
// then remove the most recent scope level and recall the state back to the previous scope's state
if(stateStack.length &&
stateStack[stateStack.length-1] !== 'explicitBlock' &&
scopeStack.length &&
RegExp.$1.length < scopeStack[scopeStack.length-1].indent) {
scopeStack.pop();
while(stateStack.length && (stateStack[stateStack.length-1] !== 'block' || stateStack[stateStack.length-1] !== 'explicitBlock')) {
stateStack.pop();
}
}
// If current chunk is the key to an object
if(/^(\s*)([^:]*)\s*([{\n])/.test(data)) {
// Grab the indent size of the key and the current outObj position from the scope stack
var indentLength = RegExp.$1.length;
var currScope = (scopeStack.length ? scopeStack[scopeStack.length-1].ref : outObj);
// Split the identifier by spaces and construct/traverse down the defined path
// TODO: Figure out how to handle commas that define the same inner content along multiple paths
RegExp.$2.split(/\s*/).forEach(function(scope) {
if(scope !== '') {
currScope[scope] = currScope[scope] || {};
currScope = currScope[scope];
}
});
// Push the deepest scope and the current indent length onto the scope stack, and push the explicitBlock vs block state onto the state stack
// TODO: Work on a state diagram to truly handle all of the possible states involved properly
scopeStack.push({ref: currScope, indent: indentLength});
stateStack.push(RegExp.$3 === '{' ? 'explicitBlock' : 'block');
// Rip out the handled chunk of data from the string
data = data.replace(/^\s*[^:]*\s*[{\n]/, '');
}
}
return data;
}
答
运行代码,它看起来像它只是不工作。
它,因为这正则表达式是第一次运行后未能达到一个无限循环:
if(/^(\s*)([^:]*)\s*([{\n])/.test(data)) {
因此,为什么浏览器被卡住。它也不会返回正确的JSON。
我建议你自己写这样的东西,或试图调试和修复现有的代码。
您是否尝试过调试代码并检查浏览器卡在哪一行? – jbkkd
@jbkkd当然,我试过......但是,调试这样的东西并不容易,在不到一秒的时间内就会杀死每个浏览器实例;) – yckart
请参阅下面的答案。 – jbkkd