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

Commit 68cd862

Browse files
author
mikesamuel
committed
normalize newlines in innerHTML in firefox.
1 parent 5749ab1 commit 68cd862

2 files changed

Lines changed: 21 additions & 30 deletions

File tree

src/prettify.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,33 +1228,33 @@ function PR_lexOne(s) {
12281228
function prettyPrintOne(s) {
12291229
try {
12301230
var tokens = PR_lexOne(s);
1231-
var out = '';
1231+
var out = [];
12321232
var lastStyle = null;
12331233
for (var i = 0; i < tokens.length; i++) {
12341234
var t = tokens[i];
12351235
if (t.style != lastStyle) {
12361236
if (lastStyle != null) {
1237-
out += '</span>';
1237+
out.push('</span>');
12381238
}
12391239
if (t.style != null) {
1240-
out += '<span class=' + t.style + '>';
1240+
out.push('<span class=', t.style, '>');
12411241
}
12421242
lastStyle = t.style;
12431243
}
12441244
var html = t.token;
12451245
if (null != t.style) {
1246-
// This interacts badly with the wiki which introduces paragraph tags
1247-
// int pre blocks for some strange reason.
1246+
// This interacts badly with some wikis which introduces paragraph tags
1247+
// into pre blocks for some strange reason.
12481248
// It's necessary for IE though which seems to lose the preformattedness
12491249
// of <pre> tags when their innerHTML is assigned.
12501250
html = html.replace(/(?:\r\n?)|\n/g, '<br>').replace(/ /g, '&nbsp; ');
12511251
}
1252-
out += html;
1252+
out.push(html);
12531253
}
12541254
if (lastStyle != null) {
1255-
out += '</span>';
1255+
out.push('</span>');
12561256
}
1257-
return out;
1257+
return out.join('');
12581258
} catch (e) {
12591259
//alert(e.stack); // DISABLE in production
12601260
return s;
@@ -1302,8 +1302,9 @@ function prettyPrint() {
13021302
// XMP tags contain unescaped entities so require special handling.
13031303
var isRawContent = 'XMP' == cs.tagName;
13041304

1305-
// fetch the content as a snippet of properly escaped HTML
1306-
var content = cs.innerHTML;
1305+
// fetch the content as a snippet of properly escaped HTML.
1306+
// Firefox adds newlines at the end.
1307+
var content = cs.innerHTML.replace(/(?:\r\n?|\n)$/, '');
13071308
if (isRawContent) {
13081309
content = PR_textToHtml(content);
13091310
}

tests/prettify_test.html

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,7 @@ <h1>HTML using XMP</h1>
327327
'&nbsp; echo `END`PUN-`END`PLNn $a<br>' +
328328
'`END`PUN}`END`PLN<br>' +
329329
'<br>' +
330-
'fib `END`LIT10`END`PLN<br>' +
331-
'`END'),
330+
'fib `END`LIT10`END'),
332331

333332
C: (
334333
'`COM#include &lt;stdio.h&gt;`END`PLN<br>' +
@@ -355,8 +354,7 @@ <h1>HTML using XMP</h1>
355354
'main`END`PUN()`END`PLN `END`PUN{`END`PLN<br>' +
356355
'&nbsp; printf`END`PUN(`END`STR"%u"`END`PUN,`END`PLN fib`END`PUN(`END' +
357356
'`LIT10`END`PUN));`END`PLN<br>' +
358-
'`END`PUN}`END`PLN<br>' +
359-
'`END'),
357+
'`END`PUN}`END'),
360358

361359
Cpp: (
362360
'`COM#include &lt;iostream&gt;`END`PLN<br>' +
@@ -390,8 +388,7 @@ <h1>HTML using XMP</h1>
390388
'`PUN)`END`PLN `END`PUN{`END`PLN<br>' +
391389
'&nbsp; cout `END&lt;&lt;`PLN fib`END`PUN(`END`LIT10`END`PUN,`END' +
392390
'`PLN `END`LIT1U`END`PUN);`END`PLN<br>' +
393-
'`END`PUN}`END`PLN<br>' +
394-
'`END'),
391+
'`END`PUN}`END'),
395392

396393
java: (
397394
'`KWDpackage`END`PLN foo`END`PUN;`END`PLN<br>' +
@@ -474,8 +471,7 @@ <h1>HTML using XMP</h1>
474471
'`PLN `END`KWDnew`END`PLN `END`TYPFibonacci`END`PUN()));`END' +
475472
'`PLN<br>' +
476473
'&nbsp; `END`PUN}`END`PLN<br>' +
477-
'`END`PUN}`END`PLN<br>' +
478-
'`END'),
474+
'`END`PUN}`END'),
479475
javascript: (
480476
'`COM/**<br>' +
481477
' * nth element in the fibonacci series.<br>' +
@@ -497,8 +493,7 @@ <h1>HTML using XMP</h1>
497493
'`END`PUN}`END`PLN<br>' +
498494
'<br>' +
499495
'document`END`PUN.`END`PLNwrite`END`PUN(`END`PLNfib`END`PUN(`END' +
500-
'`LIT10`END`PUN));`END`PLN<br>' +
501-
'`END'),
496+
'`LIT10`END`PUN));`END'),
502497
perl: (
503498
'`COM#!/usr/bin/perl`END`PLN<br>' +
504499
'<br>' +
@@ -521,8 +516,7 @@ <h1>HTML using XMP</h1>
521516
'&nbsp; `END`KWDreturn`END`PLN $a`END`PUN;`END`PLN<br>' +
522517
'`END`PUN}`END`PLN<br>' +
523518
'<br>' +
524-
'`END`KWDprint`END`PLN fib`END`PUN(`END`LIT10`END`PUN);`END`PLN<br>' +
525-
'`END'),
519+
'`END`KWDprint`END`PLN fib`END`PUN(`END`LIT10`END`PUN);`END'),
526520
python: (
527521
'`COM#!/usr/bin/python2.4`END`PLN<br>' +
528522
'<br>' +
@@ -554,8 +548,7 @@ <h1>HTML using XMP</h1>
554548
'`LIT0`END`PUN:`END`PLN `END`KWDreturn`END`PLN x<br>' +
555549
'<br>' +
556550
'`END`KWDprint`END`PLN nth`END`PUN(`END`PLNfib`END`PUN(),`END`PLN `END' +
557-
'`LIT10`END`PUN)`END`PLN<br>' +
558-
'`END'),
551+
'`LIT10`END`PUN)`END'),
559552
xml: (
560553
'`DEC&lt;!DOCTYPE series PUBLIC "fibonacci numbers"&gt;`END`PLN<br>' +
561554
'<br>' +
@@ -575,8 +568,7 @@ <h1>HTML using XMP</h1>
575568
'&nbsp; `END`TAG&lt;element `END`ATNi`END`TAG=`END`ATV"5"`END' +
576569
'`TAG&gt;`END`PLN8`END`TAG&lt;/element&gt;`END`PLN<br>' +
577570
'&nbsp; ...<br>' +
578-
'`END`TAG&lt;/series&gt;`END`PLN<br>' +
579-
'`END'),
571+
'`END`TAG&lt;/series&gt;`END'),
580572
html: (
581573
'`TAG&lt;html&gt;`END`PLN<br>' +
582574
'&nbsp; `END`TAG&lt;head&gt;`END`PLN<br>' +
@@ -626,8 +618,7 @@ <h1>HTML using XMP</h1>
626618
'`END`COM// --&gt;`END`PLN<br>' +
627619
'&nbsp; &nbsp; `END`END`TAG&lt;/script&gt;`END`PLN<br>' +
628620
'&nbsp; `END`TAG&lt;/body&gt;`END`PLN<br>' +
629-
'`END`TAG&lt;/html&gt;`END`PLN<br>' +
630-
'`END'),
621+
'`END`TAG&lt;/html&gt;`END'),
631622
htmlXmp: (
632623
'`TAG&lt;html&gt;`END`PLN<br>' +
633624
'&nbsp; `END`TAG&lt;head&gt;`END`PLN<br>' +
@@ -677,8 +668,7 @@ <h1>HTML using XMP</h1>
677668
'`END`COM// --&gt;`END`PLN<br>' +
678669
'&nbsp; &nbsp; `END`END`TAG&lt;/script&gt;`END`PLN<br>' +
679670
'&nbsp; `END`TAG&lt;/body&gt;`END`PLN<br>' +
680-
'`END`TAG&lt;/html&gt;`END`PLN<br>' +
681-
'`END')
671+
'`END`TAG&lt;/html&gt;`END')
682672
};
683673

684674

0 commit comments

Comments
 (0)