1/** 2 * @license 3 * Copyright (C) 2013 Google Inc. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18/** 19 * @fileoverview 20 * <div style="white-space: pre"> 21 * Looks at query parameters to decide which language handlers and style-sheets 22 * to load. 23 * 24 * Query Parameter Format Effect Default 25 * +------------------+---------------+------------------------------+--------+ 26 * | autorun= | true | false | If true then prettyPrint() | "true" | 27 * | | | is called on page load. | | 28 * +------------------+---------------+------------------------------+--------+ 29 * | lang= | language name | Loads the language handler | Can | 30 * | | | named "lang-<NAME>.js". | appear | 31 * | | | See available handlers at | many | 32 * | | | https://github.com/google/ | times. | 33 * | | | code-prettify/tree/master/ | | 34 * | | | src | | 35 * +------------------+---------------+------------------------------+--------+ 36 * | skin= | skin name | Loads the skin stylesheet | none. | 37 * | | | named "<NAME>.css". | | 38 * | | | https://raw.githack.com/ | | 39 * | | | google/code-prettify/master/ | | 40 * | | | styles/index.html | | 41 * +------------------+---------------+------------------------------+--------+ 42 * | callback= | JS identifier | When "prettyPrint" finishes | none | 43 * | | | window.exports[js_ident] is | | 44 * | | | called. | | 45 * | | | The callback must be under | | 46 * | | | exports to reduce the risk | | 47 * | | | of XSS via query parameter | | 48 * | | | injection. | | 49 * +------------------+---------------+------------------------------+--------+ 50 * 51 * Examples 52 * .../run_prettify.js?lang=css&skin=sunburst 53 * 1. Loads the CSS language handler which can be used to prettify CSS 54 * stylesheets, HTML <style> element bodies and style="..." attributes 55 * values. 56 * 2. Loads the sunburst.css stylesheet instead of the default prettify.css 57 * stylesheet. 58 * A gallery of stylesheets is available at 59 * https://raw.githack.com/google/code-prettify/master/styles/index.html 60 * 3. Since autorun=false is not specified, calls prettyPrint() on page load. 61 * </div> 62 */ 63 64/** 65 * @typedef {!Array.<number|string>} 66 * Alternating indices and the decorations that should be inserted there. 67 * The indices are monotonically increasing. 68 */ 69var DecorationsT; 70 71/** 72 * @typedef {!{ 73 * sourceNode: !Element, 74 * pre: !(number|boolean), 75 * langExtension: ?string, 76 * numberLines: ?(number|boolean), 77 * sourceCode: ?string, 78 * spans: ?(Array.<number|Node>), 79 * basePos: ?number, 80 * decorations: ?DecorationsT 81 * }} 82 * <dl> 83 * <dt>sourceNode<dd>the element containing the source 84 * <dt>sourceCode<dd>source as plain text 85 * <dt>pre<dd>truthy if white-space in text nodes 86 * should be considered significant. 87 * <dt>spans<dd> alternating span start indices into source 88 * and the text node or element (e.g. {@code <BR>}) corresponding to that 89 * span. 90 * <dt>decorations<dd>an array of style classes preceded 91 * by the position at which they start in job.sourceCode in order 92 * <dt>basePos<dd>integer position of this.sourceCode in the larger chunk of 93 * source. 94 * </dl> 95 */ 96var JobT; 97 98/** 99 * @typedef {!{ 100 * sourceCode: string, 101 * spans: !(Array.<number|Node>) 102 * }} 103 * <dl> 104 * <dt>sourceCode<dd>source as plain text 105 * <dt>spans<dd> alternating span start indices into source 106 * and the text node or element (e.g. {@code <BR>}) corresponding to that 107 * span. 108 * </dl> 109 */ 110var SourceSpansT; 111 112/** @define {boolean} */ 113var IN_GLOBAL_SCOPE = false; 114 115 116(function () { 117 "use strict"; 118 119 var win = window; 120 var doc = document; 121 var root = doc.documentElement; 122 var head = doc['head'] || doc.getElementsByTagName("head")[0] || root; 123 124 // From http://javascript.nwbox.com/ContentLoaded/contentloaded.js 125 // Author: Diego Perini (diego.perini at gmail.com) 126 // Summary: cross-browser wrapper for DOMContentLoaded 127 // Updated: 20101020 128 // License: MIT 129 // Version: 1.2 130 function contentLoaded(callback) { 131 var addEventListener = doc['addEventListener']; 132 var done = false, top = true, 133 add = addEventListener ? 'addEventListener' : 'attachEvent', 134 rem = addEventListener ? 'removeEventListener' : 'detachEvent', 135 pre = addEventListener ? '' : 'on', 136 137 init = function(e) { 138 if (e.type == 'readystatechange' && doc.readyState != 'complete') { 139 return; 140 } 141 (e.type == 'load' ? win : doc)[rem](pre + e.type, init, false); 142 if (!done && (done = true)) { callback.call(win, e.type || e); } 143 }, 144 145 poll = function() { 146 try { 147 root.doScroll('left'); 148 } catch(e) { 149 win.setTimeout(poll, 50); 150 return; 151 } 152 init('poll'); 153 }; 154 155 if (doc.readyState == 'complete') { 156 callback.call(win, 'lazy'); 157 } else { 158 if (doc.createEventObject && root.doScroll) { 159 try { top = !win.frameElement; } catch(e) { } 160 if (top) { poll(); } 161 } 162 doc[add](pre + 'DOMContentLoaded', init, false); 163 doc[add](pre + 'readystatechange', init, false); 164 win[add](pre + 'load', init, false); 165 } 166 } 167 168 // Given a list of URLs to stylesheets, loads the first that loads without 169 // triggering an error event. 170 function loadStylesheetsFallingBack(stylesheets) { 171 var n = stylesheets.length; 172 function load(i) { 173 if (i === n) { return; } 174 var link = doc.createElement('link'); 175 link.rel = 'stylesheet'; 176 link.type = 'text/css'; 177 if (i + 1 < n) { 178 // http://pieisgood.org/test/script-link-events/ indicates that many 179 // versions of IE do not support onerror on <link>s, though 180 // http://msdn.microsoft.com/en-us/library/ie/ms535848(v=vs.85).aspx 181 // indicates that recent IEs do support error. 182 link.error = link.onerror = function () { load(i + 1); }; 183 } 184 link.href = stylesheets[i]; 185 head.appendChild(link); 186 } 187 load(0); 188 } 189 190 var scriptQuery = ''; 191 // Look for the <script> node that loads this script to get its parameters. 192 // This starts looking at the end instead of just considering the last 193 // because deferred and async scripts run out of order. 194 // If the script is loaded twice, then this will run in reverse order. 195 var scripts = doc.getElementsByTagName('script'); 196 for (var i = scripts.length; --i >= 0;) { 197 var script = scripts[i]; 198 var match = script.src.match( 199 /^[^?#]*\/run_prettify\.js(\?[^#]*)?(?:#.*)?$/); 200 if (match) { 201 scriptQuery = match[1] || ''; 202 // Remove the script from the DOM so that multiple runs at least run 203 // multiple times even if parameter sets are interpreted in reverse 204 // order. 205 script.parentNode.removeChild(script); 206 break; 207 } 208 } 209 210 // Pull parameters into local variables. 211 var autorun = true; 212 var langs = []; 213 var skins = []; 214 var callbacks = []; 215 scriptQuery.replace( 216 /[?&]([^&=]+)=([^&]+)/g, 217 function (_, name, value) { 218 value = decodeURIComponent(value); 219 name = decodeURIComponent(name); 220 if (name == 'autorun') { autorun = !/^[0fn]/i.test(value); } else 221 if (name == 'lang') { langs.push(value); } else 222 if (name == 'skin') { skins.push(value); } else 223 if (name == 'callback') { callbacks.push(value); } 224 }); 225 226 // Use https to avoid mixed content warnings in client pages and to 227 // prevent a MITM from rewrite prettify mid-flight. 228 // This only works if this script is loaded via https : something 229 // over which we exercise no control. 230 var LOADER_BASE_URL = 231 'https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader'; 232 233 for (var i = 0, n = langs.length; i < n; ++i) (function (lang) { 234 var script = doc.createElement("script"); 235 236 // Excerpted from jQuery.ajaxTransport("script") to fire events when 237 // a script is finished loading. 238 // Attach handlers for each script 239 script.onload = script.onerror = script.onreadystatechange = function () { 240 if (script && ( 241 !script.readyState || /loaded|complete/.test(script.readyState))) { 242 // Handle memory leak in IE 243 script.onerror = script.onload = script.onreadystatechange = null; 244 245 --pendingLanguages; 246 checkPendingLanguages(); 247 248 // Remove the script 249 if (script.parentNode) { 250 script.parentNode.removeChild(script); 251 } 252 253 script = null; 254 } 255 }; 256 257 script.type = 'text/javascript'; 258 script.src = LOADER_BASE_URL 259 + '/lang-' + encodeURIComponent(langs[i]) + '.js'; 260 261 // Circumvent IE6 bugs with base elements (#2709 and #4378) by prepending 262 head.insertBefore(script, head.firstChild); 263 })(langs[i]); 264 265 var pendingLanguages = langs.length; 266 function checkPendingLanguages() { 267 if (!pendingLanguages) { 268 win.setTimeout(onLangsLoaded, 0); 269 } 270 } 271 272 var skinUrls = []; 273 for (var i = 0, n = skins.length; i < n; ++i) { 274 skinUrls.push(LOADER_BASE_URL 275 + '/skins/' + encodeURIComponent(skins[i]) + '.css'); 276 } 277 skinUrls.push(LOADER_BASE_URL + '/prettify.css'); 278 loadStylesheetsFallingBack(skinUrls); 279 280 var prettyPrint = (function () { 281 /** 282 * @license 283 * Copyright (C) 2006 Google Inc. 284 * 285 * Licensed under the Apache License, Version 2.0 (the "License"); 286 * you may not use this file except in compliance with the License. 287 * You may obtain a copy of the License at 288 * 289 * http://www.apache.org/licenses/LICENSE-2.0 290 * 291 * Unless required by applicable law or agreed to in writing, software 292 * distributed under the License is distributed on an "AS IS" BASIS, 293 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 294 * See the License for the specific language governing permissions and 295 * limitations under the License. 296 */ 297 298 /** 299 * @fileoverview 300 * some functions for browser-side pretty printing of code contained in html. 301 * 302 * <p> 303 * For a fairly comprehensive set of languages see the 304 * <a href="https://github.com/google/code-prettify#for-which-languages-does-it-work">README</a> 305 * file that came with this source. At a minimum, the lexer should work on a 306 * number of languages including C and friends, Java, Python, Bash, SQL, HTML, 307 * XML, CSS, Javascript, and Makefiles. It works passably on Ruby, PHP and Awk 308 * and a subset of Perl, but, because of commenting conventions, doesn't work on 309 * Smalltalk, Lisp-like, or CAML-like languages without an explicit lang class. 310 * <p> 311 * Usage: <ol> 312 * <li> include this source file in an html page via 313 * {@code <script type="text/javascript" src="/path/to/prettify.js"></script>} 314 * <li> define style rules. See the example page for examples. 315 * <li> mark the {@code <pre>} and {@code <code>} tags in your source with 316 * {@code class=prettyprint.} 317 * You can also use the (html deprecated) {@code <xmp>} tag, but the pretty 318 * printer needs to do more substantial DOM manipulations to support that, so 319 * some css styles may not be preserved. 320 * </ol> 321 * That's it. I wanted to keep the API as simple as possible, so there's no 322 * need to specify which language the code is in, but if you wish, you can add 323 * another class to the {@code <pre>} or {@code <code>} element to specify the 324 * language, as in {@code <pre class="prettyprint lang-java">}. Any class that 325 * starts with "lang-" followed by a file extension, specifies the file type. 326 * See the "lang-*.js" files in this directory for code that implements 327 * per-language file handlers. 328 * <p> 329 * Change log:<br> 330 * cbeust, 2006/08/22 331 * <blockquote> 332 * Java annotations (start with "@") are now captured as literals ("lit") 333 * </blockquote> 334 * @requires console 335 */ 336 337 // JSLint declarations 338 /*global console, document, navigator, setTimeout, window, define */ 339 340 341 /** 342 * {@type !{ 343 * 'createSimpleLexer': function (Array, Array): (function (JobT)), 344 * 'registerLangHandler': function (function (JobT), Array.<string>), 345 * 'PR_ATTRIB_NAME': string, 346 * 'PR_ATTRIB_NAME': string, 347 * 'PR_ATTRIB_VALUE': string, 348 * 'PR_COMMENT': string, 349 * 'PR_DECLARATION': string, 350 * 'PR_KEYWORD': string, 351 * 'PR_LITERAL': string, 352 * 'PR_NOCODE': string, 353 * 'PR_PLAIN': string, 354 * 'PR_PUNCTUATION': string, 355 * 'PR_SOURCE': string, 356 * 'PR_STRING': string, 357 * 'PR_TAG': string, 358 * 'PR_TYPE': string, 359 * 'prettyPrintOne': function (string, string, number|boolean), 360 * 'prettyPrint': function (?function, ?(HTMLElement|HTMLDocument)) 361 * }} 362 * @const 363 */ 364 var PR; 365 366 /** 367 * Split {@code prettyPrint} into multiple timeouts so as not to interfere with 368 * UI events. 369 * If set to {@code false}, {@code prettyPrint()} is synchronous. 370 */ 371 var PR_SHOULD_USE_CONTINUATION = true 372 if (typeof window !== 'undefined') { 373 window['PR_SHOULD_USE_CONTINUATION'] = PR_SHOULD_USE_CONTINUATION; 374 } 375 376 /** 377 * Pretty print a chunk of code. 378 * @param {string} sourceCodeHtml The HTML to pretty print. 379 * @param {string} opt_langExtension The language name to use. 380 * Typically, a filename extension like 'cpp' or 'java'. 381 * @param {number|boolean} opt_numberLines True to number lines, 382 * or the 1-indexed number of the first line in sourceCodeHtml. 383 * @return {string} code as html, but prettier 384 */ 385 var prettyPrintOne; 386 /** 387 * Find all the {@code <pre>} and {@code <code>} tags in the DOM with 388 * {@code class=prettyprint} and prettify them. 389 * 390 * @param {Function} opt_whenDone called when prettifying is done. 391 * @param {HTMLElement|HTMLDocument} opt_root an element or document 392 * containing all the elements to pretty print. 393 * Defaults to {@code document.body}. 394 */ 395 var prettyPrint; 396 397 398 (function () { 399 var win = (typeof window !== 'undefined') ? window : {}; 400 // Keyword lists for various languages. 401 // We use things that coerce to strings to make them compact when minified 402 // and to defeat aggressive optimizers that fold large string constants. 403 var FLOW_CONTROL_KEYWORDS = ["break,continue,do,else,for,if,return,while"]; 404 var C_KEYWORDS = [FLOW_CONTROL_KEYWORDS,"auto,case,char,const,default," + 405 "double,enum,extern,float,goto,inline,int,long,register,restrict,short,signed," + 406 "sizeof,static,struct,switch,typedef,union,unsigned,void,volatile"]; 407 var COMMON_KEYWORDS = [C_KEYWORDS,"catch,class,delete,false,import," + 408 "new,operator,private,protected,public,this,throw,true,try,typeof"]; 409 var CPP_KEYWORDS = [COMMON_KEYWORDS,"alignas,alignof,align_union,asm,axiom,bool," + 410 "concept,concept_map,const_cast,constexpr,decltype,delegate," + 411 "dynamic_cast,explicit,export,friend,generic,late_check," + 412 "mutable,namespace,noexcept,noreturn,nullptr,property,reinterpret_cast,static_assert," + 413 "static_cast,template,typeid,typename,using,virtual,where"]; 414 var JAVA_KEYWORDS = [COMMON_KEYWORDS, 415 "abstract,assert,boolean,byte,extends,finally,final,implements,import," + 416 "instanceof,interface,null,native,package,strictfp,super,synchronized," + 417 "throws,transient"]; 418 var CSHARP_KEYWORDS = [COMMON_KEYWORDS, 419 "abstract,add,alias,as,ascending,async,await,base,bool,by,byte,checked,decimal,delegate,descending," + 420 "dynamic,event,finally,fixed,foreach,from,get,global,group,implicit,in,interface," + 421 "internal,into,is,join,let,lock,null,object,out,override,orderby,params," + 422 "partial,readonly,ref,remove,sbyte,sealed,select,set,stackalloc,string,select,uint,ulong," + 423 "unchecked,unsafe,ushort,value,var,virtual,where,yield"]; 424 var COFFEE_KEYWORDS = "all,and,by,catch,class,else,extends,false,finally," + 425 "for,if,in,is,isnt,loop,new,no,not,null,of,off,on,or,return,super,then," + 426 "throw,true,try,unless,until,when,while,yes"; 427 var JSCRIPT_KEYWORDS = [COMMON_KEYWORDS, 428 "abstract,async,await,constructor,debugger,enum,eval,export,from,function," + 429 "get,import,implements,instanceof,interface,let,null,of,set,undefined," + 430 "var,with,yield,Infinity,NaN"]; 431 var PERL_KEYWORDS = "caller,delete,die,do,dump,elsif,eval,exit,foreach,for," + 432 "goto,if,import,last,local,my,next,no,our,print,package,redo,require," + 433 "sub,undef,unless,until,use,wantarray,while,BEGIN,END"; 434 var PYTHON_KEYWORDS = [FLOW_CONTROL_KEYWORDS, "and,as,assert,class,def,del," + 435 "elif,except,exec,finally,from,global,import,in,is,lambda," + 436 "nonlocal,not,or,pass,print,raise,try,with,yield," + 437 "False,True,None"]; 438 var RUBY_KEYWORDS = [FLOW_CONTROL_KEYWORDS, "alias,and,begin,case,class," + 439 "def,defined,elsif,end,ensure,false,in,module,next,nil,not,or,redo," + 440 "rescue,retry,self,super,then,true,undef,unless,until,when,yield," + 441 "BEGIN,END"]; 442 var SH_KEYWORDS = [FLOW_CONTROL_KEYWORDS, "case,done,elif,esac,eval,fi," + 443 "function,in,local,set,then,until"]; 444 var ALL_KEYWORDS = [ 445 CPP_KEYWORDS, CSHARP_KEYWORDS, JAVA_KEYWORDS, JSCRIPT_KEYWORDS, 446 PERL_KEYWORDS, PYTHON_KEYWORDS, RUBY_KEYWORDS, SH_KEYWORDS]; 447 var C_TYPES = /^(DIR|FILE|array|vector|(de|priority_)?queue|(forward_)?list|stack|(const_)?(reverse_)?iterator|(unordered_)?(multi)?(set|map)|bitset|u?(int|float)\d*)\b/; 448 449 // token style names. correspond to css classes 450 /** 451 * token style for a string literal 452 * @const 453 */ 454 var PR_STRING = 'str'; 455 /** 456 * token style for a keyword 457 * @const 458 */ 459 var PR_KEYWORD = 'kwd'; 460 /** 461 * token style for a comment 462 * @const 463 */ 464 var PR_COMMENT = 'com'; 465 /** 466 * token style for a type 467 * @const 468 */ 469 var PR_TYPE = 'typ'; 470 /** 471 * token style for a literal value. e.g. 1, null, true. 472 * @const 473 */ 474 var PR_LITERAL = 'lit'; 475 /** 476 * token style for a punctuation string. 477 * @const 478 */ 479 var PR_PUNCTUATION = 'pun'; 480 /** 481 * token style for plain text. 482 * @const 483 */ 484 var PR_PLAIN = 'pln'; 485 486 /** 487 * token style for an sgml tag. 488 * @const 489 */ 490 var PR_TAG = 'tag'; 491 /** 492 * token style for a markup declaration such as a DOCTYPE. 493 * @const 494 */ 495 var PR_DECLARATION = 'dec'; 496 /** 497 * token style for embedded source. 498 * @const 499 */ 500 var PR_SOURCE = 'src'; 501 /** 502 * token style for an sgml attribute name. 503 * @const 504 */ 505 var PR_ATTRIB_NAME = 'atn'; 506 /** 507 * token style for an sgml attribute value. 508 * @const 509 */ 510 var PR_ATTRIB_VALUE = 'atv'; 511 512 /** 513 * A class that indicates a section of markup that is not code, e.g. to allow 514 * embedding of line numbers within code listings. 515 * @const 516 */ 517 var PR_NOCODE = 'nocode'; 518 519 520 // Regex pattern below is automatically generated by regexpPrecederPatterns.pl 521 // Do not modify, your changes will be erased. 522 523 // CAVEAT: this does not properly handle the case where a regular 524 // expression immediately follows another since a regular expression may 525 // have flags for case-sensitivity and the like. Having regexp tokens 526 // adjacent is not valid in any language I'm aware of, so I'm punting. 527 // TODO: maybe style special characters inside a regexp as punctuation. 528 529 /** 530 * A set of tokens that can precede a regular expression literal in 531 * javascript 532 * http://web.archive.org/web/20070717142515/http://www.mozilla.org/js/language/js20/rationale/syntax.html 533 * has the full list, but I've removed ones that might be problematic when 534 * seen in languages that don't support regular expression literals. 535 * 536 * Specifically, I've removed any keywords that can't precede a regexp 537 * literal in a syntactically legal javascript program, and I've removed the 538 * "in" keyword since it's not a keyword in many languages, and might be used 539 * as a count of inches. 540 * 541 * The link above does not accurately describe EcmaScript rules since 542 * it fails to distinguish between (a=++/b/i) and (a++/b/i) but it works 543 * very well in practice. 544 * 545 * @private 546 * @const 547 */ 548 var REGEXP_PRECEDER_PATTERN = '(?:^^\\.?|[+-]|[!=]=?=?|\\#|%=?|&&?=?|\\(|\\*=?|[+\\-]=|->|\\/=?|::?|<<?=?|>>?>?=?|,|;|\\?|@|\\[|~|{|\\^\\^?=?|\\|\\|?=?|break|case|continue|delete|do|else|finally|instanceof|return|throw|try|typeof)\\s*'; 549 550 551 /** 552 * Given a group of {@link RegExp}s, returns a {@code RegExp} that globally 553 * matches the union of the sets of strings matched by the input RegExp. 554 * Since it matches globally, if the input strings have a start-of-input 555 * anchor (/^.../), it is ignored for the purposes of unioning. 556 * @param {Array.<RegExp>} regexs non multiline, non-global regexs. 557 * @return {RegExp} a global regex. 558 */ 559 function combinePrefixPatterns(regexs) { 560 var capturedGroupIndex = 0; 561 562 var needToFoldCase = false; 563 var ignoreCase = false; 564 for (var i = 0, n = regexs.length; i < n; ++i) { 565 var regex = regexs[i]; 566 if (regex.ignoreCase) { 567 ignoreCase = true; 568 } else if (/[a-z]/i.test(regex.source.replace( 569 /\\u[0-9a-f]{4}|\\x[0-9a-f]{2}|\\[^ux]/gi, ''))) { 570 needToFoldCase = true; 571 ignoreCase = false; 572 break; 573 } 574 } 575 576 var escapeCharToCodeUnit = { 577 'b': 8, 578 't': 9, 579 'n': 0xa, 580 'v': 0xb, 581 'f': 0xc, 582 'r': 0xd 583 }; 584 585 function decodeEscape(charsetPart) { 586 var cc0 = charsetPart.charCodeAt(0); 587 if (cc0 !== 92 /* \\ */) { 588 return cc0; 589 } 590 var c1 = charsetPart.charAt(1); 591 cc0 = escapeCharToCodeUnit[c1]; 592 if (cc0) { 593 return cc0; 594 } else if ('0' <= c1 && c1 <= '7') { 595 return parseInt(charsetPart.substring(1), 8); 596 } else if (c1 === 'u' || c1 === 'x') { 597 return parseInt(charsetPart.substring(2), 16); 598 } else { 599 return charsetPart.charCodeAt(1); 600 } 601 } 602 603 function encodeEscape(charCode) { 604 if (charCode < 0x20) { 605 return (charCode < 0x10 ? '\\x0' : '\\x') + charCode.toString(16); 606 } 607 var ch = String.fromCharCode(charCode); 608 return (ch === '\\' || ch === '-' || ch === ']' || ch === '^') 609 ? "\\" + ch : ch; 610 } 611 612 function caseFoldCharset(charSet) { 613 var charsetParts = charSet.substring(1, charSet.length - 1).match( 614 new RegExp( 615 '\\\\u[0-9A-Fa-f]{4}' 616 + '|\\\\x[0-9A-Fa-f]{2}' 617 + '|\\\\[0-3][0-7]{0,2}' 618 + '|\\\\[0-7]{1,2}' 619 + '|\\\\[\\s\\S]' 620 + '|-' 621 + '|[^-\\\\]', 622 'g')); 623 var ranges = []; 624 var inverse = charsetParts[0] === '^'; 625 626 var out = ['[']; 627 if (inverse) { out.push('^'); } 628 629 for (var i = inverse ? 1 : 0, n = charsetParts.length; i < n; ++i) { 630 var p = charsetParts[i]; 631 if (/\\[bdsw]/i.test(p)) { // Don't muck with named groups. 632 out.push(p); 633 } else { 634 var start = decodeEscape(p); 635 var end; 636 if (i + 2 < n && '-' === charsetParts[i + 1]) { 637 end = decodeEscape(charsetParts[i + 2]); 638 i += 2; 639 } else { 640 end = start; 641 } 642 ranges.push([start, end]); 643 // If the range might intersect letters, then expand it. 644 // This case handling is too simplistic. 645 // It does not deal with non-latin case folding. 646 // It works for latin source code identifiers though. 647 if (!(end < 65 || start > 122)) { 648 if (!(end < 65 || start > 90)) { 649 ranges.push([Math.max(65, start) | 32, Math.min(end, 90) | 32]); 650 } 651 if (!(end < 97 || start > 122)) { 652 ranges.push([Math.max(97, start) & ~32, Math.min(end, 122) & ~32]); 653 } 654 } 655 } 656 } 657 658 // [[1, 10], [3, 4], [8, 12], [14, 14], [16, 16], [17, 17]] 659 // -> [[1, 12], [14, 14], [16, 17]] 660 ranges.sort(function (a, b) { return (a[0] - b[0]) || (b[1] - a[1]); }); 661 var consolidatedRanges = []; 662 var lastRange = []; 663 for (var i = 0; i < ranges.length; ++i) { 664 var range = ranges[i]; 665 if (range[0] <= lastRange[1] + 1) { 666 lastRange[1] = Math.max(lastRange[1], range[1]); 667 } else { 668 consolidatedRanges.push(lastRange = range); 669 } 670 } 671 672 for (var i = 0; i < consolidatedRanges.length; ++i) { 673 var range = consolidatedRanges[i]; 674 out.push(encodeEscape(range[0])); 675 if (range[1] > range[0]) { 676 if (range[1] + 1 > range[0]) { out.push('-'); } 677 out.push(encodeEscape(range[1])); 678 } 679 } 680 out.push(']'); 681 return out.join(''); 682 } 683 684 function allowAnywhereFoldCaseAndRenumberGroups(regex) { 685 // Split into character sets, escape sequences, punctuation strings 686 // like ('(', '(?:', ')', '^'), and runs of characters that do not 687 // include any of the above. 688 var parts = regex.source.match( 689 new RegExp( 690 '(?:' 691 + '\\[(?:[^\\x5C\\x5D]|\\\\[\\s\\S])*\\]' // a character set 692 + '|\\\\u[A-Fa-f0-9]{4}' // a unicode escape 693 + '|\\\\x[A-Fa-f0-9]{2}' // a hex escape 694 + '|\\\\[0-9]+' // a back-reference or octal escape 695 + '|\\\\[^ux0-9]' // other escape sequence 696 + '|\\(\\?[:!=]' // start of a non-capturing group 697 + '|[\\(\\)\\^]' // start/end of a group, or line start 698 + '|[^\\x5B\\x5C\\(\\)\\^]+' // run of other characters 699 + ')', 700 'g')); 701 var n = parts.length; 702 703 // Maps captured group numbers to the number they will occupy in 704 // the output or to -1 if that has not been determined, or to 705 // undefined if they need not be capturing in the output. 706 var capturedGroups = []; 707 708 // Walk over and identify back references to build the capturedGroups 709 // mapping. 710 for (var i = 0, groupIndex = 0; i < n; ++i) { 711 var p = parts[i]; 712 if (p === '(') { 713 // groups are 1-indexed, so max group index is count of '(' 714 ++groupIndex; 715 } else if ('\\' === p.charAt(0)) { 716 var decimalValue = +p.substring(1); 717 if (decimalValue) { 718 if (decimalValue <= groupIndex) { 719 capturedGroups[decimalValue] = -1; 720 } else { 721 // Replace with an unambiguous escape sequence so that 722 // an octal escape sequence does not turn into a backreference 723 // to a capturing group from an earlier regex. 724 parts[i] = encodeEscape(decimalValue); 725 } 726 } 727 } 728 } 729 730 // Renumber groups and reduce capturing groups to non-capturing groups 731 // where possible. 732 for (var i = 1; i < capturedGroups.length; ++i) { 733 if (-1 === capturedGroups[i]) { 734 capturedGroups[i] = ++capturedGroupIndex; 735 } 736 } 737 for (var i = 0, groupIndex = 0; i < n; ++i) { 738 var p = parts[i]; 739 if (p === '(') { 740 ++groupIndex; 741 if (!capturedGroups[groupIndex]) { 742 parts[i] = '(?:'; 743 } 744 } else if ('\\' === p.charAt(0)) { 745 var decimalValue = +p.substring(1); 746 if (decimalValue && decimalValue <= groupIndex) { 747 parts[i] = '\\' + capturedGroups[decimalValue]; 748 } 749 } 750 } 751 752 // Remove any prefix anchors so that the output will match anywhere. 753 // ^^ really does mean an anchored match though. 754 for (var i = 0; i < n; ++i) { 755 if ('^' === parts[i] && '^' !== parts[i + 1]) { parts[i] = ''; } 756 } 757 758 // Expand letters to groups to handle mixing of case-sensitive and 759 // case-insensitive patterns if necessary. 760 if (regex.ignoreCase && needToFoldCase) { 761 for (var i = 0; i < n; ++i) { 762 var p = parts[i]; 763 var ch0 = p.charAt(0); 764 if (p.length >= 2 && ch0 === '[') { 765 parts[i] = caseFoldCharset(p); 766 } else if (ch0 !== '\\') { 767 // TODO: handle letters in numeric escapes. 768 parts[i] = p.replace( 769 /[a-zA-Z]/g, 770 function (ch) { 771 var cc = ch.charCodeAt(0); 772 return '[' + String.fromCharCode(cc & ~32, cc | 32) + ']'; 773 }); 774 } 775 } 776 } 777 778 return parts.join(''); 779 } 780 781 var rewritten = []; 782 for (var i = 0, n = regexs.length; i < n; ++i) { 783 var regex = regexs[i]; 784 if (regex.global || regex.multiline) { throw new Error('' + regex); } 785 rewritten.push( 786 '(?:' + allowAnywhereFoldCaseAndRenumberGroups(regex) + ')'); 787 } 788 789 return new RegExp(rewritten.join('|'), ignoreCase ? 'gi' : 'g'); 790 } 791 792 793 /** 794 * Split markup into a string of source code and an array mapping ranges in 795 * that string to the text nodes in which they appear. 796 * 797 * <p> 798 * The HTML DOM structure:</p> 799 * <pre> 800 * (Element "p" 801 * (Element "b" 802 * (Text "print ")) ; #1 803 * (Text "'Hello '") ; #2 804 * (Element "br") ; #3 805 * (Text " + 'World';")) ; #4 806 * </pre> 807 * <p> 808 * corresponds to the HTML 809 * {@code <p><b>print </b>'Hello '<br> + 'World';</p>}.</p> 810 * 811 * <p> 812 * It will produce the output:</p> 813 * <pre> 814 * { 815 * sourceCode: "print 'Hello '\n + 'World';", 816 * // 1 2 817 * // 012345678901234 5678901234567 818 * spans: [0, #1, 6, #2, 14, #3, 15, #4] 819 * } 820 * </pre> 821 * <p> 822 * where #1 is a reference to the {@code "print "} text node above, and so 823 * on for the other text nodes. 824 * </p> 825 * 826 * <p> 827 * The {@code} spans array is an array of pairs. Even elements are the start 828 * indices of substrings, and odd elements are the text nodes (or BR elements) 829 * that contain the text for those substrings. 830 * Substrings continue until the next index or the end of the source. 831 * </p> 832 * 833 * @param {Node} node an HTML DOM subtree containing source-code. 834 * @param {boolean|number} isPreformatted truthy if white-space in 835 * text nodes should be considered significant. 836 * @return {SourceSpansT} source code and the nodes in which they occur. 837 */ 838 function extractSourceSpans(node, isPreformatted) { 839 var nocode = /(?:^|\s)nocode(?:\s|$)/; 840 841 var chunks = []; 842 var length = 0; 843 var spans = []; 844 var k = 0; 845 846 function walk(node) { 847 var type = node.nodeType; 848 if (type == 1) { // Element 849 if (nocode.test(node.className)) { return; } 850 for (var child = node.firstChild; child; child = child.nextSibling) { 851 walk(child); 852 } 853 var nodeName = node.nodeName.toLowerCase(); 854 if ('br' === nodeName || 'li' === nodeName) { 855 chunks[k] = '\n'; 856 spans[k << 1] = length++; 857 spans[(k++ << 1) | 1] = node; 858 } 859 } else if (type == 3 || type == 4) { // Text 860 var text = node.nodeValue; 861 if (text.length) { 862 if (!isPreformatted) { 863 text = text.replace(/[ \t\r\n]+/g, ' '); 864 } else { 865 text = text.replace(/\r\n?/g, '\n'); // Normalize newlines. 866 } 867 // TODO: handle tabs here? 868 chunks[k] = text; 869 spans[k << 1] = length; 870 length += text.length; 871 spans[(k++ << 1) | 1] = node; 872 } 873 } 874 } 875 876 walk(node); 877 878 return { 879 sourceCode: chunks.join('').replace(/\n$/, ''), 880 spans: spans 881 }; 882 } 883 884 885 /** 886 * Apply the given language handler to sourceCode and add the resulting 887 * decorations to out. 888 * @param {!Element} sourceNode 889 * @param {number} basePos the index of sourceCode within the chunk of source 890 * whose decorations are already present on out. 891 * @param {string} sourceCode 892 * @param {function(JobT)} langHandler 893 * @param {DecorationsT} out 894 */ 895 function appendDecorations( 896 sourceNode, basePos, sourceCode, langHandler, out) { 897 if (!sourceCode) { return; } 898 /** @type {JobT} */ 899 var job = { 900 sourceNode: sourceNode, 901 pre: 1, 902 langExtension: null, 903 numberLines: null, 904 sourceCode: sourceCode, 905 spans: null, 906 basePos: basePos, 907 decorations: null 908 }; 909 langHandler(job); 910 out.push.apply(out, job.decorations); 911 } 912 913 var notWs = /\S/; 914 915 /** 916 * Given an element, if it contains only one child element and any text nodes 917 * it contains contain only space characters, return the sole child element. 918 * Otherwise returns undefined. 919 * <p> 920 * This is meant to return the CODE element in {@code <pre><code ...>} when 921 * there is a single child element that contains all the non-space textual 922 * content, but not to return anything where there are multiple child elements 923 * as in {@code <pre><code>...</code><code>...</code></pre>} or when there 924 * is textual content. 925 */ 926 function childContentWrapper(element) { 927 var wrapper = undefined; 928 for (var c = element.firstChild; c; c = c.nextSibling) { 929 var type = c.nodeType; 930 wrapper = (type === 1) // Element Node 931 ? (wrapper ? element : c) 932 : (type === 3) // Text Node 933 ? (notWs.test(c.nodeValue) ? element : wrapper) 934 : wrapper; 935 } 936 return wrapper === element ? undefined : wrapper; 937 } 938 939 /** Given triples of [style, pattern, context] returns a lexing function, 940 * The lexing function interprets the patterns to find token boundaries and 941 * returns a decoration list of the form 942 * [index_0, style_0, index_1, style_1, ..., index_n, style_n] 943 * where index_n is an index into the sourceCode, and style_n is a style 944 * constant like PR_PLAIN. index_n-1 <= index_n, and style_n-1 applies to 945 * all characters in sourceCode[index_n-1:index_n]. 946 * 947 * The stylePatterns is a list whose elements have the form 948 * [style : string, pattern : RegExp, DEPRECATED, shortcut : string]. 949 * 950 * Style is a style constant like PR_PLAIN, or can be a string of the 951 * form 'lang-FOO', where FOO is a language extension describing the 952 * language of the portion of the token in $1 after pattern executes. 953 * E.g., if style is 'lang-lisp', and group 1 contains the text 954 * '(hello (world))', then that portion of the token will be passed to the 955 * registered lisp handler for formatting. 956 * The text before and after group 1 will be restyled using this decorator 957 * so decorators should take care that this doesn't result in infinite 958 * recursion. For example, the HTML lexer rule for SCRIPT elements looks 959 * something like ['lang-js', /<[s]cript>(.+?)<\/script>/]. This may match 960 * '<script>foo()<\/script>', which would cause the current decorator to 961 * be called with '<script>' which would not match the same rule since 962 * group 1 must not be empty, so it would be instead styled as PR_TAG by 963 * the generic tag rule. The handler registered for the 'js' extension would 964 * then be called with 'foo()', and finally, the current decorator would 965 * be called with '<\/script>' which would not match the original rule and 966 * so the generic tag rule would identify it as a tag. 967 * 968 * Pattern must only match prefixes, and if it matches a prefix, then that 969 * match is considered a token with the same style. 970 * 971 * Context is applied to the last non-whitespace, non-comment token 972 * recognized. 973 * 974 * Shortcut is an optional string of characters, any of which, if the first 975 * character, gurantee that this pattern and only this pattern matches. 976 * 977 * @param {Array} shortcutStylePatterns patterns that always start with 978 * a known character. Must have a shortcut string. 979 * @param {Array} fallthroughStylePatterns patterns that will be tried in 980 * order if the shortcut ones fail. May have shortcuts. 981 * 982 * @return {function (JobT)} a function that takes an undecorated job and 983 * attaches a list of decorations. 984 */ 985 function createSimpleLexer(shortcutStylePatterns, fallthroughStylePatterns) { 986 var shortcuts = {}; 987 var tokenizer; 988 (function () { 989 var allPatterns = shortcutStylePatterns.concat(fallthroughStylePatterns); 990 var allRegexs = []; 991 var regexKeys = {}; 992 for (var i = 0, n = allPatterns.length; i < n; ++i) { 993 var patternParts = allPatterns[i]; 994 var shortcutChars = patternParts[3]; 995 if (shortcutChars) { 996 for (var c = shortcutChars.length; --c >= 0;) { 997 shortcuts[shortcutChars.charAt(c)] = patternParts; 998 } 999 } 1000 var regex = patternParts[1]; 1001 var k = '' + regex; 1002 if (!regexKeys.hasOwnProperty(k)) { 1003 allRegexs.push(regex); 1004 regexKeys[k] = null; 1005 } 1006 } 1007 allRegexs.push(/[\0-\uffff]/); 1008 tokenizer = combinePrefixPatterns(allRegexs); 1009 })(); 1010 1011 var nPatterns = fallthroughStylePatterns.length; 1012 1013 /** 1014 * Lexes job.sourceCode and attaches an output array job.decorations of 1015 * style classes preceded by the position at which they start in 1016 * job.sourceCode in order. 1017 * 1018 * @type{function (JobT)} 1019 */ 1020 var decorate = function (job) { 1021 var sourceCode = job.sourceCode, basePos = job.basePos; 1022 var sourceNode = job.sourceNode; 1023 /** Even entries are positions in source in ascending order. Odd enties 1024 * are style markers (e.g., PR_COMMENT) that run from that position until 1025 * the end. 1026 * @type {DecorationsT} 1027 */ 1028 var decorations = [basePos, PR_PLAIN]; 1029 var pos = 0; // index into sourceCode 1030 var tokens = sourceCode.match(tokenizer) || []; 1031 var styleCache = {}; 1032 1033 for (var ti = 0, nTokens = tokens.length; ti < nTokens; ++ti) { 1034 var token = tokens[ti]; 1035 var style = styleCache[token]; 1036 var match = void 0; 1037 1038 var isEmbedded; 1039 if (typeof style === 'string') { 1040 isEmbedded = false; 1041 } else { 1042 var patternParts = shortcuts[token.charAt(0)]; 1043 if (patternParts) { 1044 match = token.match(patternParts[1]); 1045 style = patternParts[0]; 1046 } else { 1047 for (var i = 0; i < nPatterns; ++i) { 1048 patternParts = fallthroughStylePatterns[i]; 1049 match = token.match(patternParts[1]); 1050 if (match) { 1051 style = patternParts[0]; 1052 break; 1053 } 1054 } 1055 1056 if (!match) { // make sure that we make progress 1057 style = PR_PLAIN; 1058 } 1059 } 1060 1061 isEmbedded = style.length >= 5 && 'lang-' === style.substring(0, 5); 1062 if (isEmbedded && !(match && typeof match[1] === 'string')) { 1063 isEmbedded = false; 1064 style = PR_SOURCE; 1065 } 1066 1067 if (!isEmbedded) { styleCache[token] = style; } 1068 } 1069 1070 var tokenStart = pos; 1071 pos += token.length; 1072 1073 if (!isEmbedded) { 1074 decorations.push(basePos + tokenStart, style); 1075 } else { // Treat group 1 as an embedded block of source code. 1076 var embeddedSource = match[1]; 1077 var embeddedSourceStart = token.indexOf(embeddedSource); 1078 var embeddedSourceEnd = embeddedSourceStart + embeddedSource.length; 1079 if (match[2]) { 1080 // If embeddedSource can be blank, then it would match at the 1081 // beginning which would cause us to infinitely recurse on the 1082 // entire token, so we catch the right context in match[2]. 1083 embeddedSourceEnd = token.length - match[2].length; 1084 embeddedSourceStart = embeddedSourceEnd - embeddedSource.length; 1085 } 1086 var lang = style.substring(5); 1087 // Decorate the left of the embedded source 1088 appendDecorations( 1089 sourceNode, 1090 basePos + tokenStart, 1091 token.substring(0, embeddedSourceStart), 1092 decorate, decorations); 1093 // Decorate the embedded source 1094 appendDecorations( 1095 sourceNode, 1096 basePos + tokenStart + embeddedSourceStart, 1097 embeddedSource, 1098 langHandlerForExtension(lang, embeddedSource), 1099 decorations); 1100 // Decorate the right of the embedded section 1101 appendDecorations( 1102 sourceNode, 1103 basePos + tokenStart + embeddedSourceEnd, 1104 token.substring(embeddedSourceEnd), 1105 decorate, decorations); 1106 } 1107 } 1108 job.decorations = decorations; 1109 }; 1110 return decorate; 1111 } 1112 1113 /** returns a function that produces a list of decorations from source text. 1114 * 1115 * This code treats ", ', and ` as string delimiters, and \ as a string 1116 * escape. It does not recognize perl's qq() style strings. 1117 * It has no special handling for double delimiter escapes as in basic, or 1118 * the tripled delimiters used in python, but should work on those regardless 1119 * although in those cases a single string literal may be broken up into 1120 * multiple adjacent string literals. 1121 * 1122 * It recognizes C, C++, and shell style comments. 1123 * 1124 * @param {Object} options a set of optional parameters. 1125 * @return {function (JobT)} a function that examines the source code 1126 * in the input job and builds a decoration list which it attaches to 1127 * the job. 1128 */ 1129 function sourceDecorator(options) { 1130 var shortcutStylePatterns = [], fallthroughStylePatterns = []; 1131 if (options['tripleQuotedStrings']) { 1132 // '''multi-line-string''', 'single-line-string', and double-quoted 1133 shortcutStylePatterns.push( 1134 [PR_STRING, /^(?:\'\'\'(?:[^\'\\]|\\[\s\S]|\'{1,2}(?=[^\']))*(?:\'\'\'|$)|\"\"\"(?:[^\"\\]|\\[\s\S]|\"{1,2}(?=[^\"]))*(?:\"\"\"|$)|\'(?:[^\\\']|\\[\s\S])*(?:\'|$)|\"(?:[^\\\"]|\\[\s\S])*(?:\"|$))/, 1135 null, '\'"']); 1136 } else if (options['multiLineStrings']) { 1137 // 'multi-line-string', "multi-line-string" 1138 shortcutStylePatterns.push( 1139 [PR_STRING, /^(?:\'(?:[^\\\']|\\[\s\S])*(?:\'|$)|\"(?:[^\\\"]|\\[\s\S])*(?:\"|$)|\`(?:[^\\\`]|\\[\s\S])*(?:\`|$))/, 1140 null, '\'"`']); 1141 } else { 1142 // 'single-line-string', "single-line-string" 1143 shortcutStylePatterns.push( 1144 [PR_STRING, 1145 /^(?:\'(?:[^\\\'\r\n]|\\.)*(?:\'|$)|\"(?:[^\\\"\r\n]|\\.)*(?:\"|$))/, 1146 null, '"\'']); 1147 } 1148 if (options['verbatimStrings']) { 1149 // verbatim-string-literal production from the C# grammar. See issue 93. 1150 fallthroughStylePatterns.push( 1151 [PR_STRING, /^@\"(?:[^\"]|\"\")*(?:\"|$)/, null]); 1152 } 1153 var hc = options['hashComments']; 1154 if (hc) { 1155 if (options['cStyleComments']) { 1156 if (hc > 1) { // multiline hash comments 1157 shortcutStylePatterns.push( 1158 [PR_COMMENT, /^#(?:##(?:[^#]|#(?!##))*(?:###|$)|.*)/, null, '#']); 1159 } else { 1160 // Stop C preprocessor declarations at an unclosed open comment 1161 shortcutStylePatterns.push( 1162 [PR_COMMENT, /^#(?:(?:define|e(?:l|nd)if|else|error|ifn?def|include|line|pragma|undef|warning)\b|[^\r\n]*)/, 1163 null, '#']); 1164 } 1165 // #include <stdio.h> 1166 fallthroughStylePatterns.push( 1167 [PR_STRING, 1168 /^<(?:(?:(?:\.\.\/)*|\/?)(?:[\w-]+(?:\/[\w-]+)+)?[\w-]+\.h(?:h|pp|\+\+)?|[a-z]\w*)>/, 1169 null]); 1170 } else { 1171 shortcutStylePatterns.push([PR_COMMENT, /^#[^\r\n]*/, null, '#']); 1172 } 1173 } 1174 if (options['cStyleComments']) { 1175 fallthroughStylePatterns.push([PR_COMMENT, /^\/\/[^\r\n]*/, null]); 1176 fallthroughStylePatterns.push( 1177 [PR_COMMENT, /^\/\*[\s\S]*?(?:\*\/|$)/, null]); 1178 } 1179 var regexLiterals = options['regexLiterals']; 1180 if (regexLiterals) { 1181 /** 1182 * @const 1183 */ 1184 var regexExcls = regexLiterals > 1 1185 ? '' // Multiline regex literals 1186 : '\n\r'; 1187 /** 1188 * @const 1189 */ 1190 var regexAny = regexExcls ? '.' : '[\\S\\s]'; 1191 /** 1192 * @const 1193 */ 1194 var REGEX_LITERAL = ( 1195 // A regular expression literal starts with a slash that is 1196 // not followed by * or / so that it is not confused with 1197 // comments. 1198 '/(?=[^/*' + regexExcls + '])' 1199 // and then contains any number of raw characters, 1200 + '(?:[^/\\x5B\\x5C' + regexExcls + ']' 1201 // escape sequences (\x5C), 1202 + '|\\x5C' + regexAny 1203 // or non-nesting character sets (\x5B\x5D); 1204 + '|\\x5B(?:[^\\x5C\\x5D' + regexExcls + ']' 1205 + '|\\x5C' + regexAny + ')*(?:\\x5D|$))+' 1206 // finally closed by a /. 1207 + '/'); 1208 fallthroughStylePatterns.push( 1209 ['lang-regex', 1210 RegExp('^' + REGEXP_PRECEDER_PATTERN + '(' + REGEX_LITERAL + ')') 1211 ]); 1212 } 1213 1214 var types = options['types']; 1215 if (types) { 1216 fallthroughStylePatterns.push([PR_TYPE, types]); 1217 } 1218 1219 var keywords = ("" + options['keywords']).replace(/^ | $/g, ''); 1220 if (keywords.length) { 1221 fallthroughStylePatterns.push( 1222 [PR_KEYWORD, 1223 new RegExp('^(?:' + keywords.replace(/[\s,]+/g, '|') + ')\\b'), 1224 null]); 1225 } 1226 1227 shortcutStylePatterns.push([PR_PLAIN, /^\s+/, null, ' \r\n\t\xA0']); 1228 1229 var punctuation = 1230 // The Bash man page says 1231 1232 // A word is a sequence of characters considered as a single 1233 // unit by GRUB. Words are separated by metacharacters, 1234 // which are the following plus space, tab, and newline: { } 1235 // | & $ ; < > 1236 // ... 1237 1238 // A word beginning with # causes that word and all remaining 1239 // characters on that line to be ignored. 1240 1241 // which means that only a '#' after /(?:^|[{}|&$;<>\s])/ starts a 1242 // comment but empirically 1243 // $ echo {#} 1244 // {#} 1245 // $ echo \$# 1246 // $# 1247 // $ echo }# 1248 // }# 1249 1250 // so /(?:^|[|&;<>\s])/ is more appropriate. 1251 1252 // http://gcc.gnu.org/onlinedocs/gcc-2.95.3/cpp_1.html#SEC3 1253 // suggests that this definition is compatible with a 1254 // default mode that tries to use a single token definition 1255 // to recognize both bash/python style comments and C 1256 // preprocessor directives. 1257 1258 // This definition of punctuation does not include # in the list of 1259 // follow-on exclusions, so # will not be broken before if preceeded 1260 // by a punctuation character. We could try to exclude # after 1261 // [|&;<>] but that doesn't seem to cause many major problems. 1262 // If that does turn out to be a problem, we should change the below 1263 // when hc is truthy to include # in the run of punctuation characters 1264 // only when not followint [|&;<>]. 1265 '^.[^\\s\\w.$@\'"`/\\\\]*'; 1266 if (options['regexLiterals']) { 1267 punctuation += '(?!\s*\/)'; 1268 } 1269 1270 fallthroughStylePatterns.push( 1271 // TODO(mikesamuel): recognize non-latin letters and numerals in idents 1272 [PR_LITERAL, /^@[a-z_$][a-z_$@0-9]*/i, null], 1273 [PR_TYPE, /^(?:[@_]?[A-Z]+[a-z][A-Za-z_$@0-9]*|\w+_t\b)/, null], 1274 [PR_PLAIN, /^[a-z_$][a-z_$@0-9]*/i, null], 1275 [PR_LITERAL, 1276 new RegExp( 1277 '^(?:' 1278 // A hex number 1279 + '0x[a-f0-9]+' 1280 // or an octal or decimal number, 1281 + '|(?:\\d(?:_\\d+)*\\d*(?:\\.\\d*)?|\\.\\d\\+)' 1282 // possibly in scientific notation 1283 + '(?:e[+\\-]?\\d+)?' 1284 + ')' 1285 // with an optional modifier like UL for unsigned long 1286 + '[a-z]*', 'i'), 1287 null, '0123456789'], 1288 // Don't treat escaped quotes in bash as starting strings. 1289 // See issue 144. 1290 [PR_PLAIN, /^\\[\s\S]?/, null], 1291 [PR_PUNCTUATION, new RegExp(punctuation), null]); 1292 1293 return createSimpleLexer(shortcutStylePatterns, fallthroughStylePatterns); 1294 } 1295 1296 var decorateSource = sourceDecorator({ 1297 'keywords': ALL_KEYWORDS, 1298 'hashComments': true, 1299 'cStyleComments': true, 1300 'multiLineStrings': true, 1301 'regexLiterals': true 1302 }); 1303 1304 /** 1305 * Given a DOM subtree, wraps it in a list, and puts each line into its own 1306 * list item. 1307 * 1308 * @param {Node} node modified in place. Its content is pulled into an 1309 * HTMLOListElement, and each line is moved into a separate list item. 1310 * This requires cloning elements, so the input might not have unique 1311 * IDs after numbering. 1312 * @param {number|null|boolean} startLineNum 1313 * If truthy, coerced to an integer which is the 1-indexed line number 1314 * of the first line of code. The number of the first line will be 1315 * attached to the list. 1316 * @param {boolean} isPreformatted true iff white-space in text nodes should 1317 * be treated as significant. 1318 */ 1319 function numberLines(node, startLineNum, isPreformatted) { 1320 var nocode = /(?:^|\s)nocode(?:\s|$)/; 1321 var lineBreak = /\r\n?|\n/; 1322 1323 var document = node.ownerDocument; 1324 1325 var li = document.createElement('li'); 1326 while (node.firstChild) { 1327 li.appendChild(node.firstChild); 1328 } 1329 // An array of lines. We split below, so this is initialized to one 1330 // un-split line. 1331 var listItems = [li]; 1332 1333 function walk(node) { 1334 var type = node.nodeType; 1335 if (type == 1 && !nocode.test(node.className)) { // Element 1336 if ('br' === node.nodeName.toLowerCase()) { 1337 breakAfter(node); 1338 // Discard the <BR> since it is now flush against a </LI>. 1339 if (node.parentNode) { 1340 node.parentNode.removeChild(node); 1341 } 1342 } else { 1343 for (var child = node.firstChild; child; child = child.nextSibling) { 1344 walk(child); 1345 } 1346 } 1347 } else if ((type == 3 || type == 4) && isPreformatted) { // Text 1348 var text = node.nodeValue; 1349 var match = text.match(lineBreak); 1350 if (match) { 1351 var firstLine = text.substring(0, match.index); 1352 node.nodeValue = firstLine; 1353 var tail = text.substring(match.index + match[0].length); 1354 if (tail) { 1355 var parent = node.parentNode; 1356 parent.insertBefore( 1357 document.createTextNode(tail), node.nextSibling); 1358 } 1359 breakAfter(node); 1360 if (!firstLine) { 1361 // Don't leave blank text nodes in the DOM. 1362 node.parentNode.removeChild(node); 1363 } 1364 } 1365 } 1366 } 1367 1368 // Split a line after the given node. 1369 function breakAfter(lineEndNode) { 1370 // If there's nothing to the right, then we can skip ending the line 1371 // here, and move root-wards since splitting just before an end-tag 1372 // would require us to create a bunch of empty copies. 1373 while (!lineEndNode.nextSibling) { 1374 lineEndNode = lineEndNode.parentNode; 1375 if (!lineEndNode) { return; } 1376 } 1377 1378 function breakLeftOf(limit, copy) { 1379 // Clone shallowly if this node needs to be on both sides of the break. 1380 var rightSide = copy ? limit.cloneNode(false) : limit; 1381 var parent = limit.parentNode; 1382 if (parent) { 1383 // We clone the parent chain. 1384 // This helps us resurrect important styling elements that cross lines. 1385 // E.g. in <i>Foo<br>Bar</i> 1386 // should be rewritten to <li><i>Foo</i></li><li><i>Bar</i></li>. 1387 var parentClone = breakLeftOf(parent, 1); 1388 // Move the clone and everything to the right of the original 1389 // onto the cloned parent. 1390 var next = limit.nextSibling; 1391 parentClone.appendChild(rightSide); 1392 for (var sibling = next; sibling; sibling = next) { 1393 next = sibling.nextSibling; 1394 parentClone.appendChild(sibling); 1395 } 1396 } 1397 return rightSide; 1398 } 1399 1400 var copiedListItem = breakLeftOf(lineEndNode.nextSibling, 0); 1401 1402 // Walk the parent chain until we reach an unattached LI. 1403 for (var parent; 1404 // Check nodeType since IE invents document fragments. 1405 (parent = copiedListItem.parentNode) && parent.nodeType === 1;) { 1406 copiedListItem = parent; 1407 } 1408 // Put it on the list of lines for later processing. 1409 listItems.push(copiedListItem); 1410 } 1411 1412 // Split lines while there are lines left to split. 1413 for (var i = 0; // Number of lines that have been split so far. 1414 i < listItems.length; // length updated by breakAfter calls. 1415 ++i) { 1416 walk(listItems[i]); 1417 } 1418 1419 // Make sure numeric indices show correctly. 1420 if (startLineNum === (startLineNum|0)) { 1421 listItems[0].setAttribute('value', startLineNum); 1422 } 1423 1424 var ol = document.createElement('ol'); 1425 ol.className = 'linenums'; 1426 var offset = Math.max(0, ((startLineNum - 1 /* zero index */)) | 0) || 0; 1427 for (var i = 0, n = listItems.length; i < n; ++i) { 1428 li = listItems[i]; 1429 // Stick a class on the LIs so that stylesheets can 1430 // color odd/even rows, or any other row pattern that 1431 // is co-prime with 10. 1432 li.className = 'L' + ((i + offset) % 10); 1433 if (!li.firstChild) { 1434 li.appendChild(document.createTextNode('\xA0')); 1435 } 1436 ol.appendChild(li); 1437 } 1438 1439 node.appendChild(ol); 1440 } 1441 1442 1443 /** 1444 * Breaks {@code job.sourceCode} around style boundaries in 1445 * {@code job.decorations} and modifies {@code job.sourceNode} in place. 1446 * @param {JobT} job 1447 * @private 1448 */ 1449 function recombineTagsAndDecorations(job) { 1450 var isIE8OrEarlier = /\bMSIE\s(\d+)/.exec(navigator.userAgent); 1451 isIE8OrEarlier = isIE8OrEarlier && +isIE8OrEarlier[1] <= 8; 1452 var newlineRe = /\n/g; 1453 1454 var source = job.sourceCode; 1455 var sourceLength = source.length; 1456 // Index into source after the last code-unit recombined. 1457 var sourceIndex = 0; 1458 1459 var spans = job.spans; 1460 var nSpans = spans.length; 1461 // Index into spans after the last span which ends at or before sourceIndex. 1462 var spanIndex = 0; 1463 1464 var decorations = job.decorations; 1465 var nDecorations = decorations.length; 1466 // Index into decorations after the last decoration which ends at or before 1467 // sourceIndex. 1468 var decorationIndex = 0; 1469 1470 // Remove all zero-length decorations. 1471 decorations[nDecorations] = sourceLength; 1472 var decPos, i; 1473 for (i = decPos = 0; i < nDecorations;) { 1474 if (decorations[i] !== decorations[i + 2]) { 1475 decorations[decPos++] = decorations[i++]; 1476 decorations[decPos++] = decorations[i++]; 1477 } else { 1478 i += 2; 1479 } 1480 } 1481 nDecorations = decPos; 1482 1483 // Simplify decorations. 1484 for (i = decPos = 0; i < nDecorations;) { 1485 var startPos = decorations[i]; 1486 // Conflate all adjacent decorations that use the same style. 1487 var startDec = decorations[i + 1]; 1488 var end = i + 2; 1489 while (end + 2 <= nDecorations && decorations[end + 1] === startDec) { 1490 end += 2; 1491 } 1492 decorations[decPos++] = startPos; 1493 decorations[decPos++] = startDec; 1494 i = end; 1495 } 1496 1497 nDecorations = decorations.length = decPos; 1498 1499 var sourceNode = job.sourceNode; 1500 var oldDisplay = ""; 1501 if (sourceNode) { 1502 oldDisplay = sourceNode.style.display; 1503 sourceNode.style.display = 'none'; 1504 } 1505 try { 1506 var decoration = null; 1507 while (spanIndex < nSpans) { 1508 var spanStart = spans[spanIndex]; 1509 var spanEnd = /** @type{number} */ (spans[spanIndex + 2]) 1510 || sourceLength; 1511 1512 var decEnd = decorations[decorationIndex + 2] || sourceLength; 1513 1514 var end = Math.min(spanEnd, decEnd); 1515 1516 var textNode = /** @type{Node} */ (spans[spanIndex + 1]); 1517 var styledText; 1518 if (textNode.nodeType !== 1 // Don't muck with <BR>s or <LI>s 1519 // Don't introduce spans around empty text nodes. 1520 && (styledText = source.substring(sourceIndex, end))) { 1521 // This may seem bizarre, and it is. Emitting LF on IE causes the 1522 // code to display with spaces instead of line breaks. 1523 // Emitting Windows standard issue linebreaks (CRLF) causes a blank 1524 // space to appear at the beginning of every line but the first. 1525 // Emitting an old Mac OS 9 line separator makes everything spiffy. 1526 if (isIE8OrEarlier) { 1527 styledText = styledText.replace(newlineRe, '\r'); 1528 } 1529 textNode.nodeValue = styledText; 1530 var document = textNode.ownerDocument; 1531 var span = document.createElement('span'); 1532 span.className = decorations[decorationIndex + 1]; 1533 var parentNode = textNode.parentNode; 1534 parentNode.replaceChild(span, textNode); 1535 span.appendChild(textNode); 1536 if (sourceIndex < spanEnd) { // Split off a text node. 1537 spans[spanIndex + 1] = textNode 1538 // TODO: Possibly optimize by using '' if there's no flicker. 1539 = document.createTextNode(source.substring(end, spanEnd)); 1540 parentNode.insertBefore(textNode, span.nextSibling); 1541 } 1542 } 1543 1544 sourceIndex = end; 1545 1546 if (sourceIndex >= spanEnd) { 1547 spanIndex += 2; 1548 } 1549 if (sourceIndex >= decEnd) { 1550 decorationIndex += 2; 1551 } 1552 } 1553 } finally { 1554 if (sourceNode) { 1555 sourceNode.style.display = oldDisplay; 1556 } 1557 } 1558 } 1559 1560 1561 /** Maps language-specific file extensions to handlers. */ 1562 var langHandlerRegistry = {}; 1563 /** Register a language handler for the given file extensions. 1564 * @param {function (JobT)} handler a function from source code to a list 1565 * of decorations. Takes a single argument job which describes the 1566 * state of the computation and attaches the decorations to it. 1567 * @param {Array.<string>} fileExtensions 1568 */ 1569 function registerLangHandler(handler, fileExtensions) { 1570 for (var i = fileExtensions.length; --i >= 0;) { 1571 var ext = fileExtensions[i]; 1572 if (!langHandlerRegistry.hasOwnProperty(ext)) { 1573 langHandlerRegistry[ext] = handler; 1574 } else if (win['console']) { 1575 console['warn']('cannot override language handler %s', ext); 1576 } 1577 } 1578 } 1579 function langHandlerForExtension(extension, source) { 1580 if (!(extension && langHandlerRegistry.hasOwnProperty(extension))) { 1581 // Treat it as markup if the first non whitespace character is a < and 1582 // the last non-whitespace character is a >. 1583 extension = /^\s*</.test(source) 1584 ? 'default-markup' 1585 : 'default-code'; 1586 } 1587 return langHandlerRegistry[extension]; 1588 } 1589 registerLangHandler(decorateSource, ['default-code']); 1590 registerLangHandler( 1591 createSimpleLexer( 1592 [], 1593 [ 1594 [PR_PLAIN, /^[^<?]+/], 1595 [PR_DECLARATION, /^<!\w[^>]*(?:>|$)/], 1596 [PR_COMMENT, /^<\!--[\s\S]*?(?:-\->|$)/], 1597 // Unescaped content in an unknown language 1598 ['lang-', /^<\?([\s\S]+?)(?:\?>|$)/], 1599 ['lang-', /^<%([\s\S]+?)(?:%>|$)/], 1600 [PR_PUNCTUATION, /^(?:<[%?]|[%?]>)/], 1601 ['lang-', /^<xmp\b[^>]*>([\s\S]+?)<\/xmp\b[^>]*>/i], 1602 // Unescaped content in javascript. (Or possibly vbscript). 1603 ['lang-js', /^<script\b[^>]*>([\s\S]*?)(<\/script\b[^>]*>)/i], 1604 // Contains unescaped stylesheet content 1605 ['lang-css', /^<style\b[^>]*>([\s\S]*?)(<\/style\b[^>]*>)/i], 1606 ['lang-in.tag', /^(<\/?[a-z][^<>]*>)/i] 1607 ]), 1608 ['default-markup', 'htm', 'html', 'mxml', 'xhtml', 'xml', 'xsl']); 1609 registerLangHandler( 1610 createSimpleLexer( 1611 [ 1612 [PR_PLAIN, /^[\s]+/, null, ' \t\r\n'], 1613 [PR_ATTRIB_VALUE, /^(?:\"[^\"]*\"?|\'[^\']*\'?)/, null, '\"\''] 1614 ], 1615 [ 1616 [PR_TAG, /^^<\/?[a-z](?:[\w.:-]*\w)?|\/?>$/i], 1617 [PR_ATTRIB_NAME, /^(?!style[\s=]|on)[a-z](?:[\w:-]*\w)?/i], 1618 ['lang-uq.val', /^=\s*([^>\'\"\s]*(?:[^>\'\"\s\/]|\/(?=\s)))/], 1619 [PR_PUNCTUATION, /^[=<>\/]+/], 1620 ['lang-js', /^on\w+\s*=\s*\"([^\"]+)\"/i], 1621 ['lang-js', /^on\w+\s*=\s*\'([^\']+)\'/i], 1622 ['lang-js', /^on\w+\s*=\s*([^\"\'>\s]+)/i], 1623 ['lang-css', /^style\s*=\s*\"([^\"]+)\"/i], 1624 ['lang-css', /^style\s*=\s*\'([^\']+)\'/i], 1625 ['lang-css', /^style\s*=\s*([^\"\'>\s]+)/i] 1626 ]), 1627 ['in.tag']); 1628 registerLangHandler( 1629 createSimpleLexer([], [[PR_ATTRIB_VALUE, /^[\s\S]+/]]), ['uq.val']); 1630 registerLangHandler(sourceDecorator({ 1631 'keywords': CPP_KEYWORDS, 1632 'hashComments': true, 1633 'cStyleComments': true, 1634 'types': C_TYPES 1635 }), ['c', 'cc', 'cpp', 'cxx', 'cyc', 'm']); 1636 registerLangHandler(sourceDecorator({ 1637 'keywords': 'null,true,false' 1638 }), ['json']); 1639 registerLangHandler(sourceDecorator({ 1640 'keywords': CSHARP_KEYWORDS, 1641 'hashComments': true, 1642 'cStyleComments': true, 1643 'verbatimStrings': true, 1644 'types': C_TYPES 1645 }), ['cs']); 1646 registerLangHandler(sourceDecorator({ 1647 'keywords': JAVA_KEYWORDS, 1648 'cStyleComments': true 1649 }), ['java']); 1650 registerLangHandler(sourceDecorator({ 1651 'keywords': SH_KEYWORDS, 1652 'hashComments': true, 1653 'multiLineStrings': true 1654 }), ['bash', 'bsh', 'csh', 'sh']); 1655 registerLangHandler(sourceDecorator({ 1656 'keywords': PYTHON_KEYWORDS, 1657 'hashComments': true, 1658 'multiLineStrings': true, 1659 'tripleQuotedStrings': true 1660 }), ['cv', 'py', 'python']); 1661 registerLangHandler(sourceDecorator({ 1662 'keywords': PERL_KEYWORDS, 1663 'hashComments': true, 1664 'multiLineStrings': true, 1665 'regexLiterals': 2 // multiline regex literals 1666 }), ['perl', 'pl', 'pm']); 1667 registerLangHandler(sourceDecorator({ 1668 'keywords': RUBY_KEYWORDS, 1669 'hashComments': true, 1670 'multiLineStrings': true, 1671 'regexLiterals': true 1672 }), ['rb', 'ruby']); 1673 registerLangHandler(sourceDecorator({ 1674 'keywords': JSCRIPT_KEYWORDS, 1675 'cStyleComments': true, 1676 'regexLiterals': true 1677 }), ['javascript', 'js', 'ts', 'typescript']); 1678 registerLangHandler(sourceDecorator({ 1679 'keywords': COFFEE_KEYWORDS, 1680 'hashComments': 3, // ### style block comments 1681 'cStyleComments': true, 1682 'multilineStrings': true, 1683 'tripleQuotedStrings': true, 1684 'regexLiterals': true 1685 }), ['coffee']); 1686 registerLangHandler( 1687 createSimpleLexer([], [[PR_STRING, /^[\s\S]+/]]), ['regex']); 1688 1689 /** @param {JobT} job */ 1690 function applyDecorator(job) { 1691 var opt_langExtension = job.langExtension; 1692 1693 try { 1694 // Extract tags, and convert the source code to plain text. 1695 var sourceAndSpans = extractSourceSpans(job.sourceNode, job.pre); 1696 /** Plain text. @type {string} */ 1697 var source = sourceAndSpans.sourceCode; 1698 job.sourceCode = source; 1699 job.spans = sourceAndSpans.spans; 1700 job.basePos = 0; 1701 1702 // Apply the appropriate language handler 1703 langHandlerForExtension(opt_langExtension, source)(job); 1704 1705 // Integrate the decorations and tags back into the source code, 1706 // modifying the sourceNode in place. 1707 recombineTagsAndDecorations(job); 1708 } catch (e) { 1709 if (win['console']) { 1710 console['log'](e && e['stack'] || e); 1711 } 1712 } 1713 } 1714 1715 /** 1716 * Pretty print a chunk of code. 1717 * @param sourceCodeHtml {string} The HTML to pretty print. 1718 * @param opt_langExtension {string} The language name to use. 1719 * Typically, a filename extension like 'cpp' or 'java'. 1720 * @param opt_numberLines {number|boolean} True to number lines, 1721 * or the 1-indexed number of the first line in sourceCodeHtml. 1722 */ 1723 function $prettyPrintOne(sourceCodeHtml, opt_langExtension, opt_numberLines) { 1724 /** @type{number|boolean} */ 1725 var nl = opt_numberLines || false; 1726 /** @type{string|null} */ 1727 var langExtension = opt_langExtension || null; 1728 /** @type{!Element} */ 1729 var container = document.createElement('div'); 1730 // This could cause images to load and onload listeners to fire. 1731 // E.g. <img onerror="alert(1337)" src="nosuchimage.png">. 1732 // We assume that the inner HTML is from a trusted source. 1733 // The pre-tag is required for IE8 which strips newlines from innerHTML 1734 // when it is injected into a <pre> tag. 1735 // http://stackoverflow.com/questions/451486/pre-tag-loses-line-breaks-when-setting-innerhtml-in-ie 1736 // http://stackoverflow.com/questions/195363/inserting-a-newline-into-a-pre-tag-ie-javascript 1737 container.innerHTML = '<pre>' + sourceCodeHtml + '</pre>'; 1738 container = /** @type{!Element} */(container.firstChild); 1739 if (nl) { 1740 numberLines(container, nl, true); 1741 } 1742 1743 /** @type{JobT} */ 1744 var job = { 1745 langExtension: langExtension, 1746 numberLines: nl, 1747 sourceNode: container, 1748 pre: 1, 1749 sourceCode: null, 1750 basePos: null, 1751 spans: null, 1752 decorations: null 1753 }; 1754 applyDecorator(job); 1755 return container.innerHTML; 1756 } 1757 1758 /** 1759 * Find all the {@code <pre>} and {@code <code>} tags in the DOM with 1760 * {@code class=prettyprint} and prettify them. 1761 * 1762 * @param {Function} opt_whenDone called when prettifying is done. 1763 * @param {HTMLElement|HTMLDocument} opt_root an element or document 1764 * containing all the elements to pretty print. 1765 * Defaults to {@code document.body}. 1766 */ 1767 function $prettyPrint(opt_whenDone, opt_root) { 1768 var root = opt_root || document.body; 1769 var doc = root.ownerDocument || document; 1770 function byTagName(tn) { return root.getElementsByTagName(tn); } 1771 // fetch a list of nodes to rewrite 1772 var codeSegments = [byTagName('pre'), byTagName('code'), byTagName('xmp')]; 1773 var elements = []; 1774 for (var i = 0; i < codeSegments.length; ++i) { 1775 for (var j = 0, n = codeSegments[i].length; j < n; ++j) { 1776 elements.push(codeSegments[i][j]); 1777 } 1778 } 1779 codeSegments = null; 1780 1781 var clock = Date; 1782 if (!clock['now']) { 1783 clock = { 'now': function () { return +(new Date); } }; 1784 } 1785 1786 // The loop is broken into a series of continuations to make sure that we 1787 // don't make the browser unresponsive when rewriting a large page. 1788 var k = 0; 1789 1790 var langExtensionRe = /\blang(?:uage)?-([\w.]+)(?!\S)/; 1791 var prettyPrintRe = /\bprettyprint\b/; 1792 var prettyPrintedRe = /\bprettyprinted\b/; 1793 var preformattedTagNameRe = /pre|xmp/i; 1794 var codeRe = /^code$/i; 1795 var preCodeXmpRe = /^(?:pre|code|xmp)$/i; 1796 var EMPTY = {}; 1797 1798 function doWork() { 1799 var endTime = (win['PR_SHOULD_USE_CONTINUATION'] ? 1800 clock['now']() + 250 /* ms */ : 1801 Infinity); 1802 for (; k < elements.length && clock['now']() < endTime; k++) { 1803 var cs = elements[k]; 1804 1805 // Look for a preceding comment like 1806 // <?prettify lang="..." linenums="..."?> 1807 var attrs = EMPTY; 1808 { 1809 for (var preceder = cs; (preceder = preceder.previousSibling);) { 1810 var nt = preceder.nodeType; 1811 // <?foo?> is parsed by HTML 5 to a comment node (8) 1812 // like <!--?foo?-->, but in XML is a processing instruction 1813 var value = (nt === 7 || nt === 8) && preceder.nodeValue; 1814 if (value 1815 ? !/^\??prettify\b/.test(value) 1816 : (nt !== 3 || /\S/.test(preceder.nodeValue))) { 1817 // Skip over white-space text nodes but not others. 1818 break; 1819 } 1820 if (value) { 1821 attrs = {}; 1822 value.replace( 1823 /\b(\w+)=([\w:.%+-]+)/g, 1824 function (_, name, value) { attrs[name] = value; }); 1825 break; 1826 } 1827 } 1828 } 1829 1830 var className = cs.className; 1831 if ((attrs !== EMPTY || prettyPrintRe.test(className)) 1832 // Don't redo this if we've already done it. 1833 // This allows recalling pretty print to just prettyprint elements 1834 // that have been added to the page since last call. 1835 && !prettyPrintedRe.test(className)) { 1836 1837 // make sure this is not nested in an already prettified element 1838 var nested = false; 1839 for (var p = cs.parentNode; p; p = p.parentNode) { 1840 var tn = p.tagName; 1841 if (preCodeXmpRe.test(tn) 1842 && p.className && prettyPrintRe.test(p.className)) { 1843 nested = true; 1844 break; 1845 } 1846 } 1847 if (!nested) { 1848 // Mark done. If we fail to prettyprint for whatever reason, 1849 // we shouldn't try again. 1850 cs.className += ' prettyprinted'; 1851 1852 // If the classes includes a language extensions, use it. 1853 // Language extensions can be specified like 1854 // <pre class="prettyprint lang-cpp"> 1855 // the language extension "cpp" is used to find a language handler 1856 // as passed to PR.registerLangHandler. 1857 // HTML5 recommends that a language be specified using "language-" 1858 // as the prefix instead. Google Code Prettify supports both. 1859 // http://dev.w3.org/html5/spec-author-view/the-code-element.html 1860 var langExtension = attrs['lang']; 1861 if (!langExtension) { 1862 langExtension = className.match(langExtensionRe); 1863 // Support <pre class="prettyprint"><code class="language-c"> 1864 var wrapper; 1865 if (!langExtension && (wrapper = childContentWrapper(cs)) 1866 && codeRe.test(wrapper.tagName)) { 1867 langExtension = wrapper.className.match(langExtensionRe); 1868 } 1869 1870 if (langExtension) { langExtension = langExtension[1]; } 1871 } 1872 1873 var preformatted; 1874 if (preformattedTagNameRe.test(cs.tagName)) { 1875 preformatted = 1; 1876 } else { 1877 var currentStyle = cs['currentStyle']; 1878 var defaultView = doc.defaultView; 1879 var whitespace = ( 1880 currentStyle 1881 ? currentStyle['whiteSpace'] 1882 : (defaultView 1883 && defaultView.getComputedStyle) 1884 ? defaultView.getComputedStyle(cs, null) 1885 .getPropertyValue('white-space') 1886 : 0); 1887 preformatted = whitespace 1888 && 'pre' === whitespace.substring(0, 3); 1889 } 1890 1891 // Look for a class like linenums or linenums:<n> where <n> is the 1892 // 1-indexed number of the first line. 1893 var lineNums = attrs['linenums']; 1894 if (!(lineNums = lineNums === 'true' || +lineNums)) { 1895 lineNums = className.match(/\blinenums\b(?::(\d+))?/); 1896 lineNums = 1897 lineNums 1898 ? lineNums[1] && lineNums[1].length 1899 ? +lineNums[1] : true 1900 : false; 1901 } 1902 if (lineNums) { numberLines(cs, lineNums, preformatted); } 1903 1904 // do the pretty printing 1905 var prettyPrintingJob = { 1906 langExtension: langExtension, 1907 sourceNode: cs, 1908 numberLines: lineNums, 1909 pre: preformatted, 1910 sourceCode: null, 1911 basePos: null, 1912 spans: null, 1913 decorations: null 1914 }; 1915 applyDecorator(prettyPrintingJob); 1916 } 1917 } 1918 } 1919 if (k < elements.length) { 1920 // finish up in a continuation 1921 win.setTimeout(doWork, 250); 1922 } else if ('function' === typeof opt_whenDone) { 1923 opt_whenDone(); 1924 } 1925 } 1926 1927 doWork(); 1928 } 1929 1930 /** 1931 * Contains functions for creating and registering new language handlers. 1932 * @type {Object} 1933 */ 1934 var PR = win['PR'] = { 1935 'createSimpleLexer': createSimpleLexer, 1936 'registerLangHandler': registerLangHandler, 1937 'sourceDecorator': sourceDecorator, 1938 'PR_ATTRIB_NAME': PR_ATTRIB_NAME, 1939 'PR_ATTRIB_VALUE': PR_ATTRIB_VALUE, 1940 'PR_COMMENT': PR_COMMENT, 1941 'PR_DECLARATION': PR_DECLARATION, 1942 'PR_KEYWORD': PR_KEYWORD, 1943 'PR_LITERAL': PR_LITERAL, 1944 'PR_NOCODE': PR_NOCODE, 1945 'PR_PLAIN': PR_PLAIN, 1946 'PR_PUNCTUATION': PR_PUNCTUATION, 1947 'PR_SOURCE': PR_SOURCE, 1948 'PR_STRING': PR_STRING, 1949 'PR_TAG': PR_TAG, 1950 'PR_TYPE': PR_TYPE, 1951 'prettyPrintOne': 1952 IN_GLOBAL_SCOPE 1953 ? (win['prettyPrintOne'] = $prettyPrintOne) 1954 : (prettyPrintOne = $prettyPrintOne), 1955 'prettyPrint': 1956 IN_GLOBAL_SCOPE 1957 ? (win['prettyPrint'] = $prettyPrint) 1958 : (prettyPrint = $prettyPrint) 1959 }; 1960 1961 // Make PR available via the Asynchronous Module Definition (AMD) API. 1962 // Per https://github.com/amdjs/amdjs-api/wiki/AMD: 1963 // The Asynchronous Module Definition (AMD) API specifies a 1964 // mechanism for defining modules such that the module and its 1965 // dependencies can be asynchronously loaded. 1966 // ... 1967 // To allow a clear indicator that a global define function (as 1968 // needed for script src browser loading) conforms to the AMD API, 1969 // any global define function SHOULD have a property called "amd" 1970 // whose value is an object. This helps avoid conflict with any 1971 // other existing JavaScript code that could have defined a define() 1972 // function that does not conform to the AMD API. 1973 var define = win['define']; 1974 if (typeof define === "function" && define['amd']) { 1975 define("google-code-prettify", [], function () { 1976 return PR; 1977 }); 1978 } 1979 })(); 1980 1981 return prettyPrint; 1982 })(); 1983 1984 // If this script is deferred or async and the document is already 1985 // loaded we need to wait for language handlers to load before performing 1986 // any autorun. 1987 function onLangsLoaded() { 1988 if (autorun) { 1989 contentLoaded( 1990 function () { 1991 var n = callbacks.length; 1992 var callback = n ? function () { 1993 for (var i = 0; i < n; ++i) { 1994 (function (i) { 1995 win.setTimeout( 1996 function () { 1997 win['exports'][callbacks[i]].apply(win, arguments); 1998 }, 0); 1999 })(i); 2000 } 2001 } : void 0; 2002 prettyPrint(callback); 2003 }); 2004 } 2005 } 2006 checkPendingLanguages(); 2007 2008}()); 2009