Skip to content
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
6 changes: 0 additions & 6 deletions tests/spec/core/highlight-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ import {
describe("Core — Highlight", () => {
afterAll(flushIframes);

it("highlights remote languages not bundled by default with ReSpec", async () => {
const doc = await makeRSDoc({}, "spec/core/highlight.html");
const span = doc.querySelector("code.testlang span[class*=hljs]");
expect(span.textContent).toBe("funkyFunction");
});

it("shouldn't highlight idl blocks", async () => {
const body = `
<section><pre class=idl>
Expand Down
66 changes: 0 additions & 66 deletions tests/spec/core/highlight.html

This file was deleted.

14 changes: 0 additions & 14 deletions tests/support-files/hljs-testlang.js

This file was deleted.

50 changes: 11 additions & 39 deletions worker/respec-worker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ReSpec Worker v1.0.0
// ReSpec Worker
"use strict";
// hljs is either inlined by core/worker.js (preferred) or loaded below via
// importScripts as a fallback when the inline fetch was not possible.
Expand All @@ -10,44 +10,16 @@ if (typeof self.hljs === "undefined" && self.RESPEC_HIGHLIGHT_URL) {
}
}

self.addEventListener("message", ({ data: originalData }) => {
const data = Object.assign({}, originalData);
switch (data.action) {
case "highlight-load-lang": {
const { langScript, propName, lang } = data;
try {
if (!langScript) {
throw new Error(`No script content provided for language "${lang}"`);
}
// importScripts() from blob workers is blocked for cross-origin URLs
// (blob worker origin is "null"). Create a same-origin blob URL from
// the pre-fetched script content to load the language safely.
const blob = new Blob([langScript], { type: "application/javascript" });
const objectURL = URL.createObjectURL(blob);
try {
importScripts(objectURL);
} finally {
URL.revokeObjectURL(objectURL);
}
self.hljs.registerLanguage(lang, self[propName]);
} catch (err) {
console.error("Failed to load or register language", lang, err);
}
break;
}
case "highlight": {
const { code } = data;
const langs = data.languages.length ? data.languages : undefined;
try {
const { value, language } = self.hljs.highlightAuto(code, langs);
Object.assign(data, { value, language });
} catch (err) {
console.error("Could not transform some code?", err);
// Post back the original code
Object.assign(data, { value: code, language: "" });
}
break;
}
self.addEventListener("message", ({ data }) => {
if (data.action !== "highlight") return;
const { code } = data;
const langs = data.languages?.length ? data.languages : undefined;
try {
const { value, language } = self.hljs.highlightAuto(code, langs);
Object.assign(data, { value, language });
} catch (err) {
console.error("Could not transform some code?", err);
Object.assign(data, { value: code, language: "" });
}
self.postMessage(data);
});
Loading