mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-09-01 23:37:43 +02:00
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@93554 467b73ca-7a2a-4603-9d3b-597d59a354a9
101 lines
3.3 KiB
JavaScript
101 lines
3.3 KiB
JavaScript
// Copyright (c) 2005 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
|
|
//
|
|
// See scriptaculous.js for full license.
|
|
|
|
var Builder = {
|
|
NODEMAP: {
|
|
AREA: 'map',
|
|
CAPTION: 'table',
|
|
COL: 'table',
|
|
COLGROUP: 'table',
|
|
LEGEND: 'fieldset',
|
|
OPTGROUP: 'select',
|
|
OPTION: 'select',
|
|
PARAM: 'object',
|
|
TBODY: 'table',
|
|
TD: 'table',
|
|
TFOOT: 'table',
|
|
TH: 'table',
|
|
THEAD: 'table',
|
|
TR: 'table'
|
|
},
|
|
// note: For Firefox < 1.5, OPTION and OPTGROUP tags are currently broken,
|
|
// due to a Firefox bug
|
|
node: function(elementName) {
|
|
elementName = elementName.toUpperCase();
|
|
|
|
// try innerHTML approach
|
|
var parentTag = this.NODEMAP[elementName] || 'div';
|
|
var parentElement = document.createElement(parentTag);
|
|
try { // prevent IE "feature": http://dev.rubyonrails.org/ticket/2707
|
|
parentElement.innerHTML = "<" + elementName + "></" + elementName + ">";
|
|
} catch(e) {}
|
|
var element = parentElement.firstChild || null;
|
|
|
|
// see if browser added wrapping tags
|
|
if(element && (element.tagName != elementName))
|
|
element = element.getElementsByTagName(elementName)[0];
|
|
|
|
// fallback to createElement approach
|
|
if(!element) element = document.createElement(elementName);
|
|
|
|
// abort if nothing could be created
|
|
if(!element) return;
|
|
|
|
// attributes (or text)
|
|
if(arguments[1])
|
|
if(this._isStringOrNumber(arguments[1]) ||
|
|
(arguments[1] instanceof Array)) {
|
|
this._children(element, arguments[1]);
|
|
} else {
|
|
var attrs = this._attributes(arguments[1]);
|
|
if(attrs.length) {
|
|
try { // prevent IE "feature": http://dev.rubyonrails.org/ticket/2707
|
|
parentElement.innerHTML = "<" +elementName + " " +
|
|
attrs + "></" + elementName + ">";
|
|
} catch(e) {}
|
|
element = parentElement.firstChild || null;
|
|
// workaround firefox 1.0.X bug
|
|
if(!element) {
|
|
element = document.createElement(elementName);
|
|
for(attr in arguments[1])
|
|
element[attr == 'class' ? 'className' : attr] = arguments[1][attr];
|
|
}
|
|
if(element.tagName != elementName)
|
|
element = parentElement.getElementsByTagName(elementName)[0];
|
|
}
|
|
}
|
|
|
|
// text, or array of children
|
|
if(arguments[2])
|
|
this._children(element, arguments[2]);
|
|
|
|
return element;
|
|
},
|
|
_text: function(text) {
|
|
return document.createTextNode(text);
|
|
},
|
|
_attributes: function(attributes) {
|
|
var attrs = [];
|
|
for(attribute in attributes)
|
|
attrs.push((attribute=='className' ? 'class' : attribute) +
|
|
'="' + attributes[attribute].toString().escapeHTML() + '"');
|
|
return attrs.join(" ");
|
|
},
|
|
_children: function(element, children) {
|
|
if(typeof children=='object') { // array can hold nodes and text
|
|
children.flatten().each( function(e) {
|
|
if(typeof e=='object')
|
|
element.appendChild(e)
|
|
else
|
|
if(Builder._isStringOrNumber(e))
|
|
element.appendChild(Builder._text(e));
|
|
});
|
|
} else
|
|
if(Builder._isStringOrNumber(children))
|
|
element.appendChild(Builder._text(children));
|
|
},
|
|
_isStringOrNumber: function(param) {
|
|
return(typeof param=='string' || typeof param=='number');
|
|
}
|
|
} |