diff --git a/src/core/exporter.js b/src/core/exporter.js index 41140125b1..c039ffc0ac 100644 --- a/src/core/exporter.js +++ b/src/core/exporter.js @@ -36,7 +36,7 @@ export function rsDocToDataURL(mimeType, doc = document) { return `data:${mimeType};charset=utf-8,${encodedString}`; } -function serialize(format, doc) { +export function serialize(format, doc) { const cloneDoc = doc.cloneNode(true); cleanup(cloneDoc); let result = ""; diff --git a/src/core/respec-global.js b/src/core/respec-global.js index b5b18f5391..07c8d16407 100644 --- a/src/core/respec-global.js +++ b/src/core/respec-global.js @@ -8,6 +8,7 @@ * This module also adds the legacy `document.respecIsReady` property for * backward compatibility. It is now an alias to `document.respec.ready`. */ +import { serialize } from "../core/exporter.js"; import { showWarning } from "../core/utils.js"; import { sub } from "./pubsubhub.js"; @@ -40,6 +41,10 @@ class ReSpec { get ready() { return this._respecDonePromise; } + + async toHTML() { + return serialize("html", document); + } } export function init() { diff --git a/tests/spec/SpecHelper.js b/tests/spec/SpecHelper.js index 850a24fcab..7330f73be0 100644 --- a/tests/spec/SpecHelper.js +++ b/tests/spec/SpecHelper.js @@ -49,7 +49,7 @@ export function makeRSDoc(opts, src, style = "") { } else { const doc = document.implementation.createHTMLDocument(); decorateDocument(doc, opts); - ifr.srcdoc = doc.documentElement.outerHTML; + ifr.srcdoc = `${doc.documentElement.outerHTML}`; } // trigger load document.body.appendChild(ifr); @@ -62,19 +62,11 @@ export function makeRSDoc(opts, src, style = "") { * @returns {Promise} */ export async function getExportedDoc(doc) { - const dataURL = await new Promise(resolve => { - doc.defaultView.require(["core/exporter"], ({ rsDocToDataURL }) => - resolve(rsDocToDataURL("text/html", doc)) - ); - }); - + const exportedHTML = await doc.respec.toHTML(); return new Promise(resolve => { const ifr = document.createElement("iframe"); ifr.addEventListener("load", () => resolve(ifr.contentDocument)); - ifr.srcdoc = decodeURIComponent(dataURL).replace( - "data:text/html;charset=utf-8,", - "" - ); + ifr.srcdoc = exportedHTML; document.body.appendChild(ifr); iframes.push(ifr); }); diff --git a/tests/spec/core/respec-global-spec.js b/tests/spec/core/respec-global-spec.js index 1559928800..585a154df7 100644 --- a/tests/spec/core/respec-global-spec.js +++ b/tests/spec/core/respec-global-spec.js @@ -43,4 +43,21 @@ describe("Core — Respec Global - document.respec", () => { expect(doc.respec.errors).toHaveSize(0); }); + + it("returns exported html with toHTML()", async () => { + const ops = makeStandardOps(); + const doc = await makeRSDoc(ops); + + const html = await doc.respec.toHTML(); + expect(typeof html).toBe("string"); + + const regex = new RegExp(`^\\s* is added in exported docs only. + const generator = exportedDoc.querySelector("meta[name=generator]"); + expect(generator).toBeTruthy(); + expect(generator.content).toContain("ReSpec"); + }); }); diff --git a/tools/respecDocWriter.js b/tools/respecDocWriter.js index 00aff742c3..7ce7a3c8ca 100644 --- a/tools/respecDocWriter.js +++ b/tools/respecDocWriter.js @@ -240,7 +240,7 @@ async function evaluateHTML(version, timer) { }); }); return exportDocument("html", "text/html"); - } else { + } else if (!document.respec || !document.respec.toHTML) { const { rsDocToDataURL } = await new Promise((resolve, reject) => { require(["core/exporter"], resolve, err => { reject(new Error(err.message)); @@ -249,6 +249,8 @@ async function evaluateHTML(version, timer) { const dataURL = rsDocToDataURL("text/html"); const encodedString = dataURL.replace(/^data:\w+\/\w+;charset=utf-8,/, ""); return decodeURIComponent(encodedString); + } else { + return await document.respec.toHTML(); } function timeout(promise, ms) {