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.Collections;
28 using Mono.WebBrowser;
29 using Mono.WebBrowser.DOM;
31 namespace Mono.Mozilla.DOM
33 internal class NodeList : DOMObject, INodeList
35 protected nsIDOMNodeList unmanagedNodes;
36 protected INode [] nodes;
37 protected int nodeCount;
39 public NodeList(WebBrowser control, nsIDOMNodeList nodeList) : base (control)
41 if (control.platform != control.enginePlatform)
42 unmanagedNodes = nsDOMNodeList.GetProxy (control, nodeList);
44 unmanagedNodes = nodeList;
47 public NodeList (WebBrowser control) : base (control)
52 public NodeList (WebBrowser control, bool loaded) : base (control)
56 #region IDisposable Members
57 protected override void Dispose (bool disposing)
64 base.Dispose(disposing);
69 protected void Clear ()
72 for (int i = 0; i < nodeCount; i++) {
76 unmanagedNodes = null;
81 internal virtual void Load ()
83 if (unmanagedNodes == null) return;
86 unmanagedNodes.getLength (out count);
87 nodeCount = (int) count; // hmm.... not good
88 nodes = new Node[nodeCount];
89 for (int i = 0; i < nodeCount; i++) {
91 unmanagedNodes.item ((uint)i, out node);
93 node.getNodeType (out type);
94 nodes[i] = GetTypedNode (node);
96 // case (ushort)NodeType.Element:
97 // nodes[i] = new HTMLElement (control, node as nsIDOMHTMLElement);
100 // nodes[i] = new Node (control, node);
107 #region IEnumerable members
108 public IEnumerator GetEnumerator ()
110 return new NodeListEnumerator (this);
114 #region ICollection members
115 public void CopyTo (Array dest, int index)
118 Array.Copy (nodes, 0, dest, index, Count);
122 public virtual int Count {
124 if (unmanagedNodes != null && nodes == null)
130 object ICollection.SyncRoot {
134 bool ICollection.IsSynchronized {
135 get { return false; }
140 #region IList members
141 public bool IsReadOnly
146 bool IList.IsFixedSize
151 void IList.RemoveAt (int index)
156 public void RemoveAt (int index)
158 if (index > Count || index < 0)
160 Array.Copy (nodes, index + 1, nodes, index, (nodeCount - index) - 1);
162 nodes[nodeCount] = null;
165 public void Remove (INode node)
167 this.RemoveAt (IndexOf (node));
170 void IList.Remove (object node)
172 Remove (node as INode);
175 public void Insert (int index, INode value)
179 INode[] tmp = new Node[nodeCount+1];
181 Array.Copy (nodes, 0, tmp, 0, index);
183 if (index < nodeCount)
184 Array.Copy (nodes, index, tmp, index + 1, (nodeCount - index));
189 void IList.Insert (int index, object value)
191 this.Insert (index, value as INode);
194 public int IndexOf (INode node)
196 return Array.IndexOf (nodes, node);
199 int IList.IndexOf (object node)
201 return IndexOf (node as INode);
205 public bool Contains (INode node)
207 return this.IndexOf (node) != -1;
210 bool IList.Contains (object node)
212 return Contains (node as INode);
220 public int Add (INode node)
222 this.Insert (Count + 1, node as INode);
223 return nodeCount - 1;
226 int IList.Add (object node)
228 return Add (node as INode);
231 object IList.this [int index] {
236 this [index] = value as INode;
240 public INode this [int index] {
242 if (index < 0 || index >= Count)
243 throw new ArgumentOutOfRangeException ("index");
244 return nodes [index];
247 if (index < 0 || index >= Count)
248 throw new ArgumentOutOfRangeException ("index");
249 nodes [index] = value as INode;
255 public override int GetHashCode () {
256 if (this.unmanagedNodes != null)
257 return this.unmanagedNodes.GetHashCode ();
258 return base.GetHashCode ();
261 internal class NodeListEnumerator : IEnumerator {
263 private NodeList collection;
264 private int index = -1;
266 public NodeListEnumerator (NodeList collection)
268 this.collection = collection;
271 public object Current {
275 return collection [index];
279 public bool MoveNext ()
281 if (index + 1 >= collection.Count)