[winforms] Style
[mono.git] / mcs / class / Mono.WebBrowser / Mono.Mozilla / DOM / HTMLElement.cs
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:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
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.
19 //
20 // Copyright (c) 2007, 2008 Novell, Inc.
21 //
22 // Authors:
23 //      Andreia Gaita (avidigal@novell.com)
24 //
25
26 using System;
27 using System.Runtime.InteropServices;
28 using System.Text;
29 using io=System.IO;
30 using Mono.WebBrowser;
31 using Mono.WebBrowser.DOM;
32
33 namespace Mono.Mozilla.DOM
34 {
35         internal class HTMLElement : Element, IElement
36         {
37                 protected nsIDOMHTMLElement node {
38                         get { return base.node as nsIDOMHTMLElement; }
39                         set { base.node = value as nsIDOMElement; }
40                 }
41
42                 public HTMLElement (WebBrowser control, nsIDOMHTMLElement domHtmlElement) : base (control, domHtmlElement as nsIDOMElement)
43                 {
44                         this.node = domHtmlElement;
45                 }
46
47                 #region IDisposable Members
48                 protected override  void Dispose (bool disposing)
49                 {
50                         if (!disposed) {
51                                 if (disposing) {
52                                         this.node = null;
53                                 }
54                         }
55                         base.Dispose(disposing);
56                 }               
57                 #endregion
58
59                 #region IElement Members
60                 public new string InnerHTML
61                 {
62                         get {
63                                 nsIDOMNSHTMLElement nsElem = this.node as nsIDOMNSHTMLElement;
64                                 if (nsElem == null)
65                                         return null;
66                                 nsElem.getInnerHTML (storage);
67                                 return Base.StringGet (storage);
68                         }
69                         set {
70                                 nsIDOMNSHTMLElement nsElem = this.node as nsIDOMNSHTMLElement;
71                                 if (nsElem == null)
72                                         return;
73                                 Base.StringSet (storage, value);
74                                 nsElem.setInnerHTML (storage);
75                         }
76                 }
77
78                 public override string OuterHTML
79                 {
80                         // bad emulation of outerHTML since gecko doesn't support it :P
81                         get {
82                                 try {
83                                         control.DocEncoder.Flags = DocumentEncoderFlags.OutputRaw;
84                                         if (this.Equals (Owner.DocumentElement))
85                                                 return control.DocEncoder.EncodeToString ((Document) Owner);
86                                         return control.DocEncoder.EncodeToString (this);
87                                 } catch {
88                                         string tag = this.TagName;
89                                         string str = "<" + tag;
90                                         foreach (IAttribute att in this.Attributes) {
91                                                 str += " " + att.Name + "=\"" + att.Value + "\"";
92                                         }
93                                         nsIDOMNSHTMLElement nsElem = this.node as nsIDOMNSHTMLElement;
94                                         nsElem.getInnerHTML (storage);
95                                         str += ">" + Base.StringGet (storage) + "</" + tag + ">";
96                                         return str;
97                                 }
98                         }
99                         set {
100                                 nsIDOMDocumentRange docRange = ((Document) control.Document).XPComObject as nsIDOMDocumentRange;
101                                 nsIDOMRange range;
102                                 docRange.createRange (out range);
103                                 range.setStartBefore (this.node);
104                                 nsIDOMNSRange nsRange = range as nsIDOMNSRange;
105                                 Base.StringSet (storage, value);
106                                 nsIDOMDocumentFragment fragment;
107                                 nsRange.createContextualFragment (storage, out fragment);
108                                 nsIDOMNode parent;
109                                 this.node.getParentNode (out parent);
110                                 parent = nsDOMNode.GetProxy (this.control, parent);
111                                 nsIDOMNode newNode;
112                                 parent.replaceChild (fragment as nsIDOMNode, this.node as nsIDOMNode, out newNode);
113                                 this.node = newNode as Mono.Mozilla.nsIDOMHTMLElement;
114                         }
115                 }
116
117                 public override io.Stream ContentStream {
118                         get {
119                                 try {
120                                         control.DocEncoder.Flags = DocumentEncoderFlags.OutputRaw;
121                                         if (this.Equals (Owner.DocumentElement))
122                                                 return control.DocEncoder.EncodeToStream ((Document) Owner);
123                                         return control.DocEncoder.EncodeToStream (this);
124                                 } catch {
125                                         string tag = this.TagName;
126                                         string str = "<" + tag;
127                                         foreach (IAttribute att in this.Attributes) {
128                                                 str += " " + att.Name + "=\"" + att.Value + "\"";
129                                         }
130                                         nsIDOMNSHTMLElement nsElem = this.node as nsIDOMNSHTMLElement;
131                                         nsElem.getInnerHTML (storage);
132                                         str += ">" + Base.StringGet (storage) + "</" + tag + ">";
133                                         byte[] bytes = System.Text.ASCIIEncoding.UTF8.GetBytes (str);
134                                         return new io.MemoryStream (bytes);
135                                 }
136                         }
137                 }
138
139                 
140                 public override bool Disabled
141                 {                       
142                         get {
143                                 if (this.HasAttribute ("disabled")) {
144                                         string dis = this.GetAttribute ("disabled");
145                                         return bool.Parse (dis);
146                                 }
147                                 return false;
148                         }
149                         set {
150                                 if (this.HasAttribute ("disabled")) {
151                                         this.SetAttribute ("disabled", value.ToString ());
152                                 }
153                         }
154                 }
155
156                 public override int TabIndex {
157                         get { 
158                                 int tabIndex;
159                                 ((nsIDOMNSHTMLElement)this.node).getTabIndex (out tabIndex);
160                                 return tabIndex;
161                         }
162                         set { 
163                                 ((nsIDOMNSHTMLElement)this.node).setTabIndex (value);
164                         }
165                 }
166
167                 public override int GetHashCode () {
168                         return this.hashcode;
169                 }
170                 #endregion
171         }
172 }