4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
14 * @class Roo.DomHelper
15 * Utility class for working with DOM and/or Templates. It transparently supports using HTML fragments or DOM.
16 * For more information see <a href="http://www.jackslocum.com/yui/2006/10/06/domhelper-create-elements-using-dom-html-fragments-or-templates/">this blog post with examples</a>.
19 Roo.DomHelper = function(){
20 var tempTableEl = null;
21 var emptyTags = /^(?:br|frame|hr|img|input|link|meta|range|spacer|wbr|area|param|col)$/i;
22 var tableRe = /^table|tbody|tr|td$/i;
24 // build as innerHTML where available
26 var createHtml = function(o){
27 if(typeof o == 'string'){
36 if(attr == "tag" || attr == "children" || attr == "cn" || attr == "html" || typeof o[attr] == "function") continue;
39 if(typeof s == "function"){
42 if(typeof s == "string"){
43 b += ' style="' + s + '"';
44 }else if(typeof s == "object"){
47 if(typeof s[key] != "function"){
48 b += key + ":" + s[key] + ";";
55 b += ' class="' + o["cls"] + '"';
56 }else if(attr == "htmlFor"){
57 b += ' for="' + o["htmlFor"] + '"';
59 b += " " + attr + '="' + o[attr] + '"';
63 if(emptyTags.test(o.tag)){
67 var cn = o.children || o.cn;
69 //http://bugs.kde.org/show_bug.cgi?id=71506
70 if((cn instanceof Array) || (Roo.isSafari && typeof(cn.join) == "function")){
71 for(var i = 0, len = cn.length; i < len; i++) {
72 b += createHtml(cn[i], b);
75 b += createHtml(cn, b);
81 b += "</" + o.tag + ">";
88 var createDom = function(o, parentNode){
90 // defininition craeted..
92 if (o.ns && o.ns != 'html') {
94 if (o.xmlns && typeof(xmlns[o.ns]) == 'undefined') {
95 xmlns[o.ns] = o.xmlns;
98 if (typeof(xmlns[o.ns]) == 'undefined') {
99 console.log("Trying to create namespace element " + o.ns + ", however no xmlns was sent to builder previously");
105 if (typeof(o) == 'string') {
106 return parentNode.appendChild(document.createTextNode(o));
108 o.tag = o.tag || div;
109 if (o.ns && Roo.isIE) {
111 o.tag = o.ns + ':' + o.tag;
114 var el = ns ? document.createElementNS( ns, o.tag||'div') : document.createElement(o.tag||'div');
115 var useSet = el.setAttribute ? true : false; // In IE some elements don't have setAttribute
118 if(attr == "tag" || attr == "ns" ||attr == "xmlns" ||attr == "children" || attr == "cn" || attr == "html" ||
119 attr == "style" || typeof o[attr] == "function") continue;
121 if(attr=="cls" && Roo.isIE){
122 el.className = o["cls"];
124 if(useSet) el.setAttribute(attr=="cls" ? 'class' : attr, o[attr]);
125 else el[attr] = o[attr];
128 Roo.DomHelper.applyStyles(el, o.style);
129 var cn = o.children || o.cn;
131 //http://bugs.kde.org/show_bug.cgi?id=71506
132 if((cn instanceof Array) || (Roo.isSafari && typeof(cn.join) == "function")){
133 for(var i = 0, len = cn.length; i < len; i++) {
134 createDom(cn[i], el);
141 el.innerHTML = o.html;
144 parentNode.appendChild(el);
149 var ieTable = function(depth, s, h, e){
150 tempTableEl.innerHTML = [s, h, e].join('');
151 var i = -1, el = tempTableEl;
158 // kill repeat to save bytes
168 * Nasty code for IE's broken table implementation
170 var insertIntoTable = function(tag, where, el, html){
172 tempTableEl = document.createElement('div');
177 if(where == 'afterbegin' || where == 'beforeend'){ // INTO a TD
180 if(where == 'beforebegin'){
184 before = el.nextSibling;
187 node = ieTable(4, trs, html, tre);
189 else if(tag == 'tr'){
190 if(where == 'beforebegin'){
193 node = ieTable(3, tbs, html, tbe);
194 } else if(where == 'afterend'){
195 before = el.nextSibling;
197 node = ieTable(3, tbs, html, tbe);
199 if(where == 'afterbegin'){
200 before = el.firstChild;
202 node = ieTable(4, trs, html, tre);
204 } else if(tag == 'tbody'){
205 if(where == 'beforebegin'){
208 node = ieTable(2, ts, html, te);
209 } else if(where == 'afterend'){
210 before = el.nextSibling;
212 node = ieTable(2, ts, html, te);
214 if(where == 'afterbegin'){
215 before = el.firstChild;
217 node = ieTable(3, tbs, html, tbe);
220 if(where == 'beforebegin' || where == 'afterend'){ // OUTSIDE the table
223 if(where == 'afterbegin'){
224 before = el.firstChild;
226 node = ieTable(2, ts, html, te);
228 el.insertBefore(node, before);
233 /** True to force the use of DOM instead of html fragments @type Boolean */
237 * Returns the markup for the passed Element(s) config
238 * @param {Object} o The Dom object spec (and children)
241 markup : function(o){
242 return createHtml(o);
246 * Applies a style specification to an element
247 * @param {String/HTMLElement} el The element to apply styles to
248 * @param {String/Object/Function} styles A style specification string eg "width:100px", or object in the form {width:"100px"}, or
249 * a function which returns such a specification.
251 applyStyles : function(el, styles){
254 if(typeof styles == "string"){
255 var re = /\s?([a-z\-]*)\:\s?([^;]*);?/gi;
257 while ((matches = re.exec(styles)) != null){
258 el.setStyle(matches[1], matches[2]);
260 }else if (typeof styles == "object"){
261 for (var style in styles){
262 el.setStyle(style, styles[style]);
264 }else if (typeof styles == "function"){
265 Roo.DomHelper.applyStyles(el, styles.call());
271 * Inserts an HTML fragment into the Dom
272 * @param {String} where Where to insert the html in relation to el - beforeBegin, afterBegin, beforeEnd, afterEnd.
273 * @param {HTMLElement} el The context element
274 * @param {String} html The HTML fragmenet
275 * @return {HTMLElement} The new node
277 insertHtml : function(where, el, html){
278 where = where.toLowerCase();
279 if(el.insertAdjacentHTML){
280 if(tableRe.test(el.tagName)){
282 if(rs = insertIntoTable(el.tagName.toLowerCase(), where, el, html)){
288 el.insertAdjacentHTML('BeforeBegin', html);
289 return el.previousSibling;
291 el.insertAdjacentHTML('AfterBegin', html);
292 return el.firstChild;
294 el.insertAdjacentHTML('BeforeEnd', html);
297 el.insertAdjacentHTML('AfterEnd', html);
298 return el.nextSibling;
300 throw 'Illegal insertion point -> "' + where + '"';
302 var range = el.ownerDocument.createRange();
306 range.setStartBefore(el);
307 frag = range.createContextualFragment(html);
308 el.parentNode.insertBefore(frag, el);
309 return el.previousSibling;
312 range.setStartBefore(el.firstChild);
313 frag = range.createContextualFragment(html);
314 el.insertBefore(frag, el.firstChild);
315 return el.firstChild;
318 return el.firstChild;
322 range.setStartAfter(el.lastChild);
323 frag = range.createContextualFragment(html);
324 el.appendChild(frag);
331 range.setStartAfter(el);
332 frag = range.createContextualFragment(html);
333 el.parentNode.insertBefore(frag, el.nextSibling);
334 return el.nextSibling;
336 throw 'Illegal insertion point -> "' + where + '"';
340 * Creates new Dom element(s) and inserts them before el
341 * @param {String/HTMLElement/Element} el The context element
342 * @param {Object/String} o The Dom object spec (and children) or raw HTML blob
343 * @param {Boolean} returnElement (optional) true to return a Roo.Element
344 * @return {HTMLElement/Roo.Element} The new node
346 insertBefore : function(el, o, returnElement){
347 return this.doInsert(el, o, returnElement, "beforeBegin");
351 * Creates new Dom element(s) and inserts them after el
352 * @param {String/HTMLElement/Element} el The context element
353 * @param {Object} o The Dom object spec (and children)
354 * @param {Boolean} returnElement (optional) true to return a Roo.Element
355 * @return {HTMLElement/Roo.Element} The new node
357 insertAfter : function(el, o, returnElement){
358 return this.doInsert(el, o, returnElement, "afterEnd", "nextSibling");
362 * Creates new Dom element(s) and inserts them as the first child of el
363 * @param {String/HTMLElement/Element} el The context element
364 * @param {Object/String} o The Dom object spec (and children) or raw HTML blob
365 * @param {Boolean} returnElement (optional) true to return a Roo.Element
366 * @return {HTMLElement/Roo.Element} The new node
368 insertFirst : function(el, o, returnElement){
369 return this.doInsert(el, o, returnElement, "afterBegin");
373 doInsert : function(el, o, returnElement, pos, sibling){
376 if(this.useDom || o.ns){
377 newNode = createDom(o, null);
378 el.parentNode.insertBefore(newNode, sibling ? el[sibling] : el);
380 var html = createHtml(o);
381 newNode = this.insertHtml(pos, el, html);
383 return returnElement ? Roo.get(newNode, true) : newNode;
387 * Creates new Dom element(s) and appends them to el
388 * @param {String/HTMLElement/Element} el The context element
389 * @param {Object/String} o The Dom object spec (and children) or raw HTML blob
390 * @param {Boolean} returnElement (optional) true to return a Roo.Element
391 * @return {HTMLElement/Roo.Element} The new node
393 append : function(el, o, returnElement){
396 if(this.useDom || o.ns){
397 newNode = createDom(o, null);
398 el.appendChild(newNode);
400 var html = createHtml(o);
401 newNode = this.insertHtml("beforeEnd", el, html);
403 return returnElement ? Roo.get(newNode, true) : newNode;
407 * Creates new Dom element(s) and overwrites the contents of el with them
408 * @param {String/HTMLElement/Element} el The context element
409 * @param {Object/String} o The Dom object spec (and children) or raw HTML blob
410 * @param {Boolean} returnElement (optional) true to return a Roo.Element
411 * @return {HTMLElement/Roo.Element} The new node
413 overwrite : function(el, o, returnElement){
417 while (el.childNodes.length) {
418 el.removeChild(el.firstChild);
422 el.innerHTML = createHtml(o);
425 return returnElement ? Roo.get(el.firstChild, true) : el.firstChild;
429 * Creates a new Roo.DomHelper.Template from the Dom object spec
430 * @param {Object} o The Dom object spec (and children)
431 * @return {Roo.DomHelper.Template} The new template
433 createTemplate : function(o){
434 var html = createHtml(o);
435 return new Roo.Template(html);