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">
13 // nasty IE9 hack - what a pile of crap that is..
16 if (typeof Range.prototype.createContextualFragment == "undefined") {
18 if (typeof Range != "undefined" && typeof Range.prototype.createContextualFragment == "undefined") {
19 >>>>>>> cba88e023db3bc6c12affc2e25a149cdac04fd17
20 Range.prototype.createContextualFragment = function (html) {
21 var doc = window.document;
22 var container = doc.createElement("div");
23 container.innerHTML = html;
24 var frag = doc.createDocumentFragment(), n;
25 while ((n = container.firstChild)) {
33 * @class Roo.DomHelper
34 * Utility class for working with DOM and/or Templates. It transparently supports using HTML fragments or DOM.
35 * 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>.
38 Roo.DomHelper = function(){
39 var tempTableEl = null;
40 var emptyTags = /^(?:br|frame|hr|img|input|link|meta|range|spacer|wbr|area|param|col)$/i;
41 var tableRe = /^table|tbody|tr|td$/i;
43 // build as innerHTML where available
45 var createHtml = function(o){
46 if(typeof o == 'string'){
55 if(attr == "tag" || attr == "children" || attr == "cn" || attr == "html" || typeof o[attr] == "function") continue;
58 if(typeof s == "function"){
61 if(typeof s == "string"){
62 b += ' style="' + s + '"';
63 }else if(typeof s == "object"){
66 if(typeof s[key] != "function"){
67 b += key + ":" + s[key] + ";";
74 b += ' class="' + o["cls"] + '"';
75 }else if(attr == "htmlFor"){
76 b += ' for="' + o["htmlFor"] + '"';
78 b += " " + attr + '="' + o[attr] + '"';
82 if(emptyTags.test(o.tag)){
86 var cn = o.children || o.cn;
88 //http://bugs.kde.org/show_bug.cgi?id=71506
89 if((cn instanceof Array) || (Roo.isSafari && typeof(cn.join) == "function")){
90 for(var i = 0, len = cn.length; i < len; i++) {
91 b += createHtml(cn[i], b);
94 b += createHtml(cn, b);
100 b += "</" + o.tag + ">";
107 var createDom = function(o, parentNode){
109 // defininition craeted..
111 if (o.ns && o.ns != 'html') {
113 if (o.xmlns && typeof(xmlns[o.ns]) == 'undefined') {
114 xmlns[o.ns] = o.xmlns;
117 if (typeof(xmlns[o.ns]) == 'undefined') {
118 console.log("Trying to create namespace element " + o.ns + ", however no xmlns was sent to builder previously");
124 if (typeof(o) == 'string') {
125 return parentNode.appendChild(document.createTextNode(o));
127 o.tag = o.tag || div;
128 if (o.ns && Roo.isIE) {
130 o.tag = o.ns + ':' + o.tag;
133 var el = ns ? document.createElementNS( ns, o.tag||'div') : document.createElement(o.tag||'div');
134 var useSet = el.setAttribute ? true : false; // In IE some elements don't have setAttribute
137 if(attr == "tag" || attr == "ns" ||attr == "xmlns" ||attr == "children" || attr == "cn" || attr == "html" ||
138 attr == "style" || typeof o[attr] == "function") continue;
140 if(attr=="cls" && Roo.isIE){
141 el.className = o["cls"];
143 if(useSet) el.setAttribute(attr=="cls" ? 'class' : attr, o[attr]);
144 else el[attr] = o[attr];
147 Roo.DomHelper.applyStyles(el, o.style);
148 var cn = o.children || o.cn;
150 //http://bugs.kde.org/show_bug.cgi?id=71506
151 if((cn instanceof Array) || (Roo.isSafari && typeof(cn.join) == "function")){
152 for(var i = 0, len = cn.length; i < len; i++) {
153 createDom(cn[i], el);
160 el.innerHTML = o.html;
163 parentNode.appendChild(el);
168 var ieTable = function(depth, s, h, e){
169 tempTableEl.innerHTML = [s, h, e].join('');
170 var i = -1, el = tempTableEl;
177 // kill repeat to save bytes
187 * Nasty code for IE's broken table implementation
189 var insertIntoTable = function(tag, where, el, html){
191 tempTableEl = document.createElement('div');
196 if(where == 'afterbegin' || where == 'beforeend'){ // INTO a TD
199 if(where == 'beforebegin'){
203 before = el.nextSibling;
206 node = ieTable(4, trs, html, tre);
208 else if(tag == 'tr'){
209 if(where == 'beforebegin'){
212 node = ieTable(3, tbs, html, tbe);
213 } else if(where == 'afterend'){
214 before = el.nextSibling;
216 node = ieTable(3, tbs, html, tbe);
218 if(where == 'afterbegin'){
219 before = el.firstChild;
221 node = ieTable(4, trs, html, tre);
223 } else if(tag == 'tbody'){
224 if(where == 'beforebegin'){
227 node = ieTable(2, ts, html, te);
228 } else if(where == 'afterend'){
229 before = el.nextSibling;
231 node = ieTable(2, ts, html, te);
233 if(where == 'afterbegin'){
234 before = el.firstChild;
236 node = ieTable(3, tbs, html, tbe);
239 if(where == 'beforebegin' || where == 'afterend'){ // OUTSIDE the table
242 if(where == 'afterbegin'){
243 before = el.firstChild;
245 node = ieTable(2, ts, html, te);
247 el.insertBefore(node, before);
252 /** True to force the use of DOM instead of html fragments @type Boolean */
256 * Returns the markup for the passed Element(s) config
257 * @param {Object} o The Dom object spec (and children)
260 markup : function(o){
261 return createHtml(o);
265 * Applies a style specification to an element
266 * @param {String/HTMLElement} el The element to apply styles to
267 * @param {String/Object/Function} styles A style specification string eg "width:100px", or object in the form {width:"100px"}, or
268 * a function which returns such a specification.
270 applyStyles : function(el, styles){
273 if(typeof styles == "string"){
274 var re = /\s?([a-z\-]*)\:\s?([^;]*);?/gi;
276 while ((matches = re.exec(styles)) != null){
277 el.setStyle(matches[1], matches[2]);
279 }else if (typeof styles == "object"){
280 for (var style in styles){
281 el.setStyle(style, styles[style]);
283 }else if (typeof styles == "function"){
284 Roo.DomHelper.applyStyles(el, styles.call());
290 * Inserts an HTML fragment into the Dom
291 * @param {String} where Where to insert the html in relation to el - beforeBegin, afterBegin, beforeEnd, afterEnd.
292 * @param {HTMLElement} el The context element
293 * @param {String} html The HTML fragmenet
294 * @return {HTMLElement} The new node
296 insertHtml : function(where, el, html){
297 where = where.toLowerCase();
298 if(el.insertAdjacentHTML){
299 if(tableRe.test(el.tagName)){
301 if(rs = insertIntoTable(el.tagName.toLowerCase(), where, el, html)){
307 el.insertAdjacentHTML('BeforeBegin', html);
308 return el.previousSibling;
310 el.insertAdjacentHTML('AfterBegin', html);
311 return el.firstChild;
313 el.insertAdjacentHTML('BeforeEnd', html);
316 el.insertAdjacentHTML('AfterEnd', html);
317 return el.nextSibling;
319 throw 'Illegal insertion point -> "' + where + '"';
321 var range = el.ownerDocument.createRange();
325 range.setStartBefore(el);
326 frag = range.createContextualFragment(html);
327 el.parentNode.insertBefore(frag, el);
328 return el.previousSibling;
331 range.setStartBefore(el.firstChild);
332 frag = range.createContextualFragment(html);
333 el.insertBefore(frag, el.firstChild);
334 return el.firstChild;
337 return el.firstChild;
341 range.setStartAfter(el.lastChild);
342 frag = range.createContextualFragment(html);
343 el.appendChild(frag);
350 range.setStartAfter(el);
351 frag = range.createContextualFragment(html);
352 el.parentNode.insertBefore(frag, el.nextSibling);
353 return el.nextSibling;
355 throw 'Illegal insertion point -> "' + where + '"';
359 * Creates new Dom element(s) and inserts them before el
360 * @param {String/HTMLElement/Element} el The context element
361 * @param {Object/String} o The Dom object spec (and children) or raw HTML blob
362 * @param {Boolean} returnElement (optional) true to return a Roo.Element
363 * @return {HTMLElement/Roo.Element} The new node
365 insertBefore : function(el, o, returnElement){
366 return this.doInsert(el, o, returnElement, "beforeBegin");
370 * Creates new Dom element(s) and inserts them after el
371 * @param {String/HTMLElement/Element} el The context element
372 * @param {Object} o The Dom object spec (and children)
373 * @param {Boolean} returnElement (optional) true to return a Roo.Element
374 * @return {HTMLElement/Roo.Element} The new node
376 insertAfter : function(el, o, returnElement){
377 return this.doInsert(el, o, returnElement, "afterEnd", "nextSibling");
381 * Creates new Dom element(s) and inserts them as the first child of el
382 * @param {String/HTMLElement/Element} el The context element
383 * @param {Object/String} o The Dom object spec (and children) or raw HTML blob
384 * @param {Boolean} returnElement (optional) true to return a Roo.Element
385 * @return {HTMLElement/Roo.Element} The new node
387 insertFirst : function(el, o, returnElement){
388 return this.doInsert(el, o, returnElement, "afterBegin");
392 doInsert : function(el, o, returnElement, pos, sibling){
395 if(this.useDom || o.ns){
396 newNode = createDom(o, null);
397 el.parentNode.insertBefore(newNode, sibling ? el[sibling] : el);
399 var html = createHtml(o);
400 newNode = this.insertHtml(pos, el, html);
402 return returnElement ? Roo.get(newNode, true) : newNode;
406 * Creates new Dom element(s) and appends them to el
407 * @param {String/HTMLElement/Element} el The context element
408 * @param {Object/String} o The Dom object spec (and children) or raw HTML blob
409 * @param {Boolean} returnElement (optional) true to return a Roo.Element
410 * @return {HTMLElement/Roo.Element} The new node
412 append : function(el, o, returnElement){
415 if(this.useDom || o.ns){
416 newNode = createDom(o, null);
417 el.appendChild(newNode);
419 var html = createHtml(o);
420 newNode = this.insertHtml("beforeEnd", el, html);
422 return returnElement ? Roo.get(newNode, true) : newNode;
426 * Creates new Dom element(s) and overwrites the contents of el with them
427 * @param {String/HTMLElement/Element} el The context element
428 * @param {Object/String} o The Dom object spec (and children) or raw HTML blob
429 * @param {Boolean} returnElement (optional) true to return a Roo.Element
430 * @return {HTMLElement/Roo.Element} The new node
432 overwrite : function(el, o, returnElement){
436 while (el.childNodes.length) {
437 el.removeChild(el.firstChild);
441 el.innerHTML = createHtml(o);
444 return returnElement ? Roo.get(el.firstChild, true) : el.firstChild;
448 * Creates a new Roo.DomHelper.Template from the Dom object spec
449 * @param {Object} o The Dom object spec (and children)
450 * @return {Roo.DomHelper.Template} The new template
452 createTemplate : function(o){
453 var html = createHtml(o);
454 return new Roo.Template(html);