root/mochikit/branches/scriptaculous/tests/test_MochiKit-DOM.html

Revision 578, 10.2 kB (checked in by therve@gmail.com, 3 years ago)

Sync tests

  • Property svn:mime-type set to text/html
  • Property svn:eol-style set to native
Line 
1 <html>
2 <head>
3     <script type="text/javascript" src="Test/Builder.js"></script>
4     <script type="text/javascript" src="Test/More.js"></script>
5     <script type="text/javascript" src="../MochiKit/Base.js"></script>
6     <script type="text/javascript" src="../MochiKit/Iter.js"></script>
7     <script type="text/javascript" src="../MochiKit/DOM.js"></script>
8 </head>
9 <body>
10
11 <div style="display: none;">
12     <form id="form_test">
13         <select name="select">
14             <option value="foo" selected="selected">foo</option>
15             <option value="bar">bar</option>
16             <option value="baz">baz</option>
17         </select>
18         <input type="hidden" name="hidden" value="test" />
19         <input type="radio" name="radio_off" value="1" />
20         <input type="radio" name="radio_off" value="2" />
21         <input type="radio" name="radio_off" value="3" />
22         <input type="radio" name="radio_on" value="1" />
23         <input type="radio" name="radio_on" value="2" checked="checked" />
24         <input type="radio" name="radio_on" value="3" />
25     </form>
26 </div>
27
28 <pre id="test">
29 <script type="text/javascript">
30 try {
31        
32     // Counting the number of tests is really lame
33     plan({'tests': 66});
34
35     lst = [];
36     o = {"blah": function () { lst.push("original"); }};
37     addToCallStack(o, "blah", function () { lst.push("new"); }, true);
38     addToCallStack(o, "blah", function () { lst.push("stuff"); }, true);
39     is( typeof(o.blah), 'function', 'addToCallStack has a function' );
40     is( o.blah.callStack.length, 3, 'callStack length 3' );
41     o.blah();
42     is( lst.join(" "), "original new stuff", "callStack in correct order" );
43     is( o.blah, null, "set to null" );
44     lst = [];
45     o = {"blah": function () { lst.push("original"); }};
46     addToCallStack(o, "blah",
47         function () { lst.push("new"); return false;}, false);
48     addToCallStack(o, "blah", function () { lst.push("stuff"); }, false);
49     o.blah();
50     is( lst.join(" "), "original new", "callStack in correct order (abort)" );
51     o.blah();
52     is( lst.join(" "), "original new original new", "callStack in correct order (again)" );
53    
54    
55     is( escapeHTML("<>\"&bar"), "&lt;&gt;&quot;&amp;bar", "escapeHTML" ); // for emacs highlighting: "
56
57     var isDOM = function (value, expected, message) {
58         is( escapeHTML(toHTML(value)), escapeHTML(expected), message );
59     }
60
61     var d = document.createElement('span');
62     updateNodeAttributes(d, {"foo": "bar", "baz": "wibble"});
63     isDOM( d, '<span baz="wibble" foo="bar"/>', "updateNodeAttributes" );
64
65     var d = document.createElement('span');
66     appendChildNodes(d, 'word up', [document.createElement('span')]);
67     isDOM( d, '<span>word up<span/></span>', 'appendChildNodes' );
68
69     replaceChildNodes(d, 'Think Different');
70     isDOM( d, '<span>Think Different</span>', 'replaceChildNodes' );
71    
72     d = createDOM("span");
73     isDOM( d, "<span/>", "createDOM empty" );
74
75
76     d = createDOM("span", {"foo": "bar", "baz": "wibble"});
77     isDOM( d, '<span baz="wibble" foo="bar"/>', "createDOM attributes" );
78
79     d = createDOM("span", {"foo": "bar", "baz": "wibble"}, "one", "two", "three");
80     toHTML(d);
81
82     isDOM( d, '<span baz="wibble" foo="bar">onetwothree</span>', "createDOM contents" );
83
84     var d = {"taco": "pork"};
85     registerDOMConverter("taco",
86         function (o) { return !isUndefinedOrNull(o.taco); },
87         function (o) { return "Goddamn, I like " + o.taco + " tacos"; }
88     );
89     d = createDOM("span", null, d);
90     // not yet public API
91     domConverters.unregister("taco");
92
93     isDOM( d, "<span>Goddamn, I like pork tacos</span>", "createDOM with custom converter" );
94    
95     is(
96         escapeHTML(toHTML(SPAN(null))),
97         escapeHTML(toHTML(createDOM("span", null))),
98         "createDOMFunc vs createDOM"
99     );
100
101     is( scrapeText(d), "Goddamn, I like pork tacos", "scrape OK" );
102     is( scrapeText(d, true).join(""), "Goddamn, I like pork tacos", "scrape Array OK" );
103
104     var st = DIV(null, STRONG(null, "d"), "oor ", STRONG(null, "f", SPAN(null, "r"), "a"), "me");
105     is( scrapeText(st), "door frame", "scrape in-order" );
106    
107    
108     ok( !isUndefinedOrNull(getElement("test")), "getElement might work" );
109     ok( !isUndefinedOrNull($("test")), "$alias$$ CASH MONEY alias might work" );
110
111     d = createDOM("span", null, "one", "two");
112     swapDOM(d.childNodes[0], document.createTextNode("uno"));
113     isDOM( d, "<span>unotwo</span>", "swapDOM" );
114
115     is( scrapeText(d, true).join(" "), "uno two", "multi-node scrapeText" );
116     /*
117
118         TODO:
119             addLoadEvent (async test?)
120
121     */
122
123     d = createDOM("span", {"class": "foo"});
124     setElementClass(d, "bar baz");
125     ok( d.className == "bar baz", "setElementClass");
126     toggleElementClass("bar", d);
127     ok( d.className == "baz", "toggleElementClass: " + d.className);
128     toggleElementClass("bar", d);
129     ok( hasElementClass(d, "baz", "bar"),
130         "toggleElementClass 2: " + d.className);
131     addElementClass(d, "bar");
132     ok( hasElementClass(d, "baz", "bar"),
133         "toggleElementClass 3: " + d.className);
134     ok( addElementClass(d, "blah"), "addElementClass return");
135     ok( hasElementClass(d, "baz", "bar", "blah"), "addElementClass action");
136     ok( !hasElementClass(d, "not"), "hasElementClass single");
137     ok( !hasElementClass(d, "baz", "not"), "hasElementClass multiple");
138     ok( removeElementClass(d, "blah"), "removeElementClass" );
139     ok( !removeElementClass(d, "blah"), "removeElementClass again" );
140     ok( !hasElementClass(d, "blah"), "removeElementClass again (hasElement)" );
141     removeElementClass(d, "baz");
142     ok( !swapElementClass(d, "blah", "baz"), "false swapElementClass" );
143     ok( !hasElementClass(d, "baz"), "false swapElementClass from" );
144     ok( !hasElementClass(d, "blah"), "false swapElementClass to" );
145     addElementClass(d, "blah");
146     ok( swapElementClass(d, "blah", "baz"), "swapElementClass" );
147     ok( hasElementClass(d, "baz"), "swapElementClass has toClass" );
148     ok( !hasElementClass(d, "blah"), "swapElementClass !has fromClass" );
149     ok( !swapElementClass(d, "blah", "baz"), "swapElementClass twice" );
150     ok( hasElementClass(d, "baz"), "swapElementClass has toClass" );
151     ok( !hasElementClass(d, "blah"), "swapElementClass !has fromClass" );
152
153     TABLE;
154     TBODY;
155     TR;
156     var t = TABLE(null,
157         TBODY({"class": "foo bar", "id":"tbody0"},
158             TR({"class": "foo", "id":"tr0"}),
159             TR({"class": "bar", "id":"tr1"})
160         )
161     );
162
163     var matchElements = getElementsByTagAndClassName;
164     is(
165         map(itemgetter("id"), matchElements(null, "foo", t)).join(" "),
166         "tbody0 tr0",
167         "getElementsByTagAndClassName found all tags with foo class"
168     );
169     is(
170         map(itemgetter("id"), matchElements("tr", "foo", t)).join(" "),
171         "tr0",
172         "getElementsByTagAndClassName found all tr tags with foo class"
173     );
174     is(
175         map(itemgetter("id"), matchElements("tr", null, t)).join(" "),
176         "tr0 tr1",
177         "getElementsByTagAndClassName found all tr tags"
178     );
179        
180     var MockElement = function (name, data) {
181         this.nodeName = name.toUpperCase();
182         if (typeof(data) == "string") {
183             this.nodeValue = data;
184             this.nodeType = 3;
185         } else {
186             this.nodeType = 1;
187             this.childNodes = [];
188         }
189     }
190     MockElement.prototype = {
191         createElement: function (nodeName) { return new MockElement(nodeName); },
192         createTextNode: function (text) { return new MockElement("text", text); },
193         setAttribute: function (name, value) { this[name] = value; },
194         getAttribute: function (name) { return this[name]; },
195         appendChild: function (child) { this.childNodes.push(child); },
196         toString: function () { return "MockElement(" + this.nodeName + ")"; }
197     };
198
199     var oldDoc = document;
200     var doc = new MockElement("DOCUMENT");
201     doc.body = new MockElement("BODY");
202     doc.appendChild(doc.body);
203     is( currentDocument(), document, "currentDocument() correct" );
204     withDocument(doc, function () {
205         ok( document != doc, "global doc unchanged" );
206         is( currentDocument(), doc, "currentDocument() correct" );
207         var h1 = H1();
208         var span = SPAN(null, "foo", h1);
209         appendChildNodes(currentDocument().body, span);
210     });
211     is( document, oldDoc, "doc restored" );
212     is( doc.childNodes.length, 1, "doc has one child" );
213     is( doc.body.childNodes.length, 1, "body has one child" );
214     var sp = doc.body.childNodes[0];
215     is( sp.nodeName, "SPAN", "only child is SPAN" );
216     is( sp.childNodes.length, 2, "SPAN has two childNodes" );
217     is( sp.childNodes[0].nodeValue, "foo", "first node is text" );
218     is( sp.childNodes[1].nodeName, "H1", "second child is H1" );
219
220     is( currentDocument(), document, "currentDocument() correct" );
221     try {
222         withDocument(doc, function () {
223             ok( document != doc, "global doc unchanged" );
224             is( currentDocument(), doc, "currentDocument() correct" );
225             throw new Error("foo");
226         });
227         ok( false, "didn't throw" );
228     } catch (e) {
229         ok( true, "threw" );
230     }
231
232     doc = new MockElement("DOCUMENT");
233     doc.body = new MockElement("BODY");
234     doc.appendChild(doc.body);
235     var frm;
236     withDocument(doc, function () {
237         frm = FORM({name: "ignore"},
238             INPUT({name:"foo", value:"bar"}),
239             INPUT({name:"foo", value:"bar"}),
240             INPUT({name:"baz", value:"bar"})
241         );
242     });
243     var kv = formContents(frm);
244     is( kv[0].join(","), "foo,foo,baz", "mock formContents names" );
245     is( kv[1].join(","), "bar,bar,bar", "mock formContents values" );
246     is( queryString(frm), "foo=bar&foo=bar&baz=bar", "mock queryString hook" );
247
248     var kv = formContents("form_test");
249     is( kv[0].join(","), "select,hidden,radio_on", "formContents names" );
250     is( kv[1].join(","), "foo,test,2", "formContents values" );
251     is( queryString("form_test"), "select=foo&hidden=test&radio_on=2", "queryString hook" );
252    
253     ok( true, "test suite finished!");
254    
255 } catch (err) {
256    
257     var s = "test suite failure!\n";
258     var o = {};
259     var k = null;
260     for (k in err) {
261         // ensure unique keys?!
262         if (!o[k]) {
263             s +=  k + ": " + err[k] + "\n";
264             o[k] = err[k];
265         }
266     }
267     ok ( false, s );
268
269 }
270 </script>
271 </pre>
272 </body>
273 </html>
Note: See TracBrowser for help on using the browser.