4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-DomHelper'>/**
19 </span> * @class Ext.DomHelper
20 * @alternateClassName Ext.core.DomHelper
22 * <p>The DomHelper class provides a layer of abstraction from DOM and transparently supports creating
23 * elements via DOM or using HTML fragments. It also has the ability to create HTML fragment templates
24 * from your DOM building code.</p>
26 * <p><b><u>DomHelper element specification object</u></b></p>
27 * <p>A specification object is used when creating elements. Attributes of this object
28 * are assumed to be element attributes, except for 4 special attributes:
29 * <div class="mdetail-params"><ul>
30 * <li><b><tt>tag</tt></b> : <div class="sub-desc">The tag name of the element</div></li>
31 * <li><b><tt>children</tt></b> : or <tt>cn</tt><div class="sub-desc">An array of the
32 * same kind of element definition objects to be created and appended. These can be nested
33 * as deep as you want.</div></li>
34 * <li><b><tt>cls</tt></b> : <div class="sub-desc">The class attribute of the element.
35 * This will end up being either the "class" attribute on a HTML fragment or className
36 * for a DOM node, depending on whether DomHelper is using fragments or DOM.</div></li>
37 * <li><b><tt>html</tt></b> : <div class="sub-desc">The innerHTML for the element</div></li>
38 * </ul></div></p>
39 * <p><b>NOTE:</b> For other arbitrary attributes, the value will currently <b>not</b> be automatically
40 * HTML-escaped prior to building the element's HTML string. This means that if your attribute value
41 * contains special characters that would not normally be allowed in a double-quoted attribute value,
42 * you <b>must</b> manually HTML-encode it beforehand (see {@link Ext.String#htmlEncode}) or risk
43 * malformed HTML being created. This behavior may change in a future release.</p>
45 * <p><b><u>Insertion methods</u></b></p>
46 * <p>Commonly used insertion methods:
47 * <div class="mdetail-params"><ul>
48 * <li><tt>{@link #append}</tt> : <div class="sub-desc"></div></li>
49 * <li><tt>{@link #insertBefore}</tt> : <div class="sub-desc"></div></li>
50 * <li><tt>{@link #insertAfter}</tt> : <div class="sub-desc"></div></li>
51 * <li><tt>{@link #overwrite}</tt> : <div class="sub-desc"></div></li>
52 * <li><tt>{@link #createTemplate}</tt> : <div class="sub-desc"></div></li>
53 * <li><tt>{@link #insertHtml}</tt> : <div class="sub-desc"></div></li>
54 * </ul></div></p>
56 * <p><b><u>Example</u></b></p>
57 * <p>This is an example, where an unordered list with 3 children items is appended to an existing
58 * element with id <tt>'my-div'</tt>:<br>
59 <pre><code>
60 var dh = Ext.DomHelper; // create shorthand alias
61 // specification object
66 // append children after creating
67 children: [ // may also specify 'cn' instead of 'children'
68 {tag: 'li', id: 'item0', html: 'List Item 0'},
69 {tag: 'li', id: 'item1', html: 'List Item 1'},
70 {tag: 'li', id: 'item2', html: 'List Item 2'}
74 'my-div', // the context element 'my-div' can either be the id or the actual node
75 spec // the specification object
77 </code></pre></p>
78 * <p>Element creation specification parameters in this class may also be passed as an Array of
79 * specification objects. This can be used to insert multiple sibling nodes into an existing
80 * container very efficiently. For example, to add more list items to the example above:<pre><code>
82 {tag: 'li', id: 'item3', html: 'List Item 3'},
83 {tag: 'li', id: 'item4', html: 'List Item 4'}
85 * </code></pre></p>
87 * <p><b><u>Templating</u></b></p>
88 * <p>The real power is in the built-in templating. Instead of creating or appending any elements,
89 * <tt>{@link #createTemplate}</tt> returns a Template object which can be used over and over to
90 * insert new elements. Revisiting the example above, we could utilize templating this time:
91 * <pre><code>
93 var list = dh.append('my-div', {tag: 'ul', cls: 'my-list'});
95 var tpl = dh.createTemplate({tag: 'li', id: 'item{0}', html: 'List Item {0}'});
97 for(var i = 0; i < 5, i++){
98 tpl.append(list, [i]); // use template to append to the actual node
100 * </code></pre></p>
101 * <p>An example using a template:<pre><code>
102 var html = '<a id="{0}" href="{1}" class="nav">{2}</a>';
104 var tpl = new Ext.DomHelper.createTemplate(html);
105 tpl.append('blog-roll', ['link1', 'http://www.edspencer.net/', "Ed&#39;s Site"]);
106 tpl.append('blog-roll', ['link2', 'http://www.dustindiaz.com/', "Dustin&#39;s Site"]);
107 * </code></pre></p>
109 * <p>The same example using named parameters:<pre><code>
110 var html = '<a id="{id}" href="{url}" class="nav">{text}</a>';
112 var tpl = new Ext.DomHelper.createTemplate(html);
113 tpl.append('blog-roll', {
115 url: 'http://www.edspencer.net/',
116 text: "Ed&#39;s Site"
118 tpl.append('blog-roll', {
120 url: 'http://www.dustindiaz.com/',
121 text: "Dustin&#39;s Site"
123 * </code></pre></p>
125 * <p><b><u>Compiling Templates</u></b></p>
126 * <p>Templates are applied using regular expressions. The performance is great, but if
127 * you are adding a bunch of DOM elements using the same template, you can increase
128 * performance even further by {@link Ext.Template#compile "compiling"} the template.
129 * The way "{@link Ext.Template#compile compile()}" works is the template is parsed and
130 * broken up at the different variable points and a dynamic function is created and eval'ed.
131 * The generated function performs string concatenation of these parts and the passed
132 * variables instead of using regular expressions.
133 * <pre><code>
134 var html = '<a id="{id}" href="{url}" class="nav">{text}</a>';
136 var tpl = new Ext.DomHelper.createTemplate(html);
139 //... use template like normal
140 * </code></pre></p>
142 * <p><b><u>Performance Boost</u></b></p>
143 * <p>DomHelper will transparently create HTML fragments when it can. Using HTML fragments instead
144 * of DOM can significantly boost performance.</p>
145 * <p>Element creation specification parameters may also be strings. If {@link #useDom} is <tt>false</tt>,
146 * then the string is used as innerHTML. If {@link #useDom} is <tt>true</tt>, a string specification
147 * results in the creation of a text node. Usage:</p>
148 * <pre><code>
149 Ext.DomHelper.useDom = true; // force it to use DOM; reduces performance
150 * </code></pre>
154 Ext.core.DomHelper = Ext.DomHelper = function(){
155 var tempTableEl = null,
156 emptyTags = /^(?:br|frame|hr|img|input|link|meta|range|spacer|wbr|area|param|col)$/i,
157 tableRe = /^table|tbody|tr|td$/i,
158 confRe = /tag|children|cn|html$/i,
159 tableElRe = /td|tr|tbody/i,
162 // kill repeat to save bytes
163 afterbegin = 'afterbegin',
164 afterend = 'afterend',
165 beforebegin = 'beforebegin',
166 beforeend = 'beforeend',
167 ts = '<table>',
168 te = '</table>',
169 tbs = ts+'<tbody>',
170 tbe = '</tbody>'+te,
171 trs = tbs + '<tr>',
172 tre = '</tr>'+tbe;
175 function doInsert(el, o, returnElement, pos, sibling, append){
179 newNode = createDom(o, null);
181 el.appendChild(newNode);
183 (sibling == 'firstChild' ? el : el.parentNode).insertBefore(newNode, el[sibling] || el);
186 newNode = Ext.DomHelper.insertHtml(pos, el, Ext.DomHelper.createHtml(o));
188 return returnElement ? Ext.get(newNode, true) : newNode;
191 function createDom(o, parentNode){
199 if (Ext.isArray(o)) { // Allow Arrays of siblings to be inserted
200 el = doc.createDocumentFragment(); // in one shot using a DocumentFragment
201 for (var i = 0, l = o.length; i < l; i++) {
204 } else if (typeof o == 'string') { // Allow a string as a child spec.
205 el = doc.createTextNode(o);
207 el = doc.createElement( o.tag || 'div' );
208 useSet = !!el.setAttribute; // In IE some elements don't have setAttribute
210 if(!confRe.test(attr)){
216 el.setAttribute(attr, val);
223 Ext.DomHelper.applyStyles(el, o.style);
225 if ((cn = o.children || o.cn)) {
228 el.innerHTML = o.html;
232 parentNode.appendChild(el);
237 // build as innerHTML where available
238 function createHtml(o){
246 if(typeof o == "string"){
248 } else if (Ext.isArray(o)) {
249 for (i=0; i < o.length; i++) {
251 b += createHtml(o[i]);
255 b += '<' + (o.tag = o.tag || 'div');
258 if(!confRe.test(attr)){
259 if (typeof val == "object") {
260 b += ' ' + attr + '="';
262 b += key + ':' + val[key] + ';';
266 b += ' ' + ({cls : 'class', htmlFor : 'for'}[attr] || attr) + '="' + val + '"';
270 // Now either just close the tag or try to add children and close the tag.
271 if (emptyTags.test(o.tag)) {
275 if ((cn = o.children || o.cn)) {
280 b += '</' + o.tag + '>';
286 function ieTable(depth, s, h, e){
287 tempTableEl.innerHTML = [s, h, e].join('');
291 while(++i < depth){
294 // If the result is multiple siblings, then encapsulate them into one fragment.
297 var df = document.createDocumentFragment();
308 <span id='Ext-DomHelper-method-insertIntoTable'> /**
310 * Nasty code for IE's broken table implementation
312 function insertIntoTable(tag, where, el, html) {
316 tempTableEl = tempTableEl || document.createElement('div');
318 if(tag == 'td' && (where == afterbegin || where == beforeend) ||
319 !tableElRe.test(tag) && (where == beforebegin || where == afterend)) {
322 before = where == beforebegin ? el :
323 where == afterend ? el.nextSibling :
324 where == afterbegin ? el.firstChild : null;
326 if (where == beforebegin || where == afterend) {
330 if (tag == 'td' || (tag == 'tr' && (where == beforeend || where == afterbegin))) {
331 node = ieTable(4, trs, html, tre);
332 } else if ((tag == 'tbody' && (where == beforeend || where == afterbegin)) ||
333 (tag == 'tr' && (where == beforebegin || where == afterend))) {
334 node = ieTable(3, tbs, html, tbe);
336 node = ieTable(2, ts, html, te);
338 el.insertBefore(node, before);
342 <span id='Ext-DomHelper-method-createContextualFragment'> /**
344 * Fix for IE9 createContextualFragment missing method
346 function createContextualFragment(html){
347 var div = document.createElement("div"),
348 fragment = document.createDocumentFragment(),
352 div.innerHTML = html;
353 childNodes = div.childNodes;
354 length = childNodes.length;
356 for (; i < length; i++) {
357 fragment.appendChild(childNodes[i].cloneNode(true));
364 <span id='Ext-DomHelper-method-markup'> /**
365 </span> * Returns the markup for the passed Element(s) config.
366 * @param {Object} o The DOM object spec (and children)
369 markup : function(o){
370 return createHtml(o);
373 <span id='Ext-DomHelper-method-applyStyles'> /**
374 </span> * Applies a style specification to an element.
375 * @param {String/HTMLElement} el The element to apply styles to
376 * @param {String/Object/Function} styles A style specification string e.g. 'width:100px', or object in the form {width:'100px'}, or
377 * a function which returns such a specification.
379 applyStyles : function(el, styles){
382 if (typeof styles == "function") {
383 styles = styles.call();
385 if (typeof styles == "string") {
386 styles = Ext.Element.parseStyles(styles);
388 if (typeof styles == "object") {
394 <span id='Ext-DomHelper-method-insertHtml'> /**
395 </span> * Inserts an HTML fragment into the DOM.
396 * @param {String} where Where to insert the html in relation to el - beforeBegin, afterBegin, beforeEnd, afterEnd.
398 * For example take the following HTML: `<div>Contents</div>`
400 * Using different `where` values inserts element to the following places:
402 * - beforeBegin: `<HERE><div>Contents</div>`
403 * - afterBegin: `<div><HERE>Contents</div>`
404 * - beforeEnd: `<div>Contents<HERE></div>`
405 * - afterEnd: `<div>Contents</div><HERE>`
407 * @param {HTMLElement/TextNode} el The context element
408 * @param {String} html The HTML fragment
409 * @return {HTMLElement} The new node
411 insertHtml : function(where, el, html){
420 where = where.toLowerCase();
421 // add these here because they are used in both branches of the condition.
422 hash[beforebegin] = ['BeforeBegin', 'previousSibling'];
423 hash[afterend] = ['AfterEnd', 'nextSibling'];
425 // if IE and context element is an HTMLElement
426 if (el.insertAdjacentHTML) {
427 if(tableRe.test(el.tagName) && (rs = insertIntoTable(el.tagName.toLowerCase(), where, el, html))){
431 // add these two to the hash.
432 hash[afterbegin] = ['AfterBegin', 'firstChild'];
433 hash[beforeend] = ['BeforeEnd', 'lastChild'];
434 if ((hashVal = hash[where])) {
435 el.insertAdjacentHTML(hashVal[0], html);
436 return el[hashVal[1]];
438 // if (not IE and context element is an HTMLElement) or TextNode
440 // we cannot insert anything inside a textnode so...
441 if (Ext.isTextNode(el)) {
442 where = where === 'afterbegin' ? 'beforebegin' : where;
443 where = where === 'beforeend' ? 'afterend' : where;
445 range = Ext.supports.CreateContextualFragment ? el.ownerDocument.createRange() : undefined;
446 setStart = 'setStart' + (endRe.test(where) ? 'After' : 'Before');
450 frag = range.createContextualFragment(html);
452 frag = createContextualFragment(html);
454 el.parentNode.insertBefore(frag, where == beforebegin ? el : el.nextSibling);
455 return el[(where == beforebegin ? 'previous' : 'next') + 'Sibling'];
457 rangeEl = (where == afterbegin ? 'first' : 'last') + 'Child';
460 range[setStart](el[rangeEl]);
461 frag = range.createContextualFragment(html);
463 frag = createContextualFragment(html);
466 if(where == afterbegin){
467 el.insertBefore(frag, el.firstChild);
469 el.appendChild(frag);
479 sourceClass: 'Ext.DomHelper',
480 sourceMethod: 'insertHtml',
483 msg: 'Illegal insertion point reached: "' + where + '"'
488 <span id='Ext-DomHelper-method-insertBefore'> /**
489 </span> * Creates new DOM element(s) and inserts them before el.
490 * @param {String/HTMLElement/Ext.Element} el The context element
491 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
492 * @param {Boolean} returnElement (optional) true to return a Ext.Element
493 * @return {HTMLElement/Ext.Element} The new node
495 insertBefore : function(el, o, returnElement){
496 return doInsert(el, o, returnElement, beforebegin);
499 <span id='Ext-DomHelper-method-insertAfter'> /**
500 </span> * Creates new DOM element(s) and inserts them after el.
501 * @param {String/HTMLElement/Ext.Element} el The context element
502 * @param {Object} o The DOM object spec (and children)
503 * @param {Boolean} returnElement (optional) true to return a Ext.Element
504 * @return {HTMLElement/Ext.Element} The new node
506 insertAfter : function(el, o, returnElement){
507 return doInsert(el, o, returnElement, afterend, 'nextSibling');
510 <span id='Ext-DomHelper-method-insertFirst'> /**
511 </span> * Creates new DOM element(s) and inserts them as the first child of el.
512 * @param {String/HTMLElement/Ext.Element} el The context element
513 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
514 * @param {Boolean} returnElement (optional) true to return a Ext.Element
515 * @return {HTMLElement/Ext.Element} The new node
517 insertFirst : function(el, o, returnElement){
518 return doInsert(el, o, returnElement, afterbegin, 'firstChild');
521 <span id='Ext-DomHelper-method-append'> /**
522 </span> * Creates new DOM element(s) and appends them to el.
523 * @param {String/HTMLElement/Ext.Element} el The context element
524 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
525 * @param {Boolean} returnElement (optional) true to return a Ext.Element
526 * @return {HTMLElement/Ext.Element} The new node
528 append : function(el, o, returnElement){
529 return doInsert(el, o, returnElement, beforeend, '', true);
532 <span id='Ext-DomHelper-method-overwrite'> /**
533 </span> * Creates new DOM element(s) and overwrites the contents of el with them.
534 * @param {String/HTMLElement/Ext.Element} el The context element
535 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
536 * @param {Boolean} returnElement (optional) true to return a Ext.Element
537 * @return {HTMLElement/Ext.Element} The new node
539 overwrite : function(el, o, returnElement){
541 el.innerHTML = createHtml(o);
542 return returnElement ? Ext.get(el.firstChild) : el.firstChild;
545 createHtml : createHtml,
547 <span id='Ext-DomHelper-method-createDom'> /**
548 </span> * Creates new DOM element(s) without inserting them to the document.
549 * @param {Object/String} o The DOM object spec (and children) or raw HTML blob
550 * @return {HTMLElement} The new uninserted node
553 createDom: createDom,
555 <span id='Ext-DomHelper-property-useDom'> /** True to force the use of DOM instead of html fragments @type Boolean */
556 </span> useDom : false,
558 <span id='Ext-DomHelper-method-createTemplate'> /**
559 </span> * Creates a new Ext.Template from the DOM object spec.
560 * @param {Object} o The DOM object spec (and children)
561 * @return {Ext.Template} The new template
563 createTemplate : function(o){
564 var html = Ext.DomHelper.createHtml(o);
565 return Ext.create('Ext.Template', html);