diff options
Diffstat (limited to 'js')
-rwxr-xr-x | js/document/html-document.js | 22 | ||||
-rw-r--r-- | 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, { | |||
485 | //If rules are null, assuming cross-origin issue | 485 | //If rules are null, assuming cross-origin issue |
486 | if(this._document.styleSheets[i].rules === null) { | 486 | if(this._document.styleSheets[i].rules === null) { |
487 | //TODO: Revisit URLs and URI creation logic, very hack right now | 487 | //TODO: Revisit URLs and URI creation logic, very hack right now |
488 | var fileUri, cssUrl, cssData, tag, query; | 488 | var fileUri, cssUrl, cssData, query, prefixUrl; |
489 | //TODO: Parse out relative URLs and map them to absolute | 489 | //TODO: Parse out relative URLs and map them to absolute |
490 | if (this._document.styleSheets[i].href.indexOf(chrome.extension.getURL('')) !== -1) { | 490 | if (this._document.styleSheets[i].href.indexOf(chrome.extension.getURL('')) !== -1) { |
491 | //Getting the url of the CSS file | 491 | //Getting the url of the CSS file |
492 | cssUrl = this._document.styleSheets[i].href.split('js/document/templates/montage-html')[1];//TODO: Parse out relative URLs and map them to absolute | 492 | cssUrl = this._document.styleSheets[i].href.split('js/document/templates/montage-html')[1];//TODO: Parse out relative URLs and map them to absolute |
493 | //Creating the URI of the file (this is wrong should not be splitting cssUrl) | 493 | //Creating the URI of the file (this is wrong should not be splitting cssUrl) |
494 | fileUri = this.application.ninja.coreIoApi.cloudData.root+this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]+cssUrl; | 494 | fileUri = this.application.ninja.coreIoApi.cloudData.root+this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]+cssUrl; |
495 | fileUri = fileUri.replace(/\/\//gi, '/'); | ||
495 | //Loading the data from the file | 496 | //Loading the data from the file |
496 | cssData = this.application.ninja.coreIoApi.readFile({uri: fileUri}); | 497 | cssData = this.application.ninja.coreIoApi.readFile({uri: fileUri}); |
497 | //Creating tag with file content | 498 | //Creating tag with file content |
498 | tag = this.iframe.contentWindow.document.createElement('style'); | 499 | var tag = this.iframe.contentWindow.document.createElement('style'); |
499 | tag.setAttribute('type', 'text/css'); | 500 | tag.setAttribute('type', 'text/css'); |
500 | tag.setAttribute('data-ninja-uri', fileUri); | 501 | tag.setAttribute('data-ninja-uri', fileUri); |
501 | tag.setAttribute('data-ninja-file-url', cssUrl); | 502 | tag.setAttribute('data-ninja-file-url', cssUrl); |
@@ -503,13 +504,14 @@ exports.HTMLDocument = Montage.create(TextDocument, { | |||
503 | tag.setAttribute('data-ninja-file-name', cssUrl.split('/')[cssUrl.split('/').length-1]); | 504 | tag.setAttribute('data-ninja-file-name', cssUrl.split('/')[cssUrl.split('/').length-1]); |
504 | //Copying attributes to maintain same properties as the <link> | 505 | //Copying attributes to maintain same properties as the <link> |
505 | for (var n in this._document.styleSheets[i].ownerNode.attributes) { | 506 | for (var n in this._document.styleSheets[i].ownerNode.attributes) { |
506 | if (this._document.styleSheets[i].ownerNode.attributes[n].value && this._document.styleSheets[i].ownerNode.attributes[n].name !== 'disabled') { | 507 | 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') { |
507 | tag.setAttribute(this._document.styleSheets[i].ownerNode.attributes[n].name, this._document.styleSheets[i].ownerNode.attributes[n].value); | 508 | tag.setAttribute(this._document.styleSheets[i].ownerNode.attributes[n].name, this._document.styleSheets[i].ownerNode.attributes[n].value); |
508 | } | 509 | } |
509 | } | 510 | } |
510 | //TODO: Parse out relative URLs and map them to absolute | 511 | //TODO: Fix regEx to have logic for all possible URLs strings |
511 | //console.log(cssData.content); | 512 | prefixUrl = '('+cssUrl.split(cssUrl.split('/')[cssUrl.split('/').length-1])[0]+'../'; |
512 | tag.innerHTML = cssData.content; | 513 | prefixUrl = prefixUrl.replace('(/', '('); |
514 | tag.innerHTML = cssData.content.replace(/\(\.\.\//gi, prefixUrl); | ||
513 | //Looping through DOM to insert style tag at location of link element | 515 | //Looping through DOM to insert style tag at location of link element |
514 | query = this._templateDocument.html.querySelectorAll(['link']); | 516 | query = this._templateDocument.html.querySelectorAll(['link']); |
515 | for (var j in query) { | 517 | for (var j in query) { |
@@ -522,15 +524,15 @@ exports.HTMLDocument = Montage.create(TextDocument, { | |||
522 | } | 524 | } |
523 | } else { | 525 | } else { |
524 | console.log('ERROR: Cross-Domain-Stylesheet detected, unable to load in Ninja'); | 526 | console.log('ERROR: Cross-Domain-Stylesheet detected, unable to load in Ninja'); |
525 | /* | 527 | //None local stylesheet, probably on a CDN (locked) |
526 | //None local stylesheet, probably on a CDN (locked) | ||
527 | tag = this.iframe.contentWindow.document.createElement('style'); | 528 | tag = this.iframe.contentWindow.document.createElement('style'); |
528 | tag.setAttribute('type', 'text/css'); | 529 | tag.setAttribute('type', 'text/css'); |
529 | tag.setAttribute('data-ninja-external-url', this._document.styleSheets[i].href); | 530 | tag.setAttribute('data-ninja-external-url', this._document.styleSheets[i].href); |
530 | tag.setAttribute('data-ninja-file-read-only', "true"); | 531 | tag.setAttribute('data-ninja-file-read-only', "true"); |
531 | tag.setAttribute('data-ninja-file-name', this._document.styleSheets[i].href.split('/')[this._document.styleSheets[i].href.split('/').length-1]); | 532 | tag.setAttribute('data-ninja-file-name', this._document.styleSheets[i].href.split('/')[this._document.styleSheets[i].href.split('/').length-1]); |
532 | 533 | ||
533 | //TODO: Figure out cross-domain XHR issue, might need cloud to handle | 534 | /* |
535 | //TODO: Figure out cross-domain XHR issue, might need cloud to handle | ||
534 | var xhr = new XMLHttpRequest(); | 536 | var xhr = new XMLHttpRequest(); |
535 | xhr.open("GET", this._document.styleSheets[i].href, true); | 537 | xhr.open("GET", this._document.styleSheets[i].href, true); |
536 | xhr.send(); | 538 | xhr.send(); |
@@ -538,6 +540,7 @@ exports.HTMLDocument = Montage.create(TextDocument, { | |||
538 | if (xhr.readyState === 4) { | 540 | if (xhr.readyState === 4) { |
539 | console.log(xhr); | 541 | console.log(xhr); |
540 | } | 542 | } |
543 | */ | ||
541 | //tag.innerHTML = xhr.responseText //xhr.response; | 544 | //tag.innerHTML = xhr.responseText //xhr.response; |
542 | 545 | ||
543 | //Currently no external styles will load if unable to load via XHR request | 546 | //Currently no external styles will load if unable to load via XHR request |
@@ -552,7 +555,6 @@ exports.HTMLDocument = Montage.create(TextDocument, { | |||
552 | this._templateDocument.head.insertBefore(tag, query[j]); | 555 | this._templateDocument.head.insertBefore(tag, query[j]); |
553 | } | 556 | } |
554 | } | 557 | } |
555 | */ | ||
556 | } | 558 | } |
557 | } | 559 | } |
558 | } | 560 | } |
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, { | |||
41 | }, | 41 | }, |
42 | //////////////////////////////////////////////////////////////////// | 42 | //////////////////////////////////////////////////////////////////// |
43 | // | 43 | // |
44 | appTemplatesUrl: { | 44 | getAppTemplatesUrlRegEx: { |
45 | enumerable: false, | 45 | enumerable: false, |
46 | value: new RegExp(chrome.extension.getURL('js/document/templates/montage-html/'), 'gi') | 46 | value: function () { |
47 | var regex = new RegExp(chrome.extension.getURL('js/document/templates/montage-html').replace(/\//gi, '\\\/'), 'gi'); | ||
48 | return regex; | ||
49 | } | ||
47 | }, | 50 | }, |
48 | //////////////////////////////////////////////////////////////////// | 51 | //////////////////////////////////////////////////////////////////// |
49 | // | 52 | // |
@@ -211,36 +214,59 @@ exports.IoMediator = Montage.create(Component, { | |||
211 | enumerable: false, | 214 | enumerable: false, |
212 | value: function (template) { | 215 | value: function (template) { |
213 | //Injecting head and body into old document | 216 | //Injecting head and body into old document |
214 | template.document.content.document.body.innerHTML = template.body; | ||
215 | template.document.content.document.head.innerHTML = template.head; | 217 | template.document.content.document.head.innerHTML = template.head; |
218 | template.document.content.document.body.innerHTML = template.body; | ||
216 | //Getting all CSS (style or link) tags | 219 | //Getting all CSS (style or link) tags |
217 | var styletags = template.document.content.document.getElementsByTagName('style'), | 220 | var styletags = template.document.content.document.getElementsByTagName('style'), |
218 | linktags = template.document.content.document.getElementsByTagName('link'); | 221 | linktags = template.document.content.document.getElementsByTagName('link'), |
219 | //Looping through link tags and removing file recreated elements | 222 | toremovetags = []; |
220 | for (var j in styletags) { | 223 | //Getting styles tags to be removed from document |
221 | if (styletags[j].getAttribute) { | 224 | if (styletags.length) { |
222 | if(styletags[j].getAttribute('data-ninja-uri') !== null && !styletags[j].getAttribute('data-ninja-template')) {//TODO: Use querySelectorAll | 225 | for (var j=0; j<styletags.length; j++) { |
223 | try { | 226 | if (styletags[j].getAttribute) { |
224 | //Checking head first | 227 | if(styletags[j].getAttribute('data-ninja-uri') !== null && !styletags[j].getAttribute('data-ninja-template')) { |
225 | template.document.content.document.head.removeChild(styletags[j]); | 228 | toremovetags.push(styletags[j]); |
226 | } catch (e) { | ||
227 | try { | ||
228 | //Checking body if not in head | ||
229 | template.document.content.document.body.removeChild(styletags[j]); | ||
230 | } catch (e) { | ||
231 | //Error, not found! | ||
232 | } | ||
233 | } | 229 | } |
234 | |||
235 | } | 230 | } |
236 | } | 231 | } |
237 | } | 232 | } |
238 | //TODO: Add logic to only enble tags we disabled | 233 | //Removing styles tags from document |
234 | for (var h=0; toremovetags[h]; h++) { | ||
235 | try { | ||
236 | //Checking head first | ||
237 | template.document.content.document.head.removeChild(toremovetags[h]); | ||
238 | } catch (e) { | ||
239 | try { | ||
240 | //Checking body if not in head | ||
241 | template.document.content.document.body.removeChild(toremovetags[h]); | ||
242 | } catch (e) { | ||
243 | //Error, not found! | ||
244 | } | ||
245 | } | ||
246 | } | ||
247 | //Removing disabled tags from tags that were not originally disabled by user (Ninja enables all) | ||
239 | for (var l in linktags) { | 248 | for (var l in linktags) { |
240 | if (linktags[l].getAttribute && linktags[l].getAttribute('disabled')) {//TODO: Use querySelectorAll | 249 | if (linktags[l].getAttribute && linktags[l].getAttribute('disabled')) {//TODO: Use querySelectorAll |
241 | linktags[l].removeAttribute('disabled'); | 250 | for (var p=0; toremovetags[p]; p++) { |
251 | if (toremovetags[p].getAttribute('data-ninja-file-url') === ('/'+linktags[l].getAttribute('href'))) { | ||
252 | if (!toremovetags[p].getAttribute('data-ninja-disabled')) { | ||
253 | linktags[l].removeAttribute('disabled'); | ||
254 | } | ||
255 | } | ||
256 | } | ||
242 | } | 257 | } |
243 | } | 258 | } |
259 | |||
260 | |||
261 | ///////////////////////////////////////////////////////////////////////////////////////// | ||
262 | ///////////////////////////////////////////////////////////////////////////////////////// | ||
263 | |||
264 | //TODO: Add logic for parse CSS string correct URLs since referencing is lost | ||
265 | |||
266 | ///////////////////////////////////////////////////////////////////////////////////////// | ||
267 | ///////////////////////////////////////////////////////////////////////////////////////// | ||
268 | |||
269 | |||
244 | //Checking for type of save: styles = <style> only | css = <style> and <link> (all CSS) |