【问题标题】:css to json parser freezes the browser-windowcss 到 json 解析器冻结浏览器窗口
【发布时间】:2013-07-05 07:13:41
【问题描述】:

几天前我发现了一个非常好的方法来解析css-strings(甚至嵌套)到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;
}

http://fiddle.jshell.net/5pTBr/

【问题讨论】:

  • 您是否尝试过调试代码并检查浏览器卡在哪一行?
  • @jbkkd 当然,我试过了......但是,调试这样的东西并不容易,不到一秒就会杀死每个浏览器实例;)
  • 这个有运气吗?

标签: javascript css json freeze css-parsing


【解决方案1】:

运行代码,看起来它只是不起作用。

由于此正则表达式在第一次运行后失败,因此进入了无限循环:

if(/^(\s*)([^:]*)\s*([{\n])/.test(data)) {

这就是为什么浏览器卡住了。它也不会返回正确的 JSON。

我建议您自己编写这样的代码,或者尝试调试和修复现有代码。

【讨论】:

    猜你喜欢
    • 2017-06-30
    • 2016-04-17
    • 1970-01-01
    • 2023-03-10
    • 2011-08-02
    • 2021-03-24
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    相关资源
    最近更新 更多