Skip to content

Commit

Permalink
Autoloader: Fixed and improved callbacks (#1935)
Browse files Browse the repository at this point in the history
This PR fixes and improves the internal Autoloader callbacks.

### Bug

Callbacks were stored in `success_callbacks` and `error_callbacks` and not removed after execution meaning once added, a callback was executed every time the language for which the callback was added for was requested (either directly or as a dependency).

This bug wasn't very visible because of how the Autoloader is usually used. Usually, the Autoloader is used to load the languages for the static code in a page. This means that the Autoloader gets asked for a bunch of languages, loads them asynchronously and then executes the callbacks with this [line](https://github.com/PrismJS/prism/blob/d4373f3a282f4c09e4fb4dcbd8885c7848b61073/plugins/autoloader/prism-autoloader.js#L187) (`success` is [this](https://github.com/PrismJS/prism/blob/d4373f3a282f4c09e4fb4dcbd8885c7848b61073/plugins/autoloader/prism-autoloader.js#L306) function). Because it takes a little while until the language is loaded (certainly until the next tick), all callbacks will be registered before the `onload` of the script is called meaning that [this](https://github.com/PrismJS/prism/blob/d4373f3a282f4c09e4fb4dcbd8885c7848b61073/plugins/autoloader/prism-autoloader.js#L300) never gets executed.
Thus the bug doesn't show up. (And even if it does, the success callback is usually just a `Prism.highlightElement` call.)

I discovered this bug when I used Autoloader to highlight C like code and then after that was done (some ticks later), I used Autoloader to highlight JavaScript code. What followed was that the synchronously called callbacks caused a stack overflow. Just a bit of good ol' infinite recursion.

I could have also fixed it by deferring callbacks to the next tick but I decided to rework the callback structure because this change of behavior might break some code. Autoloader invokes the callbacks of already loaded languages (and failed ones) synchronously while all other callbacks are called asynchronously. This fix keeps this behavior.
(It's another question as to whether we should call all callbacks asynchronously. <sub><sup>Yes, we should IMO.</sup></sub>)

### Improvements

1. [This](https://github.com/PrismJS/prism/blob/d4373f3a282f4c09e4fb4dcbd8885c7848b61073/plugins/autoloader/prism-autoloader.js#L218) declaration and usage of `data` is completely unnecessary and was removed.
1. Typings for `lang_data`.
  • Loading branch information
RunDevelopment authored and mAAdhaTTah committed Jun 30, 2019
1 parent 24c8e71 commit b19f512
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
56 changes: 30 additions & 26 deletions plugins/autoloader/prism-autoloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@
"yml": "yaml"
}/*]*/;

/**
* @typedef LangDataItem
* @property {{ success?: function, error?: function }[]} callbacks
* @property {boolean} [error]
* @property {boolean} [loading]
*/
/** @type {Object<string, LangDataItem>} */
var lang_data = {};

var ignored_language = 'none';
Expand Down Expand Up @@ -216,11 +223,6 @@
lang = lang_aliases[lang];
}

var data = lang_data[lang];
if (!data) {
data = lang_data[lang] = {};
}

// Look for additional dependencies defined on the <code> or <pre> tags
var deps = elt.getAttribute('data-dependencies');
if (!deps && elt.parentNode && elt.parentNode.tagName.toLowerCase() === 'pre') {
Expand Down Expand Up @@ -282,20 +284,14 @@
var load = function () {
var data = lang_data[lang];
if (!data) {
data = lang_data[lang] = {};
}
if (success) {
if (!data.success_callbacks) {
data.success_callbacks = [];
}
data.success_callbacks.push(success);
}
if (error) {
if (!data.error_callbacks) {
data.error_callbacks = [];
}
data.error_callbacks.push(error);
data = lang_data[lang] = {
callbacks: []
};
}
data.callbacks.push({
success: success,
error: error
});

if (!force && Prism.languages[lang]) {
languageSuccess(lang);
Expand Down Expand Up @@ -329,10 +325,14 @@
* @param {string} lang
*/
var languageSuccess = function (lang) {
if (lang_data[lang] && lang_data[lang].success_callbacks && lang_data[lang].success_callbacks.length) {
lang_data[lang].success_callbacks.forEach(function (f) {
f(lang);
});
if (lang_data[lang] ) {
var callbacks = lang_data[lang].callbacks;
while (callbacks.length) {
var callback = callbacks.shift().success;
if (callback) {
callback();
}
}
}
};

Expand All @@ -341,10 +341,14 @@
* @param {string} lang
*/
var languageError = function (lang) {
if (lang_data[lang] && lang_data[lang].error_callbacks && lang_data[lang].error_callbacks.length) {
lang_data[lang].error_callbacks.forEach(function (f) {
f(lang);
});
if (lang_data[lang]) {
var callbacks = lang_data[lang].callbacks;
while (callbacks.length) {
var callback = callbacks.shift().error;
if (callback) {
callback();
}
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion plugins/autoloader/prism-autoloader.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b19f512

Please sign in to comment.