Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

空格处理bug #4

Open
threeq opened this issue Sep 14, 2016 · 0 comments
Open

空格处理bug #4

threeq opened this issue Sep 14, 2016 · 0 comments

Comments

@threeq
Copy link
Contributor

threeq commented Sep 14, 2016

你好,这个我们在使用中发现一个bug,对于空格有一种情况没有处理,如下面这种情况:

<img src = "../images/icon_404.png" />

我们这边修改了一下,更改代码如下

function replaceSrc(fileContent, exclude) {
    fileContent = fileContent.replace(/((\<img[^\<\>]*? src)|(\<link[^\<\>]*? href))[\s]*=[\s]*\\?[\"\']?[^\'\"\<\>\+]+?\\?[\'\"][^\<\>]*?[/]?\>/ig, function(str){
        console.log('');
    console.log(str);
    var reg = /\s+((src)|(href))[\s]*=[\s]*\\?[\'\"][^\"\']+\\?[\'\"]/i;
        var regResult = reg.exec(str);

        if(!regResult) return str;
        var attrName = /\w+\s*=\s*/.exec(regResult[0])[0].replace(/\s*=\s*$/, '');
        var imgUrl = regResult[0].replace(/\w+\s*=\s*/, '').replace(/[\\\'\"]/g, '');
        if(!imgUrl) return str; // 避免空src引起编译失败
        if(/^(http(s?):)?\/\//.test(imgUrl)) return str; // 绝对路径的图片不处理
        if(!/\.(jpg|jpeg|png|gif|svg|webp)/i.test(imgUrl)) return str; // 非静态图片不处理
        if(exclude && imgUrl.indexOf(exclude) != -1) return str; // 不处理被排除的
    imgUrl = imgUrl.replace(/^\s*/g, ''); // 去掉左边空格

        if(!(/^[\.\/]/).test(imgUrl)) {
            imgUrl = './' + imgUrl;
        }
    var res = str.replace(reg, " "+attrName+"=\"+JSON.stringify(require("+JSON.stringify(imgUrl)+"))+\"");
    console.log(res);
        return res;
    });
    return fileContent;
}
@threeq threeq mentioned this issue Sep 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant