components/prism-core.js


Log

Author Commit Date CI Message
Michael Schmidt a36e96ab 2020-06-27T00:01:47 Added general de/activation mechanism for plugins (#2434) Keep Markup, Line numbers, and Normalize whitespace now share the same per-element class-based de/activation logic.
Michael Schmidt 2d4c94cd 2020-03-25T13:12:42 Core: Linked list implementation for matchGrammar (#1909) The token streams in `matchGrammar` are now backed by a linked list instead of an array. This guarantees O(1) time for all operations. The `matchGrammar` is now private.
Michael Schmidt f683972e 2020-02-19T12:26:45 Core: Faster Token.stringify (#2171) This removes temporary arrays in `Token.stringify`. All these changes make `Prism.highlight` about 10~23% faster.
Michael Schmidt 984e5d2e 2020-01-31T22:50:59 Core: Fixed scope problem in script mode (#2184) A function in Core was defined inside an `if` which isn't allowed in JS strict mode.
Michael Schmidt a7f70090 2019-12-02T14:10:53 Added new Filter highlightAll plugin (#2074) This adds a new plugin to Prism which allows users to filter which elements will be highlighted by the `highlightAll` and `highlightAllUnder` methods.
Michael Schmidt 0fd062d5 2019-11-01T22:31:17 Core: Fixed null reference (#2106) The new defer logic in Prism Core did not handle that the script element might be null.
Michael Schmidt 2108c60f 2019-10-25T17:34:50 Core: IE11 workaround for currentScript (#2104) This adds a new function `Prism.util.currentScript` which is a functionally equalent to `document.currentScript` but includes support for IE11.
Michael Schmidt a3785ec9 2019-10-25T17:05:23 Core: Fixed race condition caused by deferring the script (#2103) This fixes a race condition that can occur when deferring Prism components. For more details see #2102.
Tim Düsterhus d49f0f26 2019-09-03T16:40:02 Doubly check the `manual` flag (#1957) This fixes the behavior of manual highlighting where it would highlight everything even with `manual: true`. This also adds doc as to how manual highlighting works.
Michael Schmidt 2d858e0a 2019-08-31T15:43:28 Core: Minor improvements (#1973) This makes some minor improvements to Prism Core like refactoring and style clean up.
Michael Schmidt 174ed103 2019-08-29T23:38:59 Core: Fixed greedy partial lookbehinds not working (#2030) This fixes the bug that greedy patterns with a lookbehind group did not support other top-level alternatives (e.g. `/foo|(lookbehind)bar/`) causing an error when used.
Michael Schmidt e864d518 2019-08-29T23:24:17 Core: Fixed greedy targeting bug (#1932) The `target` parameter defines the token which causes rematching. Rematching is done by recursively calling `matchGrammar` where `target` will be the current token name. The rematching is done if either a match was found or we reached the target token, whatever comes first. The bug is that a token is identified by its name. But the name alone doesn't uniquely identify a token in a grammar because many tokens can have the same name (array). This fixes this bug.
Michael Schmidt 9d9e2ca4 2019-07-11T16:28:49 Removed env.elements in 'before-highlightall' hook (#1968) This removes the `env.elements` property from the environment of the `before-highlightall` hook. **Why?** - There is no plugin that is using it. - It is inherently incompatible with `env.selector`. Even if a plugin using `elements` wanted to respect `selector` it would not be able to because 1) there might be plugins after it (in `before-highlightall`) extending the selector and 2) the plugin has no access to the current `container`. **Isn't this a breaking change?** Yes, but I don't think that this will have any impact for the above reasons and because from a user perspective, it's hard to use than just iteration over your elements and calling `Prism.highlightElement` yourself.
Michael Schmidt fd691c52 2019-04-23T00:33:15 Core: Made `language-none` the default language (#1858) This makes `language-none` the default language of a highlighted element in the case that no `language-xxxx` class is provided or inherited.
Michael Schmidt 65a4e894 2019-03-28T23:47:49 Removed `parent` from the `wrap` hook's environment (#1837) This removes the `parent` property of the `wrap` hook and the parameter from `Token.stringify`.
Michael Schmidt 11d0f75e 2019-03-14T13:17:45 Applied Array.isArray (#1804) This replaces all occurrences of `Prism.util.type(variable) {!==, ===} 'Array'` with an equivalent `Array.isArray` expression.
Michael Schmidt bac09f0a 2019-03-02T17:27:19 Made prism-core a little more editor friendly (#1776) This makes the structure of prism-core easier to understand for editors.
ExE Boss b06f532f 2019-03-01T01:19:29 Core: Avoid redeclaring variables in util.clone (#1778) This changes util.clone to be more readable and to avoid redeclaring the `clone` and `id` variables.
Michael Schmidt 152a68ef 2019-02-28T21:36:57 Improved languages.DFS and util.clone (#1506) Minor improvements for the two functions.
Michael Schmidt 393ab164 2019-01-22T13:36:32 Call `insert-before` hook properly (#1709) Fixes #1706 (also includes a demo). The problem was that the `insert-before` hook was not called if `grammar` is undefined. This also fixes that `callback` was not called in that case and that `env` of the `complete` hook did not have a `highlightedCode` property. All other hooks are unchanged. The `insert-before` hook is only used by the Command line plugin, so there should be no other side effects.
Michael Schmidt fbf0b094 2019-01-22T02:20:17 Remove the Prism.tokenize language parameter (#1654) The `language` parameter of `tokenize` was unused and the only one calling `tokenize` with a value for the said parameter is Markup-templating (removed in #1653). It's not [documented](https://prismjs.com/extending.html#api) either.
Michael Schmidt d33d259c 2018-12-02T00:02:57 Ignore duplicates in insertBefore (#1628) Fix #1525 and implement option number 2.
Michael Schmidt 39686e12 2018-12-01T19:51:08 Remove unused `insertBefore` overload (#1631) Mentioned in #1505.
Michael Schmidt 2864fe24 2018-11-28T14:54:29 Improve `Prism.util.type` performance (#1545) Drop the regex in favor of a faster `slice` call.
Ran Luo bfbe4464 2018-10-24T04:08:51 Invoke `callback` after `after-highlight` hook (#1588) Previously, `callback`, as passed to `Prism.highlightElement`, would get called before all of the hooks had been run. This moves it after all the hooks, so it can truly be considered a "complete" callback. This could be a breaking change for any userland modifications that were expecting `callback` to fire before plugins, but we expect this change to mostly conform with user expectations and consider this a bugfix..
Michael Schmidt 9dfec340 2018-08-18T22:04:51 `insertBefore` now correctly updates references (#1531) Removes some workarounds for misplacing the reference when using `insertBefore`.
Golmote 659ea314 2018-04-07T10:41:08 Core: Fix support for language alias containing dash `-`
Golmote 294efaae 2018-03-26T20:48:26 Core: Greedy patterns should always be matched against the full string. Fix #1355
Golmote 5f9c078a 2018-03-25T13:46:42 Make ERB, Handlebars, PHP and Smarty highlight properly in NodeJS (#1367)
Golmote 2b8321d3 2018-03-20T08:37:58 JSX: Add support for plain text inside tags (#1357) * JSX: Add support for plain text inside tags * JSX: Use a "plain-text" token to identify plain text * TSX: Add support for plain text * Fix test after merge
Golmote f90d5559 2018-03-13T08:11:25 Core: Allow cloning of circular structures. (#1345)
Golmote e0cd47f8 2018-03-03T20:13:59 Core: Don't thow an error if lookbehing is used without anything matching.
Hisateru Tanaka 69f2e2cb 2017-12-06T04:10:50 Allow any element being root instead of document. (#1230) (better compatibility than #397)
Golmote d09982d8 2017-11-05T14:24:19 Core: Provide a way to load Prism from inside a Worker without listening to messages. (#1188) Using disableWorkerMessageHandler config.
Golmote c33be19a 2017-11-05T13:32:00 Core: Prevent error from throwing when element does not have a parentNode in highlightElement. Fix #1203
Golmote de996d7d 2017-10-21T10:17:56 Merge branch 'remove_spellcheck_attribute' of https://github.com/emmanuelgautier/prism into emmanuelgautier-remove_spellcheck_attribute # Conflicts: # components/prism-core.min.js
Golmote 25788eb5 2017-09-09T12:05:55 Merge branch 'RemoveIE8' of https://github.com/zeitgeist87/prism into zeitgeist87-RemoveIE8 # Conflicts: # components/prism-core.js # components/prism-core.min.js # plugins/line-highlight/prism-line-highlight.js # plugins/line-highlight/prism-line-highlight.min.js # prism.js
Andreas Rohner e13fdfa4 2017-02-08T17:19:22 Add minor optimizations for the greedy flag
Andreas Rohner 1736f4aa 2017-02-08T01:36:27 Fix corner cases for the greedy flag By refactoring the tokenize() method into two separate methods, it is possible to recursively clean up any broken tokens left by the greedy feature. This should fix the issue 1075.
Golmote 70cb4729 2017-05-08T11:44:21 Run before-highlight and after-highlight hooks even when no grammar is found. Fix #1134
Emmanuel Gautier 01e74afa 2017-02-28T12:42:27 remove comments spellcheck for amp validation
James DiGioia bafc4cb6 2017-01-24T16:31:10 Initial implementation of manual highlighting (#1087) * Initial implementation of manual highlighting Borrow the `manual` property of whatever already existing Prism instance there is. * Simplify setting of `Prism.manual` Cleans up the implementation w/ less code.
Golmote b8ce1e7c 2016-12-03T11:52:48 Merge branch 'gh-pages' of https://github.com/PrismJS/prism into gh-pages # Conflicts: # components/prism-core.min.js
Golmote 83bafbd4 2016-12-03T11:46:26 env.code can be modified by before-sanity-check hook even when using language-none. Fix #1066
Rob Wu 17e33bc0 2016-11-20T12:52:54 Reduce risk of XSS (#1051) * Skip non-own properties of env.attributes Use `Object.keys` instead of a for-in loop to find optional attributes. The former only grabs keys that are own properties, the latter also includes inherit properties from `Object.prototype`. This reduces the risk of XSS if an attacker somehow manages to manipulate the prototype chain of the Object prototype. * Fix root cause of XSS in autolinker plugin #1054 * command-line plugin: Safely encode attributes If an attacker has control over the values of the attributes "data-prompt", "data-user", or "data-host", then XSS was possible. This fixes the issue, by encoding quotes as the `"` entity. * show-language plugin: innerHTML -> textContent There is no need for `innerHTML` here. At best nothing happens, at worst XSS is possible (though the odds are negligible since the attacker would have to control the detected language). * toolbar plugin: innerHTML -> textContent
Andreas Rohner 32cd99ff 2016-10-24T21:21:37 Fix greedy-flag bug This bug occurs in the relatively rare case of a pattern matching the empty string. It was reported in issue #1039. If for example a HTML page contains an empty script tag `<script></script>` then the script pattern will match anything inside, which is the empty string. This empty string is then passed to the constructor of the Token class. Since `""` is falsy in Javascript the property `matchedStr` is set to `null`. But the property `matchedStr` is needed to calculate the current position for the greedy feature. A `null` value in `matchedStr` results in a `pos` that is `NaN`. This causes the bug described in issue #1039. Since the property `matchedStr` is only ever needed to calculate the length of the Token, it is more efficient to store the length directly instead of the string. As a side effect this also fixes issue #1039.
Golmote 3dc8c9e1 2016-07-11T20:36:16 Don't insert space before ">" when there is no attributes
Andreas Rohner 3780571b 2016-07-03T11:59:59 Remove IE8 plugin
Golmote c9bdcd35 2016-07-03T02:37:29 Manual merge of #988. Closes #988, fixes #987.
Golmote 9893489c 2016-06-18T14:18:27 Replace flags property of RegExp with a more compatible alternative
Andreas Rohner d80900cc 2016-06-04T19:52:38 Improvement to greedy-flag This patch expands the idea of the greedy flag and significantly improves it, by matching against the whole text instead of just the next couple of tokens. This does not only improve the results, but it should also slightly improve performance.
Chris Ruppel f130680c 2016-05-29T11:15:59 Use requestAnimationFrame instead of setTimeout
Chris Ruppel 4eb7ee45 2016-05-26T11:27:03 Allow for asynchronous loading of prism.js
Anders Grendstadbakk acd9508f 2016-05-17T14:23:02 Use toLowerCase on language names
Andreas Rohner 21dd0d14 2016-02-15T23:08:24 Use a HTML-comment to escape code
Andreas Rohner 49cf8991 2016-03-08T15:03:59 Fix minor bug with new greedy-feature This fixes a minor bug that causes invalid highlighting in some edge cases, where two greedy patterns overlap each other.
Andreas Rohner c88036ca 2016-02-19T22:27:22 Add tests for new greedy-pattern feature and fix bug in Kotlin This patch adds tests for the new greedy-pattern feature and fixes a small bug in the Kotlin language, that resulted from that. It also cleans up some of the comments and refactors a few lines in the tokenloop.
Andreas Rohner 90432f96 2016-02-18T01:22:40 Add comments to better document the greedy-pattern feature
Andreas Rohner 2705c509 2015-09-24T19:36:10 Partial solution for the "Comment-like substrings"-problem This patch introduces a new attribute called `greedy`. The attribute is a simple boolean flag. If there is no match for a greedy pattern it can concatenate the next two tokens into a single string and try to match on this string again. If a match is found on the second attempt, then the old tokens are deleted and replaced by the new match. This solves the "Comment-like substrings"-problem for exactly one comment at very little cost. With this patch the following code is highlighted correctly: "foo /* bar */ baz"; "foo // bar"; /lala"test"sdf/; This approach fails if there are more than one comments inside the string: "foo /* bar */ baz /* bar */ baz"; Signed-off-by: Andreas Rohner <andreas.rohner@gmx.net>
Lea Verou af8da8ec 2016-02-14T21:08:14 Implemented @zeitgeist87’s suggestion in #890 re: env.elements
Lea Verou 70a8602f 2016-02-13T15:21:28 Add before-highlightall hook Needed for unescaped markup plugin, as discussed in #887. Alternatively, we could reinstate @zeitgeist87’s PR about making the selector a config option. Thoughts?
Andreas Rohner b86c727a 2016-02-03T15:25:26 Fix DFS bug The DFS function uses an object as a key into another object. This doesn't work, because the key gets stringified to "[object Object]" and is therefore useless.
Andreas Rohner 7fab7a2d 2016-01-26T23:16:06 Reverse merge of last PR and remove the 'data-selector' attribute.
Andreas Rohner fa987435 2016-01-26T23:10:45 Use document.currentScript instead of document.getElementsByTagName()
Andreas Rohner 77c6ea17 2016-01-26T02:33:20 Allow users to use their own element selector Add the parameter 'data-selector' to the script tag, to allow users to set the selector used to query for code elements.
Valtteri Laitinen 417f54a1 2016-01-16T03:12:00 Simplify the “lang” regex Remove an useless (?!\*) part from the lang regex.
Andreas Rohner 02894e1e 2015-12-21T19:00:26 Prevent infinite recursion in DFS
Golmote 0e93b417 2015-09-24T09:16:57 Fix code indentation style
Andreas Rohner 09be99e7 2015-09-23T20:34:29 Improve performance by doing more work in the worker Currently the worker tokenizes the code and then turns it into a JSON string. This JSON string is sent back to the main thread as a message and parsed. Then it is turned into a HTML string and stored in the attribute env.highlightedCode. These conversions into JSON are unnecessary, because we can create the HTML string directly in the worker and send it as a message to the main thread. Signed-off-by: Andreas Rohner <andreas.rohner@gmx.net>
Golmote b2f14d96 2015-09-04T08:19:21 Merge branch 'plugin-jsonp' of https://github.com/nauzilus/prism into gh-pages Conflicts: components/prism-core.min.js
Golmote e42a2289 2015-09-03T22:47:50 Explicitly make the Worker close itself after highlighting, so that users have control on this behaviour when directly using Prism inside a Worker. Fix #492
Golmote 0356c58c 2015-09-03T19:09:48 Apply changes from #754 to all plugins so that none of them throw errors in node. Autolinker, Show invisibles and WPD can now be used with node. Removed unused minified CSS file. Also added missing space between attributes during wrapping.
Golmote de7a5cab 2015-09-03T18:42:16 Merge pull request #754 from valorkin/fix-correct-behaviour-in-nodejs Fix: node.js hacks for components and prism.js
Dmitriy Shekhovtsov 9f5c93c6 2015-09-03T13:45:26 Fix: node.js hacks for components and prism.js
Golmote ed9f2b21 2015-08-17T08:47:32 Move removal of initial line feed to a plugin
Golmote fd549959 2015-08-13T08:33:48 Fix complete hook behaviour + run gulp task
Golmote 5f133c80 2015-07-16T21:09:40 Fixed prism-core and prism-file-highlight to prevent errors in IE8. Used local variable _self instead of overwriting self. Closes #468.
Golmote 8030db91 2015-06-25T08:00:14 File Highlight: allow to specify the language (takes precedence over extension guess) + Set language on parent even if unknown. Fix #607
Ewald Grusk 1eb49594 2015-02-18T21:52:53 Make use of method `_.util.type`.
Daniel Flint c711660a 2015-01-15T09:00:47 Deep clone arrays
Golmote 703d47e6 2015-01-11T23:18:41 Simplified regexp for trailing line feeds
Golmote 9487b5be 2015-01-09T08:11:21 Removed unneeded escapes in RegExps, added some semicolons
Golmote 36999ddf 2015-01-08T19:51:55 Handle trailing \r\n and \r
Golmote 2d5089ed 2015-01-08T08:55:24 Remove leading line break Fixes #455
Lea Verou 17d9aa8a 2014-11-14T20:30:10 insertBefore() with only two arguments appends (similar to how DOM insertBefore() behaves)
Lea Verou 436a7bd6 2014-11-14T19:43:50 Make insertBefore() update pointers to the object from other language definitions. Use case/Example: In dabblet, code-highlight.js adds some extra tokens to CSS. But CSS also extends markup to highlight CSS in style elements/attributes and that happens *before* the extra CSS tokens were added. However, because references were not updated, CSS highlighted in markup was using the original CSS definition, without the added tokens. I’m surprised we haven’t caught this earlier, I’d expect it to affect many advanced uses of Prism.
Lea Verou 4b9e11c6 2014-11-14T19:19:31 CSS highlighting in HTML style attribute
Andreas Rohner 08037e5b 2014-09-05T11:01:02 Fix for DFS bug with new pattern array feature This patch implements support for the new pattern array feature in the Prism.languages.DFS function and fixes a bug in the autolinker plugin. It adds an optional parameter to the callback of Prism.languages.DFS, which contains the type of the current object as oposed to the key in the parent. In most cases both key and type are exactly the same. Only if the parent is an array the key will contain the index number and the type will contain the attribute name of the array in the parent object. The key can be used to replace the object in the parent and the type can be used to provide the necessary context.
Andreas Rohner 3309890f 2014-08-12T13:48:47 Add option to define aliases for tokens This patch adds an option called `alias`, which allows the definition of additional CSS classes for token names. This can be useful, to combine the styling of a well known token, which is already supported by most of the themes, with a semantically correct token name. The option can be set to a string literal or an array of string literals. In the following example the token name `latex-equation` is not supported by any theme, but it will be highlighted the same as a string. 'latex-equation': { pattern: /\$(\\?.)*?\$/g, alias: 'string' }
Aleksandr Zverev ba3bcd23 2014-08-20T14:42:08 Fixed missed encode call for webworkers
Andreas Rohner 43e4e693 2014-08-10T23:26:10 Allow multiple regex per token name by using arrays In most languages there are multiple different ways of declaring the same constructs (e.g. comments, strings, ...) and sometimes it is difficult or unpractical to match all of them with one single regular expression. This patch adds the possibility to use an array of pattern objects. For example there is a minor bug in the current definition of the `clike` language, that could be solved with this patch: The character immediately in front of a single line comment is highlighted as a comment as well. something// something This is because both definitions for single and multiline comments have to be matched with a single regex and the `lookbehind` parameter can only be applied to the first captured string. With this patch one could split the two definitions up and use `lookbehind` for both, thereby eliminating the bug. 'comment': [ { pattern: /(^|[^\\])\/\*[\w\W]*?\*\//g, lookbehind: true }, { pattern: /(^|[^\\:])\/\/.*?(\r?\n|$)/g, lookbehind: true } ],
Andreas Rohner fef8cd49 2014-08-10T08:24:02 Allow multiple tokens to be mapped to the same CSS class In most languages there are different ways of declaring the same constructs, but with the current implementation of Prism it is only possible to define one pattern for a particular token type. This is not a problem, as long as it is possible to match everything with one regex. But sometimes it isn't easily possible to do that, especially if things like `lookbehind` or `inside` are needed for one part but not for the other. This patch splits the token type into two parts, whereby the second part is optional: {token type}#{description} For example there is a minor bug in the current definition of the `clike` language, that could be solved with this patch: The character immediately in front of a single line comment is highlighted as a comment as well. something// something This is because both definitions for single and multiline comments have to be matched with a single regex and the `lookbehind` parameter can only be applied to the first captured string. With this patch one could split the two definitions up and use `lookbehind` for both, thereby eliminating the bug. 'comment': { pattern: /(^|[^\\])\/\*[\w\W]*?\*\//g, lookbehind: true }, 'comment#single-line': { pattern: /(^|[^\\:])\/\/.*?(\r?\n|$)/g, lookbehind: true },
Jannik Zschiesche 502ed6c1 2014-07-23T10:49:30 Don't set the `self` variable with `var` Firefox directly shadows the global variable `self` if it is present (= in a worker). So we need to set `self` without `var self` but directly.
Jannik Zschiesche eb1cd1c9 2014-07-23T09:49:34 Fix workers The issue is that in workers `window` is undefined. Therefore, in the first line, `self` will be set to `{}`. This will destroy the check for workers later on: ```js if (!self.document) { if (!self.addEventListener) { // in Node.js return self.Prism; } // In worker // ... } ``` because `self.addEventListener` will then be undefined (due to `self = {}`). The fix is to make the check for the environment more clever: 1. check for the browser: `(typeof window !== 'undefined')` 2. check for a worker: `(typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope)` (from http://stackoverflow.com/questions/7931182/reliably-detect-if-the-script-is-executing-in-a-web-worker) 3. assume we are in node
Thomas Ingram cddff711 2014-05-24T15:59:23 Simplify encoding logic
Thomas Ingram 8557daa9 2014-05-24T14:23:52 Remove leftover `console.log`
Thomas Ingram 9f6cff9b 2014-05-23T13:01:26 Whitespace cleanup
Thomas Ingram c0a0353d 2014-05-23T13:40:52 Stop prematurely escaping code block Refactor Prism.highlight to HTML encode tokens before calling Token.stringify. This is a breaking change for nearly all grammars! Previously Prism was encoding HTML entities *before* passing the code block to the parser. Now it encodes HTML entities *after* the tokens have been parsed (before rendering to HTML).
Lea Verou 62561684 2014-05-22T17:36:51 Reversed change in Core
Lea Verou 0bf2a8e5 2014-05-22T17:28:57 Updated minified files