Skip to content

Commit ab21d4c

Browse files
committed
shift DOM parsing to readAsXML to allow for customizability in fetcher
1 parent 602fc3a commit ab21d4c

File tree

4 files changed

+9
-15
lines changed

4 files changed

+9
-15
lines changed

navigator/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@readium/navigator",
3-
"version": "2.3.0",
3+
"version": "2.4.0-alpha.8",
44
"type": "module",
55
"description": "Next generation SDK for publications in Web Apps",
66
"author": "readium",

navigator/src/epub/frame/FrameBlobBuilder.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,8 @@ export default class FrameBlobBuilder {
7676

7777
// Load the HTML resource
7878
const link = await this.currentResource.link();
79-
const txt = await this.currentResource.readAsString();
80-
if(!txt) throw new Error(`Failed reading item ${link.href}`);
81-
82-
const doc = new DOMParser().parseFromString(
83-
txt,
84-
link.mediaType.string as DOMParserSupportedType
85-
);
86-
79+
const doc = await this.currentResource.readAsXML() as HTMLDocument;
80+
if(!doc) throw new Error(`Failed reading item ${link.href}`);
8781
const perror = doc.querySelector("parsererror");
8882
if (perror) {
8983
const details = perror.querySelector("div");

shared/src/fetcher/HttpFetcher.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,15 @@ export class HttpResource implements Resource {
108108
return await resp.text();
109109
}
110110

111-
async readAsXML(): Promise<XMLDocument> {
111+
async readAsXML(): Promise<Document> {
112112
const resp = await this.client(this.url);
113113
if (!resp.ok)
114114
throw new Error(
115115
`http GET request for ${this.url} failed with HTTP status code ${resp.status}`
116116
); // TODO
117117
return new DOMParser().parseFromString(
118118
await resp.text(),
119-
'application/xml'
119+
this._link.mediaType.isHTML ? (this._link.mediaType.string as 'application/xhtml+xml' | 'text/html') : 'text/xml'
120120
);
121121
}
122122
}

shared/src/fetcher/Resource.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ export abstract class Resource {
2727
return JSON.parse(str);
2828
});
2929
}
30-
readAsXML(): Promise<XMLDocument | undefined> {
31-
return this.readAsString().then(str => {
30+
readAsXML(): Promise<Document | undefined> {
31+
return this.link().then(l => this.readAsString().then(str => {
3232
if (str === undefined) return str;
33-
return new DOMParser().parseFromString(str, 'text/xml');
34-
});
33+
return new DOMParser().parseFromString(str, l.mediaType.isHTML ? (l.mediaType.string as 'application/xhtml+xml' | 'text/html') : 'text/xml');
34+
}));
3535
}
3636
abstract close(): void;
3737
}

0 commit comments

Comments
 (0)