From eebb7de4d19cddec9c763a073d8cf41d76fe70f7 Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Tue, 6 Mar 2012 17:02:37 -0800 Subject: Adding CDN support for URLs in linked CSS --- js/document/html-document.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'js/document') diff --git a/js/document/html-document.js b/js/document/html-document.js index 80930af2..1c5cec91 100755 --- a/js/document/html-document.js +++ b/js/document/html-document.js @@ -437,8 +437,8 @@ exports.HTMLDocument = Montage.create(TextDocument, { } else if (prop.indexOf('url') !== -1) { //From CSS property //TODO: Add functionality var docRootUrl = this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]).replace(/\/\//gi, '/')); - prop = prop.replace(/[^()\\""\\'']+/g, test); - function test (s) { + prop = prop.replace(/[^()\\""\\'']+/g, cssUrlToNinjaUrl); + function cssUrlToNinjaUrl (s) { if (s !== 'url') { s = docRootUrl + s; } @@ -570,8 +570,22 @@ exports.HTMLDocument = Montage.create(TextDocument, { } // fileCouldDirUrl = this._document.styleSheets[i].href.split(this._document.styleSheets[i].href.split('/')[this._document.styleSheets[i].href.split('/').length-1])[0]; - prefixUrl = 'url('+fileCouldDirUrl; //This should be re-written with better RegEx - tag.innerHTML = cssData.content.replace(/url\(/gi, prefixUrl); + + tag.innerHTML = cssData.content.replace(/url\(()(.+?)\1\)/g, detectUrl); + + function detectUrl (prop) { + return prop.replace(/[^()\\""\\'']+/g, prefixUrl);; + } + + function prefixUrl (url) { + if (url !== 'url') { + if (!url.match(/(\b(?:(?:https?|ftp|file|[A-Za-z]+):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$]))/gi)) { + url = fileCouldDirUrl+url; + } + } + return url; + } + //Looping through DOM to insert style tag at location of link element query = this._templateDocument.html.querySelectorAll(['link']); for (var j in query) { -- cgit v1.2.3