xml_test.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.dom.xmlTest');
  15. goog.setTestOnly('goog.dom.xmlTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.TagName');
  18. goog.require('goog.dom.xml');
  19. goog.require('goog.testing.jsunit');
  20. goog.require('goog.userAgent');
  21. function testSerialize() {
  22. var doc = goog.dom.xml.createDocument();
  23. var node = doc.createElement('root');
  24. doc.appendChild(node);
  25. var serializedNode = goog.dom.xml.serialize(node);
  26. assertTrue(/<root ?\/>/.test(serializedNode));
  27. var serializedDoc = goog.dom.xml.serialize(doc);
  28. assertTrue(/(<\?xml version="1.0"\?>)?<root ?\/>/.test(serializedDoc));
  29. }
  30. function testSerializeWithActiveX() {
  31. // Prefer ActiveXObject if available.
  32. var doc = goog.dom.xml.createDocument('', '', true);
  33. var node = doc.createElement('root');
  34. doc.appendChild(node);
  35. var serializedNode = goog.dom.xml.serialize(node);
  36. assertTrue(/<root ?\/>/.test(serializedNode));
  37. var serializedDoc = goog.dom.xml.serialize(doc);
  38. assertTrue(/(<\?xml version="1.0"\?>)?<root ?\/>/.test(serializedDoc));
  39. }
  40. function testBelowMaxDepthInIE() {
  41. if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('9')) {
  42. // This value is only effective in IE8 and below
  43. goog.dom.xml.MAX_ELEMENT_DEPTH = 5;
  44. var junk = '<a><b><c><d><e>Hello</e></d></c></b></a>';
  45. var doc = goog.dom.xml.loadXml(junk);
  46. assertEquals(
  47. 'Should not have caused a parse error', 0, Number(doc.parseError));
  48. }
  49. }
  50. function testAboveMaxDepthInIE() {
  51. if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('9')) {
  52. // This value is only effective in IE8 and below
  53. goog.dom.xml.MAX_ELEMENT_DEPTH = 4;
  54. var junk = '<a><b><c><d><e>Hello</e></d></c></b></a>';
  55. var doc = goog.dom.xml.loadXml(junk);
  56. assertNotEquals(
  57. 'Should have caused a parse error', 0, Number(doc.parseError));
  58. }
  59. }
  60. function testBelowMaxSizeInIE() {
  61. if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('9')) {
  62. // This value is only effective in IE8 and below
  63. goog.dom.xml.MAX_XML_SIZE_KB = 1;
  64. var junk = '<a>' + new Array(50).join('<b>junk</b>') + '</a>';
  65. var doc = goog.dom.xml.loadXml(junk);
  66. assertEquals(
  67. 'Should not have caused a parse error', 0, Number(doc.parseError));
  68. }
  69. }
  70. function testMaxSizeInIE() {
  71. if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('9')) {
  72. // This value is only effective in IE8 and below
  73. goog.dom.xml.MAX_XML_SIZE_KB = 1;
  74. var junk = '<a>' + new Array(1000).join('<b>junk</b>') + '</a>';
  75. var doc = goog.dom.xml.loadXml(junk);
  76. assertNotEquals(
  77. 'Should have caused a parse error', 0, Number(doc.parseError));
  78. }
  79. }
  80. function testSelectSingleNodeNoActiveX() {
  81. if (goog.userAgent.IE) {
  82. return;
  83. }
  84. var xml = goog.dom.xml.loadXml('<a><b><c>d</c></b></a>');
  85. var node = xml.firstChild;
  86. var bNode = goog.dom.xml.selectSingleNode(node, 'b');
  87. assertNotNull(bNode);
  88. }
  89. function testSelectSingleNodeWithActiveX() {
  90. // Enable ActiveXObject so IE has xpath support.
  91. var xml = goog.dom.xml.loadXml('<a><b><c>d</c></b></a>', true);
  92. var node = xml.firstChild;
  93. var bNode = goog.dom.xml.selectSingleNode(node, 'b');
  94. assertNotNull(bNode);
  95. }
  96. function testSelectNodesNoActiveX() {
  97. if (goog.userAgent.IE) {
  98. return;
  99. }
  100. var xml = goog.dom.xml.loadXml('<a><b><c>d</c></b><b>foo</b></a>');
  101. var node = xml.firstChild;
  102. var bNodes = goog.dom.xml.selectNodes(node, 'b');
  103. assertNotNull(bNodes);
  104. assertEquals(2, bNodes.length);
  105. }
  106. function testSelectNodesWithActiveX() {
  107. var xml = goog.dom.xml.loadXml('<a><b><c>d</c></b><b>foo</b></a>', true);
  108. var node = xml.firstChild;
  109. var bNodes = goog.dom.xml.selectNodes(node, 'b');
  110. assertNotNull(bNodes);
  111. assertEquals(2, bNodes.length);
  112. }
  113. function testSetAttributes() {
  114. var xmlElement = goog.dom.xml.createDocument().createElement('root');
  115. var domElement = goog.dom.createElement(goog.dom.TagName.DIV);
  116. var attrs =
  117. {name: 'test3', title: 'A title', random: 'woop', cellpadding: '123'};
  118. goog.dom.xml.setAttributes(xmlElement, attrs);
  119. goog.dom.xml.setAttributes(domElement, attrs);
  120. assertEquals('test3', xmlElement.getAttribute('name'));
  121. assertEquals('test3', domElement.getAttribute('name'));
  122. assertEquals('A title', xmlElement.getAttribute('title'));
  123. assertEquals('A title', domElement.getAttribute('title'));
  124. assertEquals('woop', xmlElement.getAttribute('random'));
  125. assertEquals('woop', domElement.getAttribute('random'));
  126. assertEquals('123', xmlElement.getAttribute('cellpadding'));
  127. assertEquals('123', domElement.getAttribute('cellpadding'));
  128. }