angularjs-rails 1.0.6 → 1.0.6.1

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