Skip to content

Commit

Permalink
DOMParser wrapper (utility function) to handle XML version="1.1" failure
Browse files Browse the repository at this point in the history
in some browsers, see
readium/readium-js-viewer#467
  • Loading branch information
danielweck committed Feb 4, 2016
1 parent ce092f4 commit 50fecf0
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 91 deletions.
23 changes: 13 additions & 10 deletions dev/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
var check = function(cfiParser, cfiInterpreter, cfiInstructions, cfiRuntimeErrors, cfiGenerator) {
var check = function(cfiParser, cfiInterpreter, cfiInstructions, cfiRuntimeErrors, cfiGenerator, xmlParse) {

$(document).ready(function () {

console.log(window.EPUBcfi);

function checkAPI(obj, globalName) {
function checkAPI(obj, globalName, anchor) {

if (obj && obj === window.EPUBcfi[globalName]) {
console.log("OKAY => EPUBcfi." + globalName);
if (!anchor) anchor = window.EPUBcfi;

if (obj && obj === anchor[globalName]) {
console.log("OKAY => " + globalName);
} else {
console.log("ERROR! => EPUBcfi." + globalName);
console.log("ERROR! => " + globalName);
}
}

Expand All @@ -21,7 +23,7 @@ var check = function(cfiParser, cfiInterpreter, cfiInstructions, cfiRuntimeError
checkAPI(cfiRuntimeErrors.OutOfRangeError, "OutOfRangeError");
checkAPI(cfiRuntimeErrors.TerminusError, "TerminusError");
checkAPI(cfiRuntimeErrors.CFIAssertionError, "CFIAssertionError");

checkAPI(xmlParse, "XmlParse", window);
});
};

Expand All @@ -33,10 +35,10 @@ if (typeof define == 'function' && typeof define.amd == 'object') {
require(["readium_cfi_js/cfi_API"], function () {

// to access individual feature APIs, via dependency injection (not the global window-attached objects)
require(['jquery', 'readium_cfi_js/cfi_parser', 'readium_cfi_js/cfi_interpreter', 'readium_cfi_js/cfi_instructions', 'readium_cfi_js/cfi_runtime_errors', 'readium_cfi_js/cfi_generator'],
function ($, cfiParser, cfiInterpreter, cfiInstructions, cfiRuntimeErrors, cfiGenerator) {
require(['jquery', 'readium_cfi_js/cfi_parser', 'readium_cfi_js/cfi_interpreter', 'readium_cfi_js/cfi_instructions', 'readium_cfi_js/cfi_runtime_errors', 'readium_cfi_js/cfi_generator', 'readium_cfi_js/XmlParse'],
function ($, cfiParser, cfiInterpreter, cfiInstructions, cfiRuntimeErrors, cfiGenerator, xmlParse) {

check(cfiParser, cfiInterpreter, cfiInstructions, cfiRuntimeErrors, cfiGenerator);
check(cfiParser, cfiInterpreter, cfiInstructions, cfiRuntimeErrors, cfiGenerator, xmlParse);
});
});

Expand All @@ -57,5 +59,6 @@ if (typeof define == 'function' && typeof define.amd == 'object') {
TerminusError: window.EPUBcfi.TerminusError,
CFIAssertionError: window.EPUBcfi.CFIAssertionError
},
window.EPUBcfi.Generator);
window.EPUBcfi.Generator,
window.XmlParse);
}
2 changes: 2 additions & 0 deletions dev/index_NO-RequireJS.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

<script type="text/javascript" src="../js/cfi_API.js"> </script>

<script type="text/javascript" src="../js/XmlParse.js"> </script>

<script type="text/javascript" src="index.js"> </script>

</head>
Expand Down
2 changes: 2 additions & 0 deletions dev/index_NO-RequireJS_TESTS.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

<script type="text/javascript" src="../js/cfi_API.js"> </script>

<script type="text/javascript" src="../js/XmlParse.js"> </script>

<script type="text/javascript" src="index.js"> </script>

</head>
Expand Down
72 changes: 72 additions & 0 deletions js/XmlParse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// LauncherOSX
//
// Created by Boris Schneiderman.
// Copyright (c) 2014 Readium Foundation and/or its licensees. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation and/or
// other materials provided with the distribution.
// 3. Neither the name of the organization nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.

(function(global) {


var init = function() {
var XmlParse = {};

XmlParse.fromString = function(str, contentType) {

if (!contentType) contentType = "text/xml"

if (str && (str.indexOf('version="1.1"') > 0)) {

console.warn("Replacing XML v1.1 with v1.0 (web browser compatibility).");

console.log(str.substr(0, 50));

str = str.replace(/(<\?xml[\s\S]+?)version="1.1"([\s\S]+?\?>)/, '$1version="1.0"$2');

console.log(str.substr(0, 50));
}

var parser = new window.DOMParser;
return parser.parseFromString(str, contentType);
};

global.XmlParse = XmlParse;
return XmlParse;
};


if (typeof define == 'function' && typeof define.amd == 'object') {
console.log("RequireJS ... XmlParse");

define([],
function () {
return init();
});
} else {
console.log("!RequireJS ... XmlParse");

//global.XmlParse =
init();
}

})(typeof window !== "undefined" ? window : this);
10 changes: 5 additions & 5 deletions js/cfi_API.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

(function(global) {

var init = function(cfiParser, cfiInterpreter, cfiInstructions, cfiRuntimeErrors, cfiGenerator) {
var init = function(cfiParser, cfiInterpreter, cfiInstructions, cfiRuntimeErrors, cfiGenerator, xmlParse) {

if (typeof cfiParser === "undefined") {
throw new Error("UNDEFINED?! cfiParser");
Expand Down Expand Up @@ -122,10 +122,10 @@ var init = function(cfiParser, cfiInterpreter, cfiInstructions, cfiRuntimeErrors
if (typeof define == 'function' && typeof define.amd == 'object') {
console.log("RequireJS ... cfi_API");

define(['readium_cfi_js/cfi_parser', './cfi_interpreter', './cfi_instructions', './cfi_runtime_errors', './cfi_generator'],
function (cfiParser, cfiInterpreter, cfiInstructions, cfiRuntimeErrors, cfiGenerator) {
define(['readium_cfi_js/cfi_parser', './cfi_interpreter', './cfi_instructions', './cfi_runtime_errors', './cfi_generator', './XmlParse'],
function (cfiParser, cfiInterpreter, cfiInstructions, cfiRuntimeErrors, cfiGenerator, xmlParse) {

return init(cfiParser, cfiInterpreter, cfiInstructions, cfiRuntimeErrors, cfiGenerator);
return init(cfiParser, cfiInterpreter, cfiInstructions, cfiRuntimeErrors, cfiGenerator, xmlParse);
});
} else {
console.log("!RequireJS ... cfi_API");
Expand All @@ -143,7 +143,7 @@ if (typeof define == 'function' && typeof define.amd == 'object') {
TerminusError: global.EPUBcfi.TerminusError,
CFIAssertionError: global.EPUBcfi.CFIAssertionError
},
global.EPUBcfi.Generator);
global.EPUBcfi.Generator, global.XmlParse);
}

})(typeof window !== "undefined" ? window : this);
Loading

0 comments on commit 50fecf0

Please sign in to comment.