1 // Permission is hereby granted, free of charge, to any person obtaining
\r
2 // a copy of this software and associated documentation files (the
\r
3 // "Software"), to deal in the Software without restriction, including
\r
4 // without limitation the rights to use, copy, modify, merge, publish,
\r
5 // distribute, sublicense, and/or sell copies of the Software, and to
\r
6 // permit persons to whom the Software is furnished to do so, subject to
\r
7 // the following conditions:
\r
9 // The above copyright notice and this permission notice shall be
\r
10 // included in all copies or substantial portions of the Software.
\r
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
20 // Copyright (c) 2008 Novell, Inc.
\r
23 // Andreia Gaita (avidigal@novell.com)
\r
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)
\r
49 nodes = new Node[0];
\r
52 public NodeList (WebBrowser control, bool loaded) : base (control)
\r
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;
\r
81 internal virtual void Load ()
85 unmanagedNodes.getLength (out count);
86 nodeCount = (int) count; // hmm.... not good
87 nodes = new Node[nodeCount];
88 for (int i = 0; i < nodeCount; i++) {
90 unmanagedNodes.item ((uint)i, out node);
\r
92 node.getNodeType (out type);
\r
94 case (ushort)NodeType.Element:
\r
95 nodes[i] = new HTMLElement (control, node as nsIDOMHTMLElement);
\r
98 nodes[i] = new Node (control, node);
\r
105 #region IEnumerable members
106 public IEnumerator GetEnumerator ()
108 return new NodeListEnumerator (this);
112 #region ICollection members
113 public void CopyTo (Array dest, int index)
116 Array.Copy (nodes, 0, dest, index, Count);
120 public virtual int Count {
122 if (unmanagedNodes != null && nodes == null)
128 object ICollection.SyncRoot {
132 bool ICollection.IsSynchronized {
133 get { return false; }
138 #region IList members
139 public bool IsReadOnly
144 bool IList.IsFixedSize
149 void IList.RemoveAt (int index)
154 public void RemoveAt (int index)
156 if (index > Count || index < 0)
158 Array.Copy (nodes, index + 1, nodes, index, (nodeCount - index) - 1);
160 nodes[nodeCount] = null;
163 public void Remove (INode node)
165 this.RemoveAt (IndexOf (node));
168 void IList.Remove (object node)
170 Remove (node as INode);
173 public void Insert (int index, INode value)
177 INode[] tmp = new Node[nodeCount+1];
179 Array.Copy (nodes, 0, tmp, 0, index);
181 if (index < nodeCount)
182 Array.Copy (nodes, index, tmp, index + 1, (nodeCount - index));
187 void IList.Insert (int index, object value)
189 this.Insert (index, value as INode);
192 public int IndexOf (INode node)
194 return Array.IndexOf (nodes, node);
197 int IList.IndexOf (object node)
199 return IndexOf (node as INode);
203 public bool Contains (INode node)
205 return this.IndexOf (node) != -1;
208 bool IList.Contains (object node)
210 return Contains (node as INode);
218 public int Add (INode node)
220 this.Insert (Count + 1, node as INode);
221 return nodeCount - 1;
224 int IList.Add (object node)
226 return Add (node as INode);
229 object IList.this [int index] {
234 this [index] = value as INode;
238 public INode this [int index] {
240 if (index < 0 || index >= Count)
241 throw new ArgumentOutOfRangeException ("index");
242 return nodes [index];
245 if (index < 0 || index >= Count)
246 throw new ArgumentOutOfRangeException ("index");
247 nodes [index] = value as INode;
253 public override int GetHashCode () {
254 if (this.unmanagedNodes != null)
255 return this.unmanagedNodes.GetHashCode ();
256 return base.GetHashCode ();
259 internal class NodeListEnumerator : IEnumerator {
261 private NodeList collection;
262 private int index = -1;
264 public NodeListEnumerator (NodeList collection)
266 this.collection = collection;
269 public object Current {
273 return collection [index];
277 public bool MoveNext ()
279 if (index + 1 >= collection.Count)