|
63 | 63 | window['PR_SHOULD_USE_CONTINUATION'] = true; |
64 | 64 |
|
65 | 65 | /** |
66 | | - * Find all the {@code <pre>} and {@code <code>} tags in the DOM with |
67 | | - * {@code class=prettyprint} and prettify them. |
68 | | - * |
69 | | - * @param {Function?} opt_whenDone if specified, called when the last entry |
70 | | - * has been finished. |
| 66 | + * Pretty print a chunk of code. |
| 67 | + * @param {string} sourceCodeHtml The HTML to pretty print. |
| 68 | + * @param {string} opt_langExtension The language name to use. |
| 69 | + * Typically, a filename extension like 'cpp' or 'java'. |
| 70 | + * @param {number|boolean} opt_numberLines True to number lines, |
| 71 | + * or the 1-indexed number of the first line in sourceCodeHtml. |
| 72 | + * @return {string} code as html, but prettier |
71 | 73 | */ |
72 | 74 | var prettyPrintOne; |
73 | 75 | /** |
74 | | - * Pretty print a chunk of code. |
| 76 | + * Find all the {@code <pre>} and {@code <code>} tags in the DOM with |
| 77 | + * {@code class=prettyprint} and prettify them. |
75 | 78 | * |
76 | | - * @param {string} sourceCodeHtml code as html |
77 | | - * @return {string} code as html, but prettier |
| 79 | + * @param {Function} opt_whenDone called when prettifying is done. |
| 80 | + * @param {HTMLElement|HTMLDocument} opt_root an element or document |
| 81 | + * containing all the elements to pretty print. |
| 82 | + * Defaults to {@code document.body}. |
78 | 83 | */ |
79 | 84 | var prettyPrint; |
80 | 85 |
|
@@ -1382,6 +1387,7 @@ var REGEXP_PRECEDER_PATTERN = '(?:^^\\.?|[+-]|[!=]=?=?|\\#|%=?|&&?=?|\\(|\\*=?|[ |
1382 | 1387 | } |
1383 | 1388 |
|
1384 | 1389 | /** |
| 1390 | + * Pretty print a chunk of code. |
1385 | 1391 | * @param sourceCodeHtml {string} The HTML to pretty print. |
1386 | 1392 | * @param opt_langExtension {string} The language name to use. |
1387 | 1393 | * Typically, a filename extension like 'cpp' or 'java'. |
@@ -1413,8 +1419,19 @@ var REGEXP_PRECEDER_PATTERN = '(?:^^\\.?|[+-]|[!=]=?=?|\\#|%=?|&&?=?|\\(|\\*=?|[ |
1413 | 1419 | return container.innerHTML; |
1414 | 1420 | } |
1415 | 1421 |
|
1416 | | - function prettyPrint(opt_whenDone) { |
1417 | | - function byTagName(tn) { return document.getElementsByTagName(tn); } |
| 1422 | + /** |
| 1423 | + * Find all the {@code <pre>} and {@code <code>} tags in the DOM with |
| 1424 | + * {@code class=prettyprint} and prettify them. |
| 1425 | + * |
| 1426 | + * @param {Function} opt_whenDone called when prettifying is done. |
| 1427 | + * @param {HTMLElement|HTMLDocument} opt_root an element or document |
| 1428 | + * containing all the elements to pretty print. |
| 1429 | + * Defaults to {@code document.body}. |
| 1430 | + */ |
| 1431 | + function prettyPrint(opt_whenDone, opt_root) { |
| 1432 | + var root = opt_root || document.body; |
| 1433 | + var doc = root.ownerDocument || document; |
| 1434 | + function byTagName(tn) { return root.getElementsByTagName(tn); } |
1418 | 1435 | // fetch a list of nodes to rewrite |
1419 | 1436 | var codeSegments = [byTagName('pre'), byTagName('code'), byTagName('xmp')]; |
1420 | 1437 | var elements = []; |
@@ -1493,12 +1510,13 @@ var REGEXP_PRECEDER_PATTERN = '(?:^^\\.?|[+-]|[!=]=?=?|\\#|%=?|&&?=?|\\(|\\*=?|[ |
1493 | 1510 | preformatted = 1; |
1494 | 1511 | } else { |
1495 | 1512 | var currentStyle = cs['currentStyle']; |
| 1513 | + var defaultView = doc.defaultView; |
1496 | 1514 | var whitespace = ( |
1497 | 1515 | currentStyle |
1498 | 1516 | ? currentStyle['whiteSpace'] |
1499 | | - : (document.defaultView |
1500 | | - && document.defaultView.getComputedStyle) |
1501 | | - ? document.defaultView.getComputedStyle(cs, null) |
| 1517 | + : (defaultView |
| 1518 | + && defaultView.getComputedStyle) |
| 1519 | + ? defaultView.getComputedStyle(cs, null) |
1502 | 1520 | .getPropertyValue('white-space') |
1503 | 1521 | : 0); |
1504 | 1522 | preformatted = whitespace |
@@ -1527,7 +1545,7 @@ var REGEXP_PRECEDER_PATTERN = '(?:^^\\.?|[+-]|[!=]=?=?|\\#|%=?|&&?=?|\\(|\\*=?|[ |
1527 | 1545 | if (k < elements.length) { |
1528 | 1546 | // finish up in a continuation |
1529 | 1547 | setTimeout(doWork, 250); |
1530 | | - } else if (opt_whenDone) { |
| 1548 | + } else if ('function' === typeof opt_whenDone) { |
1531 | 1549 | opt_whenDone(); |
1532 | 1550 | } |
1533 | 1551 | } |
|
0 commit comments