diff options
author | Jose Antonio Marquez | 2012-03-02 14:11:08 -0800 |
---|---|---|
committer | Jose Antonio Marquez | 2012-03-02 14:11:08 -0800 |
commit | e24631ecac0772fc51756fe4aff9638de3b95faf (patch) | |
tree | b70d2273e41670615aafd32477f2f83146ab5bbb /js | |
parent | c4bfc8b79820b55cc86d755fe590ee649813232a (diff) | |
download | ninja-e24631ecac0772fc51756fe4aff9638de3b95faf.tar.gz |
Fixing CSS URL issues
Only partially, supporting unquoted files under same root.
Diffstat (limited to 'js')
-rwxr-xr-x | js/document/html-document.js | 24 | ||||
-rw-r--r-- | 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, { | |||
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, query, prefixUrl; | 488 | var fileUri, cssUrl, cssData, query, prefixUrl, fileCouldDirUrl; |
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).replace(/\/\//gi, '/'); |
495 | fileUri = fileUri.replace(/\/\//gi, '/'); | ||
496 | //Loading the data from the file | 495 | //Loading the data from the file |
497 | cssData = this.application.ninja.coreIoApi.readFile({uri: fileUri}); | 496 | cssData = this.application.ninja.coreIoApi.readFile({uri: fileUri}); |
498 | //Creating tag with file content | 497 | //Creating tag with file content |
@@ -508,10 +507,17 @@ exports.HTMLDocument = Montage.create(TextDocument, { | |||
508 | tag.setAttribute(this._document.styleSheets[i].ownerNode.attributes[n].name, this._document.styleSheets[i].ownerNode.attributes[n].value); | 507 | tag.setAttribute(this._document.styleSheets[i].ownerNode.attributes[n].name, this._document.styleSheets[i].ownerNode.attributes[n].value); |
509 | } | 508 | } |
510 | } | 509 | } |
510 | // | ||
511 | |||
512 | 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, '/')); | ||
513 | |||
514 | |||
511 | //TODO: Fix regEx to have logic for all possible URLs strings | 515 | //TODO: Fix regEx to have logic for all possible URLs strings |
512 | prefixUrl = '('+cssUrl.split(cssUrl.split('/')[cssUrl.split('/').length-1])[0]+'../'; | 516 | prefixUrl = '('+fileCouldDirUrl+'../'; |
513 | prefixUrl = prefixUrl.replace('(/', '('); | ||
514 | tag.innerHTML = cssData.content.replace(/\(\.\.\//gi, prefixUrl); | 517 | tag.innerHTML = cssData.content.replace(/\(\.\.\//gi, prefixUrl); |
518 | |||
519 | |||
520 | |||
515 | //Looping through DOM to insert style tag at location of link element | 521 | //Looping through DOM to insert style tag at location of link element |
516 | query = this._templateDocument.html.querySelectorAll(['link']); | 522 | query = this._templateDocument.html.querySelectorAll(['link']); |
517 | for (var j in query) { | 523 | for (var j in query) { |
@@ -525,14 +531,14 @@ exports.HTMLDocument = Montage.create(TextDocument, { | |||
525 | } else { | 531 | } else { |
526 | console.log('ERROR: Cross-Domain-Stylesheet detected, unable to load in Ninja'); | 532 | console.log('ERROR: Cross-Domain-Stylesheet detected, unable to load in Ninja'); |
527 | //None local stylesheet, probably on a CDN (locked) | 533 | //None local stylesheet, probably on a CDN (locked) |
528 | tag = this.iframe.contentWindow.document.createElement('style'); | 534 | /* |
535 | tag = this.iframe.contentWindow.document.createElement('style'); | ||
529 | tag.setAttribute('type', 'text/css'); | 536 | tag.setAttribute('type', 'text/css'); |
530 | tag.setAttribute('data-ninja-external-url', this._document.styleSheets[i].href); | 537 | tag.setAttribute('data-ninja-external-url', this._document.styleSheets[i].href); |
531 | tag.setAttribute('data-ninja-file-read-only', "true"); | 538 | tag.setAttribute('data-ninja-file-read-only', "true"); |
532 | tag.setAttribute('data-ninja-file-name', this._document.styleSheets[i].href.split('/')[this._document.styleSheets[i].href.split('/').length-1]); | 539 | tag.setAttribute('data-ninja-file-name', this._document.styleSheets[i].href.split('/')[this._document.styleSheets[i].href.split('/').length-1]); |
533 | 540 | ||
534 | /* | 541 | //TODO: Figure out cross-domain XHR issue, might need cloud to handle |
535 | //TODO: Figure out cross-domain XHR issue, might need cloud to handle | ||
536 | var xhr = new XMLHttpRequest(); | 542 | var xhr = new XMLHttpRequest(); |
537 | xhr.open("GET", this._document.styleSheets[i].href, true); | 543 | xhr.open("GET", this._document.styleSheets[i].href, true); |
538 | xhr.send(); | 544 | xhr.send(); |
@@ -540,7 +546,6 @@ exports.HTMLDocument = Montage.create(TextDocument, { | |||
540 | if (xhr.readyState === 4) { | 546 | if (xhr.readyState === 4) { |
541 | console.log(xhr); | 547 | console.log(xhr); |
542 | } | 548 | } |
543 | */ | ||
544 | //tag.innerHTML = xhr.responseText //xhr.response; | 549 | //tag.innerHTML = xhr.responseText //xhr.response; |
545 | 550 | ||
546 | //Currently no external styles will load if unable to load via XHR request | 551 | //Currently no external styles will load if unable to load via XHR request |
@@ -555,6 +560,7 @@ exports.HTMLDocument = Montage.create(TextDocument, { | |||
555 | this._templateDocument.head.insertBefore(tag, query[j]); | 560 | this._templateDocument.head.insertBefore(tag, query[j]); |
556 | } | 561 | } |
557 | } | 562 | } |
563 | */ | ||
558 | } | 564 | } |
559 | } | 565 | } |
560 | } | 566 | } |
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, { | |||
313 | docStyles[styleCounter].innerHTML = this.getCssFromRules(template.css[i].cssRules); | 313 | docStyles[styleCounter].innerHTML = this.getCssFromRules(template.css[i].cssRules); |
314 | styleCounter++; | 314 | styleCounter++; |
315 | } | 315 | } |
316 | } else { | 316 | } else if (!template.css[i].ownerNode.getAttribute('data-ninja-template')){ |
317 | //Checking for attributes to be added to tag upon saving | 317 | //Checking for attributes to be added to tag upon saving |
318 | for (var k in docLinks) { | 318 | for (var k in docLinks) { |
319 | if (docLinks[k].getAttribute) { | 319 | if (docLinks[k].getAttribute) { |
@@ -330,8 +330,27 @@ exports.IoMediator = Montage.create(Component, { | |||
330 | } | 330 | } |
331 | } | 331 | } |
332 | } | 332 | } |
333 | var adjCss = this.getCssFromRules(template.css[i].cssRules), cssUrl = template.css[i].ownerNode.getAttribute('data-ninja-uri'); | 333 | var local, regex, fileCouldDirUrl, adjCss = this.getCssFromRules(template.css[i].cssRules), cssUrl = template.css[i].ownerNode.getAttribute('data-ninja-file-url'); |
334 | //console.log((template.css[i].ownerNode.getAttribute('data-ninja-uri')));//cssUrl.split(cssUrl.split('/')[cssUrl.split('/').length-1])[0] | 334 | //TODO: Assure logic for local directory |
335 | 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]; | ||
336 | // | ||
337 | fileCouldDirUrl = this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]+local).replace(/\/\//gi, '/')); | ||
338 | // | ||
339 | regex = new RegExp(fileCouldDirUrl.replace(/\//gi, '\\\/'), 'gi'); | ||
340 | // | ||
341 | if (local.split('/').length > 1) { | ||
342 | adjCss = adjCss.replace(regex, '../'); | ||
343 | } else { | ||
344 | adjCss = adjCss.replace(regex, ''); | ||
345 | } | ||
346 | |||
347 | |||
348 | //console.log(adjCss); | ||
349 | //console.log(fileCouldDirUrl); | ||
350 | //return; | ||
351 | |||
352 | |||
353 | |||
335 | //Saving data from rules array converted to string into <link> file | 354 | //Saving data from rules array converted to string into <link> file |
336 | var save = this.fio.saveFile({uri: template.css[i].ownerNode.getAttribute('data-ninja-uri'), contents: adjCss}); | 355 | var save = this.fio.saveFile({uri: template.css[i].ownerNode.getAttribute('data-ninja-uri'), contents: adjCss}); |
337 | } | 356 | } |