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) 2007, 2008 Novell, Inc.
23 // Andreia Gaita (avidigal@novell.com)
27 using System
.Runtime
.InteropServices
;
29 using System
.Collections
;
30 using Mono
.WebBrowser
;
31 using Mono
.WebBrowser
.DOM
;
33 namespace Mono
.Mozilla
.DOM
35 internal class Document
: Node
, IDocument
38 internal nsIDOMDocument node
{
39 get { return base.node as nsIDOMDocument;}
40 set { base.node = value as nsIDOMDocument; }
43 public Document (WebBrowser control
, nsIDOMHTMLDocument document
)
44 : base (control
, document
)
46 if (control
.platform
!= control
.enginePlatform
)
47 this.node
= nsDOMHTMLDocument
.GetProxy (control
, document
);
52 public Document (WebBrowser control
, nsIDOMDocument document
)
53 : base (control
, document
)
55 if (control
.platform
!= control
.enginePlatform
)
56 this.node
= nsDOMDocument
.GetProxy (control
, document
);
61 #region IDisposable Members
62 protected override void Dispose (bool disposing
)
66 this.resources
.Clear ();
70 base.Dispose (disposing
);
75 internal new nsIDOMDocument XPComObject
77 get { return this.node; }
81 #region IDocument Properties
83 public IElement Active
{
85 nsIDOMElement element
;
86 nsIWebBrowserFocus webBrowserFocus
= (nsIWebBrowserFocus
) (control
.navigation
.navigation
);
87 if (webBrowserFocus
== null)
89 webBrowserFocus
.getFocusedElement (out element
);
90 return (IElement
)GetTypedNode (element
);
91 // if ((element as nsIDOMHTMLElement) != null)
92 // return new HTMLElement (control, element as nsIDOMHTMLElement) as IElement;
93 // return new Element (control, element) as IElement;
97 public string ActiveLinkColor
{
99 if (!(node
is nsIDOMHTMLDocument
))
101 nsIDOMHTMLElement element
;
102 ((nsIDOMHTMLDocument
)node
).getBody (out element
);
103 ((nsIDOMHTMLBodyElement
)element
).getALink(storage
);
104 return Base
.StringGet (storage
);
107 if (!(node
is nsIDOMHTMLDocument
))
109 Base
.StringSet (storage
, value);
110 nsIDOMHTMLElement element
;
111 ((nsIDOMHTMLDocument
)node
).getBody (out element
);
112 ((nsIDOMHTMLBodyElement
)element
).setALink(storage
);
116 public IElementCollection Anchors
{
118 if (!(node
is nsIDOMHTMLDocument
))
120 nsIDOMHTMLCollection ret
;
121 ((nsIDOMHTMLDocument
)node
).getAnchors (out ret
);
122 return new HTMLElementCollection(control
, (nsIDOMNodeList
)ret
);
127 public IElementCollection Applets
{
129 if (!(node
is nsIDOMHTMLDocument
))
131 nsIDOMHTMLCollection ret
;
132 ((nsIDOMHTMLDocument
)node
).getApplets (out ret
);
133 return new HTMLElementCollection(control
, (nsIDOMNodeList
)ret
);
138 public string Background
{
140 if (!(node
is nsIDOMHTMLDocument
))
142 nsIDOMHTMLElement element
;
143 ((nsIDOMHTMLDocument
)node
).getBody (out element
);
144 ((nsIDOMHTMLBodyElement
)element
).getBackground(storage
);
145 return Base
.StringGet (storage
);
148 if (!(node
is nsIDOMHTMLDocument
))
150 Base
.StringSet (storage
, value);
151 nsIDOMHTMLElement element
;
152 ((nsIDOMHTMLDocument
)node
).getBody (out element
);
153 ((nsIDOMHTMLBodyElement
)element
).setBackground(storage
);
157 public string BackColor
{
159 if (!(node
is nsIDOMHTMLDocument
))
161 nsIDOMHTMLElement element
;
162 ((nsIDOMHTMLDocument
)node
).getBody (out element
);
163 ((nsIDOMHTMLBodyElement
)element
).getBgColor(storage
);
164 return Base
.StringGet (storage
);
167 if (!(node
is nsIDOMHTMLDocument
))
169 Base
.StringSet (storage
, value);
170 nsIDOMHTMLElement element
;
171 ((nsIDOMHTMLDocument
)node
).getBody (out element
);
172 ((nsIDOMHTMLBodyElement
)element
).setBgColor(storage
);
176 public IElement Body
{
178 if (!(node
is nsIDOMHTMLDocument
))
181 if (!resources
.Contains ("Body")) {
182 nsIDOMHTMLElement element
;
183 ((nsIDOMHTMLDocument
)node
).getBody (out element
);
184 resources
.Add ("Body", GetTypedNode (element
));
186 return resources
["Body"] as IElement
;
190 // This is the most ugly code *ever*. TODO: find out why a simple thing as getting the
191 // document encoding is not available in any frozen public interface
192 public string Charset
{
194 nsIDOMDocumentView docView
= (nsIDOMDocumentView
) this.node
;
195 nsIDOMAbstractView abstractView
;
196 docView
.getDefaultView (out abstractView
);
198 nsIInterfaceRequestor requestor
= (nsIInterfaceRequestor
) abstractView
;
201 requestor
.getInterface (typeof(nsIDocCharset
).GUID
, out ret
);
202 nsIDocCharset charset
= (nsIDocCharset
) Marshal
.GetObjectForIUnknown (ret
);
204 StringBuilder s
= new StringBuilder (30);
205 IntPtr r
= Marshal
.StringToHGlobalUni (s
.ToString ());
206 charset
.getCharset (ref r
);
208 return Marshal
.PtrToStringAnsi (r
);
211 nsIDOMDocumentView docView
= (nsIDOMDocumentView
) this.node
;
212 nsIDOMAbstractView abstractView
;
213 docView
.getDefaultView (out abstractView
);
215 nsIInterfaceRequestor requestor
= (nsIInterfaceRequestor
) abstractView
;
218 requestor
.getInterface (typeof (nsIDocCharset
).GUID
, out ret
);
219 nsIDocCharset charset
= (nsIDocCharset
) Marshal
.GetTypedObjectForIUnknown (ret
, typeof (nsIDocCharset
));
221 charset
.setCharset (value);
222 control
.navigation
.Go (this.Url
, LoadFlags
.CharsetChange
);
226 public string Cookie
{
228 if (!(node
is nsIDOMHTMLDocument
))
230 ((nsIDOMHTMLDocument
)node
).getCookie (storage
);
231 return Base
.StringGet (storage
);
234 if (!(node
is nsIDOMHTMLDocument
))
236 Base
.StringSet (storage
, value);
237 ((nsIDOMHTMLDocument
)node
).setCookie (storage
);
240 public string Domain
{
242 if (!(node
is nsIDOMHTMLDocument
))
244 ((nsIDOMHTMLDocument
)node
).getDomain (storage
);
245 return Base
.StringGet (storage
);
248 public IElement DocumentElement
{
250 if (!resources
.Contains ("DocumentElement")) {
251 nsIDOMElement element
;
252 this.node
.getDocumentElement (out element
);
253 resources
.Add ("DocumentElement", GetTypedNode (element
));
255 return resources
["DocumentElement"] as IElement
;
259 public IDocumentType DocType
{
261 nsIDOMDocumentType doctype
;
262 this.node
.getDoctype (out doctype
);
263 return new DocumentType (this.control
, doctype
);
268 public string ForeColor
{
270 if (!(node
is nsIDOMHTMLDocument
))
272 nsIDOMHTMLElement element
;
273 ((nsIDOMHTMLDocument
)node
).getBody (out element
);
274 ((nsIDOMHTMLBodyElement
)element
).getText(storage
);
275 return Base
.StringGet (storage
);
278 if (!(node
is nsIDOMHTMLDocument
))
280 Base
.StringSet (storage
, value);
281 nsIDOMHTMLElement element
;
282 ((nsIDOMHTMLDocument
)node
).getBody (out element
);
283 ((nsIDOMHTMLBodyElement
)element
).setText(storage
);
287 public IElementCollection Forms
{
289 if (!(node
is nsIDOMHTMLDocument
))
291 nsIDOMHTMLCollection ret
;
292 ((nsIDOMHTMLDocument
)node
).getForms (out ret
);
293 return new HTMLElementCollection(control
, (nsIDOMNodeList
)ret
);
298 public IElementCollection Images
{
300 if (!(node
is nsIDOMHTMLDocument
))
302 nsIDOMHTMLCollection ret
;
303 ((nsIDOMHTMLDocument
)node
).getImages (out ret
);
304 return new HTMLElementCollection(control
, (nsIDOMNodeList
)ret
);
309 public IDOMImplementation Implementation
{
311 nsIDOMDOMImplementation implementation
;
312 node
.getImplementation (out implementation
);
313 return new DOMImplementation (this.control
, implementation
);
317 public string LinkColor
{
319 if (!(node
is nsIDOMHTMLDocument
))
321 nsIDOMHTMLElement element
;
322 ((nsIDOMHTMLDocument
)node
).getBody (out element
);
323 ((nsIDOMHTMLBodyElement
)element
).getLink(storage
);
324 return Base
.StringGet (storage
);
327 if (!(node
is nsIDOMHTMLDocument
))
329 Base
.StringSet (storage
, value);
330 nsIDOMHTMLElement element
;
331 ((nsIDOMHTMLDocument
)node
).getBody (out element
);
332 ((nsIDOMHTMLBodyElement
)element
).setLink(storage
);
336 public IElementCollection Links
{
338 if (!(node
is nsIDOMHTMLDocument
))
340 nsIDOMHTMLCollection ret
;
341 ((nsIDOMHTMLDocument
)node
).getLinks (out ret
);
342 return new HTMLElementCollection(control
, (nsIDOMNodeList
)ret
);
347 public IStylesheetList Stylesheets
350 nsIDOMStyleSheetList styleList
;
351 nsIDOMDocumentStyle docStyle
= ((nsIDOMDocumentStyle
)this.node
);
352 docStyle
.getStyleSheets (out styleList
);
353 return new StylesheetList (this.control
, styleList
);
357 public string Title
{
359 if (!(node
is nsIDOMHTMLDocument
))
361 ((nsIDOMHTMLDocument
)node
).getTitle (storage
);
362 return Base
.StringGet (storage
);
365 Base
.StringSet (storage
, value);
366 ((nsIDOMHTMLDocument
)node
).setTitle (storage
);
372 if (!(node
is nsIDOMHTMLDocument
))
374 ((nsIDOMHTMLDocument
)node
).getURL (storage
);
375 return Base
.StringGet (storage
);
379 public string VisitedLinkColor
{
381 if (!(node
is nsIDOMHTMLDocument
))
383 nsIDOMHTMLElement element
;
384 ((nsIDOMHTMLDocument
)node
).getBody (out element
);
385 ((nsIDOMHTMLBodyElement
)element
).getVLink(storage
);
386 return Base
.StringGet (storage
);
389 if (!(node
is nsIDOMHTMLDocument
))
391 Base
.StringSet (storage
, value);
392 nsIDOMHTMLElement element
;
393 ((nsIDOMHTMLDocument
)node
).getBody (out element
);
394 ((nsIDOMHTMLBodyElement
)element
).setVLink(storage
);
398 public IWindow Window
{
400 nsIDOMDocumentView docView
= (nsIDOMDocumentView
) this.node
;
401 nsIDOMAbstractView abstractView
;
402 docView
.getDefaultView (out abstractView
);
403 nsIInterfaceRequestor requestor
= (nsIInterfaceRequestor
) abstractView
;
404 if (requestor
== null)
407 requestor
.getInterface (typeof(nsIDOMWindow
).GUID
, out ret
);
408 nsIDOMWindow window
= (nsIDOMWindow
) Marshal
.GetObjectForIUnknown (ret
);
409 return new Window (this.control
, window
);
415 #region Public IDocument Methods
416 public IAttribute
CreateAttribute (string name
)
418 nsIDOMAttr nsAttribute
;
419 Base
.StringSet (storage
, name
);
420 this.node
.createAttribute (storage
, out nsAttribute
);
421 return new Attribute (control
, nsAttribute
);
424 public IElement
CreateElement (string tagName
)
426 nsIDOMElement nsElement
;
427 Base
.StringSet (storage
, tagName
);
428 this.node
.createElement (storage
, out nsElement
);
429 if (node
is nsIDOMHTMLDocument
)
430 return new HTMLElement (control
, (nsIDOMHTMLElement
)nsElement
);
431 return new Element (control
, nsElement
);
434 public IElement
GetElementById (string id
)
436 if (!resources
.Contains ("GetElementById" + id
)) {
437 nsIDOMElement nsElement
;
438 Base
.StringSet (storage
, id
);
439 this.node
.getElementById (storage
, out nsElement
);
440 if (nsElement
== null)
442 resources
.Add ("GetElementById" + id
, GetTypedNode (nsElement
));
444 return resources
["GetElementById" + id
] as IElement
;
447 public IElementCollection
GetElementsByTagName (string name
)
449 if (!resources
.Contains ("GetElementsByTagName" + name
)) {
450 nsIDOMNodeList nodes
;
451 this.node
.getElementsByTagName (storage
, out nodes
);
454 resources
.Add ("GetElementsByTagName" + name
, new HTMLElementCollection(control
, nodes
));
456 return resources
["GetElementsByTagName" + name
] as IElementCollection
;
459 public IElement
GetElement (int x
, int y
)
461 nsIDOMNodeList nodes
;
462 this.node
.getChildNodes (out nodes
);
463 HTMLElementCollection col
= new HTMLElementCollection(control
, nodes
);
465 foreach (Element el
in col
) {
466 if (el
.Left
<= x
&& el
.Top
<= y
&&
467 el
.Left
+ el
.Width
>= x
&& el
.Top
+ el
.Height
>= y
) {
475 public void Write (string text
) {
476 if (!(node
is nsIDOMHTMLDocument
))
478 Base
.StringSet (storage
, text
);
479 ((nsIDOMHTMLDocument
)node
).write (storage
);
482 public string InvokeScript (string script
)
484 return Base
.EvalScript (this.control
, script
);
490 private System
.ComponentModel
.EventHandlerList events
;
491 internal System
.ComponentModel
.EventHandlerList Events
{
494 events
= new System
.ComponentModel
.EventHandlerList();
499 internal static object LoadStoppedEvent
= new object ();
500 public event EventHandler LoadStopped
502 add { Events.AddHandler (LoadStoppedEvent, value); }
503 remove { Events.RemoveHandler (LoadStoppedEvent, value); }
508 public override int GetHashCode () {
509 return this.node
.GetHashCode ();