5 // Jason Diamond (jason@injektilo.org)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
8 // (C) 2002 Jason Diamond http://injektilo.org/
9 // (C) 2003 Martin Willemoes Hansen
17 using NUnit.Framework;
19 namespace MonoTests.System.Xml
22 public class XmlElementTests : Assertion
24 private XmlDocument document;
27 public void GetReady ()
29 document = new XmlDocument ();
32 private void AssertElement (XmlElement element, string prefix,
33 string localName, string namespaceURI,
36 AssertEquals (prefix != String.Empty ? prefix + ":" + localName : localName, element.Name);
37 AssertEquals (prefix, element.Prefix);
38 AssertEquals (localName, element.LocalName);
39 AssertEquals (namespaceURI, element.NamespaceURI);
40 //AssertEquals (attributesCount, element.Attributes.Count);
43 // for NodeInserted Event
44 private bool Inserted = false;
45 private void OnNodeInserted (object o, XmlNodeChangedEventArgs e)
50 // for NodeChanged Event
51 private bool Changed = false;
52 private void OnNodeChanged (object o, XmlNodeChangedEventArgs e)
57 // for NodeRemoved Event
58 private bool Removed = false;
59 private void OnNodeRemoved (object o, XmlNodeChangedEventArgs e)
65 public void CloneNode ()
67 XmlElement element = document.CreateElement ("foo");
68 XmlElement child = document.CreateElement ("bar");
69 XmlElement grandson = document.CreateElement ("baz");
71 element.SetAttribute ("attr1", "val1");
72 element.SetAttribute ("attr2", "val2");
73 element.AppendChild (child);
74 child.SetAttribute ("attr3", "val3");
75 child.AppendChild (grandson);
77 document.AppendChild (element);
78 XmlNode deep = element.CloneNode (true);
79 // AssertEquals ("These should be the same", deep.OuterXml, element.OuterXml);
80 AssertNull ("This is not null", deep.ParentNode);
81 Assert ("Copies, not pointers", !Object.ReferenceEquals (element,deep));
83 XmlNode shallow = element.CloneNode (false);
84 AssertNull ("This is not null", shallow.ParentNode);
85 Assert ("Copies, not pointers", !Object.ReferenceEquals (element,shallow));
86 AssertEquals ("Shallow clones shalt have no children!", false, shallow.HasChildNodes);
90 public void ConstructionAndDefaultAttributes ()
92 string dtd = "<!DOCTYPE root [<!ELEMENT root EMPTY><!ATTLIST root foo CDATA 'def'>]>";
93 string xml = dtd + "<root />";
94 // XmlValidatingReader xvr = new XmlValidatingReader (new XmlTextReader (xml, XmlNodeType.Document, null));
95 XmlDocument doc = new XmlDocument ();
97 Console.WriteLine (doc.DocumentElement.Attributes.Count);
98 Console.WriteLine (doc.CreateElement ("root").Attributes.Count);
99 Console.WriteLine (doc.CreateElement ("root2").Attributes.Count);
103 public void CreateElement1 ()
105 XmlElement element = document.CreateElement ("name");
106 AssertElement (element, String.Empty, "name", String.Empty, 0);
110 public void CreateElement1WithPrefix ()
112 XmlElement element = document.CreateElement ("prefix:localName");
113 AssertElement (element, "prefix", "localName", String.Empty, 0);
117 public void CreateElement2 ()
119 XmlElement element = document.CreateElement ("qualifiedName", "namespaceURI");
120 AssertElement (element, String.Empty, "qualifiedName",
125 public void CreateElement2WithPrefix ()
127 XmlElement element = document.CreateElement ("prefix:localName", "namespaceURI");
128 AssertElement (element, "prefix", "localName", "namespaceURI", 0);
132 public void CreateElement3 ()
134 XmlElement element = document.CreateElement ("prefix", "localName", "namespaceURI");
135 AssertElement (element, "prefix", "localName", "namespaceURI", 0);
139 public void CreateElement3WithNullNamespace ()
141 // bug #26855, NamespaceURI should NEVER be null.
142 XmlElement element = document.CreateElement (null, "localName", null);
143 AssertElement (element, String.Empty, "localName", String.Empty, 0);
147 public void InnerAndOuterXml ()
153 element = document.CreateElement ("foo");
154 AssertEquals (String.Empty, element.InnerXml);
155 AssertEquals ("<foo />", element.OuterXml);
157 text = document.CreateTextNode ("bar");
158 element.AppendChild (text);
159 AssertEquals ("bar", element.InnerXml);
160 AssertEquals ("<foo>bar</foo>", element.OuterXml);
162 element.SetAttribute ("baz", "quux");
163 AssertEquals ("bar", element.InnerXml);
164 AssertEquals ("<foo baz=\"quux\">bar</foo>", element.OuterXml);
166 comment = document.CreateComment ("squonk");
167 element.AppendChild (comment);
168 AssertEquals ("bar<!--squonk-->", element.InnerXml);
169 AssertEquals ("<foo baz=\"quux\">bar<!--squonk--></foo>", element.OuterXml);
172 element.AppendChild(document.CreateElement("hoge"));
173 AssertEquals ("<hoge />", element.InnerXml);
177 public void SetGetAttribute ()
179 XmlElement element = document.CreateElement ("foo");
180 element.SetAttribute ("attr1", "val1");
181 element.SetAttribute ("attr2", "val2");
182 AssertEquals ("val1", element.GetAttribute ("attr1"));
183 AssertEquals ("val2", element.GetAttribute ("attr2"));
187 public void GetElementsByTagNameNoNameSpace ()
189 string xml = @"<library><book><title>XML Fun</title><author>John Doe</author>
190 <price>34.95</price></book><book><title>Bear and the Dragon</title>
191 <author>Tom Clancy</author><price>6.95</price></book><book>
192 <title>Bourne Identity</title><author>Robert Ludlum</author>
193 <price>9.95</price></book><Fluffer><Nutter><book>
194 <title>Bourne Ultimatum</title><author>Robert Ludlum</author>
195 <price>9.95</price></book></Nutter></Fluffer></library>";
197 MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml));
198 document = new XmlDocument ();
199 document.Load (memoryStream);
200 XmlNodeList libraryList = document.GetElementsByTagName ("library");
201 XmlNode xmlNode = libraryList.Item (0);
202 XmlElement xmlElement = xmlNode as XmlElement;
203 XmlNodeList bookList = xmlElement.GetElementsByTagName ("book");
204 AssertEquals ("GetElementsByTagName (string) returned incorrect count.", 4, bookList.Count);
208 public void GetElementsByTagNameUsingNameSpace ()
210 StringBuilder xml = new StringBuilder ();
211 xml.Append ("<?xml version=\"1.0\" ?><library xmlns:North=\"http://www.foo.com\" ");
212 xml.Append ("xmlns:South=\"http://www.goo.com\"><North:book type=\"non-fiction\"> ");
213 xml.Append ("<North:title type=\"intro\">XML Fun</North:title> " );
214 xml.Append ("<North:author>John Doe</North:author> " );
215 xml.Append ("<North:price>34.95</North:price></North:book> " );
216 xml.Append ("<South:book type=\"fiction\"> " );
217 xml.Append ("<South:title>Bear and the Dragon</South:title> " );
218 xml.Append ("<South:author>Tom Clancy</South:author> " );
219 xml.Append ("<South:price>6.95</South:price></South:book> " );
220 xml.Append ("<South:book type=\"fiction\"><South:title>Bourne Identity</South:title> " );
221 xml.Append ("<South:author>Robert Ludlum</South:author> " );
222 xml.Append ("<South:price>9.95</South:price></South:book></library>");
224 MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml.ToString ()));
225 document = new XmlDocument ();
226 document.Load (memoryStream);
227 XmlNodeList libraryList = document.GetElementsByTagName ("library");
228 XmlNode xmlNode = libraryList.Item (0);
229 XmlElement xmlElement = xmlNode as XmlElement;
230 XmlNodeList bookList = xmlElement.GetElementsByTagName ("book", "http://www.foo.com");
231 AssertEquals ("GetElementsByTagName (string, uri) returned incorrect count.", 1, bookList.Count);
235 public void GetElementsByTagNameNs2 ()
237 document.LoadXml (@"<root>
\r
238 <x:a xmlns:x='urn:foo' id='a'>
\r
239 <y:a xmlns:y='urn:foo' id='b'/>
\r
243 <?a processing instruction ?>
\r
247 // id='b' has different prefix. Should not caught by (name),
\r
248 // while should caught by (name, ns).
\r
249 XmlNodeList nl = document.DocumentElement.GetElementsByTagName ("x:a");
\r
250 AssertEquals (2, nl.Count);
\r
251 AssertEquals ("a", nl [0].Attributes ["id"].Value);
\r
252 AssertEquals ("c", nl [1].Attributes ["id"].Value);
\r
254 nl = document.DocumentElement.GetElementsByTagName ("a", "urn:foo");
\r
255 AssertEquals (3, nl.Count);
\r
256 AssertEquals ("a", nl [0].Attributes ["id"].Value);
\r
257 AssertEquals ("b", nl [1].Attributes ["id"].Value);
\r
258 AssertEquals ("c", nl [2].Attributes ["id"].Value);
\r
261 nl = document.DocumentElement.GetElementsByTagName ("*");
\r
262 AssertEquals (5, nl.Count);
\r
263 AssertEquals ("a", nl [0].Attributes ["id"].Value);
\r
264 AssertEquals ("b", nl [1].Attributes ["id"].Value);
\r
265 AssertEquals ("c", nl [2].Attributes ["id"].Value);
\r
266 AssertEquals ("d", nl [3].Attributes ["id"].Value);
\r
267 AssertEquals ("e", nl [4].Attributes ["id"].Value);
\r
269 // wildcard - local and ns
270 nl = document.DocumentElement.GetElementsByTagName ("*", "*");
\r
271 AssertEquals (5, nl.Count);
\r
272 AssertEquals ("a", nl [0].Attributes ["id"].Value);
\r
273 AssertEquals ("b", nl [1].Attributes ["id"].Value);
\r
274 AssertEquals ("c", nl [2].Attributes ["id"].Value);
\r
275 AssertEquals ("d", nl [3].Attributes ["id"].Value);
\r
276 AssertEquals ("e", nl [4].Attributes ["id"].Value);
\r
278 // namespace wildcard - namespace
279 nl = document.DocumentElement.GetElementsByTagName ("*", "urn:foo");
\r
280 AssertEquals (4, nl.Count);
\r
281 AssertEquals ("a", nl [0].Attributes ["id"].Value);
\r
282 AssertEquals ("b", nl [1].Attributes ["id"].Value);
\r
283 AssertEquals ("c", nl [2].Attributes ["id"].Value);
\r
284 AssertEquals ("e", nl [3].Attributes ["id"].Value);
\r
286 // namespace wildcard - local only. I dare say, such usage is not XML-ish!
287 nl = document.DocumentElement.GetElementsByTagName ("a", "*");
\r
288 AssertEquals (3, nl.Count);
\r
289 AssertEquals ("a", nl [0].Attributes ["id"].Value);
\r
290 AssertEquals ("b", nl [1].Attributes ["id"].Value);
\r
291 AssertEquals ("c", nl [2].Attributes ["id"].Value);
\r
295 public void OuterXmlWithNamespace ()
297 XmlElement element = document.CreateElement ("foo", "bar", "#foo");
298 AssertEquals ("<foo:bar xmlns:foo=\"#foo\" />", element.OuterXml);
302 public void RemoveAllAttributes ()
304 StringBuilder xml = new StringBuilder ();
305 xml.Append ("<?xml version=\"1.0\" ?><library><book type=\"non-fiction\" price=\"34.95\"> ");
306 xml.Append ("<title type=\"intro\">XML Fun</title> " );
307 xml.Append ("<author>John Doe</author></book></library>");
309 MemoryStream memoryStream = new MemoryStream (Encoding.UTF8.GetBytes (xml.ToString ()));
310 document = new XmlDocument ();
311 document.Load (memoryStream);
312 XmlNodeList bookList = document.GetElementsByTagName ("book");
313 XmlNode xmlNode = bookList.Item (0);
314 XmlElement xmlElement = xmlNode as XmlElement;
315 xmlElement.RemoveAllAttributes ();
316 AssertEquals ("attributes not properly removed.", false, xmlElement.HasAttribute ("type"));
320 public void RemoveDoesNotRemoveDefaultAttributes ()
322 string dtd = "<!DOCTYPE root [<!ELEMENT root EMPTY><!ATTLIST root foo CDATA 'def' bar CDATA #IMPLIED>]>";
323 string xml = dtd + "<root bar='baz' />";
324 XmlValidatingReader xvr = new XmlValidatingReader (xml, XmlNodeType.Document, null);
327 AssertNotNull (document.DocumentElement);
328 AssertEquals (2, document.DocumentElement.Attributes.Count);
329 AssertEquals ("baz", document.DocumentElement.GetAttribute ("bar"));
330 AssertEquals ("def", document.DocumentElement.GetAttribute ("foo"));
331 document.DocumentElement.RemoveAll ();
332 AssertEquals (1, document.DocumentElement.Attributes.Count);
333 AssertEquals ("def", document.DocumentElement.GetAttribute ("foo"));
334 AssertEquals (String.Empty, document.DocumentElement.GetAttribute ("bar"));
336 // RemoveAllAttributes
337 xvr = new XmlValidatingReader (xml, XmlNodeType.Document, null);
339 document.DocumentElement.RemoveAllAttributes ();
340 AssertEquals (1, document.DocumentElement.Attributes.Count);
342 // RemoveAttribute(name)
343 xvr = new XmlValidatingReader (xml, XmlNodeType.Document, null);
345 document.DocumentElement.RemoveAttribute ("foo");
346 AssertEquals (2, document.DocumentElement.Attributes.Count);
348 // RemoveAttribute(name, ns)
349 xvr = new XmlValidatingReader (xml, XmlNodeType.Document, null);
351 document.DocumentElement.RemoveAttribute ("foo", String.Empty);
352 AssertEquals (2, document.DocumentElement.Attributes.Count);
355 xvr = new XmlValidatingReader (xml, XmlNodeType.Document, null);
357 document.DocumentElement.RemoveAttributeAt (1);
358 AssertEquals (2, document.DocumentElement.Attributes.Count);
360 // RemoveAttributeNode
361 xvr = new XmlValidatingReader (xml, XmlNodeType.Document, null);
363 document.DocumentElement.RemoveAttributeNode (document.DocumentElement.Attributes [1]);
364 AssertEquals (2, document.DocumentElement.Attributes.Count);
366 // RemoveAttributeNode(name, ns)
367 xvr = new XmlValidatingReader (xml, XmlNodeType.Document, null);
369 document.DocumentElement.RemoveAttributeNode ("foo", String.Empty);
370 AssertEquals (2, document.DocumentElement.Attributes.Count);
374 public void SetAttributeNode ()
376 XmlDocument xmlDoc = new XmlDocument ();
377 XmlElement xmlEl = xmlDoc.CreateElement ("TestElement");
378 XmlAttribute xmlAttribute = xmlEl.SetAttributeNode ("attr1", "namespace1");
379 XmlAttribute xmlAttribute2 = xmlEl.SetAttributeNode ("attr2", "namespace2");
380 AssertEquals ("attribute name not properly created.", true, xmlAttribute.Name.Equals ("attr1"));
381 AssertEquals ("attribute namespace not properly created.", true, xmlAttribute.NamespaceURI.Equals ("namespace1"));
385 [ExpectedException (typeof (XmlException))]
386 public void SetAttributeNodeError ()
388 XmlDocument doc = new XmlDocument ();
389 doc.LoadXml ("<root xmlns:x='urn:foo'/>");
390 doc.DocumentElement.SetAttributeNode ("x:lang", "urn:foo");
394 public void SetAttributeXmlns ()
396 // should not affect Element node's xmlns
397 XmlElement el = document.CreateElement ("root");
398 el.SetAttribute ("xmlns", "urn:foo");
399 AssertEquals (String.Empty, el.NamespaceURI);
403 public void InnerTextAndEvent ()
405 XmlDocument doc = new XmlDocument ();
406 doc.LoadXml ("<root><child>text</child><child2><![CDATA[cdata]]></child2></root>");
407 doc.NodeInserted += new XmlNodeChangedEventHandler (
409 doc.NodeRemoved += new XmlNodeChangedEventHandler (
411 // If only one child of the element is Text node,
412 // then no events are fired.
413 doc.DocumentElement.FirstChild.InnerText = "no events fired.";
414 AssertEquals ("NoInsertEventFired", false, Inserted);
415 AssertEquals ("NoRemoveEventFired", false, Removed);
416 AssertEquals ("SetInnerTextToSingleText", "no events fired.", doc.DocumentElement.FirstChild.InnerText);
420 // if only one child of the element is CDataSection,
421 // then events are fired.
422 doc.DocumentElement.LastChild.InnerText = "events are fired.";
423 AssertEquals ("InsertedEventFired", true, Inserted);
424 AssertEquals ("RemovedEventFired", true, Removed);
425 AssertEquals ("SetInnerTextToCDataSection", "events are fired.", doc.DocumentElement.LastChild.InnerText);
429 public void InnerXmlSetter ()
431 XmlDocument doc = new XmlDocument ();
432 doc.LoadXml ("<root/>");
433 XmlElement el = doc.DocumentElement;
434 AssertNull ("#Simple", el.FirstChild);
435 el.InnerXml = "<foo><bar att='baz'/></foo>";
436 XmlElement child = el.FirstChild as XmlElement;
437 AssertNotNull ("#Simple.Child", child);
438 AssertEquals ("#Simple.Child.Name", "foo", child.LocalName);
440 XmlElement grandchild = child.FirstChild as XmlElement;
441 AssertNotNull ("#Simple.GrandChild", grandchild);
442 AssertEquals ("#Simple.GrandChild.Name", "bar", grandchild.LocalName);
443 AssertEquals ("#Simple.GrandChild.Attr", "baz", grandchild.GetAttribute ("att"));
445 doc.LoadXml ("<root xmlns='NS0' xmlns:ns1='NS1'><foo/><ns1:bar/><ns2:bar xmlns:ns2='NS2' /></root>");
446 el = doc.DocumentElement.FirstChild.NextSibling as XmlElement; // ns1:bar
447 AssertNull ("#Namespaced.Prepare", el.FirstChild);
448 el.InnerXml = "<ns1:baz />";
449 AssertNotNull ("#Namespaced.Child", el.FirstChild);
450 AssertEquals ("#Namespaced.Child.Name", "baz", el.FirstChild.LocalName);
451 AssertEquals ("#Namespaced.Child.NSURI", "NS1", el.FirstChild.NamespaceURI); // important!
453 el.InnerXml = "<hoge />";
454 AssertEquals ("#Namespaced.VerifyPreviousCleared", "hoge", el.FirstChild.Name);
458 public void IsEmpty ()
460 document.LoadXml ("<root><foo/><bar></bar></root>");
461 Assertion.AssertEquals ("Empty", true, ((XmlElement) document.DocumentElement.FirstChild).IsEmpty);
462 Assertion.AssertEquals ("Empty", false, ((XmlElement) document.DocumentElement.LastChild).IsEmpty);
466 public void RemoveAttribute ()
468 string xlinkURI = "http://www.w3.org/1999/XLink";
469 XmlDocument doc = new XmlDocument ();
470 doc.LoadXml ("<root a1='1' a2='2' xlink:href='urn:foo' xmlns:xlink='" + xlinkURI + "' />");
472 XmlElement el = doc.DocumentElement;
473 el.RemoveAttribute ("a1");
474 AssertNull ("RemoveAttribute", el.GetAttributeNode ("a1"));
475 el.RemoveAttribute ("xlink:href");
476 AssertNull ("RemoveAttribute", el.GetAttributeNode ("href", xlinkURI));
477 el.RemoveAllAttributes ();
478 AssertNull ("RemoveAllAttributes", el.GetAttributeNode ("a2"));
482 public void WriteToWithDefaultNamespace ()
484 XmlDocument doc = new XmlDocument ();
485 doc.LoadXml ("<RetrievalElement URI=\"\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />");
486 StringWriter sw = new StringWriter ();
487 XmlTextWriter xtw = new XmlTextWriter (sw);
488 doc.DocumentElement.WriteTo (xtw);
489 AssertEquals ("<RetrievalElement URI=\"\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />", sw.ToString());
493 public void WriteToMakesNonsenseForDefaultNSChildren ()
495 XmlDocument d = new XmlDocument ();
\r
496 XmlElement x = d.CreateElement ("root");
\r
498 XmlElement a = d.CreateElement ("a");
\r
499 XmlElement b = d.CreateElement ("b");
\r
500 b.SetAttribute ("xmlns","probe");
\r
503 XmlElement b2 = d.CreateElement ("p2", "b2", "");
\r
504 b.AppendChild (b2);
\r
505 AssertEquals ("<root><a /><b xmlns=\"probe\"><b2 /></b></root>", d.OuterXml);
\r
509 public void WriteToWithDeletedNamespacePrefix ()
511 XmlDocument doc = new XmlDocument ();
512 doc.LoadXml ("<root xmlns:foo='urn:dummy'><foo foo:bar='baz' /></root>");
513 doc.DocumentElement.RemoveAllAttributes ();
515 Assert (doc.DocumentElement.FirstChild.OuterXml.IndexOf("xmlns:foo") > 0);
519 public void WriteToWithDifferentNamespaceAttributes ()
521 XmlDocument doc = new XmlDocument ();
522 doc.LoadXml ("<root xmlns:foo='urn:dummy' xmlns:html='http://www.w3.org/1999/xhtml' html:style='font-size: 1em'></root>");
523 Assert (doc.OuterXml.IndexOf ("xmlns:html=\"http://www.w3.org/1999/xhtml\"") > 0);
527 public void WriteToDefaultAttribute ()
529 // default attributes should be ignored.
531 string dtd = "<!DOCTYPE root[<!ATTLIST root hoge CDATA 'hoge-def'><!ENTITY foo 'ent-foo'>]>";
532 string xml = dtd + "<root>&foo;</root>";
533 XmlValidatingReader xvr = new XmlValidatingReader (xml, XmlNodeType.Document,null);
534 xvr.EntityHandling = EntityHandling.ExpandCharEntities;
535 xvr.ValidationType = ValidationType.None;
537 StringWriter sw = new StringWriter ();
538 XmlTextWriter xtw = new XmlTextWriter (sw);
539 document.DocumentElement.WriteTo (xtw);
540 AssertEquals ("<root>&foo;</root>", sw.ToString ());