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

Commit 5fec3de

Browse files
fixed issue 86: newlines in <code> elements being treated as significant even when the code element is not preformatted.
1 parent a452fdc commit 5fec3de

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

src/prettify.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,27 @@ window['_pr_isIE6'] = function () {
273273
return 'XMP' === node.tagName;
274274
}
275275

276+
var newlineRe = /[\r\n]/g;
277+
/**
278+
* Are newlines and adjacent spaces significant in the given node's innerHTML?
279+
*/
280+
function isPreformatted(node, content) {
281+
// PRE means preformatted, and is a very common case, so don't create
282+
// unnecessary computed style objects.
283+
if ('PRE' === node.tagName) { return true; }
284+
if (!newlineRe.test(content)) { return true; } // Don't care
285+
var whitespace;
286+
// For disconnected nodes, IE has no currentStyle.
287+
if (node.currentStyle) {
288+
whitespace = node.currentStyle.whiteSpace;
289+
} else if (window.getComputedStyle) {
290+
// Firefox makes a best guess if node is disconnected whereas Safari
291+
// returns the empty string.
292+
whitespace = window.getComputedStyle(node, null).whiteSpace;
293+
}
294+
return !whitespace || whitespace === 'pre';
295+
}
296+
276297
function normalizedHtml(node, out) {
277298
switch (node.nodeType) {
278299
case 1: // an element
@@ -548,6 +569,9 @@ window['_pr_isIE6'] = function () {
548569
// XMP tags contain unescaped entities so require special handling.
549570
if (isRawContent(node)) {
550571
content = textToHtml(content);
572+
} else if (!isPreformatted(node, content)) {
573+
content = content.replace(/(<br\s*\/?>)[\r\n]+/g, '$1')
574+
.replace(/(?:[\r\n]+[ \t]*)+/g, ' ');
551575
}
552576
return content;
553577
}

tests/prettify_test.html

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,20 @@ <h1>CSS w/ language specified</h1>
10121012
blink { text-decoration: BLINK !IMPORTANT; font-weight: bolder }
10131013
--&gt;
10141014
</pre>
1015+
1016+
<h1>Issue 86</h1>
1017+
<p><code class="prettyprint" id="issue86_0">#One
1018+
Two words</code></p>
1019+
<p><code class="prettyprint known_ie6_failure" id="issue86_1" style="white-space: pre">#One
1020+
Two lines</code></p>
1021+
<pre class="prettyprint" id="issue86_2">#One
1022+
Two lines</pre>
1023+
<pre><code class="prettyprint known_ie6_failure" id="issue86_3">#One
1024+
Two lines</code></pre>
1025+
<xmp class="prettyprint" id="issue86_4">#One
1026+
Two lines</xmp>
1027+
<p><code class="prettyprint" id="issue86_5">#One<br>
1028+
Two lines</code></p>
10151029
</body>
10161030

10171031
<script type="text/javascript">
@@ -2375,7 +2389,16 @@ <h1>CSS w/ language specified</h1>
23752389
'`PLN BLINK `END`KWD!IMPORTANT`END`PUN;`END`PLN `END' +
23762390
'`KWDfont-weight`END`PUN:`END`PLN bolder `END`PUN}`END`PLN<br>' +
23772391
'`END`COM--&gt;`END'
2378-
)
2392+
),
2393+
issue86_0: '`COM#One Two words`END',
2394+
issue86_1: '`COM#One`END`PLN<br>' +
2395+
'`END`TYPTwo`END`PLN lines`END',
2396+
issue86_2: '`COM#One`END`PLN<br>' +
2397+
'`END`TYPTwo`END`PLN lines`END',
2398+
issue86_3: '`COM#One`END`PLN<br>' +
2399+
'`END`TYPTwo`END`PLN lines`END',
2400+
issue86_4: '`COM#One`END`PLN<br>' +
2401+
'`END`TYPTwo`END`PLN lines`END'
23792402
};
23802403
</script>
23812404

tests/test_base.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// get accurate timing.
22
// This file must be loaded after prettify.js for this to work.
33
PR_SHOULD_USE_CONTINUATION = false;
4+
var realIsIE6 = _pr_isIE6();
45
if (!/\btestcopypaste\b/.test(location.fragment)) {
56
_pr_isIE6 = function() { return false; }; // Ensure consistent output.
67
}
@@ -69,12 +70,16 @@ function runTests(goldens) {
6970
'<h1>Running tests&hellip;<\/h1>';
7071
htmlOut.push('<h1>Test results<\/h1>');
7172
for (var lang in goldens) {
73+
var container = document.getElementById(lang);
74+
if (realIsIE6 && /\bknown_ie6_failure\b/.test(container.className)) {
75+
continue;
76+
}
7277
var golden = goldens[lang].replace(/`([A-Z]{3})/g, function (_, lbl) {
7378
return (lbl == 'END'
7479
? '<\/span>'
7580
: '<span class="' + lbl.toLowerCase() + '">');
7681
});
77-
var actual = normalizedInnerHtml(document.getElementById(lang));
82+
var actual = normalizedInnerHtml(container);
7883
if (golden !== actual) { // test failed
7984
// write out
8085
var pre = commonPrefix(golden, actual);

0 commit comments

Comments
 (0)