From b2de52888a7c976cc9ae40518e3653cec0aac296 Mon Sep 17 00:00:00 2001
From: Jose Antonio Marquez
Date: Sun, 4 Mar 2012 11:15:27 -0800
Subject: Improved CSS URL cleaned
Added logic to accommodate any URLs in a CSS file inside the opened document root. Need to add logic for handling linked references above the root. Also need to reformat into a reusable method as it might be needed besides CSS.
---
js/mediators/io-mediator.js | 66 ++++++++++++++++++++++++++++++++++-----------
1 file changed, 51 insertions(+), 15 deletions(-)
(limited to 'js/mediators')
diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js
index 0a851a4d..5917edba 100644
--- a/js/mediators/io-mediator.js
+++ b/js/mediators/io-mediator.js
@@ -330,29 +330,65 @@ exports.IoMediator = Montage.create(Component, {
}
}
}
- 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 > 2) {
- adjCss = adjCss.replace(regex, '../');
- } else {
- adjCss = adjCss.replace(regex, '');
+
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ ///////////////////////////////////////////////////////////////////////////////////////////
+
+
+ var cleanedCss, fileCouldDirUrl, pathDepth, pathToDocRoot = '../';
+ dirtyCss = this.getCssFromRules(template.css[i].cssRules),
+ fileUrl = template.css[i].ownerNode.getAttribute('data-ninja-file-url'),
+ fileRootUrl = this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]+fileUrl.split(fileUrl.split('/')[fileUrl.split('/').length-1])[0]).replace(/\/\//gi, '/')),
+ localPath = fileUrl.split(fileUrl.split('/')[fileUrl.split('/').length-2])[0] || fileUrl.split(fileUrl.split('/')[fileUrl.split('/').length-1])[0] || fileUrl.split(fileUrl.split('/')[0])[0],
+ documentRootURL = this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]));
+
+ pathDepth = Math.floor(localPath.split('/').length/2);
+
+ for (var p=0; p < pathDepth; p++) {
+ pathToDocRoot += '../';
+ }
+
+ fileCouldDirUrl = this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]+localPath).replace(/\/\//gi, '/'));
+
+ cleanedCss = dirtyCss.replace(/(\b(?:(?:https?|ftp|file|[A-Za-z]+):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$]))/gi, parseNinjaUrl.bind(this));
+
+ function parseNinjaUrl (url) {
+ //
+ if (url.indexOf(fileCouldDirUrl) !== -1 || url.indexOf(fileRootUrl) !== -1) {
+ //
+ if (pathDepth < 1 || url.indexOf(fileRootUrl) > -1) {
+ //
+ url = url.replace(new RegExp(fileRootUrl.replace(/\//gi, '\\\/'), 'gi'), '');
+ } else {
+ //
+ url = url.replace(new RegExp(fileCouldDirUrl.replace(/\//gi, '\\\/'), 'gi'), '../');
+ }
+ } else {
+ //
+ if (url.indexOf(documentRootURL) !== 1) {
+ url = url.replace(new RegExp(documentRootURL.replace(/\//gi, '\\\/'), 'gi'), pathToDocRoot);
+ } else if (url.indexOf(this.application.ninja.coreIoApi.rootUrl) !== 1) {
+ //TODO: Add logic for files above root document folder
+ }
+ }
+ //console.log("Returning: " + url);
+ //console.log("-----");
+ return url;
}
+ //console.log(cleanedCss);
+
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ ///////////////////////////////////////////////////////////////////////////////////////////
+
- //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});
+ var save = this.fio.saveFile({uri: template.css[i].ownerNode.getAttribute('data-ninja-uri'), contents: cleanedCss});
+ //TODO: Add error handling for saving files
}
}
}
--
cgit v1.2.3
From 56efed8b1ed9974aade615fce2d96bc214d21540 Mon Sep 17 00:00:00 2001
From: Jose Antonio Marquez
Date: Mon, 5 Mar 2012 15:55:30 -0800
Subject: Resolved URL path issues in document and CSS
Added logic to allow for files opened and assets linked in any order all under the cloud server root.
---
js/mediators/io-mediator.js | 63 +++++++++++++++++++++------------------------
1 file changed, 30 insertions(+), 33 deletions(-)
(limited to 'js/mediators')
diff --git a/js/mediators/io-mediator.js b/js/mediators/io-mediator.js
index 5917edba..c02710c2 100644
--- a/js/mediators/io-mediator.js
+++ b/js/mediators/io-mediator.js
@@ -213,9 +213,11 @@ exports.IoMediator = Montage.create(Component, {
parseNinjaTemplateToHtml: {
enumerable: false,
value: function (template) {
+ var regexRootUrl, rootUrl = this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]));
+ regexRootUrl = new RegExp(rootUrl.replace(/\//gi, '\\\/'), 'gi');
//Injecting head and body into old document
- template.document.content.document.head.innerHTML = template.head;
- template.document.content.document.body.innerHTML = template.body;
+ template.document.content.document.head.innerHTML = template.head.replace(regexRootUrl, '');
+ template.document.content.document.body.innerHTML = template.body.replace(regexRootUrl, '');
//Getting all CSS (style or link) tags
var styletags = template.document.content.document.getElementsByTagName('style'),
linktags = template.document.content.document.getElementsByTagName('link'),
@@ -248,7 +250,7 @@ exports.IoMediator = Montage.create(Component, {
for (var l in linktags) {
if (linktags[l].getAttribute && linktags[l].getAttribute('disabled')) {//TODO: Use querySelectorAll
for (var p=0; toremovetags[p]; p++) {
- if (toremovetags[p].getAttribute('data-ninja-file-url') === ('/'+linktags[l].getAttribute('href'))) {
+ if (toremovetags[p].getAttribute('href') === linktags[l].getAttribute('href')) {
if (!toremovetags[p].getAttribute('data-ninja-disabled')) {
linktags[l].removeAttribute('disabled');
}
@@ -335,55 +337,50 @@ exports.IoMediator = Montage.create(Component, {
///////////////////////////////////////////////////////////////////////////////////////////
- var cleanedCss, fileCouldDirUrl, pathDepth, pathToDocRoot = '../';
+ var cleanedCss,
dirtyCss = this.getCssFromRules(template.css[i].cssRules),
fileUrl = template.css[i].ownerNode.getAttribute('data-ninja-file-url'),
- fileRootUrl = this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]+fileUrl.split(fileUrl.split('/')[fileUrl.split('/').length-1])[0]).replace(/\/\//gi, '/')),
- localPath = fileUrl.split(fileUrl.split('/')[fileUrl.split('/').length-2])[0] || fileUrl.split(fileUrl.split('/')[fileUrl.split('/').length-1])[0] || fileUrl.split(fileUrl.split('/')[0])[0],
- documentRootURL = this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]));
-
- pathDepth = Math.floor(localPath.split('/').length/2);
-
- for (var p=0; p < pathDepth; p++) {
- pathToDocRoot += '../';
- }
-
- fileCouldDirUrl = this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]+localPath).replace(/\/\//gi, '/'));
-
- cleanedCss = dirtyCss.replace(/(\b(?:(?:https?|ftp|file|[A-Za-z]+):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$]))/gi, parseNinjaUrl.bind(this));
+ fileRootUrl = this.application.ninja.coreIoApi.rootUrl+fileUrl.split(fileUrl.split('/')[fileUrl.split('/').length-1])[0],
+ cleanedCss = dirtyCss.replace(/(\b(?:(?:https?|ftp|file|[A-Za-z]+):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$]))/gi, parseNinjaUrl.bind(this));
+
function parseNinjaUrl (url) {
+ //console.log("Getting: " + url);
//
- if (url.indexOf(fileCouldDirUrl) !== -1 || url.indexOf(fileRootUrl) !== -1) {
+ if (url.indexOf(fileRootUrl) !== -1) {
+ url = url.replace(new RegExp(fileRootUrl.replace(/\//gi, '\\\/'), 'gi'), '');
+ } else {
+ var assetsDirs = (url.replace(new RegExp((this.application.ninja.coreIoApi.rootUrl).replace(/\//gi, '\\\/'), 'gi'), '')).split('/');
+ var fileDirs = (fileUrl.split(fileUrl.split('/')[fileUrl.split('/').length-1])[0]).split('/');
+ var counter = 0;
+ var path = '';
+ var newURL = '';
+ //
+ for (var p=0; p < fileDirs.length-1; p++) {
+ if (fileDirs[p] === assetsDirs[p]) {
+ counter++;
+ }
+ }
//
- if (pathDepth < 1 || url.indexOf(fileRootUrl) > -1) {
- //
- url = url.replace(new RegExp(fileRootUrl.replace(/\//gi, '\\\/'), 'gi'), '');
- } else {
- //
- url = url.replace(new RegExp(fileCouldDirUrl.replace(/\//gi, '\\\/'), 'gi'), '../');
+ for (var p=0; p < (fileDirs.length-counter)-1; p++) {
+ path += '../';
}
- } else {
//
- if (url.indexOf(documentRootURL) !== 1) {
- url = url.replace(new RegExp(documentRootURL.replace(/\//gi, '\\\/'), 'gi'), pathToDocRoot);
- } else if (url.indexOf(this.application.ninja.coreIoApi.rootUrl) !== 1) {
- //TODO: Add logic for files above root document folder
+ for (var p=counter; p < assetsDirs.length; p++) {
+ newURL += '/'+assetsDirs[p];
}
+ //
+ url = (path+newURL).replace(/\/\//gi, '/');
}
//console.log("Returning: " + url);
//console.log("-----");
return url;
}
- //console.log(cleanedCss);
-
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
- //return;
-
//Saving data from rules array converted to string into file
--
cgit v1.2.3