From e24631ecac0772fc51756fe4aff9638de3b95faf Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Fri, 2 Mar 2012 14:11:08 -0800 Subject: Fixing CSS URL issues Only partially, supporting unquoted files under same root. --- js/document/html-document.js | 24 +++++++++++++++--------- js/mediators/io-mediator.js | 25 ++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/js/document/html-document.js b/js/document/html-document.js index 9353027d..54647d65 100755 --- a/js/document/html-document.js +++ b/js/document/html-document.js @@ -485,14 +485,13 @@ 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, query, prefixUrl; + var fileUri, cssUrl, cssData, query, prefixUrl, fileCouldDirUrl; //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, '/'); + fileUri = (this.application.ninja.coreIoApi.cloudData.root+this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]+cssUrl).replace(/\/\//gi, '/'); //Loading the data from the file cssData = this.application.ninja.coreIoApi.readFile({uri: fileUri}); //Creating tag with file content @@ -508,10 +507,17 @@ exports.HTMLDocument = Montage.create(TextDocument, { tag.setAttribute(this._document.styleSheets[i].ownerNode.attributes[n].name, this._document.styleSheets[i].ownerNode.attributes[n].value); } } + // + + fileCouldDirUrl = this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]+cssUrl.split(cssUrl.split('/')[cssUrl.split('/').length-1])[0]).replace(/\/\//gi, '/')); + + //TODO: Fix regEx to have logic for all possible URLs strings - prefixUrl = '('+cssUrl.split(cssUrl.split('/')[cssUrl.split('/').length-1])[0]+'../'; - prefixUrl = prefixUrl.replace('(/', '('); + prefixUrl = '('+fileCouldDirUrl+'../'; 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) { @@ -525,14 +531,14 @@ 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) - tag = this.iframe.contentWindow.document.createElement('style'); + /* +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(); @@ -540,7 +546,6 @@ 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 @@ -555,6 +560,7 @@ 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 cc97db5e..64210a02 100644 --- a/js/mediators/io-mediator.js +++ b/js/mediators/io-mediator.js @@ -313,7 +313,7 @@ exports.IoMediator = Montage.create(Component, { docStyles[styleCounter].innerHTML = this.getCssFromRules(template.css[i].cssRules); styleCounter++; } - } else { + } else if (!template.css[i].ownerNode.getAttribute('data-ninja-template')){ //Checking for attributes to be added to tag upon saving for (var k in docLinks) { if (docLinks[k].getAttribute) { @@ -330,8 +330,27 @@ exports.IoMediator = Montage.create(Component, { } } } - var adjCss = this.getCssFromRules(template.css[i].cssRules), cssUrl = template.css[i].ownerNode.getAttribute('data-ninja-uri'); - //console.log((template.css[i].ownerNode.getAttribute('data-ninja-uri')));//cssUrl.split(cssUrl.split('/')[cssUrl.split('/').length-1])[0] + var local, regex, fileCouldDirUrl, adjCss = this.getCssFromRules(template.css[i].cssRules), cssUrl = template.css[i].ownerNode.getAttribute('data-ninja-file-url'); + //TODO: Assure logic for local directory + local = cssUrl.split(cssUrl.split('/')[cssUrl.split('/').length-2])[0] || cssUrl.split(cssUrl.split('/')[cssUrl.split('/').length-1])[0] || cssUrl.split(cssUrl.split('/')[0])[0]; + // + fileCouldDirUrl = this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]+local).replace(/\/\//gi, '/')); + // + regex = new RegExp(fileCouldDirUrl.replace(/\//gi, '\\\/'), 'gi'); + // + if (local.split('/').length > 1) { + adjCss = adjCss.replace(regex, '../'); + } else { + adjCss = adjCss.replace(regex, ''); + } + + + //console.log(adjCss); + //console.log(fileCouldDirUrl); + //return; + + + //Saving data from rules array converted to string into file var save = this.fio.saveFile({uri: template.css[i].ownerNode.getAttribute('data-ninja-uri'), contents: adjCss}); } -- cgit v1.2.3