1 // Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
3 // script.aculo.us is freely distributable under the terms of an MIT-style license.
4 // For details, see the script.aculo.us web site: http://script.aculo.us/
25 // note: For Firefox < 1.5, OPTION and OPTGROUP tags are currently broken,
26 // due to a Firefox bug
27 node: function(elementName
) {
28 elementName
= elementName
.toUpperCase();
30 // try innerHTML approach
31 var parentTag
= this.NODEMAP
[elementName
] || 'div';
32 var parentElement
= document
.createElement(parentTag
);
33 try { // prevent IE "feature": http://dev.rubyonrails.org/ticket/2707
34 parentElement
.innerHTML
= "<" + elementName
+ "></" + elementName
+ ">";
36 var element
= parentElement
.firstChild
|| null;
38 // see if browser added wrapping tags
39 if(element
&& (element
.tagName
.toUpperCase() != elementName
))
40 element
= element
.getElementsByTagName(elementName
)[0];
42 // fallback to createElement approach
43 if(!element
) element
= document
.createElement(elementName
);
45 // abort if nothing could be created
48 // attributes (or text)
50 if(this._isStringOrNumber(arguments
[1]) ||
51 (arguments
[1] instanceof Array
) ||
52 arguments
[1].tagName
) {
53 this._children(element
, arguments
[1]);
55 var attrs
= this._attributes(arguments
[1]);
57 try { // prevent IE "feature": http://dev.rubyonrails.org/ticket/2707
58 parentElement
.innerHTML
= "<" +elementName
+ " " +
59 attrs
+ "></" + elementName
+ ">";
61 element
= parentElement
.firstChild
|| null;
62 // workaround firefox 1.0.X bug
64 element
= document
.createElement(elementName
);
65 for(attr
in arguments
[1])
66 element
[attr
== 'class' ? 'className' : attr
] = arguments
[1][attr
];
68 if(element
.tagName
.toUpperCase() != elementName
)
69 element
= parentElement
.getElementsByTagName(elementName
)[0];
73 // text, or array of children
75 this._children(element
, arguments
[2]);
79 _text: function(text
) {
80 return document
.createTextNode(text
);
88 _attributes: function(attributes
) {
90 for(attribute
in attributes
)
91 attrs
.push((attribute
in this.ATTR_MAP
? this.ATTR_MAP
[attribute
] : attribute
) +
92 '="' + attributes
[attribute
].toString().escapeHTML().gsub(/"/,'"') + '"');
93 return attrs.join(" ");
95 _children: function(element, children) {
96 if(children.tagName) {
97 element.appendChild(children);
100 if(typeof children=='object
') { // array can hold nodes and text
101 children.flatten().each( function(e) {
102 if(typeof e=='object
')
103 element.appendChild(e)
105 if(Builder._isStringOrNumber(e))
106 element.appendChild(Builder._text(e));
109 if(Builder._isStringOrNumber(children))
110 element.appendChild(Builder._text(children));
112 _isStringOrNumber: function(param) {
113 return(typeof param=='string
' || typeof param=='number
');
115 build: function(html) {
116 var element = this.node('div
');
117 $(element).update(html.strip());
118 return element.down();
120 dump: function(scope) {
121 if(typeof scope != 'object
' && typeof scope != 'function') scope = window; //global scope
123 var tags = ("A ABBR ACRONYM ADDRESS APPLET AREA B BASE BASEFONT BDO BIG BLOCKQUOTE BODY " +
124 "BR BUTTON CAPTION CENTER CITE CODE COL COLGROUP DD DEL DFN DIR DIV DL DT EM FIELDSET " +
125 "FONT FORM FRAME FRAMESET H1 H2 H3 H4 H5 H6 HEAD HR HTML I IFRAME IMG INPUT INS ISINDEX "+
126 "KBD LABEL LEGEND LI LINK MAP MENU META NOFRAMES NOSCRIPT OBJECT OL OPTGROUP OPTION P "+
127 "PARAM PRE Q S SAMP SCRIPT SELECT SMALL SPAN STRIKE STRONG STYLE SUB SUP TABLE TBODY TD "+
128 "TEXTAREA TFOOT TH THEAD TITLE TR TT U UL VAR").split(/\s+/);
130 tags.each( function(tag){
131 scope[tag] = function() {
132 return Builder.node.apply(Builder, [tag].concat($A(arguments)));