terom@30: // script.aculo.us builder.js v1.8.0, Tue Nov 06 15:01:40 +0300 2007 terom@30: terom@30: // Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) terom@30: // terom@30: // script.aculo.us is freely distributable under the terms of an MIT-style license. terom@30: // For details, see the script.aculo.us web site: http://script.aculo.us/ terom@30: terom@30: var Builder = { terom@30: NODEMAP: { terom@30: AREA: 'map', terom@30: CAPTION: 'table', terom@30: COL: 'table', terom@30: COLGROUP: 'table', terom@30: LEGEND: 'fieldset', terom@30: OPTGROUP: 'select', terom@30: OPTION: 'select', terom@30: PARAM: 'object', terom@30: TBODY: 'table', terom@30: TD: 'table', terom@30: TFOOT: 'table', terom@30: TH: 'table', terom@30: THEAD: 'table', terom@30: TR: 'table' terom@30: }, terom@30: // note: For Firefox < 1.5, OPTION and OPTGROUP tags are currently broken, terom@30: // due to a Firefox bug terom@30: node: function(elementName) { terom@30: elementName = elementName.toUpperCase(); terom@30: terom@30: // try innerHTML approach terom@30: var parentTag = this.NODEMAP[elementName] || 'div'; terom@30: var parentElement = document.createElement(parentTag); terom@30: try { // prevent IE "feature": http://dev.rubyonrails.org/ticket/2707 terom@30: parentElement.innerHTML = "<" + elementName + ">"; terom@30: } catch(e) {} terom@30: var element = parentElement.firstChild || null; terom@30: terom@30: // see if browser added wrapping tags terom@30: if(element && (element.tagName.toUpperCase() != elementName)) terom@30: element = element.getElementsByTagName(elementName)[0]; terom@30: terom@30: // fallback to createElement approach terom@30: if(!element) element = document.createElement(elementName); terom@30: terom@30: // abort if nothing could be created terom@30: if(!element) return; terom@30: terom@30: // attributes (or text) terom@30: if(arguments[1]) terom@30: if(this._isStringOrNumber(arguments[1]) || terom@30: (arguments[1] instanceof Array) || terom@30: arguments[1].tagName) { terom@30: this._children(element, arguments[1]); terom@30: } else { terom@30: var attrs = this._attributes(arguments[1]); terom@30: if(attrs.length) { terom@30: try { // prevent IE "feature": http://dev.rubyonrails.org/ticket/2707 terom@30: parentElement.innerHTML = "<" +elementName + " " + terom@30: attrs + ">"; terom@30: } catch(e) {} terom@30: element = parentElement.firstChild || null; terom@30: // workaround firefox 1.0.X bug terom@30: if(!element) { terom@30: element = document.createElement(elementName); terom@30: for(attr in arguments[1]) terom@30: element[attr == 'class' ? 'className' : attr] = arguments[1][attr]; terom@30: } terom@30: if(element.tagName.toUpperCase() != elementName) terom@30: element = parentElement.getElementsByTagName(elementName)[0]; terom@30: } terom@30: } terom@30: terom@30: // text, or array of children terom@30: if(arguments[2]) terom@30: this._children(element, arguments[2]); terom@30: terom@30: return element; terom@30: }, terom@30: _text: function(text) { terom@30: return document.createTextNode(text); terom@30: }, terom@30: terom@30: ATTR_MAP: { terom@30: 'className': 'class', terom@30: 'htmlFor': 'for' terom@30: }, terom@30: terom@30: _attributes: function(attributes) { terom@30: var attrs = []; terom@30: for(attribute in attributes) terom@30: attrs.push((attribute in this.ATTR_MAP ? this.ATTR_MAP[attribute] : attribute) + terom@30: '="' + attributes[attribute].toString().escapeHTML().gsub(/"/,'"') + '"'); terom@30: return attrs.join(" "); terom@30: }, terom@30: _children: function(element, children) { terom@30: if(children.tagName) { terom@30: element.appendChild(children); terom@30: return; terom@30: } terom@30: if(typeof children=='object') { // array can hold nodes and text terom@30: children.flatten().each( function(e) { terom@30: if(typeof e=='object') terom@30: element.appendChild(e) terom@30: else terom@30: if(Builder._isStringOrNumber(e)) terom@30: element.appendChild(Builder._text(e)); terom@30: }); terom@30: } else terom@30: if(Builder._isStringOrNumber(children)) terom@30: element.appendChild(Builder._text(children)); terom@30: }, terom@30: _isStringOrNumber: function(param) { terom@30: return(typeof param=='string' || typeof param=='number'); terom@30: }, terom@30: build: function(html) { terom@30: var element = this.node('div'); terom@30: $(element).update(html.strip()); terom@30: return element.down(); terom@30: }, terom@30: dump: function(scope) { terom@30: if(typeof scope != 'object' && typeof scope != 'function') scope = window; //global scope terom@30: terom@30: var tags = ("A ABBR ACRONYM ADDRESS APPLET AREA B BASE BASEFONT BDO BIG BLOCKQUOTE BODY " + terom@30: "BR BUTTON CAPTION CENTER CITE CODE COL COLGROUP DD DEL DFN DIR DIV DL DT EM FIELDSET " + terom@30: "FONT FORM FRAME FRAMESET H1 H2 H3 H4 H5 H6 HEAD HR HTML I IFRAME IMG INPUT INS ISINDEX "+ terom@30: "KBD LABEL LEGEND LI LINK MAP MENU META NOFRAMES NOSCRIPT OBJECT OL OPTGROUP OPTION P "+ terom@30: "PARAM PRE Q S SAMP SCRIPT SELECT SMALL SPAN STRIKE STRONG STYLE SUB SUP TABLE TBODY TD "+ terom@30: "TEXTAREA TFOOT TH THEAD TITLE TR TT U UL VAR").split(/\s+/); terom@30: terom@30: tags.each( function(tag){ terom@30: scope[tag] = function() { terom@30: return Builder.node.apply(Builder, [tag].concat($A(arguments))); terom@30: } terom@30: }); terom@30: } terom@30: }