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

Commit cabca35

Browse files
author
mikesamuel@gmail.com
committed
Misko Hevery's IE8 newline patch
1 parent a6071de commit cabca35

2 files changed

Lines changed: 18 additions & 8 deletions

File tree

js-modules/prettify.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -702,13 +702,13 @@ var prettyPrint;
702702
'keywords': SH_KEYWORDS,
703703
'hashComments': true,
704704
'multiLineStrings': true
705-
}), ['bsh', 'csh', 'sh']);
705+
}), ['bash', 'bsh', 'csh', 'sh']);
706706
registerLangHandler(sourceDecorator({
707707
'keywords': PYTHON_KEYWORDS,
708708
'hashComments': true,
709709
'multiLineStrings': true,
710710
'tripleQuotedStrings': true
711-
}), ['cv', 'py']);
711+
}), ['cv', 'py', 'python']);
712712
registerLangHandler(sourceDecorator({
713713
'keywords': PERL_KEYWORDS,
714714
'hashComments': true,
@@ -720,12 +720,12 @@ var prettyPrint;
720720
'hashComments': true,
721721
'multiLineStrings': true,
722722
'regexLiterals': true
723-
}), ['rb']);
723+
}), ['rb', 'ruby']);
724724
registerLangHandler(sourceDecorator({
725725
'keywords': JSCRIPT_KEYWORDS,
726726
'cStyleComments': true,
727727
'regexLiterals': true
728-
}), ['js']);
728+
}), ['javascript', 'js']);
729729
registerLangHandler(sourceDecorator({
730730
'keywords': COFFEE_KEYWORDS,
731731
'hashComments': 3, // ### style block comments
@@ -770,11 +770,16 @@ var prettyPrint;
770770
* or the 1-indexed number of the first line in sourceCodeHtml.
771771
*/
772772
function prettyPrintOne(sourceCodeHtml, opt_langExtension, opt_numberLines) {
773-
var container = document.createElement('pre');
773+
var container = document.createElement('div');
774774
// This could cause images to load and onload listeners to fire.
775775
// E.g. <img onerror="alert(1337)" src="nosuchimage.png">.
776776
// We assume that the inner HTML is from a trusted source.
777-
container.innerHTML = sourceCodeHtml;
777+
// The pre-tag is required for IE8 which strips newlines from innerHTML
778+
// when it is injected into a <pre> tag.
779+
// http://stackoverflow.com/questions/451486/pre-tag-loses-line-breaks-when-setting-innerhtml-in-ie
780+
// http://stackoverflow.com/questions/195363/inserting-a-newline-into-a-pre-tag-ie-javascript
781+
container.innerHTML = '<pre>' + sourceCodeHtml + '</pre>';
782+
container = container.firstChild;
778783
if (opt_numberLines) {
779784
numberLines(container, opt_numberLines, true);
780785
}

src/prettify.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,11 +1389,16 @@ var REGEXP_PRECEDER_PATTERN = '(?:^^\\.?|[+-]|[!=]=?=?|\\#|%=?|&&?=?|\\(|\\*=?|[
13891389
* or the 1-indexed number of the first line in sourceCodeHtml.
13901390
*/
13911391
function prettyPrintOne(sourceCodeHtml, opt_langExtension, opt_numberLines) {
1392-
var container = document.createElement('pre');
1392+
var container = document.createElement('div');
13931393
// This could cause images to load and onload listeners to fire.
13941394
// E.g. <img onerror="alert(1337)" src="nosuchimage.png">.
13951395
// We assume that the inner HTML is from a trusted source.
1396-
container.innerHTML = sourceCodeHtml;
1396+
// The pre-tag is required for IE8 which strips newlines from innerHTML
1397+
// when it is injected into a <pre> tag.
1398+
// http://stackoverflow.com/questions/451486/pre-tag-loses-line-breaks-when-setting-innerhtml-in-ie
1399+
// http://stackoverflow.com/questions/195363/inserting-a-newline-into-a-pre-tag-ie-javascript
1400+
container.innerHTML = '<pre>' + sourceCodeHtml + '</pre>';
1401+
container = container.firstChild;
13971402
if (opt_numberLines) {
13981403
numberLines(container, opt_numberLines, true);
13991404
}

0 commit comments

Comments
 (0)