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

Commit b11ac12

Browse files
add optional argument to prettyPrint to allow caller to specify which nodes to prettify
1 parent 1d29cd0 commit b11ac12

File tree

2 files changed

+64
-28
lines changed

2 files changed

+64
-28
lines changed

js-modules/prettify.js

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,23 @@
6363
window['PR_SHOULD_USE_CONTINUATION'] = true;
6464

6565
/**
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
7173
*/
7274
var prettyPrintOne;
7375
/**
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.
7578
*
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}.
7883
*/
7984
var prettyPrint;
8085

@@ -763,6 +768,7 @@ var prettyPrint;
763768
}
764769

765770
/**
771+
* Pretty print a chunk of code.
766772
* @param sourceCodeHtml {string} The HTML to pretty print.
767773
* @param opt_langExtension {string} The language name to use.
768774
* Typically, a filename extension like 'cpp' or 'java'.
@@ -794,8 +800,19 @@ var prettyPrint;
794800
return container.innerHTML;
795801
}
796802

797-
function prettyPrint(opt_whenDone) {
798-
function byTagName(tn) { return document.getElementsByTagName(tn); }
803+
/**
804+
* Find all the {@code <pre>} and {@code <code>} tags in the DOM with
805+
* {@code class=prettyprint} and prettify them.
806+
*
807+
* @param {Function} opt_whenDone called when prettifying is done.
808+
* @param {HTMLElement|HTMLDocument} opt_root an element or document
809+
* containing all the elements to pretty print.
810+
* Defaults to {@code document.body}.
811+
*/
812+
function prettyPrint(opt_whenDone, opt_root) {
813+
var root = opt_root || document.body;
814+
var doc = root.ownerDocument || document;
815+
function byTagName(tn) { return root.getElementsByTagName(tn); }
799816
// fetch a list of nodes to rewrite
800817
var codeSegments = [byTagName('pre'), byTagName('code'), byTagName('xmp')];
801818
var elements = [];
@@ -874,12 +891,13 @@ var prettyPrint;
874891
preformatted = 1;
875892
} else {
876893
var currentStyle = cs['currentStyle'];
894+
var defaultView = doc.defaultView;
877895
var whitespace = (
878896
currentStyle
879897
? currentStyle['whiteSpace']
880-
: (document.defaultView
881-
&& document.defaultView.getComputedStyle)
882-
? document.defaultView.getComputedStyle(cs, null)
898+
: (defaultView
899+
&& defaultView.getComputedStyle)
900+
? defaultView.getComputedStyle(cs, null)
883901
.getPropertyValue('white-space')
884902
: 0);
885903
preformatted = whitespace
@@ -908,7 +926,7 @@ var prettyPrint;
908926
if (k < elements.length) {
909927
// finish up in a continuation
910928
setTimeout(doWork, 250);
911-
} else if (opt_whenDone) {
929+
} else if ('function' === typeof opt_whenDone) {
912930
opt_whenDone();
913931
}
914932
}

src/prettify.js

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,23 @@
6363
window['PR_SHOULD_USE_CONTINUATION'] = true;
6464

6565
/**
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
7173
*/
7274
var prettyPrintOne;
7375
/**
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.
7578
*
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}.
7883
*/
7984
var prettyPrint;
8085

@@ -1382,6 +1387,7 @@ var REGEXP_PRECEDER_PATTERN = '(?:^^\\.?|[+-]|[!=]=?=?|\\#|%=?|&&?=?|\\(|\\*=?|[
13821387
}
13831388

13841389
/**
1390+
* Pretty print a chunk of code.
13851391
* @param sourceCodeHtml {string} The HTML to pretty print.
13861392
* @param opt_langExtension {string} The language name to use.
13871393
* Typically, a filename extension like 'cpp' or 'java'.
@@ -1413,8 +1419,19 @@ var REGEXP_PRECEDER_PATTERN = '(?:^^\\.?|[+-]|[!=]=?=?|\\#|%=?|&&?=?|\\(|\\*=?|[
14131419
return container.innerHTML;
14141420
}
14151421

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); }
14181435
// fetch a list of nodes to rewrite
14191436
var codeSegments = [byTagName('pre'), byTagName('code'), byTagName('xmp')];
14201437
var elements = [];
@@ -1493,12 +1510,13 @@ var REGEXP_PRECEDER_PATTERN = '(?:^^\\.?|[+-]|[!=]=?=?|\\#|%=?|&&?=?|\\(|\\*=?|[
14931510
preformatted = 1;
14941511
} else {
14951512
var currentStyle = cs['currentStyle'];
1513+
var defaultView = doc.defaultView;
14961514
var whitespace = (
14971515
currentStyle
14981516
? 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)
15021520
.getPropertyValue('white-space')
15031521
: 0);
15041522
preformatted = whitespace
@@ -1527,7 +1545,7 @@ var REGEXP_PRECEDER_PATTERN = '(?:^^\\.?|[+-]|[!=]=?=?|\\#|%=?|&&?=?|\\(|\\*=?|[
15271545
if (k < elements.length) {
15281546
// finish up in a continuation
15291547
setTimeout(doWork, 250);
1530-
} else if (opt_whenDone) {
1548+
} else if ('function' === typeof opt_whenDone) {
15311549
opt_whenDone();
15321550
}
15331551
}

0 commit comments

Comments
 (0)