\u200b (Zero width space) characters in my JS code. Where did they come from?

前端 未结 5 645
失恋的感觉
失恋的感觉 2020-11-27 04:05

I am developing a front end of a web app using NetBeans IDE 7.0.1. Recently I had a very nasty bug, which I finally fixed.

Say I have code

var elemen         


        
5条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 05:07

    Here's a stab in the dark.

    My bet would be on Google Chrome Inspector. Searching through the Chromium source, I spotted the following block of code

        if (hasText)
            attrSpanElement.appendChild(document.createTextNode("=\u200B\""));
    
        if (linkify && (name === "src" || name === "href")) {
            var rewrittenHref = WebInspector.resourceURLForRelatedNode(node, value);
            value = value.replace(/([\/;:\)\]\}])/g, "$1\u200B");
            attrSpanElement.appendChild(linkify(rewrittenHref, value, "webkit-html-attribute-value", node.nodeName().toLowerCase() === "a"));
        } else {
            value = value.replace(/([\/;:\)\]\}])/g, "$1\u200B");
            var attrValueElement = attrSpanElement.createChild("span", "webkit-html-attribute-value");
            attrValueElement.textContent = value;
        }
    

    It's quite possible that I'm simply barking up the wrong tree here, but it looks like zero-width spaces were being inserted (to handle soft text wrapping?) during the display of attributes. Perhaps the "Copy as HTML" function had not properly removed them?


    Update

    After fiddling with the Chrome element inspector, I'm almost convinced that's where your stray \u200b came from. Notice how the line can wrap not only at visible space but also after = or chars matched by /([\/;:\)\]\}])/ thanks to the inserted zero-width space.

    chrome inspector screenshot

    Unfortunately, I am unable to replicate your problem where they inadvertently get included into your clipboard (I used Chrome 13.0.782.112 on Win XP).

    It would certainly be worth submitting a bug report should your be able to reproduce the behaviour.

提交回复
热议问题