1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
|
/* <copyright>
This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
</copyright> */
////////////////////////////////////////////////////////////////////////
//
var Montage = require("montage/core/core").Montage,
TextDocument = require("js/document/text-document").TextDocument,
NJUtils = require("js/lib/NJUtils").NJUtils,
CanvasDataManager = require("js/lib/rdge/runtime/CanvasDataManager").CanvasDataManager,
GLWorld = require("js/lib/drawing/world").World;
////////////////////////////////////////////////////////////////////////
//
exports.HTMLDocument = Montage.create(TextDocument, {
_selectionExclude: { value: null, enumerable: false },
_htmlTemplateUrl: { value: "js/document/templates/montage-html/index.html", enumerable: false},
_iframe: { value: null, enumerable: false },
_server: { value: null, enumerable: false },
_templateDocument: { value: null, enumerable: false },
_selectionModel: { value: [], enumerable: false },
_undoModel: { value: { "queue" : [], "position" : 0 }, enumerable: false},
_document: { value: null, enumerable: false },
_documentRoot: { value: null, enumerable: false },
_liveNodeList: { value: null, enumarable: false },
_stageBG: { value: null, enumerable: false },
_window: { value: null, enumerable: false },
_styles: { value: null, enumerable: false },
_stylesheets: { value: null, enumerable: false },
_stageStyleSheetId : { value: 'nj-stage-stylesheet', enumerable: false },
_userDocument: { value: null, enumerable: false },
_htmlSource: {value: "<html></html>", enumerable: false},
_glData: {value: null, enumerable: false },
_userComponents: { value: {}, enumarable: false},
_elementCounter: { value: 1, enumerable: false },
_snapping : { value: true, enumerable: false },
_layoutMode: { value: "all", enumerable: false },
_draw3DGrid: { value: false, writable: true },
_swfObject: { value: false, enumerable: false },
_zoomFactor: { value: 100, enumerable: false },
cssLoadInterval: { value: null, enumerable: false },
_savedLeftScroll: {value:null},
_savedTopScroll: {value:null},
_codeViewDocument:{
writable: true,
enumerable: true,
value:null
},
//drawUtils state
_gridHorizontalSpacing: {value:0},
_gridVerticalSpacing: {value:0},
//end - drawUtils state
_undoStack: { value: [] },
undoStack: {
get: function() {
return this._undoStack;
},
set:function(value){
this._undoStack = value;
}
},
_redoStack: { value: [], enumerable: false },
redoStack: {
get: function() {
return this._redoStack;
},
set:function(value){
this._redoStack = value;
}
},
// GETTERS / SETTERS
codeViewDocument:{
get: function() { return this._codeViewDocument; },
set: function(value) { this._codeViewDocument = value}
},
savedLeftScroll:{
get: function() { return this._savedLeftScroll; },
set: function(value) { this._savedLeftScroll = value}
},
savedTopScroll:{
get: function() { return this._savedTopScroll; },
set: function(value) { this._savedTopScroll = value}
},
gridHorizontalSpacing:{
get: function() { return this._gridHorizontalSpacing; },
set: function(value) { this._gridHorizontalSpacing = value}
},
gridVerticalSpacing:{
get: function() { return this._gridVerticalSpacing; },
set: function(value) { this._gridVerticalSpacing = value}
},
selectionExclude: {
get: function() { return this._selectionExclude; },
set: function(value) { this._selectionExclude = value; }
},
iframe: {
get: function() { return this._iframe; },
set: function(value) { this._iframe = value; }
},
server: {
get: function() { return this._server; },
set: function(value) { this._server = value; }
},
selectionModel: {
get: function() { return this._selectionModel; },
set: function(value) { this._selectionModel = value; }
},
undoModel: {
get: function() { return this._undoModel; },
set: function(value) { this._undoModel.queue = value.queue; this._undoModel.position = value.position; }
},
documentRoot: {
get: function() { return this._documentRoot; },
set: function(value) { this._documentRoot = value; }
},
stageBG: {
get: function() { return this._stageBG; },
set: function(value) { this._stageBG = value; }
},
elementCounter: {
set: function(value) { this._elementCounter = value; },
get: function() { return this._elementCounter; }
},
snapping: {
get: function() { return this._snapping; },
set: function(value) {
if(this._snapping !== value) {
this._snapping = value;
}
}
},
// TODO SEND THE EVENT --> Redraw the desired layout
layoutMode: {
get: function() { return this._layoutMode; },
set: function(mode) { this._layoutMode = mode; }
},
draw3DGrid: {
get: function() { return this._draw3DGrid; },
set: function(value) {
if(this._draw3DGrid !== value) {
this._draw3DGrid = value;
}
}
},
userComponents: {
get: function() {
return this._userComponents;
}
},
// _drawUserComponentsOnOpen:{
// value:function(){
// for(var i in this._userComponentSet){
// console.log(this._userComponentSet[i].control)
// this._userComponentSet[i].control.needsDraw = true;
// }
// }
// },
glData: {
get: function()
{
var elt = this.iframe.contentWindow.document.getElementById("UserContent");
this._glData = null;
if (elt)
{
var cdm = new CanvasDataManager();
this._glData = [];
cdm.collectGLData( elt, this._glData );
}
return this._glData;
},
set: function(value)
{
var elt = this.documentRoot;
if (elt)
{
var nWorlds= value.length;
for (var i=0; i<nWorlds; i++) {
var importStr = value[i];
var startIndex = importStr.indexOf("id: ");
if (startIndex >= 0) {
var endIndex = importStr.indexOf("\n", startIndex);
if (endIndex > 0) {
var id = importStr.substring( startIndex+4, endIndex);
if (id) {
var canvas = this.findCanvasWithID(id, elt);
if (canvas) {
if (!canvas.elementModel) {
NJUtils.makeElementModel(canvas, "Canvas", "shape", true);
}
if (canvas.elementModel) {
if (canvas.elementModel.shapeModel.GLWorld) {
canvas.elementModel.shapeModel.GLWorld.clearTree();
}
var index = importStr.indexOf( "webGL: " );
var useWebGL = (index >= 0)
var world = new GLWorld(canvas, useWebGL);
world.import( importStr );
this.buildShapeModel(canvas.elementModel, world);
}
}
}
}
}
}
}
}
},
buildShapeModel:
{
value: function( elementModel, world )
{
var shapeModel = elementModel.shapeModel;
shapeModel.shapeCount = 1; // for now...
shapeModel.useWebGl = world._useWebGL;
shapeModel.GLWorld = world;
var root = world.getGeomRoot();
if (root)
{
shapeModel.GLGeomObj = root;
shapeModel.strokeSize = root._strokeWidth;
shapeModel.stroke = root._strokeColor.slice();
shapeModel.strokeMaterial = root._strokeMaterial.dup();
shapeModel.strokeStyle = "solid";
//shapeModel.strokeStyleIndex
//shapeModel.border
//shapeModel.background
switch (root.geomType())
{
case root.GEOM_TYPE_RECTANGLE:
elementModel.selection = "Rectangle";
elementModel.pi = "RectanglePi";
shapeModel.fill = root._fillColor.slice();
shapeModel.fillMaterial = root._fillMaterial.dup();
shapeModel.tlRadius = root._tlRadius;
shapeModel.trRadius = root._trRadius;
shapeModel.blRadius = root._blRadius;
shapeModel.brRadius = root._brRadius;
break;
case root.GEOM_TYPE_CIRCLE:
elementModel.selection = "Oval";
elementModel.pi = "OvalPi";
shapeModel.fill = root._fillColor.slice();
shapeModel.fillMaterial = root._fillMaterial.dup();
shapeModel.innerRadius = root._innerRadius;
break;
case root.GEOM_TYPE_LINE:
elementModel.selection = "Line";
elementModel.pi = "LinePi";
shapeModel.slope = root._slope;
break;
default:
console.log( "geometry type not supported for file I/O, " + root.geomType());
break;
}
}
}
},
zoomFactor: {
get: function() { return this._zoomFactor; },
set: function(value) { this._zoomFactor = value; }
},
/**
* Add a reference to a component instance to the userComponents hash using the
* element UUID
*/
setComponentInstance: {
value: function(instance, el) {
this.userComponents[el.uuid] = instance;
}
},
/**
* Returns the component instance obj from the element
*/
getComponentFromElement: {
value: function(el) {
if(el) {
if(el.uuid) return this.userComponents[el.uuid];
} else {
return null;
}
}
},
/**
* search the DOM tree to find a canvas with the given id
*/
findCanvasWithID: {
value: function( id, elt ) {
var cid = elt.getAttribute( "data-RDGE-id" );
if (cid == id) return elt;
if (elt.children)
{
var nKids = elt.children.length;
for (var i=0; i<nKids; i++)
{
var child = elt.children[i];
var foundElt = this.findCanvasWithID( id, child );
if (foundElt) return foundElt;
}
}
}
},
////////////////////////////////////////////////////////////////////
//
initialize: {
value: function(file, uuid, iframe, callback) {
this.application.ninja.documentController._hackRootFlag = false;
//
this._userDocument = file;
//
this.init(file.name, file.uri, file.extension, iframe, uuid, callback);
//
this.iframe = iframe;
this.selectionExclude = ["HTML", "BODY", "Viewport", "UserContent", "stageBG"];
this.currentView = "design";
//
this.iframe.src = this._htmlTemplateUrl;
this.iframe.addEventListener("load", this, true);
}
},
////////////////////////////////////////////////////////////////////
collectGLData: {
value: function( elt, dataArray )
{
if (elt.elementModel && elt.elementModel.shapeModel && elt.elementModel.shapeModel.GLWorld)
{
var data = elt.elementModel.shapeModel.GLWorld.export();
dataArray.push( data );
}
if (elt.children)
{
var nKids = elt.children.length;
for (var i=0; i<nKids; i++)
{
var child = elt.children[i];
this.collectGLData( child, dataArray );
}
}
}
},
// OLD
inExclusion: {
value: function(element) {
if(this._selectionExclude.indexOf(element.id) === -1) {
if(this._selectionExclude.indexOf(element.nodeName) === -1) {
return -1;
}
} else if (this._selectionExclude.indexOf(element.id) === -1) {
return -1;
} else {
return 1;
}
}
},
/**
* Return the specified inline attribute from the element.
*/
GetElementAttribute: {
value: function(element, attribute) {
var value;
if(attribute === "src") {
return element[attribute].replace(window.location.href, '');
}
value = element[attribute];
if(value !== undefined) return value;
// if(value || value === false) return [value, "inline"];
// 3.
//value = this._document.defaultView.getComputedStyle(element,null).getPropertyValue(attribute);
//if(value) return value;
return null;
}
},
GetElementStyle: {
value: function(element, style) {
// return this._queryStylesheets(element, style);
}
},
SetStyle: {
value: function(type, selector, style, value) {
try {
for(var j=0; j<this._stylesheets.length;j++){
for(var i=0; i<this._stylesheets[j].cssRules.length;i++) {
if(this._stylesheets[j].cssRules[i].selectorText === type + selector) {
this._stylesheets[j].cssRules[i].style[style] = value;
return true;
}
}
}
} catch(err) {
console.log("Cannot change the style of selector: " + selector + " " + err);
}
}
},
GetElementFromPoint: {
value: function(x, y) {
return this._window.getElement(x,y);
}
},
/*
DOM Mutation Events:
DOMActivate, DOMFocusIn, DOMFocusOut, DOMAttrModified,
DOMCharacterDataModified, DOMNodeInserted, DOMNodeInsertedIntoDocument,
DOMNodeRemoved, DOMNodeRemovedFromDocument, DOMSubtreeModified, DOMContentLoaded
*/
/*
//TODO: Remove and clean up event listener (DOMSubtreeModified)
_hackCount: {
value: 0
},
*/
////////////////////////////////////////////////////////////////////
//
handleEvent: {
value: function(event){
//TODO: Clean up, using for prototyping save
this._templateDocument = {};
this._templateDocument.html = this.iframe.contentWindow.document;
this._templateDocument.head = this.iframe.contentWindow.document.getElementById("userHead");
this._templateDocument.body = this.documentRoot = this.iframe.contentWindow.document.getElementById("UserContent");
//TODO: Remove, also for prototyping
this.application.ninja.documentController._hackRootFlag = true;
//
this.stageBG = this.iframe.contentWindow.document.getElementById("stageBG");
this.stageBG.onclick = null;
this._document = this.iframe.contentWindow.document;
this._window = this.iframe.contentWindow;
//
for (var k in this._document.styleSheets) {
if (this._document.styleSheets[k].ownerNode && this._document.styleSheets[k].ownerNode.setAttribute) {
this._document.styleSheets[k].ownerNode.setAttribute('data-ninja-template', 'true');
}
}
//
if(!this.documentRoot.Ninja) this.documentRoot.Ninja = {};
//Inserting user's document into template
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//TODO: Clean up and make public method to prepend properties with Ninja URL
this._templateDocument.head.innerHTML = (this._userDocument.content.head.replace(/\b(href|src)\s*=\s*"([^"]*)"/g, ninjaUrlRedirect.bind(this))).replace(/url\(([^"]*)(.+?)\1\)/g, ninjaUrlRedirect.bind(this));
this._templateDocument.body.innerHTML = (this._userDocument.content.body.replace(/\b(href|src)\s*=\s*"([^"]*)"/g, ninjaUrlRedirect.bind(this))).replace(/url\(([^"]*)(.+?)\1\)/g, ninjaUrlRedirect.bind(this));
//
//var docRootUrl = this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]).replace(/\/\//gi, '/'));
//
function ninjaUrlRedirect (prop) {
//Checking for property value to not contain a full direct URL
if (!prop.match(/(\b(?:(?:https?|ftp|file|[A-Za-z]+):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$]))/gi)) {
//Checking for attributes and type of source
if (prop.indexOf('href') !== -1 || prop.indexOf('src') !== -1) { //From HTML attribute
//
prop = prop.replace(/"([^"]*)"/gi, ninjaUrlPrepend.bind(this));
} 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, cssUrlToNinjaUrl);
function cssUrlToNinjaUrl (s) {
if (s !== 'url') {
s = docRootUrl + s;
}
return s;
}
}
}
return prop;
}
//
function ninjaUrlPrepend (url) {
var docRootUrl = this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]).replace(/\/\//gi, '/'));
if (url.indexOf('data:image') !== -1) {
return url;
} else {
return '"'+docRootUrl+url.replace(/\"/gi, '')+'"';
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var scripttags = this._templateDocument.html.getElementsByTagName('script'), webgldata; //TODO: Use querySelectorAll
//
for (var w in scripttags) {
if (scripttags[w].getAttribute) {
if (scripttags[w].getAttribute('data-ninja-webgl') !== null) {
//TODO: Add logic to handle more than one data tag
webgldata = JSON.parse((scripttags[w].innerHTML.replace("(", "")).replace(")", ""));
}
}
}
//
if (webgldata) {
for (var n=0; webgldata.data[n]; n++) {
webgldata.data[n] = unescape(webgldata.data[n]);
}
this._templateDocument.webgl = webgldata.data;
}
//Temporarily checking for disabled special case
var stags = this.iframe.contentWindow.document.getElementsByTagName('style'),
ltags = this.iframe.contentWindow.document.getElementsByTagName('link');
//
for (var m = 0; m < ltags.length; m++) {
if (ltags[m].getAttribute('data-ninja-template') === null) {
if (ltags[m].getAttribute('disabled')) {
ltags[m].removeAttribute('disabled');
ltags[m].setAttribute('data-ninja-disabled', 'true');
}
}
}
//
for (var n = 0; n < stags.length; n++) {
if (stags[n].getAttribute('data-ninja-template') === null) {
if (stags[n].getAttribute('disabled')) {
stags[n].removeAttribute('disabled');
stags[n].setAttribute('data-ninja-disabled', 'true');
}
}
}
//Adding a handler for the main user document reel to finish loading
this._document.body.addEventListener("userTemplateDidLoad", this.userTemplateDidLoad.bind(this), false);
// Live node list of the current loaded document
this._liveNodeList = this.documentRoot.getElementsByTagName('*');
// TODO Move this to the appropriate location
var len = this._liveNodeList.length;
for(var i = 0; i < len; i++) {
NJUtils.makeModelFromElement(this._liveNodeList[i]);
}
/* this.iframe.contentWindow.document.addEventListener('DOMSubtreeModified', function (e) { */ //TODO: Remove events upon loading once
//TODO: When re-written, the best way to initialize the document is to listen for the DOM tree being modified
setTimeout(function () {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(this._document.styleSheets.length > 1) {
//Checking all styleSheets in document
for (var i in this._document.styleSheets) {
//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, fileCouldDirUrl, docRootUrl;
//
docRootUrl = this.application.ninja.coreIoApi.rootUrl+escape((this.application.ninja.documentController.documentHackReference.root.split(this.application.ninja.coreIoApi.cloudData.root)[1]).replace(/\/\//gi, '/'));
//TODO: Parse out relative URLs and map them to absolute
if (this._document.styleSheets[i].href.indexOf(this.application.ninja.coreIoApi.rootUrl) !== -1) {
//
cssUrl = this._document.styleSheets[i].href.split(this.application.ninja.coreIoApi.rootUrl)[1];
fileUri = this.application.ninja.coreIoApi.cloudData.root+cssUrl;
//TODO: Add error handling for reading file
cssData = this.application.ninja.coreIoApi.readFile({uri: fileUri});
//
var tag = this.iframe.contentWindow.document.createElement('style');
tag.setAttribute('type', 'text/css');
tag.setAttribute('data-ninja-uri', fileUri);
tag.setAttribute('data-ninja-file-url', cssUrl);
tag.setAttribute('data-ninja-file-read-only', JSON.parse(this.application.ninja.coreIoApi.isFileWritable({uri: fileUri}).content).readOnly);
tag.setAttribute('data-ninja-file-name', cssUrl.split('/')[cssUrl.split('/').length-1]);
//Copying attributes to maintain same properties as the <link>
for (var n in this._document.styleSheets[i].ownerNode.attributes) {
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') {
if (this._document.styleSheets[i].ownerNode.attributes[n].value.indexOf(docRootUrl) !== -1) {
tag.setAttribute(this._document.styleSheets[i].ownerNode.attributes[n].name, this._document.styleSheets[i].ownerNode.attributes[n].value.split(docRootUrl)[1]);
} else {
tag.setAttribute(this._document.styleSheets[i].ownerNode.attributes[n].name, this._document.styleSheets[i].ownerNode.attributes[n].value);
}
}
}
//
fileCouldDirUrl = this._document.styleSheets[i].href.split(this._document.styleSheets[i].href.split('/')[this._document.styleSheets[i].href.split('/').length-1])[0];
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) {
if (query[j].href === this._document.styleSheets[i].href) {
//Disabling style sheet to reload via inserting in style tag
query[j].setAttribute('disabled', 'true');
//Inserting tag
this._templateDocument.head.insertBefore(tag, query[j]);
}
}
} else {
console.log('ERROR: Cross-Domain-Stylesheet detected, unable to load in Ninja');
//None local stylesheet, probably on a CDN (locked)
var 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]);
//Copying attributes to maintain same properties as the <link>
for (var n in this._document.styleSheets[i].ownerNode.attributes) {
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') {
if (this._document.styleSheets[i].ownerNode.attributes[n].value.indexOf(docRootUrl) !== -1) {
tag.setAttribute(this._document.styleSheets[i].ownerNode.attributes[n].name, this._document.styleSheets[i].ownerNode.attributes[n].value.split(docRootUrl)[1]);
} else {
tag.setAttribute(this._document.styleSheets[i].ownerNode.attributes[n].name, this._document.styleSheets[i].ownerNode.attributes[n].value);
}
}
}
/*
//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();
//
if (xhr.readyState === 4) {
console.log(xhr);
}
//tag.innerHTML = xhr.responseText //xhr.response;
*/
//Temp rule so it's registered in the array
tag.innerHTML = 'noRULEjustHACK{background: #000}';
//Disabling external style sheets
query = this._templateDocument.html.querySelectorAll(['link']);
for (var k in query) {
if (query[k].href === this._document.styleSheets[i].href) {
//TODO: Removed the temp insertion of the stylesheet
//because it wasn't the proper way to do it
//need to be handled via XHR with proxy in Cloud Sim
//Disabling style sheet to reload via inserting in style tag
//var tempCSS = query[k].cloneNode(true);
//tempCSS.setAttribute('data-ninja-template', 'true');
query[k].setAttribute('disabled', 'true');
//this.iframe.contentWindow.document.head.appendChild(tempCSS);
//Inserting tag
this._templateDocument.head.insertBefore(tag, query[k]);
}
}
}
}
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
//TODO: Check if this is needed
this._stylesheets = this._document.styleSheets;
///////////////////////////////////////////
|