| 1409 | } |
| 1410 | |
| 1411 | function prettyPrint(opt_whenDone) { |
| 1412 | function byTagName(tn) { return document.getElementsByTagName(tn); } |
| 1413 | // fetch a list of nodes to rewrite |
| 1414 | var codeSegments = [byTagName('pre'), byTagName('code'), byTagName('xmp')]; |
| 1415 | var elements = []; |
| 1416 | for (var i = 0; i < codeSegments.length; ++i) { |
| 1417 | for (var j = 0, n = codeSegments[i].length; j < n; ++j) { |
| 1418 | elements.push(codeSegments[i][j]); |
| 1419 | } |
| 1420 | } |
| 1421 | codeSegments = null; |
| 1422 | |
| 1423 | var clock = Date; |
| 1424 | if (!clock['now']) { |
| 1425 | clock = { 'now': function () { return +(new Date); } }; |
| 1426 | } |
| 1427 | |
| 1428 | // The loop is broken into a series of continuations to make sure that we |
| 1429 | // don't make the browser unresponsive when rewriting a large page. |
| 1430 | var k = 0; |
| 1431 | var prettyPrintingJob; |
| 1432 | |
| 1433 | var langExtensionRe = /\blang(?:uage)?-([\w.]+)(?!\S)/; |
| 1434 | var prettyPrintRe = /\bprettyprint\b/; |
| 1435 | var prettyPrintedRe = /\bprettyprinted\b/; |
| 1436 | var preformattedTagNameRe = /pre|xmp/i; |
| 1437 | var codeRe = /^code$/i; |
| 1438 | var preCodeXmpRe = /^(?:pre|code|xmp)$/i; |
| 1439 | |
| 1440 | function doWork() { |
| 1441 | var endTime = (win['PR_SHOULD_USE_CONTINUATION'] ? |
| 1442 | clock['now']() + 250 /* ms */ : |
| 1443 | Infinity); |
| 1444 | for (; k < elements.length && clock['now']() < endTime; k++) { |
| 1445 | var cs = elements[k]; |
| 1446 | var className = cs.className; |
| 1447 | if (prettyPrintRe.test(className) |
| 1448 | // Don't redo this if we've already done it. |
| 1449 | // This allows recalling pretty print to just prettyprint elements |
| 1450 | // that have been added to the page since last call. |
| 1451 | && !prettyPrintedRe.test(className)) { |
| 1452 | |
| 1453 | // make sure this is not nested in an already prettified element |
| 1454 | var nested = false; |
| 1455 | for (var p = cs.parentNode; p; p = p.parentNode) { |
| 1456 | var tn = p.tagName; |
| 1457 | if (preCodeXmpRe.test(tn) |
| 1458 | && p.className && prettyPrintRe.test(p.className)) { |
| 1459 | nested = true; |
| 1460 | break; |
| 1461 | } |
| 1462 | } |
| 1463 | if (!nested) { |
| 1464 | // Mark done. If we fail to prettyprint for whatever reason, |
| 1465 | // we shouldn't try again. |
| 1466 | cs.className += ' prettyprinted'; |
| 1467 | |
| 1468 | // If the classes includes a language extensions, use it. |