Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSONP Highlight: Refactored JSONP logic #3018

Merged
merged 1 commit into from Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
121 changes: 74 additions & 47 deletions plugins/jsonp-highlight/prism-jsonp-highlight.js
Expand Up @@ -126,6 +126,49 @@


var jsonpCallbackCounter = 0;
/**
* Makes a JSONP request.
*
* @param {string} src The URL of the resource to request.
* @param {string | undefined | null} callbackParameter The name of the callback parameter. If falsy, `"callback"`
* will be used.
* @param {(data: unknown) => void} onSuccess
* @param {(reason: "timeout" | "network") => void} onError
* @returns {void}
*/
function jsonp(src, callbackParameter, onSuccess, onError) {
var callbackName = 'prismjsonp' + jsonpCallbackCounter++;

var uri = document.createElement('a');
uri.href = src;
uri.href += (uri.search ? '&' : '?') + (callbackParameter || 'callback') + '=' + callbackName;

var script = document.createElement('script');
script.src = uri.href;
script.onerror = function () {
cleanup();
onError('network');
};

var timeoutId = setTimeout(function () {
cleanup();
onError('timeout');
}, Prism.plugins.jsonphighlight.timeout);

function cleanup() {
clearTimeout(timeoutId);
document.head.removeChild(script);
delete window[callbackName];
}

// the JSONP callback function
window[callbackName] = function (response) {
cleanup();
onSuccess(response);
};

document.head.appendChild(script);
}

var LOADING_MESSAGE = 'Loading…';
var MISSING_ADAPTER_MESSAGE = function (name) {
Expand Down Expand Up @@ -185,61 +228,45 @@
}
}

var callbackName = 'prismjsonp' + jsonpCallbackCounter++;

var uri = document.createElement('a');
var src = uri.href = pre.getAttribute('data-jsonp');
uri.href += (uri.search ? '&' : '?') + (pre.getAttribute('data-callback') || 'callback') + '=' + callbackName;


var timeout = setTimeout(function () {
// we could clean up window[cb], but if the request finally succeeds, keeping it around is a good thing

// mark as failed
pre.setAttribute(STATUS_ATTR, STATUS_FAILED);

code.textContent = TIMEOUT_MESSAGE(src);
}, Prism.plugins.jsonphighlight.timeout);

var src = pre.getAttribute('data-jsonp');

jsonp(
src,
pre.getAttribute('data-callback'),
function (response) {
// interpret the received data using the adapter(s)
var data = null;
if (adapter) {
data = adapter(response, pre);
} else {
for (var i = 0, l = adapters.length; i < l; i++) {
data = adapters[i].adapter(response, pre);
if (data !== null) {
break;
}
}
}

var script = document.createElement('script');
script.src = uri.href;
if (data === null) {
// mark as failed
pre.setAttribute(STATUS_ATTR, STATUS_FAILED);

// the JSONP callback function
window[callbackName] = function (response) {
// clean up
document.head.removeChild(script);
clearTimeout(timeout);
delete window[callbackName];
code.textContent = UNKNOWN_FAILURE_MESSAGE;
} else {
// mark as loaded
pre.setAttribute(STATUS_ATTR, STATUS_LOADED);

// interpret the received data using the adapter(s)
var data = null;
if (adapter) {
data = adapter(response, pre);
} else {
for (var i = 0, l = adapters.length; i < l; i++) {
data = adapters[i].adapter(response, pre);
if (data !== null) {
break;
}
code.textContent = data;
Prism.highlightElement(code);
}
}

if (data === null) {
},
function () {
// mark as failed
pre.setAttribute(STATUS_ATTR, STATUS_FAILED);

code.textContent = UNKNOWN_FAILURE_MESSAGE;
} else {
// mark as loaded
pre.setAttribute(STATUS_ATTR, STATUS_LOADED);

code.textContent = data;
Prism.highlightElement(code);
code.textContent = TIMEOUT_MESSAGE(src);
}
};

document.head.appendChild(script);
);
}
});

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

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