google-code-prettify.js 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  1. // Copyright (C) 2006 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview
  16. * some functions for browser-side pretty printing of code contained in html.
  17. *
  18. * <p>
  19. * For a fairly comprehensive set of languages see the
  20. * <a href="http://google-code-prettify.googlecode.com/svn/trunk/README.html#langs">README</a>
  21. * file that came with this source. At a minimum, the lexer should work on a
  22. * number of languages including C and friends, Java, Python, Bash, SQL, HTML,
  23. * XML, CSS, Javascript, and Makefiles. It works passably on Ruby, PHP and Awk
  24. * and a subset of Perl, but, because of commenting conventions, doesn't work on
  25. * Smalltalk, Lisp-like, or CAML-like languages without an explicit lang class.
  26. * <p>
  27. * Usage: <ol>
  28. * <li> include this source file in an html page via
  29. * {@code <script type="text/javascript" src="/path/to/prettify.js"></script>}
  30. * <li> define style rules. See the example page for examples.
  31. * <li> mark the {@code <pre>} and {@code <code>} tags in your source with
  32. * {@code class=prettyprint.}
  33. * You can also use the (html deprecated) {@code <xmp>} tag, but the pretty
  34. * printer needs to do more substantial DOM manipulations to support that, so
  35. * some css styles may not be preserved.
  36. * </ol>
  37. * That's it. I wanted to keep the API as simple as possible, so there's no
  38. * need to specify which language the code is in, but if you wish, you can add
  39. * another class to the {@code <pre>} or {@code <code>} element to specify the
  40. * language, as in {@code <pre class="prettyprint lang-java">}. Any class that
  41. * starts with "lang-" followed by a file extension, specifies the file type.
  42. * See the "lang-*.js" files in this directory for code that implements
  43. * per-language file handlers.
  44. * <p>
  45. * Change log:<br>
  46. * cbeust, 2006/08/22
  47. * <blockquote>
  48. * Java annotations (start with "@") are now captured as literals ("lit")
  49. * </blockquote>
  50. * @requires console
  51. */
  52. // JSLint declarations
  53. /*global console, document, navigator, setTimeout, window, define */
  54. /**
  55. * Split {@code prettyPrint} into multiple timeouts so as not to interfere with
  56. * UI events.
  57. * If set to {@code false}, {@code prettyPrint()} is synchronous.
  58. */
  59. window['PR_SHOULD_USE_CONTINUATION'] = true;
  60. /**
  61. * Find all the {@code <pre>} and {@code <code>} tags in the DOM with
  62. * {@code class=prettyprint} and prettify them.
  63. *
  64. * @param {Function?} opt_whenDone if specified, called when the last entry
  65. * has been finished.
  66. */
  67. var prettyPrintOne;
  68. /**
  69. * Pretty print a chunk of code.
  70. *
  71. * @param {string} sourceCodeHtml code as html
  72. * @return {string} code as html, but prettier
  73. */
  74. var prettyPrint;
  75. (function () {
  76. var win = window;
  77. // Keyword lists for various languages.
  78. // We use things that coerce to strings to make them compact when minified
  79. // and to defeat aggressive optimizers that fold large string constants.
  80. var FLOW_CONTROL_KEYWORDS = ["break,continue,do,else,for,if,return,while"];
  81. var C_KEYWORDS = [FLOW_CONTROL_KEYWORDS,"auto,case,char,const,default," +
  82. "double,enum,extern,float,goto,int,long,register,short,signed,sizeof," +
  83. "static,struct,switch,typedef,union,unsigned,void,volatile"];
  84. var COMMON_KEYWORDS = [C_KEYWORDS,"catch,class,delete,false,import," +
  85. "new,operator,private,protected,public,this,throw,true,try,typeof"];
  86. var CPP_KEYWORDS = [COMMON_KEYWORDS,"alignof,align_union,asm,axiom,bool," +
  87. "concept,concept_map,const_cast,constexpr,decltype," +
  88. "dynamic_cast,explicit,export,friend,inline,late_check," +
  89. "mutable,namespace,nullptr,reinterpret_cast,static_assert,static_cast," +
  90. "template,typeid,typename,using,virtual,where"];
  91. var JAVA_KEYWORDS = [COMMON_KEYWORDS,
  92. "abstract,boolean,byte,extends,final,finally,implements,import," +
  93. "instanceof,null,native,package,strictfp,super,synchronized,throws," +
  94. "transient"];
  95. var CSHARP_KEYWORDS = [JAVA_KEYWORDS,
  96. "as,base,by,checked,decimal,delegate,descending,dynamic,event," +
  97. "fixed,foreach,from,group,implicit,in,interface,internal,into,is,let," +
  98. "lock,object,out,override,orderby,params,partial,readonly,ref,sbyte," +
  99. "sealed,stackalloc,string,select,uint,ulong,unchecked,unsafe,ushort," +
  100. "var,virtual,where"];
  101. var COFFEE_KEYWORDS = "all,and,by,catch,class,else,extends,false,finally," +
  102. "for,if,in,is,isnt,loop,new,no,not,null,of,off,on,or,return,super,then," +
  103. "throw,true,try,unless,until,when,while,yes";
  104. var JSCRIPT_KEYWORDS = [COMMON_KEYWORDS,
  105. "debugger,eval,export,function,get,null,set,undefined,var,with," +
  106. "Infinity,NaN"];
  107. var PERL_KEYWORDS = "caller,delete,die,do,dump,elsif,eval,exit,foreach,for," +
  108. "goto,if,import,last,local,my,next,no,our,print,package,redo,require," +
  109. "sub,undef,unless,until,use,wantarray,while,BEGIN,END";
  110. var PYTHON_KEYWORDS = [FLOW_CONTROL_KEYWORDS, "and,as,assert,class,def,del," +
  111. "elif,except,exec,finally,from,global,import,in,is,lambda," +
  112. "nonlocal,not,or,pass,print,raise,try,with,yield," +
  113. "False,True,None"];
  114. var RUBY_KEYWORDS = [FLOW_CONTROL_KEYWORDS, "alias,and,begin,case,class," +
  115. "def,defined,elsif,end,ensure,false,in,module,next,nil,not,or,redo," +
  116. "rescue,retry,self,super,then,true,undef,unless,until,when,yield," +
  117. "BEGIN,END"];
  118. var SH_KEYWORDS = [FLOW_CONTROL_KEYWORDS, "case,done,elif,esac,eval,fi," +
  119. "function,in,local,set,then,until"];
  120. var ALL_KEYWORDS = [
  121. CPP_KEYWORDS, CSHARP_KEYWORDS, JSCRIPT_KEYWORDS, PERL_KEYWORDS +
  122. PYTHON_KEYWORDS, RUBY_KEYWORDS, SH_KEYWORDS];
  123. var C_TYPES = /^(DIR|FILE|vector|(de|priority_)?queue|list|stack|(const_)?iterator|(multi)?(set|map)|bitset|u?(int|float)\d*)\b/;
  124. // token style names. correspond to css classes
  125. /**
  126. * token style for a string literal
  127. * @const
  128. */
  129. var PR_STRING = 'str';
  130. /**
  131. * token style for a keyword
  132. * @const
  133. */
  134. var PR_KEYWORD = 'kwd';
  135. /**
  136. * token style for a comment
  137. * @const
  138. */
  139. var PR_COMMENT = 'com';
  140. /**
  141. * token style for a type
  142. * @const
  143. */
  144. var PR_TYPE = 'typ';
  145. /**
  146. * token style for a literal value. e.g. 1, null, true.
  147. * @const
  148. */
  149. var PR_LITERAL = 'lit';
  150. /**
  151. * token style for a punctuation string.
  152. * @const
  153. */
  154. var PR_PUNCTUATION = 'pun';
  155. /**
  156. * token style for plain text.
  157. * @const
  158. */
  159. var PR_PLAIN = 'pln';
  160. /**
  161. * token style for an sgml tag.
  162. * @const
  163. */
  164. var PR_TAG = 'tag';
  165. /**
  166. * token style for a markup declaration such as a DOCTYPE.
  167. * @const
  168. */
  169. var PR_DECLARATION = 'dec';
  170. /**
  171. * token style for embedded source.
  172. * @const
  173. */
  174. var PR_SOURCE = 'src';
  175. /**
  176. * token style for an sgml attribute name.
  177. * @const
  178. */
  179. var PR_ATTRIB_NAME = 'atn';
  180. /**
  181. * token style for an sgml attribute value.
  182. * @const
  183. */
  184. var PR_ATTRIB_VALUE = 'atv';
  185. /**
  186. * A class that indicates a section of markup that is not code, e.g. to allow
  187. * embedding of line numbers within code listings.
  188. * @const
  189. */
  190. var PR_NOCODE = 'nocode';
  191. /**
  192. * A set of tokens that can precede a regular expression literal in
  193. * javascript
  194. * http://web.archive.org/web/20070717142515/http://www.mozilla.org/js/language/js20/rationale/syntax.html
  195. * has the full list, but I've removed ones that might be problematic when
  196. * seen in languages that don't support regular expression literals.
  197. *
  198. * <p>Specifically, I've removed any keywords that can't precede a regexp
  199. * literal in a syntactically legal javascript program, and I've removed the
  200. * "in" keyword since it's not a keyword in many languages, and might be used
  201. * as a count of inches.
  202. *
  203. * <p>The link above does not accurately describe EcmaScript rules since
  204. * it fails to distinguish between (a=++/b/i) and (a++/b/i) but it works
  205. * very well in practice.
  206. *
  207. * @private
  208. * @const
  209. */
  210. var REGEXP_PRECEDER_PATTERN = '(?:^^\\.?|[+-]|[!=]=?=?|\\#|%=?|&&?=?|\\(|\\*=?|[+\\-]=|->|\\/=?|::?|<<?=?|>>?>?=?|,|;|\\?|@|\\[|~|{|\\^\\^?=?|\\|\\|?=?|break|case|continue|delete|do|else|finally|instanceof|return|throw|try|typeof)\\s*';
  211. // CAVEAT: this does not properly handle the case where a regular
  212. // expression immediately follows another since a regular expression may
  213. // have flags for case-sensitivity and the like. Having regexp tokens
  214. // adjacent is not valid in any language I'm aware of, so I'm punting.
  215. // TODO: maybe style special characters inside a regexp as punctuation.
  216. /**
  217. * Given a group of {@link RegExp}s, returns a {@code RegExp} that globally
  218. * matches the union of the sets of strings matched by the input RegExp.
  219. * Since it matches globally, if the input strings have a start-of-input
  220. * anchor (/^.../), it is ignored for the purposes of unioning.
  221. * @param {Array.<RegExp>} regexs non multiline, non-global regexs.
  222. * @return {RegExp} a global regex.
  223. */
  224. function combinePrefixPatterns(regexs) {
  225. var capturedGroupIndex = 0;
  226. var needToFoldCase = false;
  227. var ignoreCase = false;
  228. for (var i = 0, n = regexs.length; i < n; ++i) {
  229. var regex = regexs[i];
  230. if (regex.ignoreCase) {
  231. ignoreCase = true;
  232. } else if (/[a-z]/i.test(regex.source.replace(
  233. /\\u[0-9a-f]{4}|\\x[0-9a-f]{2}|\\[^ux]/gi, ''))) {
  234. needToFoldCase = true;
  235. ignoreCase = false;
  236. break;
  237. }
  238. }
  239. var escapeCharToCodeUnit = {
  240. 'b': 8,
  241. 't': 9,
  242. 'n': 0xa,
  243. 'v': 0xb,
  244. 'f': 0xc,
  245. 'r': 0xd
  246. };
  247. function decodeEscape(charsetPart) {
  248. var cc0 = charsetPart.charCodeAt(0);
  249. if (cc0 !== 92 /* \\ */) {
  250. return cc0;
  251. }
  252. var c1 = charsetPart.charAt(1);
  253. cc0 = escapeCharToCodeUnit[c1];
  254. if (cc0) {
  255. return cc0;
  256. } else if ('0' <= c1 && c1 <= '7') {
  257. return parseInt(charsetPart.substring(1), 8);
  258. } else if (c1 === 'u' || c1 === 'x') {
  259. return parseInt(charsetPart.substring(2), 16);
  260. } else {
  261. return charsetPart.charCodeAt(1);
  262. }
  263. }
  264. function encodeEscape(charCode) {
  265. if (charCode < 0x20) {
  266. return (charCode < 0x10 ? '\\x0' : '\\x') + charCode.toString(16);
  267. }
  268. var ch = String.fromCharCode(charCode);
  269. return (ch === '\\' || ch === '-' || ch === ']' || ch === '^')
  270. ? "\\" + ch : ch;
  271. }
  272. function caseFoldCharset(charSet) {
  273. var charsetParts = charSet.substring(1, charSet.length - 1).match(
  274. new RegExp(
  275. '\\\\u[0-9A-Fa-f]{4}'
  276. + '|\\\\x[0-9A-Fa-f]{2}'
  277. + '|\\\\[0-3][0-7]{0,2}'
  278. + '|\\\\[0-7]{1,2}'
  279. + '|\\\\[\\s\\S]'
  280. + '|-'
  281. + '|[^-\\\\]',
  282. 'g'));
  283. var ranges = [];
  284. var inverse = charsetParts[0] === '^';
  285. var out = ['['];
  286. if (inverse) { out.push('^'); }
  287. for (var i = inverse ? 1 : 0, n = charsetParts.length; i < n; ++i) {
  288. var p = charsetParts[i];
  289. if (/\\[bdsw]/i.test(p)) { // Don't muck with named groups.
  290. out.push(p);
  291. } else {
  292. var start = decodeEscape(p);
  293. var end;
  294. if (i + 2 < n && '-' === charsetParts[i + 1]) {
  295. end = decodeEscape(charsetParts[i + 2]);
  296. i += 2;
  297. } else {
  298. end = start;
  299. }
  300. ranges.push([start, end]);
  301. // If the range might intersect letters, then expand it.
  302. // This case handling is too simplistic.
  303. // It does not deal with non-latin case folding.
  304. // It works for latin source code identifiers though.
  305. if (!(end < 65 || start > 122)) {
  306. if (!(end < 65 || start > 90)) {
  307. ranges.push([Math.max(65, start) | 32, Math.min(end, 90) | 32]);
  308. }
  309. if (!(end < 97 || start > 122)) {
  310. ranges.push([Math.max(97, start) & ~32, Math.min(end, 122) & ~32]);
  311. }
  312. }
  313. }
  314. }
  315. // [[1, 10], [3, 4], [8, 12], [14, 14], [16, 16], [17, 17]]
  316. // -> [[1, 12], [14, 14], [16, 17]]
  317. ranges.sort(function (a, b) { return (a[0] - b[0]) || (b[1] - a[1]); });
  318. var consolidatedRanges = [];
  319. var lastRange = [];
  320. for (var i = 0; i < ranges.length; ++i) {
  321. var range = ranges[i];
  322. if (range[0] <= lastRange[1] + 1) {
  323. lastRange[1] = Math.max(lastRange[1], range[1]);
  324. } else {
  325. consolidatedRanges.push(lastRange = range);
  326. }
  327. }
  328. for (var i = 0; i < consolidatedRanges.length; ++i) {
  329. var range = consolidatedRanges[i];
  330. out.push(encodeEscape(range[0]));
  331. if (range[1] > range[0]) {
  332. if (range[1] + 1 > range[0]) { out.push('-'); }
  333. out.push(encodeEscape(range[1]));
  334. }
  335. }
  336. out.push(']');
  337. return out.join('');
  338. }
  339. function allowAnywhereFoldCaseAndRenumberGroups(regex) {
  340. // Split into character sets, escape sequences, punctuation strings
  341. // like ('(', '(?:', ')', '^'), and runs of characters that do not
  342. // include any of the above.
  343. var parts = regex.source.match(
  344. new RegExp(
  345. '(?:'
  346. + '\\[(?:[^\\x5C\\x5D]|\\\\[\\s\\S])*\\]' // a character set
  347. + '|\\\\u[A-Fa-f0-9]{4}' // a unicode escape
  348. + '|\\\\x[A-Fa-f0-9]{2}' // a hex escape
  349. + '|\\\\[0-9]+' // a back-reference or octal escape
  350. + '|\\\\[^ux0-9]' // other escape sequence
  351. + '|\\(\\?[:!=]' // start of a non-capturing group
  352. + '|[\\(\\)\\^]' // start/end of a group, or line start
  353. + '|[^\\x5B\\x5C\\(\\)\\^]+' // run of other characters
  354. + ')',
  355. 'g'));
  356. var n = parts.length;
  357. // Maps captured group numbers to the number they will occupy in
  358. // the output or to -1 if that has not been determined, or to
  359. // undefined if they need not be capturing in the output.
  360. var capturedGroups = [];
  361. // Walk over and identify back references to build the capturedGroups
  362. // mapping.
  363. for (var i = 0, groupIndex = 0; i < n; ++i) {
  364. var p = parts[i];
  365. if (p === '(') {
  366. // groups are 1-indexed, so max group index is count of '('
  367. ++groupIndex;
  368. } else if ('\\' === p.charAt(0)) {
  369. var decimalValue = +p.substring(1);
  370. if (decimalValue) {
  371. if (decimalValue <= groupIndex) {
  372. capturedGroups[decimalValue] = -1;
  373. } else {
  374. // Replace with an unambiguous escape sequence so that
  375. // an octal escape sequence does not turn into a backreference
  376. // to a capturing group from an earlier regex.
  377. parts[i] = encodeEscape(decimalValue);
  378. }
  379. }
  380. }
  381. }
  382. // Renumber groups and reduce capturing groups to non-capturing groups
  383. // where possible.
  384. for (var i = 1; i < capturedGroups.length; ++i) {
  385. if (-1 === capturedGroups[i]) {
  386. capturedGroups[i] = ++capturedGroupIndex;
  387. }
  388. }
  389. for (var i = 0, groupIndex = 0; i < n; ++i) {
  390. var p = parts[i];
  391. if (p === '(') {
  392. ++groupIndex;
  393. if (!capturedGroups[groupIndex]) {
  394. parts[i] = '(?:';
  395. }
  396. } else if ('\\' === p.charAt(0)) {
  397. var decimalValue = +p.substring(1);
  398. if (decimalValue && decimalValue <= groupIndex) {
  399. parts[i] = '\\' + capturedGroups[decimalValue];
  400. }
  401. }
  402. }
  403. // Remove any prefix anchors so that the output will match anywhere.
  404. // ^^ really does mean an anchored match though.
  405. for (var i = 0; i < n; ++i) {
  406. if ('^' === parts[i] && '^' !== parts[i + 1]) { parts[i] = ''; }
  407. }
  408. // Expand letters to groups to handle mixing of case-sensitive and
  409. // case-insensitive patterns if necessary.
  410. if (regex.ignoreCase && needToFoldCase) {
  411. for (var i = 0; i < n; ++i) {
  412. var p = parts[i];
  413. var ch0 = p.charAt(0);
  414. if (p.length >= 2 && ch0 === '[') {
  415. parts[i] = caseFoldCharset(p);
  416. } else if (ch0 !== '\\') {
  417. // TODO: handle letters in numeric escapes.
  418. parts[i] = p.replace(
  419. /[a-zA-Z]/g,
  420. function (ch) {
  421. var cc = ch.charCodeAt(0);
  422. return '[' + String.fromCharCode(cc & ~32, cc | 32) + ']';
  423. });
  424. }
  425. }
  426. }
  427. return parts.join('');
  428. }
  429. var rewritten = [];
  430. for (var i = 0, n = regexs.length; i < n; ++i) {
  431. var regex = regexs[i];
  432. if (regex.global || regex.multiline) { throw new Error('' + regex); }
  433. rewritten.push(
  434. '(?:' + allowAnywhereFoldCaseAndRenumberGroups(regex) + ')');
  435. }
  436. return new RegExp(rewritten.join('|'), ignoreCase ? 'gi' : 'g');
  437. }
  438. /**
  439. * Split markup into a string of source code and an array mapping ranges in
  440. * that string to the text nodes in which they appear.
  441. *
  442. * <p>
  443. * The HTML DOM structure:</p>
  444. * <pre>
  445. * (Element "p"
  446. * (Element "b"
  447. * (Text "print ")) ; #1
  448. * (Text "'Hello '") ; #2
  449. * (Element "br") ; #3
  450. * (Text " + 'World';")) ; #4
  451. * </pre>
  452. * <p>
  453. * corresponds to the HTML
  454. * {@code <p><b>print </b>'Hello '<br> + 'World';</p>}.</p>
  455. *
  456. * <p>
  457. * It will produce the output:</p>
  458. * <pre>
  459. * {
  460. * sourceCode: "print 'Hello '\n + 'World';",
  461. * // 1 2
  462. * // 012345678901234 5678901234567
  463. * spans: [0, #1, 6, #2, 14, #3, 15, #4]
  464. * }
  465. * </pre>
  466. * <p>
  467. * where #1 is a reference to the {@code "print "} text node above, and so
  468. * on for the other text nodes.
  469. * </p>
  470. *
  471. * <p>
  472. * The {@code} spans array is an array of pairs. Even elements are the start
  473. * indices of substrings, and odd elements are the text nodes (or BR elements)
  474. * that contain the text for those substrings.
  475. * Substrings continue until the next index or the end of the source.
  476. * </p>
  477. *
  478. * @param {Node} node an HTML DOM subtree containing source-code.
  479. * @param {boolean} isPreformatted true if white-space in text nodes should
  480. * be considered significant.
  481. * @return {Object} source code and the text nodes in which they occur.
  482. */
  483. function extractSourceSpans(node, isPreformatted) {
  484. var nocode = /(?:^|\s)nocode(?:\s|$)/;
  485. var chunks = [];
  486. var length = 0;
  487. var spans = [];
  488. var k = 0;
  489. function walk(node) {
  490. switch (node.nodeType) {
  491. case 1: // Element
  492. if (nocode.test(node.className)) { return; }
  493. for (var child = node.firstChild; child; child = child.nextSibling) {
  494. walk(child);
  495. }
  496. var nodeName = node.nodeName.toLowerCase();
  497. if ('br' === nodeName || 'li' === nodeName) {
  498. chunks[k] = '\n';
  499. spans[k << 1] = length++;
  500. spans[(k++ << 1) | 1] = node;
  501. }
  502. break;
  503. case 3: case 4: // Text
  504. var text = node.nodeValue;
  505. if (text.length) {
  506. if (!isPreformatted) {
  507. text = text.replace(/[ \t\r\n]+/g, ' ');
  508. } else {
  509. text = text.replace(/\r\n?/g, '\n'); // Normalize newlines.
  510. }
  511. // TODO: handle tabs here?
  512. chunks[k] = text;
  513. spans[k << 1] = length;
  514. length += text.length;
  515. spans[(k++ << 1) | 1] = node;
  516. }
  517. break;
  518. }
  519. }
  520. walk(node);
  521. return {
  522. sourceCode: chunks.join('').replace(/\n$/, ''),
  523. spans: spans
  524. };
  525. }
  526. /**
  527. * Apply the given language handler to sourceCode and add the resulting
  528. * decorations to out.
  529. * @param {number} basePos the index of sourceCode within the chunk of source
  530. * whose decorations are already present on out.
  531. */
  532. function appendDecorations(basePos, sourceCode, langHandler, out) {
  533. if (!sourceCode) { return; }
  534. var job = {
  535. sourceCode: sourceCode,
  536. basePos: basePos
  537. };
  538. langHandler(job);
  539. out.push.apply(out, job.decorations);
  540. }
  541. var notWs = /\S/;
  542. /**
  543. * Given an element, if it contains only one child element and any text nodes
  544. * it contains contain only space characters, return the sole child element.
  545. * Otherwise returns undefined.
  546. * <p>
  547. * This is meant to return the CODE element in {@code <pre><code ...>} when
  548. * there is a single child element that contains all the non-space textual
  549. * content, but not to return anything where there are multiple child elements
  550. * as in {@code <pre><code>...</code><code>...</code></pre>} or when there
  551. * is textual content.
  552. */
  553. function childContentWrapper(element) {
  554. var wrapper = undefined;
  555. for (var c = element.firstChild; c; c = c.nextSibling) {
  556. var type = c.nodeType;
  557. wrapper = (type === 1) // Element Node
  558. ? (wrapper ? element : c)
  559. : (type === 3) // Text Node
  560. ? (notWs.test(c.nodeValue) ? element : wrapper)
  561. : wrapper;
  562. }
  563. return wrapper === element ? undefined : wrapper;
  564. }
  565. /** Given triples of [style, pattern, context] returns a lexing function,
  566. * The lexing function interprets the patterns to find token boundaries and
  567. * returns a decoration list of the form
  568. * [index_0, style_0, index_1, style_1, ..., index_n, style_n]
  569. * where index_n is an index into the sourceCode, and style_n is a style
  570. * constant like PR_PLAIN. index_n-1 <= index_n, and style_n-1 applies to
  571. * all characters in sourceCode[index_n-1:index_n].
  572. *
  573. * The stylePatterns is a list whose elements have the form
  574. * [style : string, pattern : RegExp, DEPRECATED, shortcut : string].
  575. *
  576. * Style is a style constant like PR_PLAIN, or can be a string of the
  577. * form 'lang-FOO', where FOO is a language extension describing the
  578. * language of the portion of the token in $1 after pattern executes.
  579. * E.g., if style is 'lang-lisp', and group 1 contains the text
  580. * '(hello (world))', then that portion of the token will be passed to the
  581. * registered lisp handler for formatting.
  582. * The text before and after group 1 will be restyled using this decorator
  583. * so decorators should take care that this doesn't result in infinite
  584. * recursion. For example, the HTML lexer rule for SCRIPT elements looks
  585. * something like ['lang-js', /<[s]cript>(.+?)<\/script>/]. This may match
  586. * '<script>foo()<\/script>', which would cause the current decorator to
  587. * be called with '<script>' which would not match the same rule since
  588. * group 1 must not be empty, so it would be instead styled as PR_TAG by
  589. * the generic tag rule. The handler registered for the 'js' extension would
  590. * then be called with 'foo()', and finally, the current decorator would
  591. * be called with '<\/script>' which would not match the original rule and
  592. * so the generic tag rule would identify it as a tag.
  593. *
  594. * Pattern must only match prefixes, and if it matches a prefix, then that
  595. * match is considered a token with the same style.
  596. *
  597. * Context is applied to the last non-whitespace, non-comment token
  598. * recognized.
  599. *
  600. * Shortcut is an optional string of characters, any of which, if the first
  601. * character, gurantee that this pattern and only this pattern matches.
  602. *
  603. * @param {Array} shortcutStylePatterns patterns that always start with
  604. * a known character. Must have a shortcut string.
  605. * @param {Array} fallthroughStylePatterns patterns that will be tried in
  606. * order if the shortcut ones fail. May have shortcuts.
  607. *
  608. * @return {function (Object)} a
  609. * function that takes source code and returns a list of decorations.
  610. */
  611. function createSimpleLexer(shortcutStylePatterns, fallthroughStylePatterns) {
  612. var shortcuts = {};
  613. var tokenizer;
  614. (function () {
  615. var allPatterns = shortcutStylePatterns.concat(fallthroughStylePatterns);
  616. var allRegexs = [];
  617. var regexKeys = {};
  618. for (var i = 0, n = allPatterns.length; i < n; ++i) {
  619. var patternParts = allPatterns[i];
  620. var shortcutChars = patternParts[3];
  621. if (shortcutChars) {
  622. for (var c = shortcutChars.length; --c >= 0;) {
  623. shortcuts[shortcutChars.charAt(c)] = patternParts;
  624. }
  625. }
  626. var regex = patternParts[1];
  627. var k = '' + regex;
  628. if (!regexKeys.hasOwnProperty(k)) {
  629. allRegexs.push(regex);
  630. regexKeys[k] = null;
  631. }
  632. }
  633. allRegexs.push(/[\0-\uffff]/);
  634. tokenizer = combinePrefixPatterns(allRegexs);
  635. })();
  636. var nPatterns = fallthroughStylePatterns.length;
  637. /**
  638. * Lexes job.sourceCode and produces an output array job.decorations of
  639. * style classes preceded by the position at which they start in
  640. * job.sourceCode in order.
  641. *
  642. * @param {Object} job an object like <pre>{
  643. * sourceCode: {string} sourceText plain text,
  644. * basePos: {int} position of job.sourceCode in the larger chunk of
  645. * sourceCode.
  646. * }</pre>
  647. */
  648. var decorate = function (job) {
  649. var sourceCode = job.sourceCode, basePos = job.basePos;
  650. /** Even entries are positions in source in ascending order. Odd enties
  651. * are style markers (e.g., PR_COMMENT) that run from that position until
  652. * the end.
  653. * @type {Array.<number|string>}
  654. */
  655. var decorations = [basePos, PR_PLAIN];
  656. var pos = 0; // index into sourceCode
  657. var tokens = sourceCode.match(tokenizer) || [];
  658. var styleCache = {};
  659. for (var ti = 0, nTokens = tokens.length; ti < nTokens; ++ti) {
  660. var token = tokens[ti];
  661. var style = styleCache[token];
  662. var match = void 0;
  663. var isEmbedded;
  664. if (typeof style === 'string') {
  665. isEmbedded = false;
  666. } else {
  667. var patternParts = shortcuts[token.charAt(0)];
  668. if (patternParts) {
  669. match = token.match(patternParts[1]);
  670. style = patternParts[0];
  671. } else {
  672. for (var i = 0; i < nPatterns; ++i) {
  673. patternParts = fallthroughStylePatterns[i];
  674. match = token.match(patternParts[1]);
  675. if (match) {
  676. style = patternParts[0];
  677. break;
  678. }
  679. }
  680. if (!match) { // make sure that we make progress
  681. style = PR_PLAIN;
  682. }
  683. }
  684. isEmbedded = style.length >= 5 && 'lang-' === style.substring(0, 5);
  685. if (isEmbedded && !(match && typeof match[1] === 'string')) {
  686. isEmbedded = false;
  687. style = PR_SOURCE;
  688. }
  689. if (!isEmbedded) { styleCache[token] = style; }
  690. }
  691. var tokenStart = pos;
  692. pos += token.length;
  693. if (!isEmbedded) {
  694. decorations.push(basePos + tokenStart, style);
  695. } else { // Treat group 1 as an embedded block of source code.
  696. var embeddedSource = match[1];
  697. var embeddedSourceStart = token.indexOf(embeddedSource);
  698. var embeddedSourceEnd = embeddedSourceStart + embeddedSource.length;
  699. if (match[2]) {
  700. // If embeddedSource can be blank, then it would match at the
  701. // beginning which would cause us to infinitely recurse on the
  702. // entire token, so we catch the right context in match[2].
  703. embeddedSourceEnd = token.length - match[2].length;
  704. embeddedSourceStart = embeddedSourceEnd - embeddedSource.length;
  705. }
  706. var lang = style.substring(5);
  707. // Decorate the left of the embedded source
  708. appendDecorations(
  709. basePos + tokenStart,
  710. token.substring(0, embeddedSourceStart),
  711. decorate, decorations);
  712. // Decorate the embedded source
  713. appendDecorations(
  714. basePos + tokenStart + embeddedSourceStart,
  715. embeddedSource,
  716. langHandlerForExtension(lang, embeddedSource),
  717. decorations);
  718. // Decorate the right of the embedded section
  719. appendDecorations(
  720. basePos + tokenStart + embeddedSourceEnd,
  721. token.substring(embeddedSourceEnd),
  722. decorate, decorations);
  723. }
  724. }
  725. job.decorations = decorations;
  726. };
  727. return decorate;
  728. }
  729. /** returns a function that produces a list of decorations from source text.
  730. *
  731. * This code treats ", ', and ` as string delimiters, and \ as a string
  732. * escape. It does not recognize perl's qq() style strings.
  733. * It has no special handling for double delimiter escapes as in basic, or
  734. * the tripled delimiters used in python, but should work on those regardless
  735. * although in those cases a single string literal may be broken up into
  736. * multiple adjacent string literals.
  737. *
  738. * It recognizes C, C++, and shell style comments.
  739. *
  740. * @param {Object} options a set of optional parameters.
  741. * @return {function (Object)} a function that examines the source code
  742. * in the input job and builds the decoration list.
  743. */
  744. function sourceDecorator(options) {
  745. var shortcutStylePatterns = [], fallthroughStylePatterns = [];
  746. if (options['tripleQuotedStrings']) {
  747. // '''multi-line-string''', 'single-line-string', and double-quoted
  748. shortcutStylePatterns.push(
  749. [PR_STRING, /^(?:\'\'\'(?:[^\'\\]|\\[\s\S]|\'{1,2}(?=[^\']))*(?:\'\'\'|$)|\"\"\"(?:[^\"\\]|\\[\s\S]|\"{1,2}(?=[^\"]))*(?:\"\"\"|$)|\'(?:[^\\\']|\\[\s\S])*(?:\'|$)|\"(?:[^\\\"]|\\[\s\S])*(?:\"|$))/,
  750. null, '\'"']);
  751. } else if (options['multiLineStrings']) {
  752. // 'multi-line-string', "multi-line-string"
  753. shortcutStylePatterns.push(
  754. [PR_STRING, /^(?:\'(?:[^\\\']|\\[\s\S])*(?:\'|$)|\"(?:[^\\\"]|\\[\s\S])*(?:\"|$)|\`(?:[^\\\`]|\\[\s\S])*(?:\`|$))/,
  755. null, '\'"`']);
  756. } else {
  757. // 'single-line-string', "single-line-string"
  758. shortcutStylePatterns.push(
  759. [PR_STRING,
  760. /^(?:\'(?:[^\\\'\r\n]|\\.)*(?:\'|$)|\"(?:[^\\\"\r\n]|\\.)*(?:\"|$))/,
  761. null, '"\'']);
  762. }
  763. if (options['verbatimStrings']) {
  764. // verbatim-string-literal production from the C# grammar. See issue 93.
  765. fallthroughStylePatterns.push(
  766. [PR_STRING, /^@\"(?:[^\"]|\"\")*(?:\"|$)/, null]);
  767. }
  768. var hc = options['hashComments'];
  769. if (hc) {
  770. if (options['cStyleComments']) {
  771. if (hc > 1) { // multiline hash comments
  772. shortcutStylePatterns.push(
  773. [PR_COMMENT, /^#(?:##(?:[^#]|#(?!##))*(?:###|$)|.*)/, null, '#']);
  774. } else {
  775. // Stop C preprocessor declarations at an unclosed open comment
  776. shortcutStylePatterns.push(
  777. [PR_COMMENT, /^#(?:(?:define|e(?:l|nd)if|else|error|ifn?def|include|line|pragma|undef|warning)\b|[^\r\n]*)/,
  778. null, '#']);
  779. }
  780. // #include <stdio.h>
  781. fallthroughStylePatterns.push(
  782. [PR_STRING,
  783. /^<(?:(?:(?:\.\.\/)*|\/?)(?:[\w-]+(?:\/[\w-]+)+)?[\w-]+\.h(?:h|pp|\+\+)?|[a-z]\w*)>/,
  784. null]);
  785. } else {
  786. shortcutStylePatterns.push([PR_COMMENT, /^#[^\r\n]*/, null, '#']);
  787. }
  788. }
  789. if (options['cStyleComments']) {
  790. fallthroughStylePatterns.push([PR_COMMENT, /^\/\/[^\r\n]*/, null]);
  791. fallthroughStylePatterns.push(
  792. [PR_COMMENT, /^\/\*[\s\S]*?(?:\*\/|$)/, null]);
  793. }
  794. if (options['regexLiterals']) {
  795. /**
  796. * @const
  797. */
  798. var REGEX_LITERAL = (
  799. // A regular expression literal starts with a slash that is
  800. // not followed by * or / so that it is not confused with
  801. // comments.
  802. '/(?=[^/*])'
  803. // and then contains any number of raw characters,
  804. + '(?:[^/\\x5B\\x5C]'
  805. // escape sequences (\x5C),
  806. + '|\\x5C[\\s\\S]'
  807. // or non-nesting character sets (\x5B\x5D);
  808. + '|\\x5B(?:[^\\x5C\\x5D]|\\x5C[\\s\\S])*(?:\\x5D|$))+'
  809. // finally closed by a /.
  810. + '/');
  811. fallthroughStylePatterns.push(
  812. ['lang-regex',
  813. new RegExp('^' + REGEXP_PRECEDER_PATTERN + '(' + REGEX_LITERAL + ')')
  814. ]);
  815. }
  816. var types = options['types'];
  817. if (types) {
  818. fallthroughStylePatterns.push([PR_TYPE, types]);
  819. }
  820. var keywords = ("" + options['keywords']).replace(/^ | $/g, '');
  821. if (keywords.length) {
  822. fallthroughStylePatterns.push(
  823. [PR_KEYWORD,
  824. new RegExp('^(?:' + keywords.replace(/[\s,]+/g, '|') + ')\\b'),
  825. null]);
  826. }
  827. shortcutStylePatterns.push([PR_PLAIN, /^\s+/, null, ' \r\n\t\xA0']);
  828. var punctuation =
  829. // The Bash man page says
  830. // A word is a sequence of characters considered as a single
  831. // unit by GRUB. Words are separated by metacharacters,
  832. // which are the following plus space, tab, and newline: { }
  833. // | & $ ; < >
  834. // ...
  835. // A word beginning with # causes that word and all remaining
  836. // characters on that line to be ignored.
  837. // which means that only a '#' after /(?:^|[{}|&$;<>\s])/ starts a
  838. // comment but empirically
  839. // $ echo {#}
  840. // {#}
  841. // $ echo \$#
  842. // $#
  843. // $ echo }#
  844. // }#
  845. // so /(?:^|[|&;<>\s])/ is more appropriate.
  846. // http://gcc.gnu.org/onlinedocs/gcc-2.95.3/cpp_1.html#SEC3
  847. // suggests that this definition is compatible with a
  848. // default mode that tries to use a single token definition
  849. // to recognize both bash/python style comments and C
  850. // preprocessor directives.
  851. // This definition of punctuation does not include # in the list of
  852. // follow-on exclusions, so # will not be broken before if preceeded
  853. // by a punctuation character. We could try to exclude # after
  854. // [|&;<>] but that doesn't seem to cause many major problems.
  855. // If that does turn out to be a problem, we should change the below
  856. // when hc is truthy to include # in the run of punctuation characters
  857. // only when not followint [|&;<>].
  858. /^.[^\s\w\.$@\'\"\`\/\\]*/;
  859. fallthroughStylePatterns.push(
  860. // TODO(mikesamuel): recognize non-latin letters and numerals in idents
  861. [PR_LITERAL, /^@[a-z_$][a-z_$@0-9]*/i, null],
  862. [PR_TYPE, /^(?:[@_]?[A-Z]+[a-z][A-Za-z_$@0-9]*|\w+_t\b)/, null],
  863. [PR_PLAIN, /^[a-z_$][a-z_$@0-9]*/i, null],
  864. [PR_LITERAL,
  865. new RegExp(
  866. '^(?:'
  867. // A hex number
  868. + '0x[a-f0-9]+'
  869. // or an octal or decimal number,
  870. + '|(?:\\d(?:_\\d+)*\\d*(?:\\.\\d*)?|\\.\\d\\+)'
  871. // possibly in scientific notation
  872. + '(?:e[+\\-]?\\d+)?'
  873. + ')'
  874. // with an optional modifier like UL for unsigned long
  875. + '[a-z]*', 'i'),
  876. null, '0123456789'],
  877. // Don't treat escaped quotes in bash as starting strings. See issue 144.
  878. [PR_PLAIN, /^\\[\s\S]?/, null],
  879. [PR_PUNCTUATION, punctuation, null]);
  880. return createSimpleLexer(shortcutStylePatterns, fallthroughStylePatterns);
  881. }
  882. var decorateSource = sourceDecorator({
  883. 'keywords': ALL_KEYWORDS,
  884. 'hashComments': true,
  885. 'cStyleComments': true,
  886. 'multiLineStrings': true,
  887. 'regexLiterals': true
  888. });
  889. /**
  890. * Given a DOM subtree, wraps it in a list, and puts each line into its own
  891. * list item.
  892. *
  893. * @param {Node} node modified in place. Its content is pulled into an
  894. * HTMLOListElement, and each line is moved into a separate list item.
  895. * This requires cloning elements, so the input might not have unique
  896. * IDs after numbering.
  897. * @param {boolean} isPreformatted true iff white-space in text nodes should
  898. * be treated as significant.
  899. */
  900. function numberLines(node, opt_startLineNum, isPreformatted) {
  901. var nocode = /(?:^|\s)nocode(?:\s|$)/;
  902. var lineBreak = /\r\n?|\n/;
  903. var document = node.ownerDocument;
  904. var li = document.createElement('li');
  905. while (node.firstChild) {
  906. li.appendChild(node.firstChild);
  907. }
  908. // An array of lines. We split below, so this is initialized to one
  909. // un-split line.
  910. var listItems = [li];
  911. function walk(node) {
  912. switch (node.nodeType) {
  913. case 1: // Element
  914. if (nocode.test(node.className)) { break; }
  915. if ('br' === node.nodeName) {
  916. breakAfter(node);
  917. // Discard the <BR> since it is now flush against a </LI>.
  918. if (node.parentNode) {
  919. node.parentNode.removeChild(node);
  920. }
  921. } else {
  922. for (var child = node.firstChild; child; child = child.nextSibling) {
  923. walk(child);
  924. }
  925. }
  926. break;
  927. case 3: case 4: // Text
  928. if (isPreformatted) {
  929. var text = node.nodeValue;
  930. var match = text.match(lineBreak);
  931. if (match) {
  932. var firstLine = text.substring(0, match.index);
  933. node.nodeValue = firstLine;
  934. var tail = text.substring(match.index + match[0].length);
  935. if (tail) {
  936. var parent = node.parentNode;
  937. parent.insertBefore(
  938. document.createTextNode(tail), node.nextSibling);
  939. }
  940. breakAfter(node);
  941. if (!firstLine) {
  942. // Don't leave blank text nodes in the DOM.
  943. node.parentNode.removeChild(node);
  944. }
  945. }
  946. }
  947. break;
  948. }
  949. }
  950. // Split a line after the given node.
  951. function breakAfter(lineEndNode) {
  952. // If there's nothing to the right, then we can skip ending the line
  953. // here, and move root-wards since splitting just before an end-tag
  954. // would require us to create a bunch of empty copies.
  955. while (!lineEndNode.nextSibling) {
  956. lineEndNode = lineEndNode.parentNode;
  957. if (!lineEndNode) { return; }
  958. }
  959. function breakLeftOf(limit, copy) {
  960. // Clone shallowly if this node needs to be on both sides of the break.
  961. var rightSide = copy ? limit.cloneNode(false) : limit;
  962. var parent = limit.parentNode;
  963. if (parent) {
  964. // We clone the parent chain.
  965. // This helps us resurrect important styling elements that cross lines.
  966. // E.g. in <i>Foo<br>Bar</i>
  967. // should be rewritten to <li><i>Foo</i></li><li><i>Bar</i></li>.
  968. var parentClone = breakLeftOf(parent, 1);
  969. // Move the clone and everything to the right of the original
  970. // onto the cloned parent.
  971. var next = limit.nextSibling;
  972. parentClone.appendChild(rightSide);
  973. for (var sibling = next; sibling; sibling = next) {
  974. next = sibling.nextSibling;
  975. parentClone.appendChild(sibling);
  976. }
  977. }
  978. return rightSide;
  979. }
  980. var copiedListItem = breakLeftOf(lineEndNode.nextSibling, 0);
  981. // Walk the parent chain until we reach an unattached LI.
  982. for (var parent;
  983. // Check nodeType since IE invents document fragments.
  984. (parent = copiedListItem.parentNode) && parent.nodeType === 1;) {
  985. copiedListItem = parent;
  986. }
  987. // Put it on the list of lines for later processing.
  988. listItems.push(copiedListItem);
  989. }
  990. // Split lines while there are lines left to split.
  991. for (var i = 0; // Number of lines that have been split so far.
  992. i < listItems.length; // length updated by breakAfter calls.
  993. ++i) {
  994. walk(listItems[i]);
  995. }
  996. // Make sure numeric indices show correctly.
  997. if (opt_startLineNum === (opt_startLineNum|0)) {
  998. listItems[0].setAttribute('value', opt_startLineNum);
  999. }
  1000. var ol = document.createElement('ol');
  1001. ol.className = 'linenums';
  1002. var offset = Math.max(0, ((opt_startLineNum - 1 /* zero index */)) | 0) || 0;
  1003. for (var i = 0, n = listItems.length; i < n; ++i) {
  1004. li = listItems[i];
  1005. // Stick a class on the LIs so that stylesheets can
  1006. // color odd/even rows, or any other row pattern that
  1007. // is co-prime with 10.
  1008. li.className = 'L' + ((i + offset) % 10);
  1009. if (!li.firstChild) {
  1010. li.appendChild(document.createTextNode('\xA0'));
  1011. }
  1012. ol.appendChild(li);
  1013. }
  1014. node.appendChild(ol);
  1015. }
  1016. /**
  1017. * Breaks {@code job.sourceCode} around style boundaries in
  1018. * {@code job.decorations} and modifies {@code job.sourceNode} in place.
  1019. * @param {Object} job like <pre>{
  1020. * sourceCode: {string} source as plain text,
  1021. * spans: {Array.<number|Node>} alternating span start indices into source
  1022. * and the text node or element (e.g. {@code <BR>}) corresponding to that
  1023. * span.
  1024. * decorations: {Array.<number|string} an array of style classes preceded
  1025. * by the position at which they start in job.sourceCode in order
  1026. * }</pre>
  1027. * @private
  1028. */
  1029. function recombineTagsAndDecorations(job) {
  1030. var isIE8OrEarlier = /\bMSIE\s(\d+)/.exec(navigator.userAgent);
  1031. isIE8OrEarlier = isIE8OrEarlier && +isIE8OrEarlier[1] <= 8;
  1032. var newlineRe = /\n/g;
  1033. var source = job.sourceCode;
  1034. var sourceLength = source.length;
  1035. // Index into source after the last code-unit recombined.
  1036. var sourceIndex = 0;
  1037. var spans = job.spans;
  1038. var nSpans = spans.length;
  1039. // Index into spans after the last span which ends at or before sourceIndex.
  1040. var spanIndex = 0;
  1041. var decorations = job.decorations;
  1042. var nDecorations = decorations.length;
  1043. // Index into decorations after the last decoration which ends at or before
  1044. // sourceIndex.
  1045. var decorationIndex = 0;
  1046. // Remove all zero-length decorations.
  1047. decorations[nDecorations] = sourceLength;
  1048. var decPos, i;
  1049. for (i = decPos = 0; i < nDecorations;) {
  1050. if (decorations[i] !== decorations[i + 2]) {
  1051. decorations[decPos++] = decorations[i++];
  1052. decorations[decPos++] = decorations[i++];
  1053. } else {
  1054. i += 2;
  1055. }
  1056. }
  1057. nDecorations = decPos;
  1058. // Simplify decorations.
  1059. for (i = decPos = 0; i < nDecorations;) {
  1060. var startPos = decorations[i];
  1061. // Conflate all adjacent decorations that use the same style.
  1062. var startDec = decorations[i + 1];
  1063. var end = i + 2;
  1064. while (end + 2 <= nDecorations && decorations[end + 1] === startDec) {
  1065. end += 2;
  1066. }
  1067. decorations[decPos++] = startPos;
  1068. decorations[decPos++] = startDec;
  1069. i = end;
  1070. }
  1071. nDecorations = decorations.length = decPos;
  1072. var sourceNode = job.sourceNode;
  1073. var oldDisplay;
  1074. if (sourceNode) {
  1075. oldDisplay = sourceNode.style.display;
  1076. sourceNode.style.display = 'none';
  1077. }
  1078. try {
  1079. var decoration = null;
  1080. while (spanIndex < nSpans) {
  1081. var spanStart = spans[spanIndex];
  1082. var spanEnd = spans[spanIndex + 2] || sourceLength;
  1083. var decEnd = decorations[decorationIndex + 2] || sourceLength;
  1084. var end = Math.min(spanEnd, decEnd);
  1085. var textNode = spans[spanIndex + 1];
  1086. var styledText;
  1087. if (textNode.nodeType !== 1 // Don't muck with <BR>s or <LI>s
  1088. // Don't introduce spans around empty text nodes.
  1089. && (styledText = source.substring(sourceIndex, end))) {
  1090. // This may seem bizarre, and it is. Emitting LF on IE causes the
  1091. // code to display with spaces instead of line breaks.
  1092. // Emitting Windows standard issue linebreaks (CRLF) causes a blank
  1093. // space to appear at the beginning of every line but the first.
  1094. // Emitting an old Mac OS 9 line separator makes everything spiffy.
  1095. if (isIE8OrEarlier) {
  1096. styledText = styledText.replace(newlineRe, '\r');
  1097. }
  1098. textNode.nodeValue = styledText;
  1099. var document = textNode.ownerDocument;
  1100. var span = document.createElement('span');
  1101. span.className = decorations[decorationIndex + 1];
  1102. var parentNode = textNode.parentNode;
  1103. parentNode.replaceChild(span, textNode);
  1104. span.appendChild(textNode);
  1105. if (sourceIndex < spanEnd) { // Split off a text node.
  1106. spans[spanIndex + 1] = textNode
  1107. // TODO: Possibly optimize by using '' if there's no flicker.
  1108. = document.createTextNode(source.substring(end, spanEnd));
  1109. parentNode.insertBefore(textNode, span.nextSibling);
  1110. }
  1111. }
  1112. sourceIndex = end;
  1113. if (sourceIndex >= spanEnd) {
  1114. spanIndex += 2;
  1115. }
  1116. if (sourceIndex >= decEnd) {
  1117. decorationIndex += 2;
  1118. }
  1119. }
  1120. } finally {
  1121. if (sourceNode) {
  1122. sourceNode.style.display = oldDisplay;
  1123. }
  1124. }
  1125. }
  1126. /** Maps language-specific file extensions to handlers. */
  1127. var langHandlerRegistry = {};
  1128. /** Register a language handler for the given file extensions.
  1129. * @param {function (Object)} handler a function from source code to a list
  1130. * of decorations. Takes a single argument job which describes the
  1131. * state of the computation. The single parameter has the form
  1132. * {@code {
  1133. * sourceCode: {string} as plain text.
  1134. * decorations: {Array.<number|string>} an array of style classes
  1135. * preceded by the position at which they start in
  1136. * job.sourceCode in order.
  1137. * The language handler should assigned this field.
  1138. * basePos: {int} the position of source in the larger source chunk.
  1139. * All positions in the output decorations array are relative
  1140. * to the larger source chunk.
  1141. * } }
  1142. * @param {Array.<string>} fileExtensions
  1143. */
  1144. function registerLangHandler(handler, fileExtensions) {
  1145. for (var i = fileExtensions.length; --i >= 0;) {
  1146. var ext = fileExtensions[i];
  1147. if (!langHandlerRegistry.hasOwnProperty(ext)) {
  1148. langHandlerRegistry[ext] = handler;
  1149. } else if (win['console']) {
  1150. console['warn']('cannot override language handler %s', ext);
  1151. }
  1152. }
  1153. }
  1154. function langHandlerForExtension(extension, source) {
  1155. if (!(extension && langHandlerRegistry.hasOwnProperty(extension))) {
  1156. // Treat it as markup if the first non whitespace character is a < and
  1157. // the last non-whitespace character is a >.
  1158. extension = /^\s*</.test(source)
  1159. ? 'default-markup'
  1160. : 'default-code';
  1161. }
  1162. return langHandlerRegistry[extension];
  1163. }
  1164. registerLangHandler(decorateSource, ['default-code']);
  1165. registerLangHandler(
  1166. createSimpleLexer(
  1167. [],
  1168. [
  1169. [PR_PLAIN, /^[^<?]+/],
  1170. [PR_DECLARATION, /^<!\w[^>]*(?:>|$)/],
  1171. [PR_COMMENT, /^<\!--[\s\S]*?(?:-\->|$)/],
  1172. // Unescaped content in an unknown language
  1173. ['lang-', /^<\?([\s\S]+?)(?:\?>|$)/],
  1174. ['lang-', /^<%([\s\S]+?)(?:%>|$)/],
  1175. [PR_PUNCTUATION, /^(?:<[%?]|[%?]>)/],
  1176. ['lang-', /^<xmp\b[^>]*>([\s\S]+?)<\/xmp\b[^>]*>/i],
  1177. // Unescaped content in javascript. (Or possibly vbscript).
  1178. ['lang-js', /^<script\b[^>]*>([\s\S]*?)(<\/script\b[^>]*>)/i],
  1179. // Contains unescaped stylesheet content
  1180. ['lang-css', /^<style\b[^>]*>([\s\S]*?)(<\/style\b[^>]*>)/i],
  1181. ['lang-in.tag', /^(<\/?[a-z][^<>]*>)/i]
  1182. ]),
  1183. ['default-markup', 'htm', 'html', 'mxml', 'xhtml', 'xml', 'xsl']);
  1184. registerLangHandler(
  1185. createSimpleLexer(
  1186. [
  1187. [PR_PLAIN, /^[\s]+/, null, ' \t\r\n'],
  1188. [PR_ATTRIB_VALUE, /^(?:\"[^\"]*\"?|\'[^\']*\'?)/, null, '\"\'']
  1189. ],
  1190. [
  1191. [PR_TAG, /^^<\/?[a-z](?:[\w.:-]*\w)?|\/?>$/i],
  1192. [PR_ATTRIB_NAME, /^(?!style[\s=]|on)[a-z](?:[\w:-]*\w)?/i],
  1193. ['lang-uq.val', /^=\s*([^>\'\"\s]*(?:[^>\'\"\s\/]|\/(?=\s)))/],
  1194. [PR_PUNCTUATION, /^[=<>\/]+/],
  1195. ['lang-js', /^on\w+\s*=\s*\"([^\"]+)\"/i],
  1196. ['lang-js', /^on\w+\s*=\s*\'([^\']+)\'/i],
  1197. ['lang-js', /^on\w+\s*=\s*([^\"\'>\s]+)/i],
  1198. ['lang-css', /^style\s*=\s*\"([^\"]+)\"/i],
  1199. ['lang-css', /^style\s*=\s*\'([^\']+)\'/i],
  1200. ['lang-css', /^style\s*=\s*([^\"\'>\s]+)/i]
  1201. ]),
  1202. ['in.tag']);
  1203. registerLangHandler(
  1204. createSimpleLexer([], [[PR_ATTRIB_VALUE, /^[\s\S]+/]]), ['uq.val']);
  1205. registerLangHandler(sourceDecorator({
  1206. 'keywords': CPP_KEYWORDS,
  1207. 'hashComments': true,
  1208. 'cStyleComments': true,
  1209. 'types': C_TYPES
  1210. }), ['c', 'cc', 'cpp', 'cxx', 'cyc', 'm']);
  1211. registerLangHandler(sourceDecorator({
  1212. 'keywords': 'null,true,false'
  1213. }), ['json']);
  1214. registerLangHandler(sourceDecorator({
  1215. 'keywords': CSHARP_KEYWORDS,
  1216. 'hashComments': true,
  1217. 'cStyleComments': true,
  1218. 'verbatimStrings': true,
  1219. 'types': C_TYPES
  1220. }), ['cs']);
  1221. registerLangHandler(sourceDecorator({
  1222. 'keywords': JAVA_KEYWORDS,
  1223. 'cStyleComments': true
  1224. }), ['java']);
  1225. registerLangHandler(sourceDecorator({
  1226. 'keywords': SH_KEYWORDS,
  1227. 'hashComments': true,
  1228. 'multiLineStrings': true
  1229. }), ['bsh', 'csh', 'sh']);
  1230. registerLangHandler(sourceDecorator({
  1231. 'keywords': PYTHON_KEYWORDS,
  1232. 'hashComments': true,
  1233. 'multiLineStrings': true,
  1234. 'tripleQuotedStrings': true
  1235. }), ['cv', 'py']);
  1236. registerLangHandler(sourceDecorator({
  1237. 'keywords': PERL_KEYWORDS,
  1238. 'hashComments': true,
  1239. 'multiLineStrings': true,
  1240. 'regexLiterals': true
  1241. }), ['perl', 'pl', 'pm']);
  1242. registerLangHandler(sourceDecorator({
  1243. 'keywords': RUBY_KEYWORDS,
  1244. 'hashComments': true,
  1245. 'multiLineStrings': true,
  1246. 'regexLiterals': true
  1247. }), ['rb']);
  1248. registerLangHandler(sourceDecorator({
  1249. 'keywords': JSCRIPT_KEYWORDS,
  1250. 'cStyleComments': true,
  1251. 'regexLiterals': true
  1252. }), ['js']);
  1253. registerLangHandler(sourceDecorator({
  1254. 'keywords': COFFEE_KEYWORDS,
  1255. 'hashComments': 3, // ### style block comments
  1256. 'cStyleComments': true,
  1257. 'multilineStrings': true,
  1258. 'tripleQuotedStrings': true,
  1259. 'regexLiterals': true
  1260. }), ['coffee']);
  1261. registerLangHandler(
  1262. createSimpleLexer([], [[PR_STRING, /^[\s\S]+/]]), ['regex']);
  1263. function applyDecorator(job) {
  1264. var opt_langExtension = job.langExtension;
  1265. try {
  1266. // Extract tags, and convert the source code to plain text.
  1267. var sourceAndSpans = extractSourceSpans(job.sourceNode, job.pre);
  1268. /** Plain text. @type {string} */
  1269. var source = sourceAndSpans.sourceCode;
  1270. job.sourceCode = source;
  1271. job.spans = sourceAndSpans.spans;
  1272. job.basePos = 0;
  1273. // Apply the appropriate language handler
  1274. langHandlerForExtension(opt_langExtension, source)(job);
  1275. // Integrate the decorations and tags back into the source code,
  1276. // modifying the sourceNode in place.
  1277. recombineTagsAndDecorations(job);
  1278. } catch (e) {
  1279. if (win['console']) {
  1280. console['log'](e && e['stack'] ? e['stack'] : e);
  1281. }
  1282. }
  1283. }
  1284. /**
  1285. * @param sourceCodeHtml {string} The HTML to pretty print.
  1286. * @param opt_langExtension {string} The language name to use.
  1287. * Typically, a filename extension like 'cpp' or 'java'.
  1288. * @param opt_numberLines {number|boolean} True to number lines,
  1289. * or the 1-indexed number of the first line in sourceCodeHtml.
  1290. */
  1291. function prettyPrintOne(sourceCodeHtml, opt_langExtension, opt_numberLines) {
  1292. var container = document.createElement('pre');
  1293. // This could cause images to load and onload listeners to fire.
  1294. // E.g. <img onerror="alert(1337)" src="nosuchimage.png">.
  1295. // We assume that the inner HTML is from a trusted source.
  1296. container.innerHTML = sourceCodeHtml;
  1297. if (opt_numberLines) {
  1298. numberLines(container, opt_numberLines, true);
  1299. }
  1300. var job = {
  1301. langExtension: opt_langExtension,
  1302. numberLines: opt_numberLines,
  1303. sourceNode: container,
  1304. pre: 1
  1305. };
  1306. applyDecorator(job);
  1307. return container.innerHTML;
  1308. }
  1309. function prettyPrint(opt_whenDone) {
  1310. function byTagName(tn) { return document.getElementsByTagName(tn); }
  1311. // fetch a list of nodes to rewrite
  1312. var codeSegments = [byTagName('pre'), byTagName('code'), byTagName('xmp')];
  1313. var elements = [];
  1314. for (var i = 0; i < codeSegments.length; ++i) {
  1315. for (var j = 0, n = codeSegments[i].length; j < n; ++j) {
  1316. elements.push(codeSegments[i][j]);
  1317. }
  1318. }
  1319. codeSegments = null;
  1320. var clock = Date;
  1321. if (!clock['now']) {
  1322. clock = { 'now': function () { return +(new Date); } };
  1323. }
  1324. // The loop is broken into a series of continuations to make sure that we
  1325. // don't make the browser unresponsive when rewriting a large page.
  1326. var k = 0;
  1327. var prettyPrintingJob;
  1328. var langExtensionRe = /\blang(?:uage)?-([\w.]+)(?!\S)/;
  1329. var prettyPrintRe = /\bprettyprint\b/;
  1330. var prettyPrintedRe = /\bprettyprinted\b/;
  1331. var preformattedTagNameRe = /pre|xmp/i;
  1332. var codeRe = /^code$/i;
  1333. var preCodeXmpRe = /^(?:pre|code|xmp)$/i;
  1334. function doWork() {
  1335. var endTime = (win['PR_SHOULD_USE_CONTINUATION'] ?
  1336. clock['now']() + 250 /* ms */ :
  1337. Infinity);
  1338. for (; k < elements.length && clock['now']() < endTime; k++) {
  1339. var cs = elements[k];
  1340. var className = cs.className;
  1341. if (prettyPrintRe.test(className)
  1342. // Don't redo this if we've already done it.
  1343. // This allows recalling pretty print to just prettyprint elements
  1344. // that have been added to the page since last call.
  1345. && !prettyPrintedRe.test(className)) {
  1346. // make sure this is not nested in an already prettified element
  1347. var nested = false;
  1348. for (var p = cs.parentNode; p; p = p.parentNode) {
  1349. var tn = p.tagName;
  1350. if (preCodeXmpRe.test(tn)
  1351. && p.className && prettyPrintRe.test(p.className)) {
  1352. nested = true;
  1353. break;
  1354. }
  1355. }
  1356. if (!nested) {
  1357. // Mark done. If we fail to prettyprint for whatever reason,
  1358. // we shouldn't try again.
  1359. cs.className += ' prettyprinted';
  1360. // If the classes includes a language extensions, use it.
  1361. // Language extensions can be specified like
  1362. // <pre class="prettyprint lang-cpp">
  1363. // the language extension "cpp" is used to find a language handler
  1364. // as passed to PR.registerLangHandler.
  1365. // HTML5 recommends that a language be specified using "language-"
  1366. // as the prefix instead. Google Code Prettify supports both.
  1367. // http://dev.w3.org/html5/spec-author-view/the-code-element.html
  1368. var langExtension = className.match(langExtensionRe);
  1369. // Support <pre class="prettyprint"><code class="language-c">
  1370. var wrapper;
  1371. if (!langExtension && (wrapper = childContentWrapper(cs))
  1372. && codeRe.test(wrapper.tagName)) {
  1373. langExtension = wrapper.className.match(langExtensionRe);
  1374. }
  1375. if (langExtension) { langExtension = langExtension[1]; }
  1376. var preformatted;
  1377. if (preformattedTagNameRe.test(cs.tagName)) {
  1378. preformatted = 1;
  1379. } else {
  1380. var currentStyle = cs['currentStyle'];
  1381. var whitespace = (
  1382. currentStyle
  1383. ? currentStyle['whiteSpace']
  1384. : (document.defaultView
  1385. && document.defaultView.getComputedStyle)
  1386. ? document.defaultView.getComputedStyle(cs, null)
  1387. .getPropertyValue('white-space')
  1388. : 0);
  1389. preformatted = whitespace
  1390. && 'pre' === whitespace.substring(0, 3);
  1391. }
  1392. // Look for a class like linenums or linenums:<n> where <n> is the
  1393. // 1-indexed number of the first line.
  1394. var lineNums = cs.className.match(/\blinenums\b(?::(\d+))?/);
  1395. lineNums = lineNums
  1396. ? lineNums[1] && lineNums[1].length ? +lineNums[1] : true
  1397. : false;
  1398. if (lineNums) { numberLines(cs, lineNums, preformatted); }
  1399. // do the pretty printing
  1400. prettyPrintingJob = {
  1401. langExtension: langExtension,
  1402. sourceNode: cs,
  1403. numberLines: lineNums,
  1404. pre: preformatted
  1405. };
  1406. applyDecorator(prettyPrintingJob);
  1407. }
  1408. }
  1409. }
  1410. if (k < elements.length) {
  1411. // finish up in a continuation
  1412. setTimeout(doWork, 250);
  1413. } else if (opt_whenDone) {
  1414. opt_whenDone();
  1415. }
  1416. }
  1417. doWork();
  1418. }
  1419. /**
  1420. * Contains functions for creating and registering new language handlers.
  1421. * @type {Object}
  1422. */
  1423. var PR = win['PR'] = {
  1424. 'createSimpleLexer': createSimpleLexer,
  1425. 'registerLangHandler': registerLangHandler,
  1426. 'sourceDecorator': sourceDecorator,
  1427. 'PR_ATTRIB_NAME': PR_ATTRIB_NAME,
  1428. 'PR_ATTRIB_VALUE': PR_ATTRIB_VALUE,
  1429. 'PR_COMMENT': PR_COMMENT,
  1430. 'PR_DECLARATION': PR_DECLARATION,
  1431. 'PR_KEYWORD': PR_KEYWORD,
  1432. 'PR_LITERAL': PR_LITERAL,
  1433. 'PR_NOCODE': PR_NOCODE,
  1434. 'PR_PLAIN': PR_PLAIN,
  1435. 'PR_PUNCTUATION': PR_PUNCTUATION,
  1436. 'PR_SOURCE': PR_SOURCE,
  1437. 'PR_STRING': PR_STRING,
  1438. 'PR_TAG': PR_TAG,
  1439. 'PR_TYPE': PR_TYPE,
  1440. 'prettyPrintOne': win['prettyPrintOne'] = prettyPrintOne,
  1441. 'prettyPrint': win['prettyPrint'] = prettyPrint
  1442. };
  1443. // Make PR available via the Asynchronous Module Definition (AMD) API.
  1444. // Per https://github.com/amdjs/amdjs-api/wiki/AMD:
  1445. // The Asynchronous Module Definition (AMD) API specifies a
  1446. // mechanism for defining modules such that the module and its
  1447. // dependencies can be asynchronously loaded.
  1448. // ...
  1449. // To allow a clear indicator that a global define function (as
  1450. // needed for script src browser loading) conforms to the AMD API,
  1451. // any global define function SHOULD have a property called "amd"
  1452. // whose value is an object. This helps avoid conflict with any
  1453. // other existing JavaScript code that could have defined a define()
  1454. // function that does not conform to the AMD API.
  1455. if (typeof define === "function" && define['amd']) {
  1456. define(function () {
  1457. return PR;
  1458. });
  1459. }
  1460. })();