-
Notifications
You must be signed in to change notification settings - Fork 7
/
inspector.js
4227 lines (3764 loc) · 126 KB
/
inspector.js
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
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
/**
* Here's the server side of the remote inspector.
*
* The WalkerActor is the client's view of the debuggee's DOM. It's gives
* the client a tree of NodeActor objects.
*
* The walker presents the DOM tree mostly unmodified from the source DOM
* tree, but with a few key differences:
*
* - Empty text nodes are ignored. This is pretty typical of developer
* tools, but maybe we should reconsider that on the server side.
* - iframes with documents loaded have the loaded document as the child,
* the walker provides one big tree for the whole document tree.
*
* There are a few ways to get references to NodeActors:
*
* - When you first get a WalkerActor reference, it comes with a free
* reference to the root document's node.
* - Given a node, you can ask for children, siblings, and parents.
* - You can issue querySelector and querySelectorAll requests to find
* other elements.
* - Requests that return arbitrary nodes from the tree (like querySelector
* and querySelectorAll) will also return any nodes the client hasn't
* seen in order to have a complete set of parents.
*
* Once you have a NodeFront, you should be able to answer a few questions
* without further round trips, like the node's name, namespace/tagName,
* attributes, etc. Other questions (like a text node's full nodeValue)
* might require another round trip.
*
* The protocol guarantees that the client will always know the parent of
* any node that is returned by the server. This means that some requests
* (like querySelector) will include the extra nodes needed to satisfy this
* requirement. The client keeps track of this parent relationship, so the
* node fronts form a tree that is a subset of the actual DOM tree.
*
*
* We maintain this guarantee to support the ability to release subtrees on
* the client - when a node is disconnected from the DOM tree we want to be
* able to free the client objects for all the children nodes.
*
* So to be able to answer "all the children of a given node that we have
* seen on the client side", we guarantee that every time we've seen a node,
* we connect it up through its parents.
*/
const {Cc, Ci, Cu, Cr} = require("devtools/sham/chrome");
const Services = require("devtools/sham/services");
const protocol = require("devtools/server/protocol");
const {Arg, Option, method, RetVal, types} = protocol;
const {LongStringActor, ShortLongString} = require("devtools/server/actors/string");
const promise = require("devtools/sham/promise");
const {Task} = require("devtools/sham/task");
const object = require("sdk/util/object");
const events = require("sdk/event/core");
const {Class} = require("sdk/core/heritage");
/*const {WalkerSearch} = require("devtools/server/actors/utils/walker-search");*/
const {PageStyleActor, getFontPreviewData} = require("devtools/server/actors/styles");
const {
HighlighterActor,
CustomHighlighterActor,
isTypeRegistered,
} = require("devtools/server/actors/highlighters");
const {
isAnonymous,
isNativeAnonymous,
isXBLAnonymous,
isShadowAnonymous,
getFrameElement
} = require("devtools/shared/layout/utils");
const {getLayoutChangesObserver, releaseLayoutChangesObserver} =
require("devtools/server/actors/layout");
/*let CSS = require("CSS");*/
const {EventParsers} = require("devtools/shared/event-parsers");
const FONT_FAMILY_PREVIEW_TEXT = "The quick brown fox jumps over the lazy dog";
const FONT_FAMILY_PREVIEW_TEXT_SIZE = 20;
const PSEUDO_CLASSES = [":hover", ":active", ":focus"];
const HIDDEN_CLASS = "__fx-devtools-hide-shortcut__";
const XHTML_NS = "http://www.w3.org/1999/xhtml";
const XUL_NS = 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul';
const IMAGE_FETCHING_TIMEOUT = 500;
// The possible completions to a ':' with added score to give certain values
// some preference.
const PSEUDO_SELECTORS = [
[":active", 1],
[":hover", 1],
[":focus", 1],
[":visited", 0],
[":link", 0],
[":first-letter", 0],
[":first-child", 2],
[":before", 2],
[":after", 2],
[":lang(", 0],
[":not(", 3],
[":first-of-type", 0],
[":last-of-type", 0],
[":only-of-type", 0],
[":only-child", 2],
[":nth-child(", 3],
[":nth-last-child(", 0],
[":nth-of-type(", 0],
[":nth-last-of-type(", 0],
[":last-child", 2],
[":root", 0],
[":empty", 0],
[":target", 0],
[":enabled", 0],
[":disabled", 0],
[":checked", 1],
["::selection", 0]
];
var HELPER_SHEET = ".__fx-devtools-hide-shortcut__ { visibility: hidden !important } ";
HELPER_SHEET += ":-moz-devtools-highlighted { outline: 2px dashed #F06!important; outline-offset: -2px!important } ";
let DevToolsUtils = require("devtools/shared/DevToolsUtils");
let AsyncUtils = require("devtools/shared/async-utils");
/*loader.lazyGetter(this, "DOMParser", function() {
return Cc["@mozilla.org/xmlextras/domparser;1"].createInstance(Ci.nsIDOMParser);
});
loader.lazyGetter(this, "eventListenerService", function() {
return Cc["@mozilla.org/eventlistenerservice;1"]
.getService(Ci.nsIEventListenerService);
});
loader.lazyGetter(this, "CssLogic", () => require("devtools/shared/styleinspector/css-logic").CssLogic);*/
// XXX: A poor man's makeInfallible until we move it out of transport.js
// Which should be very soon.
function makeInfallible(handler) {
return function(...args) {
try {
return handler.apply(this, args);
} catch(ex) {
console.error(ex);
}
return undefined;
}
}
// A resolve that hits the main loop first.
function delayedResolve(value) {
let deferred = promise.defer();
Services.tm.mainThread.dispatch(makeInfallible(function delayedResolveHandler() {
deferred.resolve(value);
}), 0);
return deferred.promise;
}
types.addDictType("imageData", {
// The image data
data: "nullable:longstring",
// The original image dimensions
size: "json"
});
/**
* We only send nodeValue up to a certain size by default. This stuff
* controls that size.
*/
exports.DEFAULT_VALUE_SUMMARY_LENGTH = 50;
var gValueSummaryLength = exports.DEFAULT_VALUE_SUMMARY_LENGTH;
exports.getValueSummaryLength = function() {
return gValueSummaryLength;
};
exports.setValueSummaryLength = function(val) {
gValueSummaryLength = val;
};
// When the user selects a node to inspect in e10s, the parent process
// has a CPOW that wraps the node being inspected. It uses the
// message manager to send this node to the child, which stores the
// node in gInspectingNode. Then a findInspectingNode request is sent
// over the remote debugging protocol, and gInspectingNode is returned
// to the parent as a NodeFront.
var gInspectingNode = null;
// We expect this function to be called from the child.js frame script
// when it receives the node to be inspected over the message manager.
exports.setInspectingNode = function(val) {
gInspectingNode = val;
};
/**
* Server side of the node actor.
*/
var NodeActor = exports.NodeActor = protocol.ActorClass({
typeName: "domnode",
initialize: function(walker, node) {
protocol.Actor.prototype.initialize.call(this, null);
this.walker = walker;
this.rawNode = node;
this._eventParsers = new EventParsers().parsers;
// Storing the original display of the node, to track changes when reflows
// occur
this.wasDisplayed = this.isDisplayed;
},
toString: function() {
return "[NodeActor " + this.actorID + " for " + this.rawNode.toString() + "]";
},
/**
* Instead of storing a connection object, the NodeActor gets its connection
* from its associated walker.
*/
get conn() {
return this.walker.conn;
},
isDocumentElement: function() {
return this.rawNode.ownerDocument &&
this.rawNode.ownerDocument.documentElement === this.rawNode;
},
destroy: function () {
protocol.Actor.prototype.destroy.call(this);
if (this.mutationObserver) {
if (!Cu.isDeadWrapper(this.mutationObserver)) {
this.mutationObserver.disconnect();
}
this.mutationObserver = null;
}
this.rawNode = null;
this.walker = null;
},
// Returns the JSON representation of this object over the wire.
form: function(detail) {
if (detail === "actorid") {
return this.actorID;
}
let parentNode = this.walker.parentNode(this);
let singleTextChild = this.walker.singleTextChild(this);
let form = {
actor: this.actorID,
baseURI: this.rawNode.baseURI,
parent: parentNode ? parentNode.actorID : undefined,
nodeType: this.rawNode.nodeType,
namespaceURI: this.rawNode.namespaceURI,
nodeName: this.rawNode.nodeName,
numChildren: this.numChildren,
singleTextChild: singleTextChild ? singleTextChild.form() : undefined,
// doctype attributes
name: this.rawNode.name,
publicId: this.rawNode.publicId,
systemId: this.rawNode.systemId,
attrs: this.writeAttrs(),
isBeforePseudoElement: this.isBeforePseudoElement,
isAfterPseudoElement: this.isAfterPseudoElement,
isAnonymous: isAnonymous(this.rawNode),
isNativeAnonymous: isNativeAnonymous(this.rawNode),
isXBLAnonymous: isXBLAnonymous(this.rawNode),
isShadowAnonymous: isShadowAnonymous(this.rawNode),
pseudoClassLocks: this.writePseudoClassLocks(),
isDisplayed: this.isDisplayed,
hasEventListeners: this._hasEventListeners,
};
if (this.isDocumentElement()) {
form.isDocumentElement = true;
}
if (this.rawNode.nodeValue) {
// We only include a short version of the value if it's longer than
// gValueSummaryLength
if (this.rawNode.nodeValue.length > gValueSummaryLength) {
form.shortValue = this.rawNode.nodeValue.substring(0, gValueSummaryLength);
form.incompleteValue = true;
} else {
form.shortValue = this.rawNode.nodeValue;
}
}
// Add an extra API for custom properties added by other
// modules/extensions.
form.setFormProperty = (name, value) => {
if (!form.props) {
form.props = {};
}
form.props[name] = value;
};
// Fire an event so, other modules can create its own properties
// that should be passed to the client (within the form.props field).
events.emit(NodeActor, "form", {
target: this,
data: form
});
return form;
},
/**
* Watch the given document node for mutations using the DOM observer
* API.
*/
watchDocument: function(callback) {
let node = this.rawNode;
// Create the observer on the node's actor. The node will make sure
// the observer is cleaned up when the actor is released.
let observer = new node.defaultView.MutationObserver(callback);
observer.mergeAttributeRecords = true;
observer.observe(node, {
nativeAnonymousChildList: true,
attributes: true,
characterData: true,
childList: true,
subtree: true
});
this.mutationObserver = observer;
},
get isBeforePseudoElement() {
return this.rawNode.nodeName === "_moz_generated_content_before"
},
get isAfterPseudoElement() {
return this.rawNode.nodeName === "_moz_generated_content_after"
},
// Estimate the number of children that the walker will return without making
// a call to children() if possible.
get numChildren() {
// For pseudo elements, childNodes.length returns 1, but the walker
// will return 0.
if (this.isBeforePseudoElement || this.isAfterPseudoElement) {
return 0;
}
let rawNode = this.rawNode;
let numChildren = rawNode.childNodes.length;
let hasAnonChildren = rawNode.nodeType === Ci.nsIDOMNode.ELEMENT_NODE &&
rawNode.ownerDocument.getAnonymousNodes(rawNode);
if (numChildren === 0 &&
(rawNode.contentDocument || rawNode.getSVGDocument)) {
// This might be an iframe with virtual children.
numChildren = 1;
}
// Normal counting misses ::before/::after. Also, some anonymous children
// may ultimately be skipped, so we have to consult with the walker.
if (numChildren === 0 || hasAnonChildren) {
numChildren = this.walker.children(this).nodes.length;
}
return numChildren;
},
get computedStyle() {
return CssLogic.getComputedStyle(this.rawNode);
},
/**
* Is the node's display computed style value other than "none"
*/
get isDisplayed() {
// Consider all non-element nodes as displayed.
if (isNodeDead(this) ||
this.rawNode.nodeType !== Ci.nsIDOMNode.ELEMENT_NODE ||
this.isAfterPseudoElement ||
this.isBeforePseudoElement) {
return true;
}
let style = this.computedStyle;
if (!style) {
return true;
} else {
return style.display !== "none";
}
},
/**
* Are there event listeners that are listening on this node? This method
* uses all parsers registered via event-parsers.js.registerEventParser() to
* check if there are any event listeners.
*/
get _hasEventListeners() {
let parsers = this._eventParsers;
for (let [,{hasListeners}] of parsers) {
try {
if (hasListeners && hasListeners(this.rawNode)) {
return true;
}
} catch(e) {
// An object attached to the node looked like a listener but wasn't...
// do nothing.
}
}
return false;
},
writeAttrs: function() {
if (!this.rawNode.attributes) {
return undefined;
}
return [...this.rawNode.attributes].map(attr => {
return {namespace: attr.namespace, name: attr.name, value: attr.value };
});
},
writePseudoClassLocks: function() {
if (this.rawNode.nodeType !== Ci.nsIDOMNode.ELEMENT_NODE) {
return undefined;
}
let ret = undefined;
for (let pseudo of PSEUDO_CLASSES) {
if (DOMUtils.hasPseudoClassLock(this.rawNode, pseudo)) {
ret = ret || [];
ret.push(pseudo);
}
}
return ret;
},
/**
* Gets event listeners and adds their information to the events array.
*
* @param {Node} node
* Node for which we are to get listeners.
*/
getEventListeners: function(node) {
let parsers = this._eventParsers;
let dbg = this.parent().tabActor.makeDebugger();
let events = [];
for (let [,{getListeners, normalizeHandler}] of parsers) {
try {
let eventInfos = getListeners(node);
if (!eventInfos) {
continue;
}
for (let eventInfo of eventInfos) {
if (normalizeHandler) {
eventInfo.normalizeHandler = normalizeHandler;
}
this.processHandlerForEvent(node, events, dbg, eventInfo);
}
} catch(e) {
// An object attached to the node looked like a listener but wasn't...
// do nothing.
}
}
events.sort((a, b) => {
return a.type.localeCompare(b.type);
});
return events;
},
/**
* Process a handler
*
* @param {Node} node
* The node for which we want information.
* @param {Array} events
* The events array contains all event objects that we have gathered
* so far.
* @param {Debugger} dbg
* JSDebugger instance.
* @param {Object} eventInfo
* See event-parsers.js.registerEventParser() for a description of the
* eventInfo object.
*
* @return {Array}
* An array of objects where a typical object looks like this:
* {
* type: "click",
* handler: function() { doSomething() },
* origin: "http://www.mozilla.com",
* searchString: 'onclick="doSomething()"',
* tags: tags,
* DOM0: true,
* capturing: true,
* hide: {
* dom0: true
* }
* }
*/
processHandlerForEvent: function(node, events, dbg, eventInfo) {
let type = eventInfo.type || "";
let handler = eventInfo.handler;
let tags = eventInfo.tags || "";
let hide = eventInfo.hide || {};
let override = eventInfo.override || {};
let global = Cu.getGlobalForObject(handler);
let globalDO = dbg.addDebuggee(global);
let listenerDO = globalDO.makeDebuggeeValue(handler);
if (eventInfo.normalizeHandler) {
listenerDO = eventInfo.normalizeHandler(listenerDO);
}
// If the listener is an object with a 'handleEvent' method, use that.
if (listenerDO.class === "Object" || listenerDO.class === "XULElement") {
let desc;
while (!desc && listenerDO) {
desc = listenerDO.getOwnPropertyDescriptor("handleEvent");
listenerDO = listenerDO.proto;
}
if (desc && desc.value) {
listenerDO = desc.value;
}
}
if (listenerDO.isBoundFunction) {
listenerDO = listenerDO.boundTargetFunction;
}
let script = listenerDO.script;
let scriptSource = script.source.text;
let functionSource =
scriptSource.substr(script.sourceStart, script.sourceLength);
/*
The script returned is the whole script and
scriptSource.substr(script.sourceStart, script.sourceLength) returns
something like:
() { doSomething(); }
So we need to work back to the preceeding \n, ; or } so we can get the
appropriate function info e.g.:
() => { doSomething(); }
function doit() { doSomething(); }
doit: function() { doSomething(); }
*/
let scriptBeforeFunc = scriptSource.substr(0, script.sourceStart);
let lastEnding = Math.max(
scriptBeforeFunc.lastIndexOf(";"),
scriptBeforeFunc.lastIndexOf("}"),
scriptBeforeFunc.lastIndexOf("{"),
scriptBeforeFunc.lastIndexOf("("),
scriptBeforeFunc.lastIndexOf(","),
scriptBeforeFunc.lastIndexOf("!")
);
if (lastEnding !== -1) {
let functionPrefix = scriptBeforeFunc.substr(lastEnding + 1);
functionSource = functionPrefix + functionSource;
}
let dom0 = false;
if (typeof node.hasAttribute !== "undefined") {
dom0 = !!node.hasAttribute("on" + type);
} else {
dom0 = !!node["on" + type];
}
let line = script.startLine;
let url = script.url;
let origin = url + (dom0 ? "" : ":" + line);
let searchString;
if (dom0) {
searchString = "on" + type + "=\"" + script.source.text + "\"";
} else {
scriptSource = " " + scriptSource;
}
let eventObj = {
type: typeof override.type !== "undefined" ? override.type : type,
handler: functionSource.trim(),
origin: typeof override.origin !== "undefined" ?
override.origin : origin,
searchString: typeof override.searchString !== "undefined" ?
override.searchString : searchString,
tags: tags,
DOM0: typeof override.dom0 !== "undefined" ? override.dom0 : dom0,
capturing: typeof override.capturing !== "undefined" ?
override.capturing : eventInfo.capturing,
hide: hide
};
events.push(eventObj);
dbg.removeDebuggee(globalDO);
},
/**
* Returns a LongStringActor with the node's value.
*/
getNodeValue: method(function() {
return new LongStringActor(this.conn, this.rawNode.nodeValue || "");
}, {
request: {},
response: {
value: RetVal("longstring")
}
}),
/**
* Set the node's value to a given string.
*/
setNodeValue: method(function(value) {
this.rawNode.nodeValue = value;
}, {
request: { value: Arg(0) },
response: {}
}),
/**
* Get a unique selector string for this node.
*/
getUniqueSelector: method(function() {
return CssLogic.findCssSelector(this.rawNode);
}, {
request: {},
response: {
value: RetVal("string")
}
}),
/**
* Scroll the selected node into view.
*/
scrollIntoView: method(function() {
this.rawNode.scrollIntoView(true);
}, {
request: {},
response: {}
}),
/**
* Get the node's image data if any (for canvas and img nodes).
* Returns an imageData object with the actual data being a LongStringActor
* and a size json object.
* The image data is transmitted as a base64 encoded png data-uri.
* The method rejects if the node isn't an image or if the image is missing
*
* Accepts a maxDim request parameter to resize images that are larger. This
* is important as the resizing occurs server-side so that image-data being
* transfered in the longstring back to the client will be that much smaller
*/
getImageData: method(function(maxDim) {
return imageToImageData(this.rawNode, maxDim).then(imageData => {
return {
data: LongStringActor(this.conn, imageData.data),
size: imageData.size
};
});
}, {
request: {maxDim: Arg(0, "nullable:number")},
response: RetVal("imageData")
}),
/**
* Get all event listeners that are listening on this node.
*/
getEventListenerInfo: method(function() {
if (this.rawNode.nodeName.toLowerCase() === "html") {
return this.getEventListeners(this.rawNode.ownerGlobal);
}
return this.getEventListeners(this.rawNode);
}, {
request: {},
response: {
events: RetVal("json")
}
}),
/**
* Modify a node's attributes. Passed an array of modifications
* similar in format to "attributes" mutations.
* {
* attributeName: <string>
* attributeNamespace: <optional string>
* newValue: <optional string> - If null or undefined, the attribute
* will be removed.
* }
*
* Returns when the modifications have been made. Mutations will
* be queued for any changes made.
*/
modifyAttributes: method(function(modifications) {
let rawNode = this.rawNode;
for (let change of modifications) {
if (change.newValue == null) {
if (change.attributeNamespace) {
rawNode.removeAttributeNS(change.attributeNamespace, change.attributeName);
} else {
rawNode.removeAttribute(change.attributeName);
}
} else {
if (change.attributeNamespace) {
rawNode.setAttributeNS(change.attributeNamespace, change.attributeName, change.newValue);
} else {
rawNode.setAttribute(change.attributeName, change.newValue);
}
}
}
}, {
request: {
modifications: Arg(0, "array:json")
},
response: {}
}),
/**
* Given the font and fill style, get the image data of a canvas with the
* preview text and font.
* Returns an imageData object with the actual data being a LongStringActor
* and the width of the text as a string.
* The image data is transmitted as a base64 encoded png data-uri.
*/
getFontFamilyDataURL: method(function(font, fillStyle="black") {
let doc = this.rawNode.ownerDocument;
let options = {
previewText: FONT_FAMILY_PREVIEW_TEXT,
previewFontSize: FONT_FAMILY_PREVIEW_TEXT_SIZE,
fillStyle: fillStyle
}
let { dataURL, size } = getFontPreviewData(font, doc, options);
return { data: LongStringActor(this.conn, dataURL), size: size };
}, {
request: {font: Arg(0, "string"), fillStyle: Arg(1, "nullable:string")},
response: RetVal("imageData")
})
});
/**
* Client side of the node actor.
*
* Node fronts are strored in a tree that mirrors the DOM tree on the
* server, but with a few key differences:
* - Not all children will be necessary loaded for each node.
* - The order of children isn't guaranteed to be the same as the DOM.
* Children are stored in a doubly-linked list, to make addition/removal
* and traversal quick.
*
* Due to the order/incompleteness of the child list, it is safe to use
* the parent node from clients, but the `children` request should be used
* to traverse children.
*/
var NodeFront = protocol.FrontClass(NodeActor, {
initialize: function(conn, form, detail, ctx) {
this._parent = null; // The parent node
this._child = null; // The first child of this node.
this._next = null; // The next sibling of this node.
this._prev = null; // The previous sibling of this node.
protocol.Front.prototype.initialize.call(this, conn, form, detail, ctx);
},
/**
* Destroy a node front. The node must have been removed from the
* ownership tree before this is called, unless the whole walker front
* is being destroyed.
*/
destroy: function() {
protocol.Front.prototype.destroy.call(this);
},
// Update the object given a form representation off the wire.
form: function(form, detail, ctx) {
if (detail === "actorid") {
this.actorID = form;
return;
}
// Shallow copy of the form. We could just store a reference, but
// eventually we'll want to update some of the data.
this._form = object.merge(form);
this._form.attrs = this._form.attrs ? this._form.attrs.slice() : [];
if (form.parent) {
// Get the owner actor for this actor (the walker), and find the
// parent node of this actor from it, creating a standin node if
// necessary.
let parentNodeFront = ctx.marshallPool().ensureParentFront(form.parent);
this.reparent(parentNodeFront);
}
if (form.singleTextChild) {
this.singleTextChild =
types.getType("domnode").read(form.singleTextChild, ctx);
} else {
this.singleTextChild = undefined;
}
},
/**
* Returns the parent NodeFront for this NodeFront.
*/
parentNode: function() {
return this._parent;
},
/**
* Process a mutation entry as returned from the walker's `getMutations`
* request. Only tries to handle changes of the node's contents
* themselves (character data and attribute changes), the walker itself
* will keep the ownership tree up to date.
*/
updateMutation: function(change) {
if (change.type === "attributes") {
// We'll need to lazily reparse the attributes after this change.
this._attrMap = undefined;
// Update any already-existing attributes.
let found = false;
for (let i = 0; i < this.attributes.length; i++) {
let attr = this.attributes[i];
if (attr.name == change.attributeName &&
attr.namespace == change.attributeNamespace) {
if (change.newValue !== null) {
attr.value = change.newValue;
} else {
this.attributes.splice(i, 1);
}
found = true;
break;
}
}
// This is a new attribute. The null check is because of Bug 1192270,
// in the case of a newly added then removed attribute
if (!found && change.newValue !== null) {
this.attributes.push({
name: change.attributeName,
namespace: change.attributeNamespace,
value: change.newValue
});
}
} else if (change.type === "characterData") {
this._form.shortValue = change.newValue;
this._form.incompleteValue = change.incompleteValue;
} else if (change.type === "pseudoClassLock") {
this._form.pseudoClassLocks = change.pseudoClassLocks;
}
},
// Some accessors to make NodeFront feel more like an nsIDOMNode
get id() {
return this.getAttribute("id");
},
get nodeType() {
return this._form.nodeType;
},
get namespaceURI() {
return this._form.namespaceURI;
},
get nodeName() {
return this._form.nodeName;
},
get doctypeString() {
return '<!DOCTYPE ' + this._form.name +
(this._form.publicId ? ' PUBLIC "' + this._form.publicId + '"': '') +
(this._form.systemId ? ' "' + this._form.systemId + '"' : '') +
'>';
},
get baseURI() {
return this._form.baseURI;
},
get className() {
return this.getAttribute("class") || '';
},
get hasChildren() {
return this._form.numChildren > 0;
},
get numChildren() {
return this._form.numChildren;
},
get hasEventListeners() {
return this._form.hasEventListeners;
},
get isBeforePseudoElement() {
return this._form.isBeforePseudoElement;
},
get isAfterPseudoElement() {
return this._form.isAfterPseudoElement;
},
get isPseudoElement() {
return this.isBeforePseudoElement || this.isAfterPseudoElement;
},
get isAnonymous() {
return this._form.isAnonymous;
},
get tagName() {
return this.nodeType === Ci.nsIDOMNode.ELEMENT_NODE ? this.nodeName : null;
},
get shortValue() {
return this._form.shortValue;
},
get incompleteValue() {
return !!this._form.incompleteValue;
},
get isDocumentElement() {
return !!this._form.isDocumentElement;
},
// doctype properties
get name() {
return this._form.name;
},
get publicId() {
return this._form.publicId;
},
get systemId() {
return this._form.systemId;
},
getAttribute: function(name) {
let attr = this._getAttribute(name);
return attr ? attr.value : null;
},
hasAttribute: function(name) {
this._cacheAttributes();
return (name in this._attrMap);
},
get hidden() {
let cls = this.getAttribute("class");
return cls && cls.indexOf(HIDDEN_CLASS) > -1;
},
get attributes() {
return this._form.attrs;
},
get pseudoClassLocks() {
return this._form.pseudoClassLocks || [];
},
hasPseudoClassLock: function(pseudo) {
return this.pseudoClassLocks.some(locked => locked === pseudo);
},
get isDisplayed() {
// The NodeActor's form contains the isDisplayed information as a boolean
// starting from FF32. Before that, the property is missing
return "isDisplayed" in this._form ? this._form.isDisplayed : true;
},
get isTreeDisplayed() {
let parent = this;
while (parent) {
if (!parent.isDisplayed) {
return false;
}
parent = parent.parentNode();
}
return true;
},
getNodeValue: protocol.custom(function() {
if (!this.incompleteValue) {
return delayedResolve(new ShortLongString(this.shortValue));
} else {
return this._getNodeValue();
}
}, {
impl: "_getNodeValue"
}),
// Accessors for custom form properties.
getFormProperty: function(name) {
return this._form.props ? this._form.props[name] : null;
},
hasFormProperty: function(name) {
return this._form.props ? (name in this._form.props) : null;