Why doesn't IE7 copy
 blocks to the clipboard correctly?

后端 未结 7 1075
一生所求
一生所求 2021-02-03 19:20

We\'ve noticed that IE7 has an odd behavor with code blocks posted on Stack Overflow. For example, this little code block:

public PageSizer(string href, int inde         


        
7条回答
  •  一整个雨季
    2021-02-03 20:17

    It seems that this is a known bug for IE6 and prettify.js has a workaround for it. Specifically it replaces the BR tags with '\r\n'.

    By modifying the check to allow for IE6 or 7 then the cut-and-paste will work correctly from IE7, but it will render with a newline followed by a space. By checking for IE7 and providing just a '\r' instead of a '\r\n' it will continue to cut-and-paste and render correctly.

    Add this code to prettify.js:

    function _pr_isIE7() {
      var isIE7 = navigator && navigator.userAgent &&
           /\bMSIE 7\./.test(navigator.userAgent);
      _pr_isIE7 = function () { return isIE7; };
      return isIE7;
    }
    

    and then modify the prettyPrint function as follows:

       function prettyPrint(opt_whenDone) {
         var isIE6 = _pr_isIE6();
    +    var isIE7 = _pr_isIE7();
    

    ...

    -        if (isIE6 && cs.tagName === 'PRE') {
    +        if ((isIE6 || isIE7) && cs.tagName === 'PRE') {
              var lineBreaks = cs.getElementsByTagName('br');
    +         var newline;
    +         if (isIE6) {
    +           newline = '\r\n';
    +         } else {
    +           newline = '\r';
    +         }
              for (var j = lineBreaks.length; --j >= 0;) {
                var lineBreak = lineBreaks[j];
                lineBreak.parentNode.replaceChild(
    -               document.createTextNode('\r\n'), lineBreak);
    +               document.createTextNode(newline), lineBreak);
              }
    

    You can see a working example here.

    Note: I haven't tested the original workaround in IE6, so I'm guessing it renders without the space caused by the '\n' that is seen in IE7, otherwise the fix is simpler.

提交回复
热议问题