UNPKG

35 kBJavaScriptView Raw
1/// <reference lib="WebWorker"/>
2
3var _self = (typeof window !== 'undefined')
4 ? window // if in browser
5 : (
6 (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope)
7 ? self // if in worker
8 : {} // if in node js
9 );
10
11/**
12 * Prism: Lightweight, robust, elegant syntax highlighting
13 *
14 * @license MIT <https://opensource.org/licenses/MIT>
15 * @author Lea Verou <https://lea.verou.me>
16 * @namespace
17 * @public
18 */
19var Prism = (function (_self){
20
21// Private helper vars
22var lang = /\blang(?:uage)?-([\w-]+)\b/i;
23var uniqueId = 0;
24
25
26var _ = {
27 /**
28 * By default, Prism will attempt to highlight all code elements (by calling {@link Prism.highlightAll}) on the
29 * current page after the page finished loading. This might be a problem if e.g. you wanted to asynchronously load
30 * additional languages or plugins yourself.
31 *
32 * By setting this value to `true`, Prism will not automatically highlight all code elements on the page.
33 *
34 * You obviously have to change this value before the automatic highlighting started. To do this, you can add an
35 * empty Prism object into the global scope before loading the Prism script like this:
36 *
37 * ```js
38 * window.Prism = window.Prism || {};
39 * Prism.manual = true;
40 * // add a new <script> to load Prism's script
41 * ```
42 *
43 * @default false
44 * @type {boolean}
45 * @memberof Prism
46 * @public
47 */
48 manual: _self.Prism && _self.Prism.manual,
49 disableWorkerMessageHandler: _self.Prism && _self.Prism.disableWorkerMessageHandler,
50
51 /**
52 * A namespace for utility methods.
53 *
54 * All function in this namespace that are not explicitly marked as _public_ are for __internal use only__ and may
55 * change or disappear at any time.
56 *
57 * @namespace
58 * @memberof Prism
59 */
60 util: {
61 encode: function encode(tokens) {
62 if (tokens instanceof Token) {
63 return new Token(tokens.type, encode(tokens.content), tokens.alias);
64 } else if (Array.isArray(tokens)) {
65 return tokens.map(encode);
66 } else {
67 return tokens.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/\u00a0/g, ' ');
68 }
69 },
70
71 /**
72 * Returns the name of the type of the given value.
73 *
74 * @param {any} o
75 * @returns {string}
76 * @example
77 * type(null) === 'Null'
78 * type(undefined) === 'Undefined'
79 * type(123) === 'Number'
80 * type('foo') === 'String'
81 * type(true) === 'Boolean'
82 * type([1, 2]) === 'Array'
83 * type({}) === 'Object'
84 * type(String) === 'Function'
85 * type(/abc+/) === 'RegExp'
86 */
87 type: function (o) {
88 return Object.prototype.toString.call(o).slice(8, -1);
89 },
90
91 /**
92 * Returns a unique number for the given object. Later calls will still return the same number.
93 *
94 * @param {Object} obj
95 * @returns {number}
96 */
97 objId: function (obj) {
98 if (!obj['__id']) {
99 Object.defineProperty(obj, '__id', { value: ++uniqueId });
100 }
101 return obj['__id'];
102 },
103
104 /**
105 * Creates a deep clone of the given object.
106 *
107 * The main intended use of this function is to clone language definitions.
108 *
109 * @param {T} o
110 * @param {Record<number, any>} [visited]
111 * @returns {T}
112 * @template T
113 */
114 clone: function deepClone(o, visited) {
115 visited = visited || {};
116
117 var clone, id;
118 switch (_.util.type(o)) {
119 case 'Object':
120 id = _.util.objId(o);
121 if (visited[id]) {
122 return visited[id];
123 }
124 clone = /** @type {Record<string, any>} */ ({});
125 visited[id] = clone;
126
127 for (var key in o) {
128 if (o.hasOwnProperty(key)) {
129 clone[key] = deepClone(o[key], visited);
130 }
131 }
132
133 return /** @type {any} */ (clone);
134
135 case 'Array':
136 id = _.util.objId(o);
137 if (visited[id]) {
138 return visited[id];
139 }
140 clone = [];
141 visited[id] = clone;
142
143 (/** @type {Array} */(/** @type {any} */(o))).forEach(function (v, i) {
144 clone[i] = deepClone(v, visited);
145 });
146
147 return /** @type {any} */ (clone);
148
149 default:
150 return o;
151 }
152 },
153
154 /**
155 * Returns the Prism language of the given element set by a `language-xxxx` or `lang-xxxx` class.
156 *
157 * If no language is set for the element or the element is `null` or `undefined`, `none` will be returned.
158 *
159 * @param {Element} element
160 * @returns {string}
161 */
162 getLanguage: function (element) {
163 while (element && !lang.test(element.className)) {
164 element = element.parentElement;
165 }
166 if (element) {
167 return (element.className.match(lang) || [, 'none'])[1].toLowerCase();
168 }
169 return 'none';
170 },
171
172 /**
173 * Returns the script element that is currently executing.
174 *
175 * This does __not__ work for line script element.
176 *
177 * @returns {HTMLScriptElement | null}
178 */
179 currentScript: function () {
180 if (typeof document === 'undefined') {
181 return null;
182 }
183 if ('currentScript' in document && 1 < 2 /* hack to trip TS' flow analysis */) {
184 return /** @type {any} */ (document.currentScript);
185 }
186
187 // IE11 workaround
188 // we'll get the src of the current script by parsing IE11's error stack trace
189 // this will not work for inline scripts
190
191 try {
192 throw new Error();
193 } catch (err) {
194 // Get file src url from stack. Specifically works with the format of stack traces in IE.
195 // A stack will look like this:
196 //
197 // Error
198 // at _.util.currentScript (http://localhost/components/prism-core.js:119:5)
199 // at Global code (http://localhost/components/prism-core.js:606:1)
200
201 var src = (/at [^(\r\n]*\((.*):.+:.+\)$/i.exec(err.stack) || [])[1];
202 if (src) {
203 var scripts = document.getElementsByTagName('script');
204 for (var i in scripts) {
205 if (scripts[i].src == src) {
206 return scripts[i];
207 }
208 }
209 }
210 return null;
211 }
212 },
213
214 /**
215 * Returns whether a given class is active for `element`.
216 *
217 * The class can be activated if `element` or one of its ancestors has the given class and it can be deactivated
218 * if `element` or one of its ancestors has the negated version of the given class. The _negated version_ of the
219 * given class is just the given class with a `no-` prefix.
220 *
221 * Whether the class is active is determined by the closest ancestor of `element` (where `element` itself is
222 * closest ancestor) that has the given class or the negated version of it. If neither `element` nor any of its
223 * ancestors have the given class or the negated version of it, then the default activation will be returned.
224 *
225 * In the paradoxical situation where the closest ancestor contains __both__ the given class and the negated
226 * version of it, the class is considered active.
227 *
228 * @param {Element} element
229 * @param {string} className
230 * @param {boolean} [defaultActivation=false]
231 * @returns {boolean}
232 */
233 isActive: function (element, className, defaultActivation) {
234 var no = 'no-' + className;
235
236 while (element) {
237 var classList = element.classList;
238 if (classList.contains(className)) {
239 return true;
240 }
241 if (classList.contains(no)) {
242 return false;
243 }
244 element = element.parentElement;
245 }
246 return !!defaultActivation;
247 }
248 },
249
250 /**
251 * This namespace contains all currently loaded languages and the some helper functions to create and modify languages.
252 *
253 * @namespace
254 * @memberof Prism
255 * @public
256 */
257 languages: {
258 /**
259 * Creates a deep copy of the language with the given id and appends the given tokens.
260 *
261 * If a token in `redef` also appears in the copied language, then the existing token in the copied language
262 * will be overwritten at its original position.
263 *
264 * ## Best practices
265 *
266 * Since the position of overwriting tokens (token in `redef` that overwrite tokens in the copied language)
267 * doesn't matter, they can technically be in any order. However, this can be confusing to others that trying to
268 * understand the language definition because, normally, the order of tokens matters in Prism grammars.
269 *
270 * Therefore, it is encouraged to order overwriting tokens according to the positions of the overwritten tokens.
271 * Furthermore, all non-overwriting tokens should be placed after the overwriting ones.
272 *
273 * @param {string} id The id of the language to extend. This has to be a key in `Prism.languages`.
274 * @param {Grammar} redef The new tokens to append.
275 * @returns {Grammar} The new language created.
276 * @public
277 * @example
278 * Prism.languages['css-with-colors'] = Prism.languages.extend('css', {
279 * // Prism.languages.css already has a 'comment' token, so this token will overwrite CSS' 'comment' token
280 * // at its original position
281 * 'comment': { ... },
282 * // CSS doesn't have a 'color' token, so this token will be appended
283 * 'color': /\b(?:red|green|blue)\b/
284 * });
285 */
286 extend: function (id, redef) {
287 var lang = _.util.clone(_.languages[id]);
288
289 for (var key in redef) {
290 lang[key] = redef[key];
291 }
292
293 return lang;
294 },
295
296 /**
297 * Inserts tokens _before_ another token in a language definition or any other grammar.
298 *
299 * ## Usage
300 *
301 * This helper method makes it easy to modify existing languages. For example, the CSS language definition
302 * not only defines CSS highlighting for CSS documents, but also needs to define highlighting for CSS embedded
303 * in HTML through `<style>` elements. To do this, it needs to modify `Prism.languages.markup` and add the
304 * appropriate tokens. However, `Prism.languages.markup` is a regular JavaScript object literal, so if you do
305 * this:
306 *
307 * ```js
308 * Prism.languages.markup.style = {
309 * // token
310 * };
311 * ```
312 *
313 * then the `style` token will be added (and processed) at the end. `insertBefore` allows you to insert tokens
314 * before existing tokens. For the CSS example above, you would use it like this:
315 *
316 * ```js
317 * Prism.languages.insertBefore('markup', 'cdata', {
318 * 'style': {
319 * // token
320 * }
321 * });
322 * ```
323 *
324 * ## Special cases
325 *
326 * If the grammars of `inside` and `insert` have tokens with the same name, the tokens in `inside`'s grammar
327 * will be ignored.
328 *
329 * This behavior can be used to insert tokens after `before`:
330 *
331 * ```js
332 * Prism.languages.insertBefore('markup', 'comment', {
333 * 'comment': Prism.languages.markup.comment,
334 * // tokens after 'comment'
335 * });
336 * ```
337 *
338 * ## Limitations
339 *
340 * The main problem `insertBefore` has to solve is iteration order. Since ES2015, the iteration order for object
341 * properties is guaranteed to be the insertion order (except for integer keys) but some browsers behave
342 * differently when keys are deleted and re-inserted. So `insertBefore` can't be implemented by temporarily
343 * deleting properties which is necessary to insert at arbitrary positions.
344 *
345 * To solve this problem, `insertBefore` doesn't actually insert the given tokens into the target object.
346 * Instead, it will create a new object and replace all references to the target object with the new one. This
347 * can be done without temporarily deleting properties, so the iteration order is well-defined.
348 *
349 * However, only references that can be reached from `Prism.languages` or `insert` will be replaced. I.e. if
350 * you hold the target object in a variable, then the value of the variable will not change.
351 *
352 * ```js
353 * var oldMarkup = Prism.languages.markup;
354 * var newMarkup = Prism.languages.insertBefore('markup', 'comment', { ... });
355 *
356 * assert(oldMarkup !== Prism.languages.markup);
357 * assert(newMarkup === Prism.languages.markup);
358 * ```
359 *
360 * @param {string} inside The property of `root` (e.g. a language id in `Prism.languages`) that contains the
361 * object to be modified.
362 * @param {string} before The key to insert before.
363 * @param {Grammar} insert An object containing the key-value pairs to be inserted.
364 * @param {Object<string, any>} [root] The object containing `inside`, i.e. the object that contains the
365 * object to be modified.
366 *
367 * Defaults to `Prism.languages`.
368 * @returns {Grammar} The new grammar object.
369 * @public
370 */
371 insertBefore: function (inside, before, insert, root) {
372 root = root || /** @type {any} */ (_.languages);
373 var grammar = root[inside];
374 /** @type {Grammar} */
375 var ret = {};
376
377 for (var token in grammar) {
378 if (grammar.hasOwnProperty(token)) {
379
380 if (token == before) {
381 for (var newToken in insert) {
382 if (insert.hasOwnProperty(newToken)) {
383 ret[newToken] = insert[newToken];
384 }
385 }
386 }
387
388 // Do not insert token which also occur in insert. See #1525
389 if (!insert.hasOwnProperty(token)) {
390 ret[token] = grammar[token];
391 }
392 }
393 }
394
395 var old = root[inside];
396 root[inside] = ret;
397
398 // Update references in other language definitions
399 _.languages.DFS(_.languages, function(key, value) {
400 if (value === old && key != inside) {
401 this[key] = ret;
402 }
403 });
404
405 return ret;
406 },
407
408 // Traverse a language definition with Depth First Search
409 DFS: function DFS(o, callback, type, visited) {
410 visited = visited || {};
411
412 var objId = _.util.objId;
413
414 for (var i in o) {
415 if (o.hasOwnProperty(i)) {
416 callback.call(o, i, o[i], type || i);
417
418 var property = o[i],
419 propertyType = _.util.type(property);
420
421 if (propertyType === 'Object' && !visited[objId(property)]) {
422 visited[objId(property)] = true;
423 DFS(property, callback, null, visited);
424 }
425 else if (propertyType === 'Array' && !visited[objId(property)]) {
426 visited[objId(property)] = true;
427 DFS(property, callback, i, visited);
428 }
429 }
430 }
431 }
432 },
433
434 plugins: {},
435
436 /**
437 * This is the most high-level function in Prism’s API.
438 * It fetches all the elements that have a `.language-xxxx` class and then calls {@link Prism.highlightElement} on
439 * each one of them.
440 *
441 * This is equivalent to `Prism.highlightAllUnder(document, async, callback)`.
442 *
443 * @param {boolean} [async=false] Same as in {@link Prism.highlightAllUnder}.
444 * @param {HighlightCallback} [callback] Same as in {@link Prism.highlightAllUnder}.
445 * @memberof Prism
446 * @public
447 */
448 highlightAll: function(async, callback) {
449 _.highlightAllUnder(document, async, callback);
450 },
451
452 /**
453 * Fetches all the descendants of `container` that have a `.language-xxxx` class and then calls
454 * {@link Prism.highlightElement} on each one of them.
455 *
456 * The following hooks will be run:
457 * 1. `before-highlightall`
458 * 2. `before-all-elements-highlight`
459 * 3. All hooks of {@link Prism.highlightElement} for each element.
460 *
461 * @param {ParentNode} container The root element, whose descendants that have a `.language-xxxx` class will be highlighted.
462 * @param {boolean} [async=false] Whether each element is to be highlighted asynchronously using Web Workers.
463 * @param {HighlightCallback} [callback] An optional callback to be invoked on each element after its highlighting is done.
464 * @memberof Prism
465 * @public
466 */
467 highlightAllUnder: function(container, async, callback) {
468 var env = {
469 callback: callback,
470 container: container,
471 selector: 'code[class*="language-"], [class*="language-"] code, code[class*="lang-"], [class*="lang-"] code'
472 };
473
474 _.hooks.run('before-highlightall', env);
475
476 env.elements = Array.prototype.slice.apply(env.container.querySelectorAll(env.selector));
477
478 _.hooks.run('before-all-elements-highlight', env);
479
480 for (var i = 0, element; element = env.elements[i++];) {
481 _.highlightElement(element, async === true, env.callback);
482 }
483 },
484
485 /**
486 * Highlights the code inside a single element.
487 *
488 * The following hooks will be run:
489 * 1. `before-sanity-check`
490 * 2. `before-highlight`
491 * 3. All hooks of {@link Prism.highlight}. These hooks will be run by an asynchronous worker if `async` is `true`.
492 * 4. `before-insert`
493 * 5. `after-highlight`
494 * 6. `complete`
495 *
496 * Some the above hooks will be skipped if the element doesn't contain any text or there is no grammar loaded for
497 * the element's language.
498 *
499 * @param {Element} element The element containing the code.
500 * It must have a class of `language-xxxx` to be processed, where `xxxx` is a valid language identifier.
501 * @param {boolean} [async=false] Whether the element is to be highlighted asynchronously using Web Workers
502 * to improve performance and avoid blocking the UI when highlighting very large chunks of code. This option is
503 * [disabled by default](https://prismjs.com/faq.html#why-is-asynchronous-highlighting-disabled-by-default).
504 *
505 * Note: All language definitions required to highlight the code must be included in the main `prism.js` file for
506 * asynchronous highlighting to work. You can build your own bundle on the
507 * [Download page](https://prismjs.com/download.html).
508 * @param {HighlightCallback} [callback] An optional callback to be invoked after the highlighting is done.
509 * Mostly useful when `async` is `true`, since in that case, the highlighting is done asynchronously.
510 * @memberof Prism
511 * @public
512 */
513 highlightElement: function(element, async, callback) {
514 // Find language
515 var language = _.util.getLanguage(element);
516 var grammar = _.languages[language];
517
518 // Set language on the element, if not present
519 element.className = element.className.replace(lang, '').replace(/\s+/g, ' ') + ' language-' + language;
520
521 // Set language on the parent, for styling
522 var parent = element.parentElement;
523 if (parent && parent.nodeName.toLowerCase() === 'pre') {
524 parent.className = parent.className.replace(lang, '').replace(/\s+/g, ' ') + ' language-' + language;
525 }
526
527 var code = element.textContent;
528
529 var env = {
530 element: element,
531 language: language,
532 grammar: grammar,
533 code: code
534 };
535
536 function insertHighlightedCode(highlightedCode) {
537 env.highlightedCode = highlightedCode;
538
539 _.hooks.run('before-insert', env);
540
541 env.element.innerHTML = env.highlightedCode;
542
543 _.hooks.run('after-highlight', env);
544 _.hooks.run('complete', env);
545 callback && callback.call(env.element);
546 }
547
548 _.hooks.run('before-sanity-check', env);
549
550 if (!env.code) {
551 _.hooks.run('complete', env);
552 callback && callback.call(env.element);
553 return;
554 }
555
556 _.hooks.run('before-highlight', env);
557
558 if (!env.grammar) {
559 insertHighlightedCode(_.util.encode(env.code));
560 return;
561 }
562
563 if (async && _self.Worker) {
564 var worker = new Worker(_.filename);
565
566 worker.onmessage = function(evt) {
567 insertHighlightedCode(evt.data);
568 };
569
570 worker.postMessage(JSON.stringify({
571 language: env.language,
572 code: env.code,
573 immediateClose: true
574 }));
575 }
576 else {
577 insertHighlightedCode(_.highlight(env.code, env.grammar, env.language));
578 }
579 },
580
581 /**
582 * Low-level function, only use if you know what you’re doing. It accepts a string of text as input
583 * and the language definitions to use, and returns a string with the HTML produced.
584 *
585 * The following hooks will be run:
586 * 1. `before-tokenize`
587 * 2. `after-tokenize`
588 * 3. `wrap`: On each {@link Token}.
589 *
590 * @param {string} text A string with the code to be highlighted.
591 * @param {Grammar} grammar An object containing the tokens to use.
592 *
593 * Usually a language definition like `Prism.languages.markup`.
594 * @param {string} language The name of the language definition passed to `grammar`.
595 * @returns {string} The highlighted HTML.
596 * @memberof Prism
597 * @public
598 * @example
599 * Prism.highlight('var foo = true;', Prism.languages.javascript, 'javascript');
600 */
601 highlight: function (text, grammar, language) {
602 var env = {
603 code: text,
604 grammar: grammar,
605 language: language
606 };
607 _.hooks.run('before-tokenize', env);
608 env.tokens = _.tokenize(env.code, env.grammar);
609 _.hooks.run('after-tokenize', env);
610 return Token.stringify(_.util.encode(env.tokens), env.language);
611 },
612
613 /**
614 * This is the heart of Prism, and the most low-level function you can use. It accepts a string of text as input
615 * and the language definitions to use, and returns an array with the tokenized code.
616 *
617 * When the language definition includes nested tokens, the function is called recursively on each of these tokens.
618 *
619 * This method could be useful in other contexts as well, as a very crude parser.
620 *
621 * @param {string} text A string with the code to be highlighted.
622 * @param {Grammar} grammar An object containing the tokens to use.
623 *
624 * Usually a language definition like `Prism.languages.markup`.
625 * @returns {TokenStream} An array of strings and tokens, a token stream.
626 * @memberof Prism
627 * @public
628 * @example
629 * let code = `var foo = 0;`;
630 * let tokens = Prism.tokenize(code, Prism.languages.javascript);
631 * tokens.forEach(token => {
632 * if (token instanceof Prism.Token && token.type === 'number') {
633 * console.log(`Found numeric literal: ${token.content}`);
634 * }
635 * });
636 */
637 tokenize: function(text, grammar) {
638 var rest = grammar.rest;
639 if (rest) {
640 for (var token in rest) {
641 grammar[token] = rest[token];
642 }
643
644 delete grammar.rest;
645 }
646
647 var tokenList = new LinkedList();
648 addAfter(tokenList, tokenList.head, text);
649
650 matchGrammar(text, tokenList, grammar, tokenList.head, 0);
651
652 return toArray(tokenList);
653 },
654
655 /**
656 * @namespace
657 * @memberof Prism
658 * @public
659 */
660 hooks: {
661 all: {},
662
663 /**
664 * Adds the given callback to the list of callbacks for the given hook.
665 *
666 * The callback will be invoked when the hook it is registered for is run.
667 * Hooks are usually directly run by a highlight function but you can also run hooks yourself.
668 *
669 * One callback function can be registered to multiple hooks and the same hook multiple times.
670 *
671 * @param {string} name The name of the hook.
672 * @param {HookCallback} callback The callback function which is given environment variables.
673 * @public
674 */
675 add: function (name, callback) {
676 var hooks = _.hooks.all;
677
678 hooks[name] = hooks[name] || [];
679
680 hooks[name].push(callback);
681 },
682
683 /**
684 * Runs a hook invoking all registered callbacks with the given environment variables.
685 *
686 * Callbacks will be invoked synchronously and in the order in which they were registered.
687 *
688 * @param {string} name The name of the hook.
689 * @param {Object<string, any>} env The environment variables of the hook passed to all callbacks registered.
690 * @public
691 */
692 run: function (name, env) {
693 var callbacks = _.hooks.all[name];
694
695 if (!callbacks || !callbacks.length) {
696 return;
697 }
698
699 for (var i=0, callback; callback = callbacks[i++];) {
700 callback(env);
701 }
702 }
703 },
704
705 Token: Token
706};
707_self.Prism = _;
708
709
710// Typescript note:
711// The following can be used to import the Token type in JSDoc:
712//
713// @typedef {InstanceType<import("./prism-core")["Token"]>} Token
714
715/**
716 * Creates a new token.
717 *
718 * @param {string} type See {@link Token#type type}
719 * @param {string | TokenStream} content See {@link Token#content content}
720 * @param {string|string[]} [alias] The alias(es) of the token.
721 * @param {string} [matchedStr=""] A copy of the full string this token was created from.
722 * @class
723 * @global
724 * @public
725 */
726function Token(type, content, alias, matchedStr) {
727 /**
728 * The type of the token.
729 *
730 * This is usually the key of a pattern in a {@link Grammar}.
731 *
732 * @type {string}
733 * @see GrammarToken
734 * @public
735 */
736 this.type = type;
737 /**
738 * The strings or tokens contained by this token.
739 *
740 * This will be a token stream if the pattern matched also defined an `inside` grammar.
741 *
742 * @type {string | TokenStream}
743 * @public
744 */
745 this.content = content;
746 /**
747 * The alias(es) of the token.
748 *
749 * @type {string|string[]}
750 * @see GrammarToken
751 * @public
752 */
753 this.alias = alias;
754 // Copy of the full string this token was created from
755 this.length = (matchedStr || '').length | 0;
756}
757
758/**
759 * A token stream is an array of strings and {@link Token Token} objects.
760 *
761 * Token streams have to fulfill a few properties that are assumed by most functions (mostly internal ones) that process
762 * them.
763 *
764 * 1. No adjacent strings.
765 * 2. No empty strings.
766 *
767 * The only exception here is the token stream that only contains the empty string and nothing else.
768 *
769 * @typedef {Array<string | Token>} TokenStream
770 * @global
771 * @public
772 */
773
774/**
775 * Converts the given token or token stream to an HTML representation.
776 *
777 * The following hooks will be run:
778 * 1. `wrap`: On each {@link Token}.
779 *
780 * @param {string | Token | TokenStream} o The token or token stream to be converted.
781 * @param {string} language The name of current language.
782 * @returns {string} The HTML representation of the token or token stream.
783 * @memberof Token
784 * @static
785 */
786Token.stringify = function stringify(o, language) {
787 if (typeof o == 'string') {
788 return o;
789 }
790 if (Array.isArray(o)) {
791 var s = '';
792 o.forEach(function (e) {
793 s += stringify(e, language);
794 });
795 return s;
796 }
797
798 var env = {
799 type: o.type,
800 content: stringify(o.content, language),
801 tag: 'span',
802 classes: ['token', o.type],
803 attributes: {},
804 language: language
805 };
806
807 var aliases = o.alias;
808 if (aliases) {
809 if (Array.isArray(aliases)) {
810 Array.prototype.push.apply(env.classes, aliases);
811 } else {
812 env.classes.push(aliases);
813 }
814 }
815
816 _.hooks.run('wrap', env);
817
818 var attributes = '';
819 for (var name in env.attributes) {
820 attributes += ' ' + name + '="' + (env.attributes[name] || '').replace(/"/g, '&quot;') + '"';
821 }
822
823 return '<' + env.tag + ' class="' + env.classes.join(' ') + '"' + attributes + '>' + env.content + '</' + env.tag + '>';
824};
825
826/**
827 * @param {string} text
828 * @param {LinkedList<string | Token>} tokenList
829 * @param {any} grammar
830 * @param {LinkedListNode<string | Token>} startNode
831 * @param {number} startPos
832 * @param {RematchOptions} [rematch]
833 * @returns {void}
834 * @private
835 *
836 * @typedef RematchOptions
837 * @property {string} cause
838 * @property {number} reach
839 */
840function matchGrammar(text, tokenList, grammar, startNode, startPos, rematch) {
841 for (var token in grammar) {
842 if (!grammar.hasOwnProperty(token) || !grammar[token]) {
843 continue;
844 }
845
846 var patterns = grammar[token];
847 patterns = Array.isArray(patterns) ? patterns : [patterns];
848
849 for (var j = 0; j < patterns.length; ++j) {
850 if (rematch && rematch.cause == token + ',' + j) {
851 return;
852 }
853
854 var patternObj = patterns[j],
855 inside = patternObj.inside,
856 lookbehind = !!patternObj.lookbehind,
857 greedy = !!patternObj.greedy,
858 lookbehindLength = 0,
859 alias = patternObj.alias;
860
861 if (greedy && !patternObj.pattern.global) {
862 // Without the global flag, lastIndex won't work
863 var flags = patternObj.pattern.toString().match(/[imsuy]*$/)[0];
864 patternObj.pattern = RegExp(patternObj.pattern.source, flags + 'g');
865 }
866
867 /** @type {RegExp} */
868 var pattern = patternObj.pattern || patternObj;
869
870 for ( // iterate the token list and keep track of the current token/string position
871 var currentNode = startNode.next, pos = startPos;
872 currentNode !== tokenList.tail;
873 pos += currentNode.value.length, currentNode = currentNode.next
874 ) {
875
876 if (rematch && pos >= rematch.reach) {
877 break;
878 }
879
880 var str = currentNode.value;
881
882 if (tokenList.length > text.length) {
883 // Something went terribly wrong, ABORT, ABORT!
884 return;
885 }
886
887 if (str instanceof Token) {
888 continue;
889 }
890
891 var removeCount = 1; // this is the to parameter of removeBetween
892
893 if (greedy && currentNode != tokenList.tail.prev) {
894 pattern.lastIndex = pos;
895 var match = pattern.exec(text);
896 if (!match) {
897 break;
898 }
899
900 var from = match.index + (lookbehind && match[1] ? match[1].length : 0);
901 var to = match.index + match[0].length;
902 var p = pos;
903
904 // find the node that contains the match
905 p += currentNode.value.length;
906 while (from >= p) {
907 currentNode = currentNode.next;
908 p += currentNode.value.length;
909 }
910 // adjust pos (and p)
911 p -= currentNode.value.length;
912 pos = p;
913
914 // the current node is a Token, then the match starts inside another Token, which is invalid
915 if (currentNode.value instanceof Token) {
916 continue;
917 }
918
919 // find the last node which is affected by this match
920 for (
921 var k = currentNode;
922 k !== tokenList.tail && (p < to || typeof k.value === 'string');
923 k = k.next
924 ) {
925 removeCount++;
926 p += k.value.length;
927 }
928 removeCount--;
929
930 // replace with the new match
931 str = text.slice(pos, p);
932 match.index -= pos;
933 } else {
934 pattern.lastIndex = 0;
935
936 var match = pattern.exec(str);
937 }
938
939 if (!match) {
940 continue;
941 }
942
943 if (lookbehind) {
944 lookbehindLength = match[1] ? match[1].length : 0;
945 }
946
947 var from = match.index + lookbehindLength,
948 matchStr = match[0].slice(lookbehindLength),
949 to = from + matchStr.length,
950 before = str.slice(0, from),
951 after = str.slice(to);
952
953 var reach = pos + str.length;
954 if (rematch && reach > rematch.reach) {
955 rematch.reach = reach;
956 }
957
958 var removeFrom = currentNode.prev;
959
960 if (before) {
961 removeFrom = addAfter(tokenList, removeFrom, before);
962 pos += before.length;
963 }
964
965 removeRange(tokenList, removeFrom, removeCount);
966
967 var wrapped = new Token(token, inside ? _.tokenize(matchStr, inside) : matchStr, alias, matchStr);
968 currentNode = addAfter(tokenList, removeFrom, wrapped);
969
970 if (after) {
971 addAfter(tokenList, currentNode, after);
972 }
973
974 if (removeCount > 1) {
975 // at least one Token object was removed, so we have to do some rematching
976 // this can only happen if the current pattern is greedy
977 matchGrammar(text, tokenList, grammar, currentNode.prev, pos, {
978 cause: token + ',' + j,
979 reach: reach
980 });
981 }
982 }
983 }
984 }
985}
986
987/**
988 * @typedef LinkedListNode
989 * @property {T} value
990 * @property {LinkedListNode<T> | null} prev The previous node.
991 * @property {LinkedListNode<T> | null} next The next node.
992 * @template T
993 * @private
994 */
995
996/**
997 * @template T
998 * @private
999 */
1000function LinkedList() {
1001 /** @type {LinkedListNode<T>} */
1002 var head = { value: null, prev: null, next: null };
1003 /** @type {LinkedListNode<T>} */
1004 var tail = { value: null, prev: head, next: null };
1005 head.next = tail;
1006
1007 /** @type {LinkedListNode<T>} */
1008 this.head = head;
1009 /** @type {LinkedListNode<T>} */
1010 this.tail = tail;
1011 this.length = 0;
1012}
1013
1014/**
1015 * Adds a new node with the given value to the list.
1016 * @param {LinkedList<T>} list
1017 * @param {LinkedListNode<T>} node
1018 * @param {T} value
1019 * @returns {LinkedListNode<T>} The added node.
1020 * @template T
1021 */
1022function addAfter(list, node, value) {
1023 // assumes that node != list.tail && values.length >= 0
1024 var next = node.next;
1025
1026 var newNode = { value: value, prev: node, next: next };
1027 node.next = newNode;
1028 next.prev = newNode;
1029 list.length++;
1030
1031 return newNode;
1032}
1033/**
1034 * Removes `count` nodes after the given node. The given node will not be removed.
1035 * @param {LinkedList<T>} list
1036 * @param {LinkedListNode<T>} node
1037 * @param {number} count
1038 * @template T
1039 */
1040function removeRange(list, node, count) {
1041 var next = node.next;
1042 for (var i = 0; i < count && next !== list.tail; i++) {
1043 next = next.next;
1044 }
1045 node.next = next;
1046 next.prev = node;
1047 list.length -= i;
1048}
1049/**
1050 * @param {LinkedList<T>} list
1051 * @returns {T[]}
1052 * @template T
1053 */
1054function toArray(list) {
1055 var array = [];
1056 var node = list.head.next;
1057 while (node !== list.tail) {
1058 array.push(node.value);
1059 node = node.next;
1060 }
1061 return array;
1062}
1063
1064
1065if (!_self.document) {
1066 if (!_self.addEventListener) {
1067 // in Node.js
1068 return _;
1069 }
1070
1071 if (!_.disableWorkerMessageHandler) {
1072 // In worker
1073 _self.addEventListener('message', function (evt) {
1074 var message = JSON.parse(evt.data),
1075 lang = message.language,
1076 code = message.code,
1077 immediateClose = message.immediateClose;
1078
1079 _self.postMessage(_.highlight(code, _.languages[lang], lang));
1080 if (immediateClose) {
1081 _self.close();
1082 }
1083 }, false);
1084 }
1085
1086 return _;
1087}
1088
1089// Get current script and highlight
1090var script = _.util.currentScript();
1091
1092if (script) {
1093 _.filename = script.src;
1094
1095 if (script.hasAttribute('data-manual')) {
1096 _.manual = true;
1097 }
1098}
1099
1100function highlightAutomaticallyCallback() {
1101 if (!_.manual) {
1102 _.highlightAll();
1103 }
1104}
1105
1106if (!_.manual) {
1107 // If the document state is "loading", then we'll use DOMContentLoaded.
1108 // If the document state is "interactive" and the prism.js script is deferred, then we'll also use the
1109 // DOMContentLoaded event because there might be some plugins or languages which have also been deferred and they
1110 // might take longer one animation frame to execute which can create a race condition where only some plugins have
1111 // been loaded when Prism.highlightAll() is executed, depending on how fast resources are loaded.
1112 // See https://github.com/PrismJS/prism/issues/2102
1113 var readyState = document.readyState;
1114 if (readyState === 'loading' || readyState === 'interactive' && script && script.defer) {
1115 document.addEventListener('DOMContentLoaded', highlightAutomaticallyCallback);
1116 } else {
1117 if (window.requestAnimationFrame) {
1118 window.requestAnimationFrame(highlightAutomaticallyCallback);
1119 } else {
1120 window.setTimeout(highlightAutomaticallyCallback, 16);
1121 }
1122 }
1123}
1124
1125return _;
1126
1127})(_self);
1128
1129if (typeof module !== 'undefined' && module.exports) {
1130 module.exports = Prism;
1131}
1132
1133// hack for components to work correctly in node.js
1134if (typeof global !== 'undefined') {
1135 global.Prism = Prism;
1136}
1137
1138// some additional documentation/types
1139
1140/**
1141 * The expansion of a simple `RegExp` literal to support additional properties.
1142 *
1143 * @typedef GrammarToken
1144 * @property {RegExp} pattern The regular expression of the token.
1145 * @property {boolean} [lookbehind=false] If `true`, then the first capturing group of `pattern` will (effectively)
1146 * behave as a lookbehind group meaning that the captured text will not be part of the matched text of the new token.
1147 * @property {boolean} [greedy=false] Whether the token is greedy.
1148 * @property {string|string[]} [alias] An optional alias or list of aliases.
1149 * @property {Grammar} [inside] The nested grammar of this token.
1150 *
1151 * The `inside` grammar will be used to tokenize the text value of each token of this kind.
1152 *
1153 * This can be used to make nested and even recursive language definitions.
1154 *
1155 * Note: This can cause infinite recursion. Be careful when you embed different languages or even the same language into
1156 * each another.
1157 * @global
1158 * @public
1159*/
1160
1161/**
1162 * @typedef Grammar
1163 * @type {Object<string, RegExp | GrammarToken | Array<RegExp | GrammarToken>>}
1164 * @property {Grammar} [rest] An optional grammar object that will be appended to this grammar.
1165 * @global
1166 * @public
1167 */
1168
1169/**
1170 * A function which will invoked after an element was successfully highlighted.
1171 *
1172 * @callback HighlightCallback
1173 * @param {Element} element The element successfully highlighted.
1174 * @returns {void}
1175 * @global
1176 * @public
1177*/
1178
1179/**
1180 * @callback HookCallback
1181 * @param {Object<string, any>} env The environment variables of the hook.
1182 * @returns {void}
1183 * @global
1184 * @public
1185 */