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

Commit 5e7113e

Browse files
author
mikesamuel
committed
Get tests working under IE by normalizing inner html.
1 parent f395274 commit 5e7113e

2 files changed

Lines changed: 55 additions & 6 deletions

File tree

README.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ <h3>It doesn't work on <tt>&lt;obfuscated code sample&gt;</tt>?</h3>
8484
&mdash; i.e. outside the scope of this tool.</p>
8585

8686
<h3>Which browsers does it work with?</h3>
87-
<p>It's been tested with IE 6 and Firefox.</p>
87+
<p>It's been tested with IE 6 and Firefox. Look at
88+
<a href="tests/prettify_test.html">the test page</a> to see if it works in
89+
your browser.</p>
8890

8991

9092
<br><br><br>
@@ -93,7 +95,7 @@ <h3>Which browsers does it work with?</h3>
9395
<div class="footer">
9496
<!-- Created: Tue Oct 3 17:51:56 PDT 2006 -->
9597
<!-- hhmts start -->
96-
Last modified: Wed Oct 4 12:48:10 PDT 2006
98+
Last modified: Mon Oct 9 16:47:24 PDT 2006
9799
<!-- hhmts end -->
98100
</div>
99101
</body>

tests/prettify_test.html

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,8 @@ <h1>HTML using XMP</h1>
315315
'&nbsp; local a`END`PUN=`END`LIT1`END`PLN<br>' +
316316
'&nbsp; local b`END`PUN=`END`LIT1`END`PLN<br>' +
317317
'&nbsp; `END`KWDwhile`END`PLN `END`PUN[`END`PLN `END`STR"$n"`END' +
318-
'`PLN `END`PUN!=`END`PLN `END`LIT0`END`PLN `END`PUN]`END' +
319-
'`PLN `END`PUN;`END`PLN `END`KWDdo`END`PLN<br>' +
318+
'`PLN `END`PUN!=`END`PLN `END`LIT0`END`PLN `END`PUN]`END`PLN `END' +
319+
'`PUN;`END`PLN `END`KWDdo`END`PLN<br>' +
320320
'&nbsp; &nbsp; local tmp`END`PUN=`END`PLN$a<br>' +
321321
'&nbsp; &nbsp; a`END`PUN=`END`PLN$`END`PUN((`END`PLN $a `END`PUN+`END' +
322322
'`PLN $b `END`PUN))`END`PLN<br>' +
@@ -708,7 +708,54 @@ <h1>HTML using XMP</h1>
708708
return plainText.replace(/\046/g, '&amp;')
709709
.replace(/\074/g, '&lt;')
710710
.replace(/\076/g, '&gt;')
711-
.replace(/\042/g, '&quot;');
711+
.replace(/\042/g, '&quot;')
712+
.replace(/\xa0/g, '&nbsp;');
713+
}
714+
715+
/** convert a plain text string to html by escaping html special chars. */
716+
function htmlDQSafe(plainText) {
717+
return plainText.replace(/\046/g, '&amp;')
718+
.replace(/\074/g, '&lt;')
719+
.replace(/\076/g, '&gt;')
720+
.replace(/\xa0/g, '&nbsp;');
721+
}
722+
723+
/**
724+
* get normalized markup. innerHTML varies enough across browsers that we
725+
* can't use it.
726+
*/
727+
function normalizedInnerHtml(node, opt_out) {
728+
var out = [];
729+
for (var child = node.firstChild; child; child = child.nextSibling) {
730+
normalizedHtml(child, out);
731+
}
732+
return out.join('');
733+
}
734+
var annoy = 10;
735+
function normalizedHtml(node, out) {
736+
switch (node.nodeType) {
737+
case 1: // an element
738+
var name = node.tagName.toLowerCase();
739+
out.push('\074', name);
740+
for (var i = 0; i < node.attributes.length; ++i) {
741+
out.push(' ');
742+
normalizedHtml(node.attributes[i], out);
743+
}
744+
out.push('>');
745+
for (var child = node.firstChild; child; child = child.nextSibling) {
746+
normalizedHtml(child, out);
747+
}
748+
if (node.firstChild || !/^(?:br|link|img)$/.test(name)) {
749+
out.push('<\/', name, '>');
750+
}
751+
break;
752+
case 2: // an attribute
753+
out.push(node.name.toLowerCase(), '="', htmlDQSafe(node.value), '"');
754+
break;
755+
case 3: case 4: // text
756+
out.push(htmlDQSafe(node.nodeValue));
757+
break;
758+
}
712759
}
713760

714761
var htmlOut = [];
@@ -720,7 +767,7 @@ <h1>HTML using XMP</h1>
720767
? '<\/span>'
721768
: '<span class="' + lbl.toLowerCase() + '">');
722769
});
723-
var actual = document.getElementById(lang).innerHTML;
770+
var actual = normalizedInnerHtml(document.getElementById(lang));
724771
if (golden !== actual) { // test failed
725772
// write out
726773
var pre = commonPrefix(golden, actual);

0 commit comments

Comments
 (0)