Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adjusting component load strategies for speed and (IE) compatibility.

  • Loading branch information...
commit 7418ec9dcb7207fd4fb9a8e944811fee59f3e40f 1 parent 101d9c3
@joseph authored
Showing with 25 additions and 44 deletions.
  1. +24 −43 src/core/component.js
  2. +1 −1  test/componentry/index.html
View
67 src/core/component.js
@@ -101,37 +101,33 @@ Monocle.Component = function (book, id, index, chapters, source) {
// loadFrameFromJavaScript directly.
//
function loadFrameFromHTML(src, frame, callback) {
- // Compress whitespace.
- src = src.replace(/\n/g, '\\n').replace(/\r/, '\\r');
-
- // Escape single-quotes.
- src = src.replace(/\'/g, '\\\'');
-
- // Remove scripts. (DISABLED -- Monocle should leave this to implementers.)
- //var scriptFragment = "<script[^>]*>([\\S\\s]*?)<\/script>";
- //src = src.replace(new RegExp(scriptFragment, 'img'), '');
-
- // BROWSERHACK: Gecko chokes on the DOCTYPE declaration.
- if (Monocle.Browser.is.Gecko) {
- var doctypeFragment = "<!DOCTYPE[^>]*>";
- src = src.replace(new RegExp(doctypeFragment, 'm'), '');
+ var fn = function () {
+ Monocle.Events.deafen(frame, 'load', fn);
+ Monocle.defer(callback);
}
+ Monocle.Events.listen(frame, 'load', fn);
+
+ // Load the component into the iframe using document.write().
+ frame.contentDocument.open('text/html', 'replace');
+ frame.contentDocument.write(src);
+ frame.contentDocument.close();
- loadFrameFromJavaScript(src, frame, callback);
+ // ALTERNATIVE: load the component into the iframe with a JS URL.
+ // frame.contentWindow['monCmptData'] = src;
+ // src = "javascript:window['monCmptData'];"
+ // frame.src = src;
}
// LOAD STRATEGY: JAVASCRIPT
// Like the HTML strategy, but assumes that the src string is already clean.
//
+ // DEPRECATED: HTML strategy is faster now, so this just unescapes the string
+ // and loads it as HTML.
+ //
function loadFrameFromJavaScript(src, frame, callback) {
- src = "javascript:'"+src+"';";
- var fn = function () {
- Monocle.Events.deafen(frame, 'load', fn);
- Monocle.defer(callback);
- }
- Monocle.Events.listen(frame, 'load', fn);
- frame.src = src;
+ console.deprecation("Use { 'html': src } -- no need to clean the string.");
+ loadFrameFromHTML(eval("'"+src+"'"), frame, callback);
}
@@ -160,30 +156,15 @@ Monocle.Component = function (book, id, index, chapters, source) {
// Loads the array of DOM nodes into the body of the frame (replacing all
// existing nodes), then invokes the callback.
//
+ // DEPRECATED: now just transforms to HTML and loads as that.
+ //
function loadFrameFromNodes(nodes, frame, callback) {
- var destDoc = frame.contentDocument;
- destDoc.documentElement.innerHTML = "";
- var destHd = destDoc.createElement("head");
- var destBdy = destDoc.createElement("body");
-
+ console.deprecation("Use { 'html': node.outerHTML } or similar.");
+ var src = "";
for (var i = 0; i < nodes.length; ++i) {
- var node = destDoc.importNode(nodes[i], true);
- destBdy.appendChild(node);
+ src += nodes[i].outerHTML || '';
}
-
- var oldHead = destDoc.getElementsByTagName('head')[0];
- if (oldHead) {
- destDoc.documentElement.replaceChild(destHd, oldHead);
- } else {
- destDoc.documentElement.appendChild(destHd);
- }
- if (destDoc.body) {
- destDoc.documentElement.replaceChild(destBdy, destDoc.body);
- } else {
- destDoc.documentElement.appendChild(destBdy);
- }
-
- if (callback) { callback(); }
+ loadFrameFromHTML(src, frame, callback);
}
View
2  test/componentry/index.html
@@ -65,7 +65,7 @@
return { doc: this.frameFor(cmptId).contentDocument };
});
var bookData_NODES = basicBookData(function (cmptId, callback) {
- return { nodes: this.frameFor(cmptId).contentDocument.body.childNodes };
+ return { nodes: [this.frameFor(cmptId).contentDocument.documentElement] };
});
Please sign in to comment.
Something went wrong with that request. Please try again.