css冻结,css到json解析器冻结浏览器窗口

我在几天前发现了一个非常好的方法来解析css -string(甚至嵌套)到json。但是,似乎有一个问题在其中。css到json解析器冻结浏览器窗口

如果我们试图解析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;

}

2013-07-05

yckart

+0

您是否尝试过调试代码并检查浏览器卡在哪一行? –

+0

@jbkkd当然,我试过......但是,调试这样的东西并不容易,在不到一秒的时间内就会杀死每个浏览器实例;) –

+0

请参阅下面的答案。 –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值