Skip to content
This repository was archived by the owner on Apr 22, 2020. It is now read-only.

Commit 81a9a8b

Browse files
author
mikesamuel
committed
Fixed copy and paste of prettified markup on IE.
If the output is a <pre> containing <br>s, IE 6 will lose newlines when converting the clipboard's content to plain text. Added a step to rewrite <br> to CRLF on IE 6 only.
1 parent 78b5e48 commit 81a9a8b

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

src/prettify.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,17 @@ var PR_ATTRIB_VALUE = 'atv';
110110
/** the number of characters between tab columns */
111111
var PR_TAB_WIDTH = 8;
112112

113-
// some string utilities
114113
function PR_isWordChar(ch) {
115114
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
116115
}
117116

117+
function PR_isIE6() {
118+
var isIE6 = navigator && navigator.userAgent
119+
&& /\bMSIE 6\./.test(navigator.userAgent);
120+
PR_isIE6 = function () { return isIE6; }
121+
return isIE6;
122+
}
123+
118124
/** Splice one array into another.
119125
* Like the python <code>
120126
* container[containerPosition:containerPosition + countReplaced] = inserted
@@ -827,7 +833,7 @@ function PR_recombineTagsAndDecorations(
827833
var htmlChunk = PR_textToHtml(
828834
tabExpander(sourceText.substring(outputIdx, sourceIdx)))
829835
.replace(/(\r\n?|\n| ) /g, '$1&nbsp;')
830-
.replace(/\r\n?|\n/g, '<br>');
836+
.replace(/\r\n?|\n/g, '<br />');
831837
html.push(htmlChunk);
832838
outputIdx = sourceIdx;
833839
}
@@ -928,6 +934,8 @@ var PR_SHOULD_USE_CONTINUATION = true;
928934
* has been finished.
929935
*/
930936
function prettyPrint(opt_whenDone) {
937+
var isIE6 = PR_isIE6();
938+
931939
// fetch a list of nodes to rewrite
932940
var codeSegments = [
933941
document.getElementsByTagName('pre'),
@@ -988,8 +996,24 @@ function prettyPrint(opt_whenDone) {
988996
}
989997
}
990998
pre.innerHTML = newContent;
999+
9911000
// remove the old
9921001
cs.parentNode.replaceChild(pre, cs);
1002+
pre = cs;
1003+
}
1004+
1005+
// Replace <br>s with line-feeds so that copying and pasting works on
1006+
// IE 6.
1007+
// Doing this on other browsers breaks lots of stuff since \r\n is
1008+
// treated as two newlines on Firefox, and doing this also slows down
1009+
// rendering.
1010+
if (isIE6 && cs.tagName === 'PRE') {
1011+
var lineBreaks = cs.getElementsByTagName('br');
1012+
for (var i = lineBreaks.length; --i >= 0;) {
1013+
var lineBreak = lineBreaks[i];
1014+
lineBreak.parentNode.replaceChild(
1015+
document.createTextNode('\r\n'), lineBreak);
1016+
}
9931017
}
9941018
}
9951019
}

tests/prettify_test.html

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
<title>Code Prettifier</title>
55
<script src="../src/prettify.js" type="text/javascript"
66
onerror="alert('Error: failed to load ' + this.src)"></script>
7-
<script type="Text/javascript">
7+
<script type="text/javascript">
88
// get accurate timing
99
PR_SHOULD_USE_CONTINUATION = false;
10+
function PR_isIE6() { return false; } // Ensure consistent output.
1011
</script>
1112
<link rel="stylesheet" type="text/css" href="../src/prettify.css" />
1213
<style type="text/css">
@@ -458,6 +459,21 @@ <h1>Bug 14b - comments not ignored</h1>
458459
&lt;p&gt;And another one inside the end tag&lt;/p<!-- GOTCHA!! -->&gt;
459460
</pre>
460461

462+
<h1>Bug 20 - missing blank lines</h1>
463+
<pre class="prettyprint" id="issue20"
464+
>&lt;html&gt;
465+
466+
&lt;head&gt;</pre>
467+
468+
<h1>Bug 21 - code doesn't copy and paste well in IE</h1>
469+
<pre class="prettyprint" id="issue21"
470+
>&lt;html&gt;
471+
&lt;head&gt;
472+
&lt;title&gt;Test&lt;/title&gt;
473+
&lt;/head&gt;
474+
&lt;/html&gt;</pre>
475+
<p>To test this bug, disable overriding of PR_isIE6 above, and copy and paste
476+
the above into Notepad.</p>
461477

462478
</body>
463479

@@ -1049,9 +1065,21 @@ <h1>Bug 14b - comments not ignored</h1>
10491065
'`END`PUN{}`END'),
10501066
issue14b: (
10511067
'`COM&lt;!-- There\'s an `END<acronym title="tag soup">`COMHTML`END' +
1052-
'</acronym>`COM comment in my comment --&gt;`END`PLN<br>' +
1053-
'`END`PUN&lt;`END`TAGp`END`PUN&gt;`END`PLNAnd another one inside the end tag`END' +
1054-
'`PUN&lt;/`END`TAGp`END`PUN&gt;`END')
1068+
'</acronym>`COM comment in my comment --&gt;`END`PLN<br>' +
1069+
'`END`PUN&lt;`END`TAGp`END`PUN&gt;`END' +
1070+
'`PLNAnd another one inside the end tag`END' +
1071+
'`PUN&lt;/`END`TAGp`END`PUN&gt;`END'),
1072+
issue20: (
1073+
'`PUN&lt;`END`TAGhtml`END`PUN&gt;`END`PLN<br>' +
1074+
'<br>' +
1075+
'`END`PUN&lt;`END`TAGhead`END`PUN&gt;`END'),
1076+
issue21: (
1077+
'`PUN&lt;`END`TAGhtml`END`PUN&gt;`END`PLN<br>' +
1078+
'&nbsp; `END`PUN&lt;`END`TAGhead`END`PUN&gt;`END`PLN<br>' +
1079+
'&nbsp; &nbsp; `END`PUN&lt;`END`TAGtitle`END`PUN&gt;`END`PLNTest`END' +
1080+
'`PUN&lt;/`END`TAGtitle`END`PUN&gt;`END`PLN<br>' +
1081+
'&nbsp; `END`PUN&lt;/`END`TAGhead`END`PUN&gt;`END`PLN<br>' +
1082+
'`END`PUN&lt;/`END`TAGhtml`END`PUN&gt;`END')
10551083
};
10561084

10571085

0 commit comments

Comments
 (0)