describe("Document to SVG conversion", function () {
"use strict";
describe("getSvgForDocument", function () {
var defaultZoomLevel = 1;
var aRenderSize = function (
width,
height,
viewportWidth,
viewportHeight,
left,
top,
) {
return {
left: left || 0,
top: top || 0,
width: width || 123,
height: height || 456,
viewportWidth: viewportWidth || width || 123,
viewportHeight: viewportHeight || height || 456,
rootFontSize: "123px",
};
};
var aRenderSizeWithRootFontSize = function (rootFontSize) {
var size = aRenderSize();
size.rootFontSize = rootFontSize;
return size;
};
var sandbox;
beforeEach(function () {
sandbox = document.createElement("div");
document.body.appendChild(sandbox);
spyOn(documentHelper, "rewriteTagNameSelectorsToLowerCase");
});
afterEach(function () {
document.body.removeChild(sandbox);
});
it("should return a SVG with embeded HTML", function () {
var doc = document.implementation.createHTMLDocument("");
doc.body.innerHTML = "Test content";
var svgCode = document2svg.getSvgForDocument(
doc.documentElement,
aRenderSize(),
defaultZoomLevel,
);
expect(svgCode).toMatch(
new RegExp(
'",
),
);
});
it("should return a SVG with embedded image", function () {
var doc = document.implementation.createHTMLDocument(""),
canonicalXML;
doc.body.innerHTML =
'';
var svgCode = document2svg.getSvgForDocument(
doc.documentElement,
aRenderSize(),
defaultZoomLevel,
);
expect(svgCode).not.toBeNull();
canonicalXML = svgCode.replace(/ +\/>/, "/>");
expect(canonicalXML).toMatch(
new RegExp(
'",
),
);
});
it("should return a SVG with the given size", function () {
var doc = document.implementation.createHTMLDocument("");
doc.body.innerHTML = "content";
var svgCode = document2svg.getSvgForDocument(
doc.documentElement,
aRenderSize(123, 987, 200, 1000, 2, 7),
defaultZoomLevel,
);
expect(svgCode).toMatch(
new RegExp(
'",
),
);
});
it("should zoom by the given factor", function () {
var doc = document.implementation.createHTMLDocument("");
doc.body.innerHTML = "content";
var zoomFactor = 10;
var svgCode = document2svg.getSvgForDocument(
doc.documentElement,
aRenderSize(123, 987, 12, 99),
zoomFactor,
);
expect(svgCode).toMatch(
new RegExp(
'",
),
);
});
it("should ignore zoom factor 0", function () {
var doc = document.implementation.createHTMLDocument("");
doc.body.innerHTML = "content";
var zoomLevel = 0;
var svgCode = document2svg.getSvgForDocument(
doc.documentElement,
aRenderSize(123, 987),
zoomLevel,
);
expect(svgCode).not.toMatch(new RegExp("scale"));
});
it("should return a SVG with a root font size to preserve rem units", function () {
var doc = document.implementation.createHTMLDocument("");
doc.body.innerHTML = "Test content";
var svgCode = document2svg.getSvgForDocument(
doc.documentElement,
aRenderSizeWithRootFontSize("42px"),
defaultZoomLevel,
);
expect(svgCode).toMatch(
new RegExp(
'",
),
);
});
it("should raise an error on invalid source", function () {
var doc = document.implementation.createHTMLDocument("");
doc.body.innerHTML = "content";
var error = new Error();
spyOn(browser, "validateXHTML").and.throwError(error);
expect(function () {
document2svg.getSvgForDocument(
doc.documentElement,
aRenderSize(),
1,
);
}).toThrow(error);
});
it("should work around collapsing margins in Chrome & Safari", function () {
// Bottom margin that would trigger a collapsing margin with the following SVG
var topChild = document.createElement("div");
topChild.style.marginBottom = "200px";
topChild.innerHTML = "text";
sandbox.appendChild(topChild);
var doc = document.implementation.createHTMLDocument("");
// Margin top will inside the SVG will collapse
doc.body.innerHTML =
'