1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html> <head>
<title>Extract Source Spans Test</title>
<script src="extractSourceSpans.js"></script>
<script src="http://github.com/douglascrockford/JSON-js/raw/master/json2.js"></script>
<style>
.ok { background: #dfd }
.error, .failure { background: #fdd }
td { font-family: monospace }
.actual { white-space: pre }
tr { vertical-align: top }
.break { padding-left: 2px; border-right-style: dotted !important }
.odd, .even { border-style: solid; border-width: 1px }
.even { background: #fff; border-color: #888 }
.odd { background: #ddd; border-color: #000 }
</style>
</head>
<body>
<h1>Extract Source Spans Test</h1>
<table border=1 cellpadding=2 cellspacing=0>
<tr><th colspan=3>Test space preserved in PRE</th></tr>
<tr>
<td><pre class="testinput"><b>print </b>'Hello '<br> + '<World>';</pre></td>
<td class="golden"><pre>^print ^'Hello '^\n^ + '<World>';^</pre></td>
</tr>
<tr><th colspan=3>Test class="nocode"</th></tr>
<tr>
<td><pre class="testinput"><span class=nocode>1. </span><b>print </b>'Hello '<br><span class=nocode>2. </span> + '<World>';</pre></td>
<td class="golden"><pre>^print ^'Hello '^\n^ + '<World>';^</pre></td>
</tr>
<tr><th colspan=3>Test whitespace normalized in code</th></tr>
<tr>
<td><code class="testinput"><b>print </b>'Hello '
+ '<World>';</pre></td>
<td class="golden"><pre>^print ^'Hello ' + '<World>';^</pre></td>
</tr>
<tr><th colspan=3>Test XMP</th></tr>
<tr>
<td><xmp class="testinput">print 'Hello '
+ '<World>';</xmp></td>
<td class="golden"><pre>^print 'Hello '\n + '<World>';^</pre></td>
</tr>
<tr><th colspan=3>Test tabs</th></tr>
<tr>
<td><pre class="testinput">print 'Hello '
	+ '<World>';</pre></td>
<td class="golden"><pre>^print 'Hello '\n\t+ '<World>';^</pre></td>
</tr>
<tr><th colspan=3>Test number lines output</th></tr>
<tr>
<td><pre class="testinput"><ul><li><b>print </b>'Hello '</li><li> + '<World>';</pre></li></ul></td>
<td class="golden"><pre>^print ^'Hello '^\n^ + '<World>';^^</pre></td>
</tr>
</table>
<script>
if (!document.body.getElementsByClassName) {
document.body.getElementsByClassName = function (className) {
className = className.replace(/\s+/g, ' ').replace(/^\s*|\s*$/g, ' ');
var results = [];
function walk(node) {
if (node.nodeType !== 1) { return; }
// This test should be order-insensitive.
if ((' ' + node.className + ' ').indexOf(className) >= 0) {
results[results.length] = node;
}
for (var child = node.firstChild; child; child = child.nextSibling) {
walk(child);
}
}
walk(document.body);
return results;
};
}
setTimeout(function () {
function stringify(s) {
return JSON.stringify(s).replace(/\r/g, '\\r').replace(/\n/g, '\\n')
.replace(/\t/g, '\\t');
}
var testInputs = Array.prototype.slice.call(
document.body.getElementsByClassName('testinput'), 0);
for (var i = 0, n = testInputs.length; i < n; ++i) {
var testInput = testInputs[i];
var testResult = testInput.parentNode.nextSibling;
while (testResult.nodeType !== 1) { testResult = testResult.nextSibling; }
var actual = document.createElement('TD');
actual.className = 'actual';
testResult.parentNode.appendChild(actual);
try {
var sourceAndSpans = extractSourceSpans(testInput);
var source = sourceAndSpans.source;
var actualText = '^';
var actualHtml = '';
var spans = sourceAndSpans.spans;
for (var j = 0, m = spans.length; j < m; j += 2) {
var start = spans[j], end = spans[j + 2] || source.length;
var span = source.substring(start, end);
actualText += span + '^';
var spanClass = ((j & 2) ? 'odd' : 'even');
if (spans[j + 1].nodeName === 'BR') {
spanClass += ' break';
}
actualHtml += '<span class="' + spanClass+ '">'
+ span.replace(/&/g, '&').replace(/</g, '<') + '<\/span>';
}
actual.innerHTML = '<pre>' + actualHtml + '<\/pre>';
var goldenText = testResult.innerText || testResult.textContent;
var goldenNormalized = '"' + goldenText.replace(/(?:\r\n?|\n)$/, '') + '"';
var actualNormalized = stringify(actualText);
var passed = actualNormalized === goldenNormalized;
if (!passed) {
console.log(goldenNormalized + ' !==\n' + actualNormalized);
}
actual.className += passed ? ' ok' : ' failure';
} catch (ex) {
actual.className += ' error';
actual.appendChild(document.createTextNode('Error: ' + (ex.message || ex)));
}
}
}, 0)</script>
<hr>
<address></address>
<!-- hhmts start --> Last modified: Tue Mar 29 16:38:23 PDT 2011 <!-- hhmts end -->
</body> </html>
|