From c85800cf1449ab99bad91e0bbb5ab36c1632f3c0 Mon Sep 17 00:00:00 2001 From: Sid Vishnoi <8426945+sidvishnoi@users.noreply.github.com> Date: Wed, 24 Mar 2021 17:04:59 +0530 Subject: [PATCH 1/8] feat(core/respec-global): add toHTML() method --- src/core/exporter.js | 2 +- src/core/respec-global.js | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) 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() { From 66185844d1d5ad3c4aab7e3c764e7b4dc7d5a577 Mon Sep 17 00:00:00 2001 From: Sid Vishnoi <8426945+sidvishnoi@users.noreply.github.com> Date: Wed, 24 Mar 2021 17:05:47 +0530 Subject: [PATCH 2/8] use document.respec.toHTML() in respecDocWriter --- tools/respecDocWriter.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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) { From ce840d0708a13da459453884f6da6bbdd94a2821 Mon Sep 17 00:00:00 2001 From: Sid Vishnoi <8426945+sidvishnoi@users.noreply.github.com> Date: Thu, 25 Mar 2021 14:08:33 +0530 Subject: [PATCH 3/8] Add test, fix toHTML not returning html with DOCTYPE tests help! --- src/core/respec-global.js | 2 +- tests/spec/core/respec-global-spec.js | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/core/respec-global.js b/src/core/respec-global.js index 07c8d16407..b06a68fe51 100644 --- a/src/core/respec-global.js +++ b/src/core/respec-global.js @@ -43,7 +43,7 @@ class ReSpec { } async toHTML() { - return serialize("html", document); + return `${serialize("html", document)}`; } } diff --git a/tests/spec/core/respec-global-spec.js b/tests/spec/core/respec-global-spec.js index 1559928800..c6e1d242ed 100644 --- a/tests/spec/core/respec-global-spec.js +++ b/tests/spec/core/respec-global-spec.js @@ -43,4 +43,15 @@ 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 exportedDoc = await doc.respec.toHTML(); + expect(typeof exportedDoc).toBe("string"); + expect(exportedDoc.slice(0, 50)).toMatch( + /^\s* Date: Thu, 25 Mar 2021 14:17:22 +0530 Subject: [PATCH 4/8] tested wrong thing --- src/core/respec-global.js | 2 +- tests/spec/core/respec-global-spec.js | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/core/respec-global.js b/src/core/respec-global.js index b06a68fe51..07c8d16407 100644 --- a/src/core/respec-global.js +++ b/src/core/respec-global.js @@ -43,7 +43,7 @@ class ReSpec { } async toHTML() { - return `${serialize("html", document)}`; + return serialize("html", document); } } diff --git a/tests/spec/core/respec-global-spec.js b/tests/spec/core/respec-global-spec.js index c6e1d242ed..08fbdbdb63 100644 --- a/tests/spec/core/respec-global-spec.js +++ b/tests/spec/core/respec-global-spec.js @@ -50,8 +50,7 @@ describe("Core — Respec Global - document.respec", () => { const exportedDoc = await doc.respec.toHTML(); expect(typeof exportedDoc).toBe("string"); - expect(exportedDoc.slice(0, 50)).toMatch( - /^\s* Date: Thu, 25 Mar 2021 14:21:29 +0530 Subject: [PATCH 5/8] use toHTML() in testing exported docs --- tests/spec/SpecHelper.js | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/tests/spec/SpecHelper.js b/tests/spec/SpecHelper.js index 850a24fcab..09eae987e9 100644 --- a/tests/spec/SpecHelper.js +++ b/tests/spec/SpecHelper.js @@ -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); }); From 0d201bbab95be8db53a26ff836f2357923df8f83 Mon Sep 17 00:00:00 2001 From: Sid Vishnoi <8426945+sidvishnoi@users.noreply.github.com> Date: Thu, 25 Mar 2021 14:33:46 +0530 Subject: [PATCH 6/8] fix test regex --- tests/spec/core/respec-global-spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/spec/core/respec-global-spec.js b/tests/spec/core/respec-global-spec.js index 08fbdbdb63..071b10ac5c 100644 --- a/tests/spec/core/respec-global-spec.js +++ b/tests/spec/core/respec-global-spec.js @@ -50,7 +50,7 @@ describe("Core — Respec Global - document.respec", () => { const exportedDoc = await doc.respec.toHTML(); expect(typeof exportedDoc).toBe("string"); - const regex = new RegExp(`^${doc.doctype || ""}\\s* Date: Thu, 25 Mar 2021 14:45:03 +0530 Subject: [PATCH 7/8] ensure test document has --- tests/spec/SpecHelper.js | 2 +- tests/spec/core/respec-global-spec.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/spec/SpecHelper.js b/tests/spec/SpecHelper.js index 09eae987e9..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); diff --git a/tests/spec/core/respec-global-spec.js b/tests/spec/core/respec-global-spec.js index 071b10ac5c..27ee9052b4 100644 --- a/tests/spec/core/respec-global-spec.js +++ b/tests/spec/core/respec-global-spec.js @@ -50,7 +50,8 @@ describe("Core — Respec Global - document.respec", () => { const exportedDoc = await doc.respec.toHTML(); expect(typeof exportedDoc).toBe("string"); - const regex = new RegExp(`^${doc.doctype || ""}\\s*\\s* Date: Thu, 25 Mar 2021 18:16:29 +0530 Subject: [PATCH 8/8] test for meta generator --- tests/spec/core/respec-global-spec.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/spec/core/respec-global-spec.js b/tests/spec/core/respec-global-spec.js index 27ee9052b4..585a154df7 100644 --- a/tests/spec/core/respec-global-spec.js +++ b/tests/spec/core/respec-global-spec.js @@ -48,10 +48,16 @@ describe("Core — Respec Global - document.respec", () => { const ops = makeStandardOps(); const doc = await makeRSDoc(ops); - const exportedDoc = await doc.respec.toHTML(); - expect(typeof exportedDoc).toBe("string"); + 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"); }); });