From ff77f861fba349dd36d6c15c9a545459c3a76583 Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Fri, 2 Mar 2012 10:34:01 -0800 Subject: Fixing IO RegEx Fixed parsing files to have correct URLs with RegEx, inner content of CSS files still needs to be fixed. --- js/document/html-document.js | 22 ++++++----- js/mediators/io-mediator.js | 92 +++++++++++++++++++++++++++++--------------- 2 files changed, 74 insertions(+), 40 deletions(-) diff --git a/js/document/html-document.js b/js/document/html-document.js index 1691e3e4..9353027d 100755 --- a/js/document/html-document.js +++ b/js/document/html-document.js @@ -485,17 +485,18 @@ exports.HTMLDocument = Montage.create(TextDocument, { //If rules are null, assuming cross-origin issue if(this._document.styleSheets[i].rules === null) { //TODO: Revisit URLs and URI creation logic, very hack right now - var fileUri, cssUrl, cssData, tag, query; + var fileUri, cssUrl, cssData, query, prefixUrl; //TODO: Parse out relative URLs and map them to absolute if (this._document.styleSheets[i].href.indexOf(chrome.extension.getURL('')) !== -1) { //Getting the url of the CSS file cssUrl = this._document.styleSheets[i].href.split('js/document/templates/montage-html')[1];//TODO: Parse out relative URLs and map them to absolute //Creating the URI of the file (this is wrong should not be splitting cssUrl) fileUri = this.application.ninja.coreIoApi.cloudData.root+this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]+cssUrl; + fileUri = fileUri.replace(/\/\//gi, '/'); //Loading the data from the file cssData = this.application.ninja.coreIoApi.readFile({uri: fileUri}); //Creating tag with file content - tag = this.iframe.contentWindow.document.createElement('style'); + var tag = this.iframe.contentWindow.document.createElement('style'); tag.setAttribute('type', 'text/css'); tag.setAttribute('data-ninja-uri', fileUri); tag.setAttribute('data-ninja-file-url', cssUrl); @@ -503,13 +504,14 @@ exports.HTMLDocument = Montage.create(TextDocument, { tag.setAttribute('data-ninja-file-name', cssUrl.split('/')[cssUrl.split('/').length-1]); //Copying attributes to maintain same properties as the for (var n in this._document.styleSheets[i].ownerNode.attributes) { - if (this._document.styleSheets[i].ownerNode.attributes[n].value && this._document.styleSheets[i].ownerNode.attributes[n].name !== 'disabled') { + if (this._document.styleSheets[i].ownerNode.attributes[n].value && this._document.styleSheets[i].ownerNode.attributes[n].name !== 'disabled' && this._document.styleSheets[i].ownerNode.attributes[n].name !== 'disabled') { tag.setAttribute(this._document.styleSheets[i].ownerNode.attributes[n].name, this._document.styleSheets[i].ownerNode.attributes[n].value); } } - //TODO: Parse out relative URLs and map them to absolute - //console.log(cssData.content); - tag.innerHTML = cssData.content; + //TODO: Fix regEx to have logic for all possible URLs strings + prefixUrl = '('+cssUrl.split(cssUrl.split('/')[cssUrl.split('/').length-1])[0]+'../'; + prefixUrl = prefixUrl.replace('(/', '('); + tag.innerHTML = cssData.content.replace(/\(\.\.\//gi, prefixUrl); //Looping through DOM to insert style tag at location of link element query = this._templateDocument.html.querySelectorAll(['link']); for (var j in query) { @@ -522,15 +524,15 @@ exports.HTMLDocument = Montage.create(TextDocument, { } } else { console.log('ERROR: Cross-Domain-Stylesheet detected, unable to load in Ninja'); - /* -//None local stylesheet, probably on a CDN (locked) + //None local stylesheet, probably on a CDN (locked) tag = this.iframe.contentWindow.document.createElement('style'); tag.setAttribute('type', 'text/css'); tag.setAttribute('data-ninja-external-url', this._document.styleSheets[i].href); tag.setAttribute('data-ninja-file-read-only', "true"); tag.setAttribute('data-ninja-file-name', this._document.styleSheets[i].href.split('/')[this._document.styleSheets[i].href.split('/').length-1]); - //TODO: Figure out cross-domain XHR issue, might need cloud to handle + /* +//TODO: Figure out cross-domain XHR issue, might need cloud to handle var xhr = new XMLHttpRequest(); xhr.open("GET", this._document.styleSheets[i].href, true); xhr.send(); @@ -538,6 +540,7 @@ exports.HTMLDocument = Montage.create(TextDocument, { if (xhr.readyState === 4) { console.log(xhr); } +*/ //tag.innerHTML = xhr.responseText //xhr.response; //Currently no external styles will load if unable to load via XHR request @@ -552,7 +555,6 @@ exports.HTMLDocument = Montage.create(TextDocument, { this._templateDocument.head.insertBefore(tag, query[j]); } } -*/ } } } diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js index e763c67c..cc97db5e 100644 --- a/js/mediators/io-mediator.js +++ b/js/mediators/io-mediator.js @@ -41,9 +41,12 @@ exports.IoMediator = Montage.create(Component, { }, //////////////////////////////////////////////////////////////////// // - appTemplatesUrl: { + getAppTemplatesUrlRegEx: { enumerable: false, - value: new RegExp(chrome.extension.getURL('js/document/templates/montage-html/'), 'gi') + value: function () { + var regex = new RegExp(chrome.extension.getURL('js/document/templates/montage-html').replace(/\//gi, '\\\/'), 'gi'); + return regex; + } }, //////////////////////////////////////////////////////////////////// // @@ -211,36 +214,59 @@ exports.IoMediator = Montage.create(Component, { enumerable: false, value: function (template) { //Injecting head and body into old document - template.document.content.document.body.innerHTML = template.body; template.document.content.document.head.innerHTML = template.head; + template.document.content.document.body.innerHTML = template.body; //Getting all CSS (style or link) tags var styletags = template.document.content.document.getElementsByTagName('style'), - linktags = template.document.content.document.getElementsByTagName('link'); - //Looping through link tags and removing file recreated elements - for (var j in styletags) { - if (styletags[j].getAttribute) { - if(styletags[j].getAttribute('data-ninja-uri') !== null && !styletags[j].getAttribute('data-ninja-template')) {//TODO: Use querySelectorAll - try { - //Checking head first - template.document.content.document.head.removeChild(styletags[j]); - } catch (e) { - try { - //Checking body if not in head - template.document.content.document.body.removeChild(styletags[j]); - } catch (e) { - //Error, not found! - } + linktags = template.document.content.document.getElementsByTagName('link'), + toremovetags = []; + //Getting styles tags to be removed from document + if (styletags.length) { + for (var j=0; j only | css =