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

Commit 6bf1de6

Browse files
author
mikesamuel@gmail.com
committed
Rewrite newlines using OS specific line break sequence
1 parent 1e6036a commit 6bf1de6

3 files changed

Lines changed: 18 additions & 8 deletions

File tree

js-modules/extractSourceSpans.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function extractSourceSpans(node) {
7878
if (!isPreformatted) {
7979
text = text.replace(/[ \t\r\n]+/g, ' ');
8080
} else {
81-
text = text.replace(/\r\n?/g, '\r\n'); // Normalize newlines.
81+
text = text.replace(/\r\n?/g, '\n'); // Normalize newlines.
8282
}
8383
// TODO: handle tabs here?
8484
chunks[k] = text;
@@ -93,7 +93,7 @@ function extractSourceSpans(node) {
9393
walk(node);
9494

9595
return {
96-
source: chunks.join('').replace(/\r\n$/, ''),
96+
source: chunks.join('').replace(/\n$/, ''),
9797
spans: spans
9898
};
9999
}

js-modules/recombineTagsAndDecorations.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
* @private
1313
*/
1414
function recombineTagsAndDecorations(job) {
15+
var isIE = /\bMSIE\b/.test(navigator.userAgent);
16+
var newlineRe = /\n/g;
17+
1518
var source = job.source;
1619
var sourceLength = source.length;
1720
// Index into source after the last code-unit recombined.
@@ -29,7 +32,7 @@ function recombineTagsAndDecorations(job) {
2932

3033
// Simplify decorations.
3134
var decPos = 0;
32-
for (i = 0; i < nDecorations;) {
35+
for (var i = 0; i < nDecorations;) {
3336
// Skip over any zero-length decorations.
3437
var startPos = decorations[i];
3538
var start = i;
@@ -65,7 +68,9 @@ function recombineTagsAndDecorations(job) {
6568

6669
var textNode = spans[spanIndex + 1];
6770
if (textNode.nodeType !== 1) { // Don't muck with <BR>s or <LI>s
68-
textNode.nodeValue = source.substring(sourceIndex, end);
71+
var styledText = source.substring(sourceIndex, end);
72+
if (isIE) { styledText = styledText.replace(newLineRe, '\r\n'); }
73+
textNode.nodeValue = styledText;
6974
var document = textNode.ownerDocument;
7075
var span = document.createElement('SPAN');
7176
span.className = decorations[decorationIndex + 1];

src/prettify.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ window['PR']
516516
if (!isPreformatted) {
517517
text = text.replace(/[ \t\r\n]+/g, ' ');
518518
} else {
519-
text = text.replace(/\r\n?/g, '\r\n'); // Normalize newlines.
519+
text = text.replace(/\r\n?/g, '\n'); // Normalize newlines.
520520
}
521521
// TODO: handle tabs here?
522522
chunks[k] = text;
@@ -531,7 +531,7 @@ window['PR']
531531
walk(node);
532532

533533
return {
534-
source: chunks.join('').replace(/\r\n$/, ''),
534+
source: chunks.join('').replace(/\n$/, ''),
535535
spans: spans
536536
};
537537
}
@@ -1009,6 +1009,9 @@ window['PR']
10091009
* @private
10101010
*/
10111011
function recombineTagsAndDecorations(job) {
1012+
var isIE = /\bMSIE\b/.test(navigator.userAgent);
1013+
var newlineRe = /\r\n?|\n/g;
1014+
10121015
var source = job.source;
10131016
var sourceLength = source.length;
10141017
// Index into source after the last code-unit recombined.
@@ -1026,7 +1029,7 @@ window['PR']
10261029

10271030
// Simplify decorations.
10281031
var decPos = 0;
1029-
for (i = 0; i < nDecorations;) {
1032+
for (var i = 0; i < nDecorations;) {
10301033
// Skip over any zero-length decorations.
10311034
var startPos = decorations[i];
10321035
var start = i;
@@ -1062,7 +1065,9 @@ window['PR']
10621065

10631066
var textNode = spans[spanIndex + 1];
10641067
if (textNode.nodeType !== 1) { // Don't muck with <BR>s or <LI>s
1065-
textNode.nodeValue = source.substring(sourceIndex, end);
1068+
var styledText = source.substring(sourceIndex, end);
1069+
if (isIE) { styledText = styledText.replace(newLineRe, '\r\n'); }
1070+
textNode.nodeValue = styledText;
10661071
var document = textNode.ownerDocument;
10671072
var span = document.createElement('SPAN');
10681073
span.className = decorations[decorationIndex + 1];

0 commit comments

Comments
 (0)