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

iframe loader improvement #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
90 changes: 47 additions & 43 deletions src/navigator/iframe-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ export class IFrameLoader implements IContentLoader {
private publicationURI?: string;

private isIE: boolean;

private useSrcdoc: boolean = false;

private readiumCssBasePath?: string;
private loaderEvents: { [eventName: string]: Function[] } = {};
private injectableResources: Resource[];
private injectableResources: Resource[] = [];
private readiumCssResources: Resource[];

private loaderConfig: IIframeLoaderConfig;

Expand All @@ -34,6 +36,10 @@ export class IFrameLoader implements IContentLoader {
this.readiumCssBasePath = path;
}

public enableUseSrcdoc(): void {
this.useSrcdoc = true;
}

public addContentLoadedListener(listener: Function): void {
const eventName = 'iframeLoaded';
this.addListener(eventName, listener);
Expand Down Expand Up @@ -115,12 +121,13 @@ export class IFrameLoader implements IContentLoader {
}

this.injectBaseHref(doc, headElement, href);
let allResources = this.injectableResources;
if (config.useReadiumCss === true) {
const useOverride = config.useReadiumCssOverride === true;
this.injectReadiumCss(headElement, useOverride);
this.injectReadiumCss(config.useReadiumCssOverride === true);
allResources = this.injectableResources.concat(this.readiumCssResources);
}

applyResourcesToDocument(this.injectableResources, doc);
applyResourcesToDocument(allResources, doc);

if (contentType.includes('xml')) {
return new XMLSerializer().serializeToString(doc);
Expand All @@ -140,49 +147,42 @@ export class IFrameLoader implements IContentLoader {
headEle.insertBefore(baseElement, headEle.firstChild);
}

private injectReadiumCss(headEle: HTMLHeadElement, useOverride: boolean): void {
private injectReadiumCss(useOverride: boolean): void {
this.readiumCssResources = [];

if (!this.readiumCssBasePath) {
return;
}
const beforeCss = this.creatCssLink(`${this.readiumCssBasePath}/ReadiumCSS-before.css`);
const defaultCss = this.creatCssLink(`${this.readiumCssBasePath}/ReadiumCSS-default.css`);
const afterCss = this.creatCssLink(`${this.readiumCssBasePath}/ReadiumCSS-after.css`);

// Need to insert before any node except <base>
let refNode: Node | null = null;
if (headEle.firstChild) {
// firstChild should be <base>
refNode = headEle.firstChild.nextSibling;
}

headEle.insertBefore(beforeCss, refNode);
headEle.insertBefore(defaultCss, refNode);
headEle.appendChild(afterCss);
this.readiumCssResources.push({
href: `${this.readiumCssBasePath}/ReadiumCSS-before.css`,
type: 'text/css',
target: 'head',
insertion: 'append',
});

if (useOverride) {
const overrideCss = this.creatCssLink(`${this.readiumCssBasePath}/ReadiumCSS-override.css`);
headEle.insertBefore(overrideCss, refNode);
}
}
this.readiumCssResources.push({
href: `${this.readiumCssBasePath}/ReadiumCSS-default.css`,
type: 'text/css',
target: 'head',
insertion: 'append',
});

private creatCssLink(href: string): HTMLLinkElement {
const cssLink = document.createElement('link');
cssLink.rel = 'stylesheet';
cssLink.type = 'text/css';
cssLink.href = href;
this.readiumCssResources.push({
href: `${this.readiumCssBasePath}/ReadiumCSS-after.css`,
type: 'text/css',
target: 'head',
insertion: 'append',
});

return cssLink;
if (useOverride) {
this.readiumCssResources.push({
href: `${this.readiumCssBasePath}/ReadiumCSS-override.css`,
type: 'text/css',
target: 'head',
insertion: 'append',
});
}

private createJSElement(href: string): HTMLScriptElement {
const el = document.createElement('script');
el.setAttribute('type', 'text/javascript');

const blob = new Blob([href], { type: 'application/javascript' });
const url = window.URL.createObjectURL(blob);
el.setAttribute('src', url);

return el;
}

private iframeLoaded(iframe: HTMLIFrameElement): void {
Expand Down Expand Up @@ -212,10 +212,12 @@ export class IFrameLoader implements IContentLoader {
contentDocumentData,
contentType,
new URL(contentDocumentURI, iframe.baseURI || document.baseURI || location.href).href,
this.loaderConfig
this.loaderConfig,
);

if (!this.isIE) {
if (this.useSrcdoc) {
iframe.setAttribute('srcdoc', basedContentData);
} else if (!this.isIE) {
documentDataUri = window.URL.createObjectURL(
new Blob([basedContentData], { type: contentType }),
);
Expand Down Expand Up @@ -262,7 +264,9 @@ export class IFrameLoader implements IContentLoader {
this.iframeUnloaded(iframe);
});

if (!this.isIE) {
if (this.useSrcdoc) {
// intentionally blank
} else if (!this.isIE) {
iframe.setAttribute('src', documentDataUri);
} else if (iframe.contentWindow) {
iframe.contentWindow.document.close();
Expand Down
2 changes: 1 addition & 1 deletion test/specs/spine-item-view.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('SpineItemView', () => {
// wait for auto-resize
await sleep(100);
const pageSize = siv.getTotalSize(pageWidth);
assert.equal(pageSize, 285);
assert.equal(pageSize, 283);

const page4Size = siv4.getTotalSize(pageWidth);
assert.approximately(page4Size, 14844, 1);
Expand Down