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

Introduce Prism.js for code highlighting #2530

Merged
merged 3 commits into from
Nov 10, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ www/assets/js/catchpoint-rum.js
www/assets/js/dygraph-combined.js
www/assets/js/codeflask.module.js
www/assets/js/jquery.js
www/assets/js/prism.js
4 changes: 4 additions & 0 deletions www/assets/css/pagestyle2.css
Original file line number Diff line number Diff line change
Expand Up @@ -8542,3 +8542,7 @@ wpt-header .wptheader_nav_menu_section a.banner_lfwp:hover {
.table-cell-error {
background: red;
}
/* Prism.js overwrite */
pre[class*="language-"] {
background: none !important;
}
3 changes: 3 additions & 0 deletions www/assets/css/vendor/prism.css

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

8 changes: 8 additions & 0 deletions www/assets/js/prism.js

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions www/assets/js/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,21 @@ async function initCodeField(source, language = "js") {
codeArea.onUpdate((code) => (hidden.value = code));
window.codeFlasks[source] = codeArea;
}

async function loadPrism() {
if (window.Prism) {
return window.Prism;
}
window.Prism = window.Prism || {};
window.Prism.manual = true;

const ss = document.createElement("link");
ss.rel = "stylesheet";
ss.type = "text/css";
ss.href = "/assets/css/vendor/prism.css";
document.head.appendChild(ss);

// todo: find a Prism distro that is a proper module
await import("/assets/js/prism.js");
return window.Prism;
}
15 changes: 10 additions & 5 deletions www/assets/js/waterfall.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function htmlEncode(value) {
}
}

function SelectRequest(step, request) {
async function SelectRequest(step, request) {
InitRequestDialog(step);
var stepLabel = "step" + step;
$("#request-dialog-" + stepLabel).css(
Expand Down Expand Up @@ -376,12 +376,12 @@ function SelectRequest(step, request) {
if (r["headers"] !== undefined) {
if (r.headers["request"] !== undefined) {
for (i = 0; i < r.headers.request.length; i++) {
requestHeaders += htmlEncode(r.headers.request[i]) + "<br>";
requestHeaders += htmlEncode(r.headers.request[i]) + "\n";
}
}
if (r.headers["response"] !== undefined) {
for (i = 0; i < r.headers.response.length; i++) {
responseHeaders += htmlEncode(r.headers.response[i]) + "<br>";
responseHeaders += htmlEncode(r.headers.response[i]) + "\n";
}
}
}
Expand Down Expand Up @@ -433,8 +433,8 @@ function SelectRequest(step, request) {
}
}
$("#request-details-" + stepLabel).html(details);
$("#request-headers-" + stepLabel).html(requestHeaders);
$("#response-headers-" + stepLabel).html(responseHeaders);
$("#request-headers-code-" + stepLabel).text(requestHeaders);
$("#response-headers-code-" + stepLabel).text(responseHeaders);
if (document.getElementById("urlEntry")) {
//only do requestBlocking if on a page where we can resubmit the test
$("#blocking-" + stepLabel).html(blocking + "</ul>");
Expand All @@ -448,6 +448,11 @@ function SelectRequest(step, request) {
}

$("#request-raw-details-json-" + stepLabel).text(json);
const Prism = await loadPrism();
Prism.highlightAllUnder(
document.querySelector("#dialog-contents-" + stepLabel)
);

$("#request-dialog-" + stepLabel).jqmShow();

// highlight the selected request
Expand Down
6 changes: 3 additions & 3 deletions www/waterfall.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1753,9 +1753,9 @@ function CreateWaterfallHtml($url, $requests, $id, $run, $cached, $page_data, $w
<div class="jqmdBC">
<div id="dialog-contents$stepSuffix" class="jqmdMSG">
<div id="request-details$stepSuffix" class="dialog-tab-content request-details"></div>
<div id="request-headers$stepSuffix" class="dialog-tab-content request-headers"></div>
<div id="response-headers$stepSuffix" class="dialog-tab-content response-headers"></div>
<div id="request-raw-details$stepSuffix" class="dialog-tab-content request-raw-details"><pre id="request-raw-details-json$stepSuffix"></pre></div>
<div id="request-headers$stepSuffix" class="dialog-tab-content request-headers"><pre><code class="language-http" id="request-headers-code$stepSuffix"></code></pre></div>
<div id="response-headers$stepSuffix" class="dialog-tab-content response-headers"><pre><code class="language-http" id="response-headers-code$stepSuffix"></code></pre></div>
<div id="request-raw-details$stepSuffix" class="dialog-tab-content request-raw-details"><pre><code class="language-js" id="request-raw-details-json$stepSuffix"></code></pre></div>
<div id="response-body$stepSuffix" class="dialog-tab-content response-body"></div>
<div id="blocking$stepSuffix" class="dialog-tab-content request-blocking"></div>
</div>
Expand Down