summaryrefslogtreecommitdiffstats
path: root/dom/base/test/test_bug352728.xhtml
blob: 6f85810d61d9f78dd7de0feb7c6590c94403104a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<html xmlns="http://www.w3.org/1999/xhtml">
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=352728
-->
<head>
  <title>Test for Bug 352728</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>        
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=352728">Mozilla Bug 352728</a>
<p id="display"></p>
<div id="content" style="display: none">
  
</div>
<pre id="test">
<!-- First make sure that a script consisting of multiple CDATA sections is
     even supported -->
<script class="testbody" type="text/javascript">
  var cdataTest1 = false;
  var cdataTest2 = false;
  var cdataTest3 = false;
</script>

<script class="testbody" type="text/javascript">
<![CDATA[
  cdataTest1 = true;
]]>
  cdataTest2 = true;
<![CDATA[
  cdataTest3 = true;
]]>
</script>

<script class="testbody" type="text/javascript">
  is(cdataTest1, true, "Check first CDATA section");
  is(cdataTest2, true, "Check in between CDATA sections");
  is(cdataTest3, true, "Check second CDATA section");
</script>

<script class="testbody" type="text/javascript">
<![CDATA[

/** Test for Bug 352728 **/
function checkTypes(aNode, aNodeType, aTypeArray)
{
  for (var i = 0; i < aTypeArray.length; ++i) {
    ok(aNode instanceof aTypeArray[i], aNodeType + " type test " + i,
       aNodeType + " should be a " + aTypeArray[i]);
  }
}

function checkInterfaces(aNode, aNodeType, aInterfaceArray)
{
  for (var i = 0; i < aInterfaceArray.length; ++i) {
    ok(aNode instanceof SpecialPowers.Ci[aInterfaceArray[i]],
       aNodeType + " interface test " + i,
       aNodeType + " should be a " + aInterfaceArray[i]);
  }
}

function testCharacterData(aNode, aText)
{
  is(aNode.length, aText.length, "Text length should match");
  is(aNode.data, aText, "Text content should match");
  is(aNode.nodeValue, aText, "Check nodeValue");
  is(aNode.localName, undefined, "Check localName")
  is(aNode.namespaceURI, undefined, "Check namespaceURI");
}

function testComment(aText)
{
  try {
    var comment = document.createComment(aText);
    var types = [ Comment, CharacterData, Node ];
    checkTypes(comment, "comment", types);

    var interfaces = [ "nsIDOMComment", "nsIDOMCharacterData", "nsIDOMNode",
                       "nsIDOMEventTarget" ];
    checkInterfaces(comment, "comment", interfaces);

    testCharacterData(comment, aText);
    is(comment.nodeName, "#comment", "Check nodeName");
    is(comment.nodeType, Node.COMMENT_NODE, "Check nodeType");
  } catch (e) {
    ok(0, "Correct functioning of comment stuff", "something broke: " + e);
  }
}

function testCDATASection(aText, aShouldSucceed)
{
  try {
    var cdataSection = document.createCDATASection(aText);
    var types = [ CDATASection, CharacterData, Node ];
    checkTypes(cdataSection, "CDATA section", types);

    var interfaces = [ "nsIDOMCDATASection", "nsIDOMCharacterData",
                       "nsIDOMNode", "nsIDOMEventTarget" ];
    checkInterfaces(cdataSection, "CDATA section", interfaces);

    testCharacterData(cdataSection, aText);
    is(cdataSection.nodeName, "#cdata-section", "Check nodeName");
    is(cdataSection.nodeType, Node.CDATA_SECTION_NODE, "Check nodeType");

    if (!aShouldSucceed) {
      ok(0, "Invalid CDATA section creation",
]]>
         "Shouldn't create CDATA section with embedded \"]]&gt;\"");
<![CDATA[
    }
  } catch (e) {
    if (aShouldSucceed) {
      ok(0, "Correct functioning of CDATA section stuff",
         "something broke: " + e);
    } else {
      is(e.name, "InvalidCharacterError", "Check exception");
      is(e.code, DOMException.INVALID_CHARACTER_ERR, "Check exception code");
    }
  }
}

function testPI(aTarget, aData, aShouldSucceed, aReason)
{
  try {
    var pi = document.createProcessingInstruction(aTarget, aData);
    var types = [ ProcessingInstruction, Node ];
    checkTypes(pi, "processing instruction", types);

    var interfaces = [ "nsIDOMProcessingInstruction", "nsIDOMNode",
                       "nsIDOMEventTarget" ];
    checkInterfaces(pi, "processing instruction", interfaces);

    is(pi.target, aTarget, "Check target");
    is(pi.data, aData, "Check data");
    is(pi.nodeName, aTarget, "Check nodeName");
    is(pi.nodeValue, aData, "Check nodeValue");
    is(pi.localName, undefined, "Check localName")
    is(pi.namespaceURI, undefined, "Check namespaceURI");
    
    is(pi.nodeType, Node.PROCESSING_INSTRUCTION_NODE, "Check nodeType");

    if (!aShouldSucceed) {
      ok(0, "Invalid processing instruction creation", aReason);
    }
  } catch (e) {
    if (aShouldSucceed) {
      ok(0, "Correct functioning of processing instruction stuff",
         "something broke: " + e);
    } else {
      is(e.name, "InvalidCharacterError", "Check exception");
      is(e.code, DOMException.INVALID_CHARACTER_ERR, "Check exception code");
    }
  }
}

testComment("Some text");
testComment("Some text with a '-' in it");
testComment("Some text with a '-' and a '-' and another '-'");
testComment("Some text -- this should create a node!");
testComment("<!-- This is an HTML comment -->");

testCDATASection("Some text", true);
testCDATASection("Some text with a '?' in it", true);
testCDATASection("Some text with a '>' in it", true);
testCDATASection("Some text with a '?' and a '>' in it", true);
testCDATASection("Some text with a '? >' in it", true);
testCDATASection("Some text -- ?> this should be ok", true);
]]>
testCDATASection("Some text ]]&gt; this should not create a node!", false);

<![CDATA[

testPI("foo", "bar", true);
testPI("foo:bar", "baz", true);
testPI("foo", "bar?", true);
testPI("foo", "bar>", true);
testPI("foo", "bar? >", true);
testPI("<aaa", "bar", false, "Target should not contain '<'");
testPI("aaa>", "bar", false, "Target should not contain '>'");
testPI("aa?", "bar", false, "Target should not contain '?'");
testPI("foo", "bar?>", false, "Data should not contain '?>'");
]]>
</script>
</pre>
</body>
</html>