]> git.proxmox.com Git - extjs.git/blame - extjs/packages/core/test/specs/dom/DomHelper.js
add extjs 6.0.1 sources
[extjs.git] / extjs / packages / core / test / specs / dom / DomHelper.js
CommitLineData
6527f429
DM
1xdescribe("Ext.core.DomHelper", function(){\r
2 // TODO this spec is just unmaintanable, because of subtleGradient magic...\r
3 // no dynamic spec generation !\r
4 var TestHelper = {\r
5 dom:{\r
6 cleanBody: function(){\r
7 document.getElementsByTagName('body')[0].innerHTML = '';\r
8 },\r
9\r
10 parentNodeName: {\r
11 '*' : 'div',\r
12 frame : 'frameset',\r
13 col : 'colgroup',\r
14 colgroup : 'table',\r
15 option : 'select',\r
16 optgroup : 'select',\r
17 tbody : 'table',\r
18 thead : 'table',\r
19 tfoot : 'table',\r
20 tr : 'tbody',\r
21 td : 'tr'\r
22 },\r
23\r
24 voidNode: {\r
25 area : true,\r
26 br : true,\r
27 col : true,\r
28 frame : true,\r
29 hr : true,\r
30 img : true,\r
31 input : true,\r
32 link : true,\r
33 meta : true,\r
34 param : true,\r
35 range : true,\r
36 spacer : true,\r
37 wbr : true\r
38 }\r
39\r
40 },\r
41 String:{\r
42 html: function(tagName, className){\r
43 var HTML = '<'+tagName+' class='+className+'>';\r
44 if (!TestHelper.dom.voidNode[tagName]) HTML += '</'+tagName+'>';\r
45 // console.log(HTML);\r
46 return HTML;\r
47 }\r
48 }\r
49 }; \r
50 \r
51 xdescribe('useDom = false', function(){\r
52 beforeEach(function(){\r
53 Ext.core.DomHelper.useDom = false;\r
54 });\r
55 //Ext_core_DomHelper_Tests.apply(this, arguments);\r
56 });\r
57 \r
58 xdescribe('useDom = true', function(){\r
59 beforeEach(function(){\r
60 Ext.core.DomHelper.useDom = false;\r
61 });\r
62 afterEach(function(){\r
63 Ext.core.DomHelper.useDom = true;\r
64 });\r
65 //Ext_core_DomHelper_Tests.apply(this, arguments);\r
66 });\r
67 \r
68 \r
69function Ext_core_DomHelper_Tests() {\r
70 var dh = Ext.core.DomHelper,\r
71 el, els;\r
72 beforeEach(function(){\r
73 el = Ext.getBody().createChild({\r
74 id : 'DomHelperHelper',\r
75 children: [\r
76 {cls: 'child', id: 'firstChild'},\r
77 {cls: 'child'},\r
78 {cls: 'child'},\r
79 {cls: 'child'},\r
80 {cls: 'child'},\r
81 {cls: 'child2'},\r
82 {cls: 'child2'},\r
83 {cls: 'child'},\r
84 {cls: 'child'},\r
85 {cls: 'child'}\r
86 ]\r
87 });\r
88 \r
89 els = Ext.select('#DomHelperHelper > div');\r
90 });\r
91 \r
92 afterEach(TestHelper.dom.cleanBody);\r
93 \r
94 describe("markup", function() {\r
95 it("should return the markup for a string", function() {\r
96 var markup = '<div class="class">my div!</div>';\r
97 \r
98 expect(dh.markup(markup)).toEqual(markup);\r
99 });\r
100 \r
101 it("should return the markup for an object", function() {\r
102 var markup = '<div class="class"><span style="padding:10px;">test</span><br/></div>',\r
103 html = {\r
104 cls: 'class',\r
105 children: [\r
106 {\r
107 tag : 'span',\r
108 html: 'test',\r
109 style: {\r
110 padding: '10px'\r
111 }\r
112 },\r
113 {tag: 'br'}\r
114 ]\r
115 };\r
116 \r
117 expect(dh.markup(html)).toEqual(markup);\r
118 });\r
119 });\r
120 \r
121 describe("applyStyles", function() {\r
122 it("should apply styles from a string", function() {\r
123 dh.applyStyles(el, 'padding:10px;');\r
124 \r
125 expect(el.dom.style.padding).toMatch(/^(?:10px|10px 10px 10px 10px)$/);\r
126 });\r
127 \r
128 it("should apply styles from an object", function() {\r
129 dh.applyStyles(el, {\r
130 padding: '10px'\r
131 });\r
132 \r
133 expect(el.dom.style.padding).toMatch(/^(?:10px|10px 10px 10px 10px)$/);\r
134 });\r
135 \r
136 // TODO: Implement using raw numbers in DomHelper.applyStyles\r
137 xit("should apply styles from an object using numbers", function() {\r
138 dh.applyStyles(el, {\r
139 padding: 10\r
140 });\r
141 \r
142 expect(el.dom.style.padding).toMatch(/^(?:10px|10px 10px 10px 10px)$/);\r
143 });\r
144 \r
145 it("should apply styles from a function", function() {\r
146 dh.applyStyles(el, function() {\r
147 return {\r
148 padding: '10px'\r
149 };\r
150 });\r
151 \r
152 expect(el.dom.style.padding).toMatch(/^(?:10px|10px 10px 10px 10px)$/);\r
153 });\r
154 });\r
155 \r
156 describe("generateStyles", function() {\r
157 it("should un-camel case names", function() {\r
158 expect(dh.generateStyles({\r
159 backgroundColor: 'red'\r
160 })).toEqual('background-color:red;');\r
161 });\r
162 \r
163 it("should concat multiple values", function() {\r
164 expect(dh.generateStyles({\r
165 backgroundColor: 'red',\r
166 color: 'green',\r
167 'font-size': '12px'\r
168 })).toEqual('background-color:red;color:green;font-size:12px;');\r
169 });\r
170 \r
171 it("should use a passed buffer", function() {\r
172 var buffer = [];\r
173 dh.generateStyles({\r
174 color: 'red'\r
175 }, buffer);\r
176 expect(buffer.join('')).toEqual('color:red;');\r
177 });\r
178 \r
179 it("should not html encode by default", function() {\r
180 expect(dh.generateStyles({\r
181 fontFamily: '"Arial"'\r
182 })).toEqual('font-family:"Arial";');\r
183 });\r
184 \r
185 it("should encode html entity values", function() {\r
186 expect(dh.generateStyles({\r
187 fontFamily: '"Arial"'\r
188 }, null, true)).toEqual('font-family:&quot;Arial&quot;;');\r
189 });\r
190 });\r
191 \r
192 describe("insertHtml", function() {\r
193 \r
194 var testEl, HTML = '<div id="newChild"></div>';\r
195 \r
196 describe("basic",function(){\r
197 beforeEach(function(){\r
198 testEl = Ext.getDom(els.first());\r
199 expect(Ext.get('newChild')).toBeNull();\r
200 });\r
201 afterEach(function(){\r
202 var el = document.getElementById('newChild');\r
203 expect(el).toBeDefined();\r
204 testEl.innerHTML = '';\r
205 });\r
206\r
207 it("should insert a new element beforeBegin", function() { dh.insertHtml('beforeBegin', testEl, HTML); });\r
208 it("should insert a new element afterBegin", function() { dh.insertHtml('afterBegin', testEl, HTML); });\r
209 it("should insert a new element beforeEnd", function() { dh.insertHtml('beforeEnd', testEl, HTML); });\r
210 it("should insert a new element afterEnd", function() { dh.insertHtml('afterEnd', testEl, HTML); });\r
211 \r
212 describe("textNode", function(){\r
213 var UID=0, text, textNode;\r
214 beforeEach(function(){\r
215 textNode = document.createTextNode(text = "howdy" + (UID++));\r
216 testEl.appendChild(textNode);\r
217 expect(testEl.firstChild).toEqual(textNode);\r
218 expect(testEl.firstChild.nodeValue).toEqual(text);\r
219 });\r
220 afterEach(function(){\r
221 expect(testEl.childNodes.length).toEqual(2);\r
222 testEl.removeChild(textNode);\r
223 expect(testEl.childNodes.length).toEqual(1);\r
224 \r
225 textNode = null;\r
226 testEl.innerHTML = '';\r
227 expect(testEl.childNodes.length).toEqual(0);\r
228 });\r
229 \r
230 it("should insert a new element beforeBegin", function() { dh.insertHtml('beforeBegin', textNode, HTML); });\r
231 it("should insert a new element afterBegin", function() { dh.insertHtml('afterBegin', textNode, HTML); });\r
232 it("should insert a new element beforeEnd", function() { dh.insertHtml('beforeEnd', textNode, HTML); });\r
233 it("should insert a new element afterEnd", function() { dh.insertHtml('afterEnd', textNode, HTML); });\r
234 });\r
235\r
236 });\r
237 \r
238 describe("details", function(){\r
239 var outside = {afterEnd:true, beforeBegin:true},\r
240 inside = {afterBegin:true, beforeEnd:true},\r
241 everywhere = {afterEnd:true, beforeBegin:true, afterBegin:true, beforeEnd:true};\r
242 \r
243 for (var tagName in TestHelper.dom.voidNode) {\r
244 insertHtml_shouldSupport(outside, tagName, tagName);\r
245 }\r
246 \r
247 insertHtml_shouldSupport(everywhere, 'div', 'div');\r
248 insertHtml_shouldSupport(everywhere, 'a', 'p');\r
249 insertHtml_shouldSupport(everywhere, 'a', 'a');\r
250 \r
251 insertHtml_shouldSupport(inside, 'tr', 'td');\r
252 insertHtml_shouldSupport(outside, 'tr', 'tr');\r
253 \r
254 insertHtml_shouldSupport(inside, 'tbody', 'tr');\r
255 insertHtml_shouldSupport(inside, 'tfoot', 'tr');\r
256 insertHtml_shouldSupport(inside, 'thead', 'tr');\r
257 \r
258 insertHtml_shouldSupport(outside, 'tbody', 'tbody');\r
259 insertHtml_shouldSupport(outside, 'tbody', 'thead');\r
260 insertHtml_shouldSupport(outside, 'tbody', 'tfoot');\r
261 insertHtml_shouldSupport(outside, 'tfoot', 'tbody');\r
262 insertHtml_shouldSupport(outside, 'tfoot', 'thead');\r
263 insertHtml_shouldSupport(outside, 'tfoot', 'tfoot');\r
264 insertHtml_shouldSupport(outside, 'thead', 'tbody');\r
265 insertHtml_shouldSupport(outside, 'thead', 'thead');\r
266 insertHtml_shouldSupport(outside, 'thead', 'tfoot');\r
267 \r
268 \r
269 function insertHtml_shouldSupport(where, tagName, tagNameToInsert){\r
270 if (where.afterBegin) insertHtml_shouldSupport_inside('afterBegin', tagName, tagNameToInsert);\r
271 if (where.beforeEnd) insertHtml_shouldSupport_inside('beforeEnd', tagName, tagNameToInsert);\r
272 if (where.afterEnd) insertHtml_shouldSupport_outside('afterEnd', tagName, tagNameToInsert);\r
273 if (where.beforeBegin) insertHtml_shouldSupport_outside('beforeBegin', tagName, tagNameToInsert);\r
274 \r
275 if (where.afterBegin || where.beforeEnd){\r
276 it(["should overwrite the HTML",\r
277 "of a", tagName.toUpperCase(), "Element"].join(' '), function(){\r
278 \r
279 var html = TestHelper.String.html(tagNameToInsert, 'overwrite');\r
280 \r
281 var target = document.createElement(tagName);\r
282 expect(target.parentNode).toBeNull();\r
283 expect(target.childNodes.length).toBe(0);\r
284 expect(target.firstChild).toBeNull();\r
285 \r
286 dh.overwrite(target, html);\r
287 expect(target.childNodes.length).toBe(1);\r
288 expect(target.firstChild).toBeDefined();\r
289 expect(target.firstChild.className).toBe('overwrite');\r
290 });\r
291 \r
292 \r
293 if (Ext.core.Element.prototype.setHTML) it(["should set the HTML",\r
294 "of a", tagName.toUpperCase(), "Element"].join(' '), function(){\r
295 \r
296 var html = TestHelper.String.html(tagNameToInsert, 'overwrite');\r
297 \r
298 var target = document.createElement(tagName);\r
299 expect(target.parentNode).toBeNull();\r
300 expect(target.childNodes.length).toBe(0);\r
301 expect(target.firstChild).toBeNull();\r
302 \r
303 Ext.fly(target).setHTML(html);\r
304 expect(target.childNodes.length).toBe(1);\r
305 expect(target.firstChild).toBeDefined();\r
306 expect(target.firstChild.className).toBe('overwrite');\r
307 });\r
308 }\r
309 }\r
310 \r
311 function insertHtml_shouldSupport_inside(where, tagName, tagNameToInsert){\r
312 it(["should insertHtml", tagNameToInsert.toUpperCase(), where,\r
313 "of a " + tagName.toUpperCase() + " Element",\r
314 "when it has no parentNode"].join(' '), function(){\r
315 \r
316 var html = TestHelper.String.html(tagNameToInsert, where);\r
317 \r
318 var target = document.createElement(tagName);\r
319 expect(target.parentNode).toBeNull();\r
320 expect(target.childNodes.length).toBe(0);\r
321 expect(target.firstChild).toBeNull();\r
322 \r
323 dh.insertHtml(where, target, html);\r
324 expect(target.childNodes.length).toBe(1);\r
325 expect(target.firstChild).toBeDefined();\r
326 expect(target.firstChild.className).toBe(where);\r
327 });\r
328 }\r
329 \r
330 function insertHtml_shouldSupport_outside(where, tagName, tagNameToInsert){\r
331 it(["should insertHtml", tagNameToInsert.toUpperCase(), where,\r
332 "of a " + tagName.toUpperCase() + " Element"].join(' '), function(){\r
333 \r
334 var html = TestHelper.String.html(tagNameToInsert, where);\r
335 \r
336 var target = document.createElement(tagName);\r
337 document.createElement(TestHelper.dom.parentNodeName[tagName] || TestHelper.dom.parentNodeName['*']).appendChild(target);\r
338 var parentNode = target.parentNode;\r
339 \r
340 expect(parentNode).toBeDefined();\r
341 expect(target.childNodes.length).toBe(0);\r
342 expect(target.firstChild).toBeNull();\r
343 expect(parentNode.childNodes.length).toBe(1);\r
344 \r
345 dh.insertHtml(where, target, html);\r
346 parentNode.removeChild(target);\r
347 \r
348 expect(parentNode.childNodes.length).toBe(1);\r
349 expect(parentNode.firstChild.tagName.toLowerCase()).toBe(tagNameToInsert);\r
350 expect(parentNode.firstChild.className).toBe(where);\r
351 });\r
352 }\r
353 });\r
354 \r
355 });\r
356 \r
357 describe("overwrite", function() {\r
358 it("should overwrite the el", function() {\r
359 var node = Ext.getDom(els.first());\r
360 \r
361 expect(node.innerHTML).toEqual('');\r
362 \r
363 dh.overwrite(node, {\r
364 tag : 'span',\r
365 html: 'hello'\r
366 });\r
367 \r
368 expect(node.innerHTML.toLowerCase()).toEqual('<span>hello</span>');\r
369 });\r
370 \r
371 describe("table innerHTML bug", function(){\r
372 it("should set the html of a tr Element, even when it has no parentNode", function(){\r
373 var tr = document.createElement('tr');\r
374 expect(tr.parentNode).toEqual(null);\r
375 expect(tr.childNodes.length).toEqual(0);\r
376 \r
377 dh.overwrite(tr, '<td>cell1');\r
378 expect(tr.childNodes.length).toEqual(1);\r
379 expect(tr.lastChild.innerHTML).toEqual('cell1');\r
380 \r
381 dh.overwrite(tr, '<td>cell2');\r
382 expect(tr.childNodes.length).toEqual(1);\r
383 expect(tr.lastChild.innerHTML).toEqual('cell2');\r
384 \r
385 });\r
386 \r
387 it("should set the html of an Element", function(){\r
388 var html = '<a href="http://mootools.net/">Link</a>';\r
389 var parent = document.createElement('div');\r
390 dh.overwrite(parent, html);\r
391 expect(parent.innerHTML.toLowerCase()).toEqual(html.toLowerCase());\r
392 });\r
393 \r
394 it("should set the html of an Element with multiple arguments", function(){\r
395 var html = ['<p>Paragraph</p>', '<a href="http://mootools.net/">Link</a>'];\r
396 var parent = document.createElement('div');\r
397 dh.overwrite(parent, html);\r
398 \r
399 expect(parent.innerHTML.toLowerCase()).toEqual(html.join('').toLowerCase());\r
400 });\r
401 \r
402 it("should set the html of a select Element", function(){\r
403 var html = '<option>option 1</option><option selected="selected">option 2</option>';\r
404 var select = document.createElement('select');\r
405 dh.overwrite(select, html);\r
406 \r
407 expect(select.childNodes.length).toEqual(2);\r
408 expect(select.options.length).toEqual(2);\r
409 expect(select.selectedIndex).toEqual(1);\r
410 });\r
411 \r
412 it("should set the html of a table Element", function(){\r
413 var html = '<tbody><tr><td>cell 1</td><td>cell 2</td></tr><tr><td class="cell">cell 1</td><td>cell 2</td></tr></tbody>';\r
414 var table = document.createElement('table');\r
415 \r
416 dh.overwrite(table, html);\r
417 \r
418 expect(table.childNodes.length).toEqual(1);\r
419 expect(table.firstChild.firstChild.childNodes.length).toEqual(2);\r
420 expect(table.firstChild.lastChild.firstChild.className).toEqual('cell');\r
421 });\r
422 \r
423 it("should set the html of a tbody Element", function(){\r
424 var html = '<tr><td>cell 1</td><td>cell 2</td></tr><tr><td class="cell">cell 1</td><td>cell 2</td></tr>';\r
425 var tbody = document.createElement('tbody');\r
426 document.createElement('table').appendChild(tbody);\r
427 dh.overwrite(tbody, html);\r
428 \r
429 expect(tbody.childNodes.length).toEqual(2);\r
430 expect(tbody.lastChild.firstChild.className).toEqual('cell');\r
431 });\r
432 \r
433 it("should set the html of a thead Element", function(){\r
434 var html = '<tr><td>cell 1</td><td>cell 2</td></tr><tr><td class="cell">cell 1</td><td>cell 2</td></tr>';\r
435 var thead = document.createElement('thead');\r
436 document.createElement('table').appendChild(thead);\r
437 dh.overwrite(thead, html);\r
438 \r
439 expect(thead.childNodes.length).toEqual(2);\r
440 expect(thead.lastChild.firstChild.className).toEqual('cell');\r
441 });\r
442 \r
443 it("should set the html of a tfoot Element", function(){\r
444 var html = '<tr><td>cell 1</td><td>cell 2</td></tr><tr><td class="cell">cell 1</td><td>cell 2</td></tr>';\r
445 var tfoot = document.createElement('tfoot');\r
446 document.createElement('table').appendChild(tfoot);\r
447 dh.overwrite(tfoot, html);\r
448 \r
449 expect(tfoot.childNodes.length).toEqual(2);\r
450 expect(tfoot.lastChild.firstChild.className).toEqual('cell');\r
451 });\r
452 \r
453 it("should set the html of a tr Element", function(){\r
454 var html = '<td class="cell">cell 1</td><td>cell 2</td>';\r
455 var tr = document.createElement('tr');\r
456 document.createElement('tbody').appendChild(tr);\r
457 document.createElement('table').appendChild(tr.parentNode);\r
458 dh.overwrite(tr, html);\r
459 \r
460 expect(tr.childNodes.length).toEqual(2);\r
461 expect(tr.firstChild.className).toEqual('cell');\r
462 });\r
463 \r
464 it("should set the html of a td Element", function(){\r
465 var html = '<span class="span">Some Span</span><a href="#">Some Link</a>';\r
466 var td = document.createElement('td');\r
467 document.createElement('tr').appendChild(td);\r
468 document.createElement('tbody').appendChild(td.parentNode);\r
469 document.createElement('table').appendChild(td.parentNode.parentNode);\r
470 dh.overwrite(td, html);\r
471 \r
472 expect(td.childNodes.length).toEqual(2);\r
473 expect(td.firstChild.className).toEqual('span');\r
474 });\r
475 \r
476 });\r
477 });\r
478}\r
479});\r
480\r