3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
10 Ext.apply(Ext.DomHelper,
13 afterbegin = 'afterbegin',
14 afterend = 'afterend',
15 beforebegin = 'beforebegin',
16 beforeend = 'beforeend';
19 function doInsert(el, o, returnElement, pos, sibling, append){
23 newNode = createDom(o, null);
25 el.appendChild(newNode);
27 (sibling == 'firstChild' ? el : el.parentNode).insertBefore(newNode, el[sibling] || el);
30 newNode = Ext.DomHelper.insertHtml(pos, el, Ext.DomHelper.createHtml(o));
32 return returnElement ? Ext.get(newNode, true) : newNode;
37 function createDom(o, parentNode){
45 if (Ext.isArray(o)) { // Allow Arrays of siblings to be inserted
46 el = doc.createDocumentFragment(); // in one shot using a DocumentFragment
47 Ext.each(o, function(v) {
50 } else if (Ext.isString(o)) { // Allow a string as a child spec.
51 el = doc.createTextNode(o);
53 el = doc.createElement( o.tag || 'div' );
54 useSet = !!el.setAttribute; // In IE some elements don't have setAttribute
55 Ext.iterate(o, function(attr, val){
56 if(!/tag|children|cn|html|style/.test(attr)){
61 el.setAttribute(attr, val);
68 Ext.DomHelper.applyStyles(el, o.style);
70 if ((cn = o.children || o.cn)) {
73 el.innerHTML = o.html;
77 parentNode.appendChild(el);
84 * Creates a new Ext.Template from the DOM object spec.
85 * @param {Object} o The DOM object spec (and children)
86 * @return {Ext.Template} The new template
88 createTemplate : function(o){
89 var html = Ext.DomHelper.createHtml(o);
90 return new Ext.Template(html);
93 /** True to force the use of DOM instead of html fragments @type Boolean */
97 * Creates new DOM element(s) and inserts them before el.
98 * @param {Mixed} el The context element
99 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
100 * @param {Boolean} returnElement (optional) true to return a Ext.Element
101 * @return {HTMLElement/Ext.Element} The new node
104 insertBefore : function(el, o, returnElement){
105 return doInsert(el, o, returnElement, beforebegin);
109 * Creates new DOM element(s) and inserts them after el.
110 * @param {Mixed} el The context element
111 * @param {Object} o The DOM object spec (and children)
112 * @param {Boolean} returnElement (optional) true to return a Ext.Element
113 * @return {HTMLElement/Ext.Element} The new node
116 insertAfter : function(el, o, returnElement){
117 return doInsert(el, o, returnElement, afterend, 'nextSibling');
121 * Creates new DOM element(s) and inserts them as the first child of el.
122 * @param {Mixed} el The context element
123 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
124 * @param {Boolean} returnElement (optional) true to return a Ext.Element
125 * @return {HTMLElement/Ext.Element} The new node
128 insertFirst : function(el, o, returnElement){
129 return doInsert(el, o, returnElement, afterbegin, 'firstChild');
133 * Creates new DOM element(s) and appends them to el.
134 * @param {Mixed} el The context element
135 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
136 * @param {Boolean} returnElement (optional) true to return a Ext.Element
137 * @return {HTMLElement/Ext.Element} The new node
140 append: function(el, o, returnElement){
141 return doInsert(el, o, returnElement, beforeend, '', true);
145 * Creates new DOM element(s) without inserting them to the document.
146 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
147 * @return {HTMLElement} The new uninserted node