1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2008 Novell, Inc.
23 // Andreia Gaita (avidigal@novell.com)
27 using System.Runtime.InteropServices;
28 using Mono.WebBrowser;
29 using Mono.WebBrowser.DOM;
31 namespace Mono.Mozilla.DOM
33 internal class Element : Node, IElement
35 internal nsIDOMElement node {
36 get { return base.node as nsIDOMElement;}
37 set { base.node = value as nsIDOMNode; }
40 public Element(WebBrowser control, nsIDOMElement domElement) : base (control, domElement as nsIDOMNode)
42 this.node = domElement;
45 #region IDisposable Members
46 protected override void Dispose (bool disposing)
53 base.Dispose(disposing);
58 public virtual IElement AppendChild (IElement child) {
60 Element elem = (Element) child;
61 this.node.appendChild (elem.node, out newChild);
62 return new Element (control, newChild as nsIDOMElement);
65 public virtual string InnerText
69 nsIDOMDocumentRange docRange = ((Document) control.Document).XPComObject as nsIDOMDocumentRange;
71 docRange.createRange (out range);
72 range.selectNodeContents (this.node);
73 range.toString (storage);
74 return Base.StringGet (storage);
78 Base.StringSet (storage, value);
79 this.node.setNodeValue (storage);
83 public virtual string OuterText
87 nsIDOMDocumentRange docRange = ((Document) control.Document).XPComObject as nsIDOMDocumentRange;
89 docRange.createRange (out range);
91 node.getParentNode (out parent);
92 range.selectNodeContents (parent);
93 range.toString (storage);
94 return Base.StringGet (storage);
98 Base.StringSet (storage, value);
100 node.getParentNode (out parent);
101 parent.setNodeValue (storage);
105 public virtual string InnerHTML {
106 get { return String.Empty; }
110 public virtual string OuterHTML {
111 get { return String.Empty; }
115 public IElementCollection All {
118 if (!resources.Contains ("All")) {
120 HTMLElementCollection col = new HTMLElementCollection (control);
121 Recurse (col, this.node);
122 resources.Add ("All", col);
124 return resources["All"] as IElementCollection;
128 private void Recurse (HTMLElementCollection col, nsIDOMNode parent) {
129 nsIDOMNodeList children;
130 parent.getChildNodes (out children);
132 children.getLength (out count);
134 for (int i = 0; i < count;i++) {
136 children.item ((uint)i, out node);
138 node.getNodeType (out type);
139 if (type == (ushort)NodeType.Element) {
140 col.Add (new HTMLElement (control, (nsIDOMHTMLElement)node));
147 public IElementCollection Children {
150 if (!resources.Contains ("Children")) {
151 nsIDOMNodeList children;
152 this.node.getChildNodes (out children);
153 resources.Add ("Children", new HTMLElementCollection (control, children));
155 return resources["Children"] as IElementCollection;
160 public virtual int TabIndex {
165 public virtual string TagName {
167 node.getTagName (storage);
168 return Base.StringGet (storage);
172 public virtual bool Disabled {
173 get { return false; }
178 public virtual int ClientWidth {
180 nsIDOMNSHTMLElement e = this.node as nsIDOMNSHTMLElement;
182 e.getClientWidth (out ret);
186 public virtual int ClientHeight {
188 nsIDOMNSHTMLElement e = this.node as nsIDOMNSHTMLElement;
190 e.getClientHeight (out ret);
194 public virtual int ScrollHeight {
196 nsIDOMNSHTMLElement e = this.node as nsIDOMNSHTMLElement;
198 e.getScrollHeight(out ret);
202 public virtual int ScrollWidth {
204 nsIDOMNSHTMLElement e = this.node as nsIDOMNSHTMLElement;
206 e.getScrollWidth (out ret);
210 public virtual int ScrollLeft {
212 nsIDOMNSHTMLElement e = this.node as nsIDOMNSHTMLElement;
214 e.getScrollLeft (out ret);
218 nsIDOMNSHTMLElement e = this.node as nsIDOMNSHTMLElement;
219 e.setScrollLeft (value);
222 public virtual int ScrollTop {
224 nsIDOMNSHTMLElement e = this.node as nsIDOMNSHTMLElement;
226 e.getScrollTop (out ret);
230 nsIDOMNSHTMLElement e = this.node as nsIDOMNSHTMLElement;
231 e.setScrollTop (value);
234 public virtual int OffsetHeight {
236 nsIDOMNSHTMLElement e = this.node as nsIDOMNSHTMLElement;
238 e.getOffsetHeight (out ret);
242 public virtual int OffsetWidth {
244 nsIDOMNSHTMLElement e = this.node as nsIDOMNSHTMLElement;
246 e.getOffsetWidth (out ret);
250 public virtual int OffsetLeft {
252 nsIDOMNSHTMLElement e = this.node as nsIDOMNSHTMLElement;
254 e.getOffsetLeft (out ret);
258 public virtual int OffsetTop {
260 nsIDOMNSHTMLElement e = this.node as nsIDOMNSHTMLElement;
262 e.getOffsetTop (out ret);
267 public virtual IElement OffsetParent {
269 nsIDOMNSHTMLElement e = this.node as nsIDOMNSHTMLElement;
271 e.getOffsetParent (out ret);
272 if ((ret as nsIDOMHTMLElement) != null)
273 return new HTMLElement (this.control, ret as nsIDOMHTMLElement);
274 return new Element (this.control, ret);
283 public void Blur () {
284 nsIDOMNSHTMLElement elm = node as nsIDOMNSHTMLElement;
290 public void Focus () {
291 nsIDOMNSHTMLElement elm = node as nsIDOMNSHTMLElement;
297 public IElementCollection GetElementsByTagName (string name)
299 if (!resources.Contains ("GetElementsByTagName" + name)) {
300 nsIDOMNodeList nodes;
301 this.node.getElementsByTagName (storage, out nodes);
302 resources.Add ("GetElementsByTagName" + name, new HTMLElementCollection(control, nodes));
304 return resources["GetElementsByTagName" + name] as IElementCollection;
307 public override int GetHashCode () {
308 return this.hashcode;
311 public virtual bool HasAttribute (string name)
314 Base.StringSet (storage, name);
315 node.hasAttribute (storage, out ret);
319 public virtual string GetAttribute (string name) {
320 UniString ret = new UniString (String.Empty);
321 Base.StringSet (storage, name);
322 node.getAttribute (storage, ret.Handle);
323 return ret.ToString ();
326 public void ScrollIntoView (bool alignWithTop)
328 nsIDOMNSHTMLElement elm = node as nsIDOMNSHTMLElement;
330 elm.scrollIntoView (alignWithTop);
334 public virtual void SetAttribute (string name, string value) {
335 UniString strVal = new UniString (value);
336 Base.StringSet (storage, name);
337 node.setAttribute (storage, strVal.Handle);
345 ((nsIDOMNSHTMLElement)this.node).getOffsetTop (out ret);
353 ((nsIDOMNSHTMLElement)this.node).getOffsetLeft (out ret);
361 ((nsIDOMNSHTMLElement)this.node).getOffsetWidth (out ret);
366 internal int Height {
369 ((nsIDOMNSHTMLElement)this.node).getOffsetHeight (out ret);