Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(915)

Side by Side Diff: lib/src/prism/prism.js

Issue 1418513006: update elements and fix some bugs (Closed) Base URL: git@github.com:dart-lang/polymer_elements.git@master
Patch Set: code review updates Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/src/prism/plugins/wpd/prism-wpd.min.js ('k') | lib/src/prism/tests/helper/components.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* ********************************************** 2 /* **********************************************
3 Begin prism-core.js 3 Begin prism-core.js
4 ********************************************** */ 4 ********************************************** */
5 5
6 var _self = (typeof window !== 'undefined') 6 var _self = (typeof window !== 'undefined')
7 ? window // if in browser 7 ? window // if in browser
8 : ( 8 : (
9 (typeof WorkerGlobalScope !== 'undefined' && self instanceof Wor kerGlobalScope) 9 (typeof WorkerGlobalScope !== 'undefined' && self instanceof Wor kerGlobalScope)
10 ? self // if in worker 10 ? self // if in worker
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 if (_.util.type(o[i]) === 'Object') { 138 if (_.util.type(o[i]) === 'Object') {
139 _.languages.DFS(o[i], callback); 139 _.languages.DFS(o[i], callback);
140 } 140 }
141 else if (_.util.type(o[i]) === 'Array') { 141 else if (_.util.type(o[i]) === 'Array') {
142 _.languages.DFS(o[i], callback, i); 142 _.languages.DFS(o[i], callback, i);
143 } 143 }
144 } 144 }
145 } 145 }
146 } 146 }
147 }, 147 },
148 148 » plugins: {},
149 »
149 highlightAll: function(async, callback) { 150 highlightAll: function(async, callback) {
150 var elements = document.querySelectorAll('code[class*="language- "], [class*="language-"] code, code[class*="lang-"], [class*="lang-"] code'); 151 var elements = document.querySelectorAll('code[class*="language- "], [class*="language-"] code, code[class*="lang-"], [class*="lang-"] code');
151 152
152 for (var i=0, element; element = elements[i++];) { 153 for (var i=0, element; element = elements[i++];) {
153 _.highlightElement(element, async === true, callback); 154 _.highlightElement(element, async === true, callback);
154 } 155 }
155 }, 156 },
156 157
157 highlightElement: function(element, async, callback) { 158 highlightElement: function(element, async, callback) {
158 // Find language 159 // Find language
(...skipping 11 matching lines...) Expand all
170 // Set language on the element, if not present 171 // Set language on the element, if not present
171 element.className = element.className.replace(lang, '').replace( /\s+/g, ' ') + ' language-' + language; 172 element.className = element.className.replace(lang, '').replace( /\s+/g, ' ') + ' language-' + language;
172 173
173 // Set language on the parent, for styling 174 // Set language on the parent, for styling
174 parent = element.parentNode; 175 parent = element.parentNode;
175 176
176 if (/pre/i.test(parent.nodeName)) { 177 if (/pre/i.test(parent.nodeName)) {
177 parent.className = parent.className.replace(lang, '').re place(/\s+/g, ' ') + ' language-' + language; 178 parent.className = parent.className.replace(lang, '').re place(/\s+/g, ' ') + ' language-' + language;
178 } 179 }
179 180
180 if (!grammar) {
181 return;
182 }
183
184 var code = element.textContent; 181 var code = element.textContent;
185 182
186 if(!code) {
187 return;
188 }
189
190 code = code.replace(/^(?:\r?\n|\r)/,'');
191
192 var env = { 183 var env = {
193 element: element, 184 element: element,
194 language: language, 185 language: language,
195 grammar: grammar, 186 grammar: grammar,
196 code: code 187 code: code
197 }; 188 };
198 189
190 if (!code || !grammar) {
191 _.hooks.run('complete', env);
192 return;
193 }
194
199 _.hooks.run('before-highlight', env); 195 _.hooks.run('before-highlight', env);
200 196
201 if (async && _self.Worker) { 197 if (async && _self.Worker) {
202 var worker = new Worker(_.filename); 198 var worker = new Worker(_.filename);
203 199
204 worker.onmessage = function(evt) { 200 worker.onmessage = function(evt) {
205 » » » » env.highlightedCode = Token.stringify(JSON.parse (evt.data), language); 201 » » » » env.highlightedCode = evt.data;
206 202
207 _.hooks.run('before-insert', env); 203 _.hooks.run('before-insert', env);
208 204
209 env.element.innerHTML = env.highlightedCode; 205 env.element.innerHTML = env.highlightedCode;
210 206
211 callback && callback.call(env.element); 207 callback && callback.call(env.element);
212 _.hooks.run('after-highlight', env); 208 _.hooks.run('after-highlight', env);
209 _.hooks.run('complete', env);
213 }; 210 };
214 211
215 worker.postMessage(JSON.stringify({ 212 worker.postMessage(JSON.stringify({
216 language: env.language, 213 language: env.language,
217 » » » » code: env.code 214 » » » » code: env.code,
215 » » » » immediateClose: true
218 })); 216 }));
219 } 217 }
220 else { 218 else {
221 env.highlightedCode = _.highlight(env.code, env.grammar, env.language); 219 env.highlightedCode = _.highlight(env.code, env.grammar, env.language);
222 220
223 _.hooks.run('before-insert', env); 221 _.hooks.run('before-insert', env);
224 222
225 env.element.innerHTML = env.highlightedCode; 223 env.element.innerHTML = env.highlightedCode;
226 224
227 callback && callback.call(element); 225 callback && callback.call(element);
228 226
229 _.hooks.run('after-highlight', env); 227 _.hooks.run('after-highlight', env);
228 _.hooks.run('complete', env);
230 } 229 }
231 }, 230 },
232 231
233 highlight: function (text, grammar, language) { 232 highlight: function (text, grammar, language) {
234 var tokens = _.tokenize(text, grammar); 233 var tokens = _.tokenize(text, grammar);
235 return Token.stringify(_.util.encode(tokens), language); 234 return Token.stringify(_.util.encode(tokens), language);
236 }, 235 },
237 236
238 tokenize: function(text, grammar, language) { 237 tokenize: function(text, grammar, language) {
239 var Token = _.Token; 238 var Token = _.Token;
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 if (o.alias) { 377 if (o.alias) {
379 var aliases = _.util.type(o.alias) === 'Array' ? o.alias : [o.al ias]; 378 var aliases = _.util.type(o.alias) === 'Array' ? o.alias : [o.al ias];
380 Array.prototype.push.apply(env.classes, aliases); 379 Array.prototype.push.apply(env.classes, aliases);
381 } 380 }
382 381
383 _.hooks.run('wrap', env); 382 _.hooks.run('wrap', env);
384 383
385 var attributes = ''; 384 var attributes = '';
386 385
387 for (var name in env.attributes) { 386 for (var name in env.attributes) {
388 » » attributes += name + '="' + (env.attributes[name] || '') + '"'; 387 » » attributes += (attributes ? ' ' : '') + name + '="' + (env.attri butes[name] || '') + '"';
389 } 388 }
390 389
391 return '<' + env.tag + ' class="' + env.classes.join(' ') + '" ' + attri butes + '>' + env.content + '</' + env.tag + '>'; 390 return '<' + env.tag + ' class="' + env.classes.join(' ') + '" ' + attri butes + '>' + env.content + '</' + env.tag + '>';
392 391
393 }; 392 };
394 393
395 if (!_self.document) { 394 if (!_self.document) {
396 if (!_self.addEventListener) { 395 if (!_self.addEventListener) {
397 // in Node.js 396 // in Node.js
398 return _self.Prism; 397 return _self.Prism;
399 } 398 }
400 // In worker 399 // In worker
401 _self.addEventListener('message', function(evt) { 400 _self.addEventListener('message', function(evt) {
402 var message = JSON.parse(evt.data), 401 var message = JSON.parse(evt.data),
403 lang = message.language, 402 lang = message.language,
404 » » code = message.code; 403 » » code = message.code,
404 » » immediateClose = message.immediateClose;
405 405
406 » » _self.postMessage(JSON.stringify(_.util.encode(_.tokenize(code, _.languages[lang])))); 406 » » _self.postMessage(_.highlight(code, _.languages[lang], lang));
407 » » _self.close(); 407 » » if (immediateClose) {
408 » » » _self.close();
409 » » }
408 }, false); 410 }, false);
409 411
410 return _self.Prism; 412 return _self.Prism;
411 } 413 }
412 414
413 // Get current script and highlight 415 // Get current script and highlight
414 var script = document.getElementsByTagName('script'); 416 var script = document.getElementsByTagName('script');
415 417
416 script = script[script.length - 1]; 418 script = script[script.length - 1];
417 419
418 if (script) { 420 if (script) {
419 _.filename = script.src; 421 _.filename = script.src;
420 422
421 if (document.addEventListener && !script.hasAttribute('data-manual')) { 423 if (document.addEventListener && !script.hasAttribute('data-manual')) {
422 document.addEventListener('DOMContentLoaded', _.highlightAll); 424 document.addEventListener('DOMContentLoaded', _.highlightAll);
423 } 425 }
424 } 426 }
425 427
426 return _self.Prism; 428 return _self.Prism;
427 429
428 })(); 430 })();
429 431
430 if (typeof module !== 'undefined' && module.exports) { 432 if (typeof module !== 'undefined' && module.exports) {
431 module.exports = Prism; 433 module.exports = Prism;
432 } 434 }
433 435
436 // hack for components to work correctly in node.js
437 if (typeof global !== 'undefined') {
438 global.Prism = Prism;
439 }
440
434 441
435 /* ********************************************** 442 /* **********************************************
436 Begin prism-markup.js 443 Begin prism-markup.js
437 ********************************************** */ 444 ********************************************** */
438 445
439 Prism.languages.markup = { 446 Prism.languages.markup = {
440 'comment': /<!--[\w\W]*?-->/, 447 'comment': /<!--[\w\W]*?-->/,
441 'prolog': /<\?[\w\W]+?\?>/, 448 'prolog': /<\?[\w\W]+?\?>/,
442 'doctype': /<!DOCTYPE[\w\W]+?>/, 449 'doctype': /<!DOCTYPE[\w\W]+?>/,
443 'cdata': /<!\[CDATA\[[\w\W]*?]]>/i, 450 'cdata': /<!\[CDATA\[[\w\W]*?]]>/i,
444 'tag': { 451 'tag': {
445 » » pattern: /<\/?[^\s>\/]+(?:\s+[^\s>\/=]+(?:=(?:("|')(?:\\\1|\\?(? !\1)[\w\W])*\1|[^\s'">=]+))?)*\s*\/?>/i, 452 » » pattern: /<\/?[^\s>\/=.]+(?:\s+[^\s>\/=]+(?:=(?:("|')(?:\\\1|\\? (?!\1)[\w\W])*\1|[^\s'">=]+))?)*\s*\/?>/i,
446 inside: { 453 inside: {
447 'tag': { 454 'tag': {
448 pattern: /^<\/?[^\s>\/]+/i, 455 pattern: /^<\/?[^\s>\/]+/i,
449 inside: { 456 inside: {
450 'punctuation': /^<\/?/, 457 'punctuation': /^<\/?/,
451 'namespace': /^[^\s>\/:]+:/ 458 'namespace': /^[^\s>\/:]+:/
452 } 459 }
453 }, 460 },
454 'attr-value': { 461 'attr-value': {
455 pattern: /=(?:('|")[\w\W]*?(\1)|[^\s>]+)/i, 462 pattern: /=(?:('|")[\w\W]*?(\1)|[^\s>]+)/i,
(...skipping 15 matching lines...) Expand all
471 }; 478 };
472 479
473 // Plugin to make entity title show the real entity, idea by Roman Komarov 480 // Plugin to make entity title show the real entity, idea by Roman Komarov
474 Prism.hooks.add('wrap', function(env) { 481 Prism.hooks.add('wrap', function(env) {
475 482
476 if (env.type === 'entity') { 483 if (env.type === 'entity') {
477 env.attributes['title'] = env.content.replace(/&amp;/, '&'); 484 env.attributes['title'] = env.content.replace(/&amp;/, '&');
478 } 485 }
479 }); 486 });
480 487
488 Prism.languages.xml = Prism.languages.markup;
489 Prism.languages.html = Prism.languages.markup;
490 Prism.languages.mathml = Prism.languages.markup;
491 Prism.languages.svg = Prism.languages.markup;
492
481 493
482 /* ********************************************** 494 /* **********************************************
483 Begin prism-css.js 495 Begin prism-css.js
484 ********************************************** */ 496 ********************************************** */
485 497
486 Prism.languages.css = { 498 Prism.languages.css = {
487 'comment': /\/\*[\w\W]*?\*\//, 499 'comment': /\/\*[\w\W]*?\*\//,
488 'atrule': { 500 'atrule': {
489 pattern: /@[\w-]+?.*?(;|(?=\s*\{))/i, 501 pattern: /@[\w-]+?.*?(;|(?=\s*\{))/i,
490 inside: { 502 inside: {
491 'rule': /@[\w-]+/ 503 'rule': /@[\w-]+/
492 // See rest below 504 // See rest below
493 } 505 }
494 }, 506 },
495 'url': /url\((?:(["'])(\\(?:\r\n|[\w\W])|(?!\1)[^\\\r\n])*\1|.*?)\)/i, 507 'url': /url\((?:(["'])(\\(?:\r\n|[\w\W])|(?!\1)[^\\\r\n])*\1|.*?)\)/i,
496 'selector': /[^\{\}\s][^\{\};]*?(?=\s*\{)/, 508 'selector': /[^\{\}\s][^\{\};]*?(?=\s*\{)/,
497 'string': /("|')(\\(?:\r\n|[\w\W])|(?!\1)[^\\\r\n])*\1/, 509 'string': /("|')(\\(?:\r\n|[\w\W])|(?!\1)[^\\\r\n])*\1/,
498 'property': /(\b|\B)[\w-]+(?=\s*:)/i, 510 'property': /(\b|\B)[\w-]+(?=\s*:)/i,
499 'important': /\B!important\b/i, 511 'important': /\B!important\b/i,
500 'function': /[-a-z0-9]+(?=\()/i, 512 'function': /[-a-z0-9]+(?=\()/i,
501 'punctuation': /[(){};:]/ 513 'punctuation': /[(){};:]/
502 }; 514 };
503 515
504 Prism.languages.css['atrule'].inside.rest = Prism.util.clone(Prism.languages.css ); 516 Prism.languages.css['atrule'].inside.rest = Prism.util.clone(Prism.languages.css );
505 517
506 if (Prism.languages.markup) { 518 if (Prism.languages.markup) {
507 Prism.languages.insertBefore('markup', 'tag', { 519 Prism.languages.insertBefore('markup', 'tag', {
508 'style': { 520 'style': {
509 » » » pattern: /<style[\w\W]*?>[\w\W]*?<\/style>/i, 521 » » » pattern: /(<style[\w\W]*?>)[\w\W]*?(?=<\/style>)/i,
510 » » » inside: { 522 » » » lookbehind: true,
511 » » » » 'tag': { 523 » » » inside: Prism.languages.css,
512 » » » » » pattern: /<style[\w\W]*?>|<\/style>/i,
513 » » » » » inside: Prism.languages.markup.tag.insid e
514 » » » » },
515 » » » » rest: Prism.languages.css
516 » » » },
517 alias: 'language-css' 524 alias: 'language-css'
518 } 525 }
519 }); 526 });
520 527
521 Prism.languages.insertBefore('inside', 'attr-value', { 528 Prism.languages.insertBefore('inside', 'attr-value', {
522 'style-attr': { 529 'style-attr': {
523 pattern: /\s*style=("|').*?\1/i, 530 pattern: /\s*style=("|').*?\1/i,
524 inside: { 531 inside: {
525 'attr-name': { 532 'attr-name': {
526 pattern: /^\s*style/i, 533 pattern: /^\s*style/i,
(...skipping 18 matching lines...) Expand all
545 'comment': [ 552 'comment': [
546 { 553 {
547 pattern: /(^|[^\\])\/\*[\w\W]*?\*\//, 554 pattern: /(^|[^\\])\/\*[\w\W]*?\*\//,
548 lookbehind: true 555 lookbehind: true
549 }, 556 },
550 { 557 {
551 pattern: /(^|[^\\:])\/\/.*/, 558 pattern: /(^|[^\\:])\/\/.*/,
552 lookbehind: true 559 lookbehind: true
553 } 560 }
554 ], 561 ],
555 » 'string': /("|')(\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/, 562 » 'string': /(["'])(\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
556 'class-name': { 563 'class-name': {
557 » » pattern: /((?:(?:class|interface|extends|implements|trait|instan ceof|new)\s+)|(?:catch\s+\())[a-z0-9_\.\\]+/i, 564 » » pattern: /((?:\b(?:class|interface|extends|implements|trait|inst anceof|new)\s+)|(?:catch\s+\())[a-z0-9_\.\\]+/i,
558 lookbehind: true, 565 lookbehind: true,
559 inside: { 566 inside: {
560 punctuation: /(\.|\\)/ 567 punctuation: /(\.|\\)/
561 } 568 }
562 }, 569 },
563 'keyword': /\b(if|else|while|do|for|return|in|instanceof|function|new|tr y|throw|catch|finally|null|break|continue)\b/, 570 'keyword': /\b(if|else|while|do|for|return|in|instanceof|function|new|tr y|throw|catch|finally|null|break|continue)\b/,
564 'boolean': /\b(true|false)\b/, 571 'boolean': /\b(true|false)\b/,
565 'function': /[a-z0-9_]+(?=\()/i, 572 'function': /[a-z0-9_]+(?=\()/i,
566 » 'number': /\b-?(0x[\dA-Fa-f]+|\d*\.?\d+([Ee]-?\d+)?)\b/, 573 » 'number': /\b-?(?:0x[\da-f]+|\d*\.?\d+(?:e[+-]?\d+)?)\b/i,
567 » 'operator': /[-+]{1,2}|!|<=?|>=?|={1,3}|&{1,2}|\|?\||\?|\*|\/|~|\^|%/, 574 » 'operator': /--?|\+\+?|!=?=?|<=?|>=?|==?=?|&&?|\|\|?|\?|\*|\/|~|\^|%/,
568 'punctuation': /[{}[\];(),.:]/ 575 'punctuation': /[{}[\];(),.:]/
569 }; 576 };
570 577
571 578
572 /* ********************************************** 579 /* **********************************************
573 Begin prism-javascript.js 580 Begin prism-javascript.js
574 ********************************************** */ 581 ********************************************** */
575 582
576 Prism.languages.javascript = Prism.languages.extend('clike', { 583 Prism.languages.javascript = Prism.languages.extend('clike', {
577 'keyword': /\b(as|async|await|break|case|catch|class|const|continue|debu gger|default|delete|do|else|enum|export|extends|false|finally|for|from|function| get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private |protected|public|return|set|static|super|switch|this|throw|true|try|typeof|var| void|while|with|yield)\b/, 584 'keyword': /\b(as|async|await|break|case|catch|class|const|continue|debu gger|default|delete|do|else|enum|export|extends|false|finally|for|from|function| get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private |protected|public|return|set|static|super|switch|this|throw|true|try|typeof|var| void|while|with|yield)\b/,
578 'number': /\b-?(0x[\dA-Fa-f]+|0b[01]+|0o[0-7]+|\d*\.?\d+([Ee][+-]?\d+)?| NaN|Infinity)\b/, 585 'number': /\b-?(0x[\dA-Fa-f]+|0b[01]+|0o[0-7]+|\d*\.?\d+([Ee][+-]?\d+)?| NaN|Infinity)\b/,
579 » 'function': /(?!\d)[a-z0-9_$]+(?=\()/i 586 » // Allow for all non-ASCII characters (See http://stackoverflow.com/a/20 08444)
587 » 'function': /[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*(?=\()/i
580 }); 588 });
581 589
582 Prism.languages.insertBefore('javascript', 'keyword', { 590 Prism.languages.insertBefore('javascript', 'keyword', {
583 'regex': { 591 'regex': {
584 pattern: /(^|[^/])\/(?!\/)(\[.+?]|\\.|[^/\\\r\n])+\/[gimyu]{0,5} (?=\s*($|[\r\n,.;})]))/, 592 pattern: /(^|[^/])\/(?!\/)(\[.+?]|\\.|[^/\\\r\n])+\/[gimyu]{0,5} (?=\s*($|[\r\n,.;})]))/,
585 lookbehind: true 593 lookbehind: true
586 } 594 }
587 }); 595 });
588 596
589 Prism.languages.insertBefore('javascript', 'class-name', { 597 Prism.languages.insertBefore('javascript', 'class-name', {
(...skipping 11 matching lines...) Expand all
601 } 609 }
602 }, 610 },
603 'string': /[\s\S]+/ 611 'string': /[\s\S]+/
604 } 612 }
605 } 613 }
606 }); 614 });
607 615
608 if (Prism.languages.markup) { 616 if (Prism.languages.markup) {
609 Prism.languages.insertBefore('markup', 'tag', { 617 Prism.languages.insertBefore('markup', 'tag', {
610 'script': { 618 'script': {
611 » » » pattern: /<script[\w\W]*?>[\w\W]*?<\/script>/i, 619 » » » pattern: /(<script[\w\W]*?>)[\w\W]*?(?=<\/script>)/i,
612 » » » inside: { 620 » » » lookbehind: true,
613 » » » » 'tag': { 621 » » » inside: Prism.languages.javascript,
614 » » » » » pattern: /<script[\w\W]*?>|<\/script>/i,
615 » » » » » inside: Prism.languages.markup.tag.insid e
616 » » » » },
617 » » » » rest: Prism.languages.javascript
618 » » » },
619 alias: 'language-javascript' 622 alias: 'language-javascript'
620 } 623 }
621 }); 624 });
622 } 625 }
623 626
627 Prism.languages.js = Prism.languages.javascript;
624 628
625 /* ********************************************** 629 /* **********************************************
626 Begin prism-file-highlight.js 630 Begin prism-file-highlight.js
627 ********************************************** */ 631 ********************************************** */
628 632
629 (function () { 633 (function () {
630 » if (!self.Prism || !self.document || !document.querySelector) { 634 » if (typeof self === 'undefined' || !self.Prism || !self.document || !doc ument.querySelector) {
631 return; 635 return;
632 } 636 }
633 637
634 self.Prism.fileHighlight = function() { 638 self.Prism.fileHighlight = function() {
635 639
636 var Extensions = { 640 var Extensions = {
637 'js': 'javascript', 641 'js': 'javascript',
638 'html': 'markup', 642 'html': 'markup',
639 'svg': 'markup', 643 'svg': 'markup',
640 'xml': 'markup', 644 'xml': 'markup',
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
695 699
696 xhr.send(null); 700 xhr.send(null);
697 }); 701 });
698 } 702 }
699 703
700 }; 704 };
701 705
702 self.Prism.fileHighlight(); 706 self.Prism.fileHighlight();
703 707
704 })(); 708 })();
OLDNEW
« no previous file with comments | « lib/src/prism/plugins/wpd/prism-wpd.min.js ('k') | lib/src/prism/tests/helper/components.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698