OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2009 Google Inc. All rights reserved. | |
3 * Copyright (C) 2009 Joseph Pecoraro | |
4 * | |
5 * Redistribution and use in source and binary forms, with or without | |
6 * modification, are permitted provided that the following conditions are | |
7 * met: | |
8 * | |
9 * * Redistributions of source code must retain the above copyright | |
10 * notice, this list of conditions and the following disclaimer. | |
11 * * Redistributions in binary form must reproduce the above | |
12 * copyright notice, this list of conditions and the following disclaimer | |
13 * in the documentation and/or other materials provided with the | |
14 * distribution. | |
15 * * Neither the name of Google Inc. nor the names of its | |
16 * contributors may be used to endorse or promote products derived from | |
17 * this software without specific prior written permission. | |
18 * | |
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
30 */ | |
31 | |
32 WebInspector.DOMNode = function(doc, payload) { | |
33 this.ownerDocument = doc; | |
34 | |
35 this.id = payload.id; | |
36 this.nodeType = payload.nodeType; | |
37 this.nodeName = payload.nodeName; | |
38 this.localName = payload.localName; | |
39 this._nodeValue = payload.nodeValue; | |
40 this.textContent = this.nodeValue; | |
41 | |
42 this.attributes = []; | |
43 this._attributesMap = {}; | |
44 if (payload.attributes) | |
45 this._setAttributesPayload(payload.attributes); | |
46 | |
47 this._childNodeCount = payload.childNodeCount; | |
48 this.children = null; | |
49 | |
50 this.nextSibling = null; | |
51 this.prevSibling = null; | |
52 this.firstChild = null; | |
53 this.lastChild = null; | |
54 this.parentNode = null; | |
55 | |
56 if (payload.children) | |
57 this._setChildrenPayload(payload.children); | |
58 | |
59 this._computedStyle = null; | |
60 this.style = null; | |
61 this._matchedCSSRules = []; | |
62 | |
63 if (this.nodeType == Node.ELEMENT_NODE) { | |
64 if (this.nodeName == "HTML") | |
65 this.ownerDocument.documentElement = this; | |
66 if (this.nodeName == "BODY") | |
67 this.ownerDocument.body = this; | |
68 } | |
69 } | |
70 | |
71 WebInspector.DOMNode.prototype = { | |
72 hasAttributes: function() | |
73 { | |
74 return this.attributes.length > 0; | |
75 }, | |
76 | |
77 hasChildNodes: function() { | |
78 return this._childNodeCount > 0; | |
79 }, | |
80 | |
81 get nodeValue() { | |
82 return this._nodeValue; | |
83 }, | |
84 | |
85 set nodeValue(value) { | |
86 if (this.nodeType != Node.TEXT_NODE) | |
87 return; | |
88 var self = this; | |
89 var callback = function() | |
90 { | |
91 self._nodeValue = value; | |
92 self.textContent = value; | |
93 }; | |
94 this.ownerDocument._domAgent.setTextNodeValueAsync(this, value, callback
); | |
95 }, | |
96 | |
97 getAttribute: function(name) | |
98 { | |
99 var attr = this._attributesMap[name]; | |
100 return attr ? attr.value : undefined; | |
101 }, | |
102 | |
103 setAttribute: function(name, value) | |
104 { | |
105 var self = this; | |
106 var callback = function() | |
107 { | |
108 var attr = self._attributesMap[name]; | |
109 if (attr) | |
110 attr.value = value; | |
111 else | |
112 attr = self._addAttribute(name, value); | |
113 }; | |
114 this.ownerDocument._domAgent.setAttributeAsync(this, name, value, callba
ck); | |
115 }, | |
116 | |
117 removeAttribute: function(name) | |
118 { | |
119 var self = this; | |
120 var callback = function() | |
121 { | |
122 delete self._attributesMap[name]; | |
123 for (var i = 0; i < self.attributes.length; ++i) { | |
124 if (self.attributes[i].name == name) { | |
125 self.attributes.splice(i, 1); | |
126 break; | |
127 } | |
128 } | |
129 }; | |
130 this.ownerDocument._domAgent.removeAttributeAsync(this, name, callback); | |
131 }, | |
132 | |
133 _setAttributesPayload: function(attrs) | |
134 { | |
135 for (var i = 0; i < attrs.length; i += 2) | |
136 this._addAttribute(attrs[i], attrs[i + 1]); | |
137 }, | |
138 | |
139 _insertChild: function(prev, payload) | |
140 { | |
141 var node = new WebInspector.DOMNode(this.ownerDocument, payload); | |
142 if (!prev) | |
143 // First node | |
144 this.children = [ node ]; | |
145 else | |
146 this.children.splice(this.children.indexOf(prev) + 1, 0, node); | |
147 this._renumber(); | |
148 return node; | |
149 }, | |
150 | |
151 removeChild_: function(node) | |
152 { | |
153 this.children.splice(this.children.indexOf(node), 1); | |
154 node.parentNode = null; | |
155 this._renumber(); | |
156 }, | |
157 | |
158 _setChildrenPayload: function(payloads) | |
159 { | |
160 this.children = []; | |
161 for (var i = 0; i < payloads.length; ++i) { | |
162 var payload = payloads[i]; | |
163 var node = new WebInspector.DOMNode(this.ownerDocument, payload); | |
164 this.children.push(node); | |
165 } | |
166 this._renumber(); | |
167 }, | |
168 | |
169 _renumber: function() | |
170 { | |
171 this._childNodeCount = this.children.length; | |
172 if (this._childNodeCount == 0) { | |
173 this.firstChild = null; | |
174 this.lastChild = null; | |
175 return; | |
176 } | |
177 this.firstChild = this.children[0]; | |
178 this.lastChild = this.children[this._childNodeCount - 1]; | |
179 for (var i = 0; i < this._childNodeCount; ++i) { | |
180 var child = this.children[i]; | |
181 child.nextSibling = i + 1 < this._childNodeCount ? this.children[i +
1] : null; | |
182 child.prevSibling = i - 1 >= 0 ? this.children[i - 1] : null; | |
183 child.parentNode = this; | |
184 } | |
185 }, | |
186 | |
187 _addAttribute: function(name, value) | |
188 { | |
189 var attr = { | |
190 "name": name, | |
191 "value": value, | |
192 "_node": this | |
193 }; | |
194 this._attributesMap[name] = attr; | |
195 this.attributes.push(attr); | |
196 }, | |
197 | |
198 _setStyles: function(computedStyle, inlineStyle, styleAttributes, matchedCSS
Rules) | |
199 { | |
200 this._computedStyle = new WebInspector.CSSStyleDeclaration(computedStyle
); | |
201 this.style = new WebInspector.CSSStyleDeclaration(inlineStyle); | |
202 | |
203 for (var name in styleAttributes) { | |
204 if (this._attributesMap[name]) | |
205 this._attributesMap[name].style = new WebInspector.CSSStyleDecla
ration(styleAttributes[name]); | |
206 } | |
207 | |
208 this._matchedCSSRules = []; | |
209 for (var i = 0; i < matchedCSSRules.length; i++) | |
210 this._matchedCSSRules.push(WebInspector.CSSStyleDeclaration.parseRul
e(matchedCSSRules[i])); | |
211 }, | |
212 | |
213 _clearStyles: function() | |
214 { | |
215 this.computedStyle = null; | |
216 this.style = null; | |
217 for (var name in this._attributesMap) | |
218 this._attributesMap[name].style = null; | |
219 this._matchedCSSRules = null; | |
220 } | |
221 } | |
222 | |
223 WebInspector.DOMDocument = function(domAgent, defaultView, payload) | |
224 { | |
225 WebInspector.DOMNode.call(this, this, payload); | |
226 this._listeners = {}; | |
227 this._domAgent = domAgent; | |
228 this.defaultView = defaultView; | |
229 } | |
230 | |
231 WebInspector.DOMDocument.prototype = { | |
232 | |
233 addEventListener: function(name, callback) | |
234 { | |
235 var listeners = this._listeners[name]; | |
236 if (!listeners) { | |
237 listeners = []; | |
238 this._listeners[name] = listeners; | |
239 } | |
240 listeners.push(callback); | |
241 }, | |
242 | |
243 removeEventListener: function(name, callback) | |
244 { | |
245 var listeners = this._listeners[name]; | |
246 if (!listeners) | |
247 return; | |
248 | |
249 var index = listeners.indexOf(callback); | |
250 if (index != -1) | |
251 listeners.splice(index, 1); | |
252 }, | |
253 | |
254 _fireDomEvent: function(name, event) | |
255 { | |
256 var listeners = this._listeners[name]; | |
257 if (!listeners) | |
258 return; | |
259 | |
260 for (var i = 0; i < listeners.length; ++i) { | |
261 var listener = listeners[i]; | |
262 listener.call(this, event); | |
263 } | |
264 } | |
265 } | |
266 | |
267 WebInspector.DOMDocument.prototype.__proto__ = WebInspector.DOMNode.prototype; | |
268 | |
269 | |
270 WebInspector.DOMWindow = function(domAgent) | |
271 { | |
272 this._domAgent = domAgent; | |
273 } | |
274 | |
275 WebInspector.DOMWindow.prototype = { | |
276 get document() | |
277 { | |
278 return this._domAgent.document; | |
279 }, | |
280 | |
281 get Node() | |
282 { | |
283 return WebInspector.DOMNode; | |
284 }, | |
285 | |
286 get Element() | |
287 { | |
288 return WebInspector.DOMNode; | |
289 }, | |
290 | |
291 Object: function() | |
292 { | |
293 }, | |
294 | |
295 getComputedStyle: function(node) | |
296 { | |
297 return node._computedStyle; | |
298 }, | |
299 | |
300 getMatchedCSSRules: function(node, pseudoElement, authorOnly) | |
301 { | |
302 return node._matchedCSSRules; | |
303 } | |
304 } | |
305 | |
306 WebInspector.DOMAgent = function() { | |
307 this._window = new WebInspector.DOMWindow(this); | |
308 this._idToDOMNode = null; | |
309 this.document = null; | |
310 } | |
311 | |
312 WebInspector.DOMAgent.prototype = { | |
313 get domWindow() | |
314 { | |
315 return this._window; | |
316 }, | |
317 | |
318 getChildNodesAsync: function(parent, callback) | |
319 { | |
320 var children = parent.children; | |
321 if (children) { | |
322 callback(children); | |
323 return; | |
324 } | |
325 function mycallback() { | |
326 callback(parent.children); | |
327 } | |
328 var callId = WebInspector.Callback.wrap(mycallback); | |
329 InspectorController.getChildNodes(callId, parent.id); | |
330 }, | |
331 | |
332 setAttributeAsync: function(node, name, value, callback) | |
333 { | |
334 var mycallback = this._didApplyDomChange.bind(this, node, callback); | |
335 InspectorController.setAttribute(WebInspector.Callback.wrap(mycallback),
node.id, name, value); | |
336 }, | |
337 | |
338 removeAttributeAsync: function(node, name, callback) | |
339 { | |
340 var mycallback = this._didApplyDomChange.bind(this, node, callback); | |
341 InspectorController.removeAttribute(WebInspector.Callback.wrap(mycallbac
k), node.id, name); | |
342 }, | |
343 | |
344 setTextNodeValueAsync: function(node, text, callback) | |
345 { | |
346 var mycallback = this._didApplyDomChange.bind(this, node, callback); | |
347 InspectorController.setTextNodeValue(WebInspector.Callback.wrap(mycallba
ck), node.id, text); | |
348 }, | |
349 | |
350 _didApplyDomChange: function(node, callback, success) | |
351 { | |
352 if (!success) | |
353 return; | |
354 callback(); | |
355 // TODO(pfeldman): Fix this hack. | |
356 var elem = WebInspector.panels.elements.treeOutline.findTreeElement(node
); | |
357 if (elem) { | |
358 elem._updateTitle(); | |
359 } | |
360 }, | |
361 | |
362 _attributesUpdated: function(nodeId, attrsArray) | |
363 { | |
364 var node = this._idToDOMNode[nodeId]; | |
365 node._setAttributesPayload(attrsArray); | |
366 }, | |
367 | |
368 nodeForId: function(nodeId) { | |
369 return this._idToDOMNode[nodeId]; | |
370 }, | |
371 | |
372 _setDocument: function(payload) | |
373 { | |
374 this._idToDOMNode = {}; | |
375 if (payload) { | |
376 this.document = new WebInspector.DOMDocument(this, this._window, pay
load); | |
377 this._idToDOMNode[payload.id] = this.document; | |
378 this._bindNodes(this.document.children); | |
379 } else | |
380 this.document = null; | |
381 WebInspector.panels.elements.reset(); | |
382 }, | |
383 | |
384 _setDetachedRoot: function(payload) | |
385 { | |
386 var root = new WebInspector.DOMNode(this.document, payload); | |
387 this._idToDOMNode[payload.id] = root; | |
388 }, | |
389 | |
390 _setChildNodes: function(parentId, payloads) | |
391 { | |
392 var parent = this._idToDOMNode[parentId]; | |
393 parent._setChildrenPayload(payloads); | |
394 this._bindNodes(parent.children); | |
395 }, | |
396 | |
397 _bindNodes: function(children) | |
398 { | |
399 for (var i = 0; i < children.length; ++i) { | |
400 var child = children[i]; | |
401 this._idToDOMNode[child.id] = child; | |
402 if (child.children) | |
403 this._bindNodes(child.children); | |
404 } | |
405 }, | |
406 | |
407 _childNodeCountUpdated: function(nodeId, newValue) | |
408 { | |
409 var node = this._idToDOMNode[nodeId]; | |
410 node._childNodeCount = newValue; | |
411 var outline = WebInspector.panels.elements.treeOutline; | |
412 var treeElement = outline.findTreeElement(node); | |
413 if (treeElement) | |
414 treeElement.hasChildren = newValue; | |
415 }, | |
416 | |
417 _childNodeInserted: function(parentId, prevId, payload) | |
418 { | |
419 var parent = this._idToDOMNode[parentId]; | |
420 var prev = this._idToDOMNode[prevId]; | |
421 var node = parent._insertChild(prev, payload); | |
422 this._idToDOMNode[node.id] = node; | |
423 var event = { target : node, relatedNode : parent }; | |
424 this.document._fireDomEvent("DOMNodeInserted", event); | |
425 }, | |
426 | |
427 _childNodeRemoved: function(parentId, nodeId) | |
428 { | |
429 var parent = this._idToDOMNode[parentId]; | |
430 var node = this._idToDOMNode[nodeId]; | |
431 parent.removeChild_(node); | |
432 var event = { target : node, relatedNode : parent }; | |
433 this.document._fireDomEvent("DOMNodeRemoved", event); | |
434 delete this._idToDOMNode[nodeId]; | |
435 } | |
436 } | |
437 | |
438 WebInspector.Cookies = {} | |
439 | |
440 WebInspector.Cookies.getCookiesAsync = function(callback, cookieDomain) | |
441 { | |
442 function mycallback(cookies, cookiesString) { | |
443 if (cookiesString) | |
444 callback(WebInspector.Cookies.buildCookiesFromString(cookiesString),
false); | |
445 else | |
446 callback(cookies, true); | |
447 } | |
448 var callId = WebInspector.Callback.wrap(mycallback); | |
449 InspectorController.getCookies(callId, cookieDomain); | |
450 } | |
451 | |
452 WebInspector.Cookies.buildCookiesFromString = function(rawCookieString) | |
453 { | |
454 var rawCookies = rawCookieString.split(/;\s*/); | |
455 var cookies = []; | |
456 | |
457 if (!(/^\s*$/.test(rawCookieString))) { | |
458 for (var i = 0; i < rawCookies.length; ++i) { | |
459 var cookie = rawCookies[i]; | |
460 var delimIndex = cookie.indexOf("="); | |
461 var name = cookie.substring(0, delimIndex); | |
462 var value = cookie.substring(delimIndex + 1); | |
463 var size = name.length + value.length; | |
464 cookies.push({ name: name, value: value, size: size }); | |
465 } | |
466 } | |
467 | |
468 return cookies; | |
469 } | |
470 | |
471 WebInspector.EventListeners = {} | |
472 | |
473 WebInspector.EventListeners.getEventListenersForNodeAsync = function(node, callb
ack) | |
474 { | |
475 if (!node) | |
476 return; | |
477 | |
478 var callId = WebInspector.Callback.wrap(callback); | |
479 InspectorController.getEventListenersForNode(callId, node.id); | |
480 } | |
481 | |
482 WebInspector.CSSStyleDeclaration = function(payload) | |
483 { | |
484 this.id = payload.id; | |
485 this.width = payload.width; | |
486 this.height = payload.height; | |
487 this.__disabledProperties = payload.__disabledProperties; | |
488 this.__disabledPropertyValues = payload.__disabledPropertyValues; | |
489 this.__disabledPropertyPriorities = payload.__disabledPropertyPriorities; | |
490 this.uniqueStyleProperties = payload.uniqueStyleProperties; | |
491 this._shorthandValues = payload.shorthandValues; | |
492 this._propertyMap = {}; | |
493 this._longhandProperties = {}; | |
494 this.length = payload.properties.length; | |
495 | |
496 for (var i = 0; i < this.length; ++i) { | |
497 var property = payload.properties[i]; | |
498 var name = property.name; | |
499 this[i] = name; | |
500 this._propertyMap[name] = property; | |
501 } | |
502 | |
503 // Index longhand properties. | |
504 for (var i = 0; i < this.uniqueStyleProperties.length; ++i) { | |
505 var name = this.uniqueStyleProperties[i]; | |
506 var property = this._propertyMap[name]; | |
507 if (property.shorthand) { | |
508 var longhands = this._longhandProperties[property.shorthand]; | |
509 if (!longhands) { | |
510 longhands = []; | |
511 this._longhandProperties[property.shorthand] = longhands; | |
512 } | |
513 longhands.push(name); | |
514 } | |
515 } | |
516 } | |
517 | |
518 WebInspector.CSSStyleDeclaration.parseStyle = function(payload) | |
519 { | |
520 return new WebInspector.CSSStyleDeclaration(payload); | |
521 } | |
522 | |
523 WebInspector.CSSStyleDeclaration.parseRule = function(payload) | |
524 { | |
525 var rule = {}; | |
526 rule.id = payload.id; | |
527 rule.selectorText = payload.selectorText; | |
528 rule.style = new WebInspector.CSSStyleDeclaration(payload.style); | |
529 rule.style.parentRule = rule; | |
530 rule.isUserAgent = payload.isUserAgent; | |
531 rule.isUser = payload.isUser; | |
532 rule.isViaInspector = payload.isViaInspector; | |
533 if (payload.parentStyleSheet) | |
534 rule.parentStyleSheet = { href: payload.parentStyleSheet.href }; | |
535 | |
536 return rule; | |
537 } | |
538 | |
539 WebInspector.CSSStyleDeclaration.prototype = { | |
540 getPropertyValue: function(name) | |
541 { | |
542 var property = this._propertyMap[name]; | |
543 return property ? property.value : ""; | |
544 }, | |
545 | |
546 getPropertyPriority: function(name) | |
547 { | |
548 var property = this._propertyMap[name]; | |
549 return property ? property.priority : ""; | |
550 }, | |
551 | |
552 getPropertyShorthand: function(name) | |
553 { | |
554 var property = this._propertyMap[name]; | |
555 return property ? property.shorthand : ""; | |
556 }, | |
557 | |
558 isPropertyImplicit: function(name) | |
559 { | |
560 var property = this._propertyMap[name]; | |
561 return property ? property.implicit : ""; | |
562 }, | |
563 | |
564 styleTextWithShorthands: function() | |
565 { | |
566 var cssText = ""; | |
567 var foundProperties = {}; | |
568 for (var i = 0; i < this.length; ++i) { | |
569 var individualProperty = this[i]; | |
570 var shorthandProperty = this.getPropertyShorthand(individualProperty
); | |
571 var propertyName = (shorthandProperty || individualProperty); | |
572 | |
573 if (propertyName in foundProperties) | |
574 continue; | |
575 | |
576 if (shorthandProperty) { | |
577 var value = this.getPropertyValue(shorthandProperty); | |
578 var priority = this.getShorthandPriority(shorthandProperty); | |
579 } else { | |
580 var value = this.getPropertyValue(individualProperty); | |
581 var priority = this.getPropertyPriority(individualProperty); | |
582 } | |
583 | |
584 foundProperties[propertyName] = true; | |
585 | |
586 cssText += propertyName + ": " + value; | |
587 if (priority) | |
588 cssText += " !" + priority; | |
589 cssText += "; "; | |
590 } | |
591 | |
592 return cssText; | |
593 }, | |
594 | |
595 getLonghandProperties: function(name) | |
596 { | |
597 return this._longhandProperties[name] || []; | |
598 }, | |
599 | |
600 getShorthandValue: function(shorthandProperty) | |
601 { | |
602 return this._shorthandValues[shorthandProperty]; | |
603 }, | |
604 | |
605 getShorthandPriority: function(shorthandProperty) | |
606 { | |
607 var priority = this.getPropertyPriority(shorthandProperty); | |
608 if (priority) | |
609 return priority; | |
610 | |
611 var longhands = this._longhandProperties[shorthandProperty]; | |
612 return longhands ? this.getPropertyPriority(longhands[0]) : null; | |
613 } | |
614 } | |
615 | |
616 WebInspector.attributesUpdated = function() | |
617 { | |
618 this.domAgent._attributesUpdated.apply(this.domAgent, arguments); | |
619 } | |
620 | |
621 WebInspector.setDocument = function() | |
622 { | |
623 this.domAgent._setDocument.apply(this.domAgent, arguments); | |
624 } | |
625 | |
626 WebInspector.setDetachedRoot = function() | |
627 { | |
628 this.domAgent._setDetachedRoot.apply(this.domAgent, arguments); | |
629 } | |
630 | |
631 WebInspector.setChildNodes = function() | |
632 { | |
633 this.domAgent._setChildNodes.apply(this.domAgent, arguments); | |
634 } | |
635 | |
636 WebInspector.childNodeCountUpdated = function() | |
637 { | |
638 this.domAgent._childNodeCountUpdated.apply(this.domAgent, arguments); | |
639 } | |
640 | |
641 WebInspector.childNodeInserted = function() | |
642 { | |
643 this.domAgent._childNodeInserted.apply(this.domAgent, arguments); | |
644 } | |
645 | |
646 WebInspector.childNodeRemoved = function() | |
647 { | |
648 this.domAgent._childNodeRemoved.apply(this.domAgent, arguments); | |
649 } | |
650 | |
651 WebInspector.didGetCookies = WebInspector.Callback.processCallback; | |
652 WebInspector.didGetChildNodes = WebInspector.Callback.processCallback; | |
653 WebInspector.didPerformSearch = WebInspector.Callback.processCallback; | |
654 WebInspector.didApplyDomChange = WebInspector.Callback.processCallback; | |
655 WebInspector.didRemoveAttribute = WebInspector.Callback.processCallback; | |
656 WebInspector.didSetTextNodeValue = WebInspector.Callback.processCallback; | |
657 WebInspector.didGetEventListenersForNode = WebInspector.Callback.processCallback
; | |
OLD | NEW |