PageRenderTime 16ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/testing/selenium-core/lib/scriptaculous/builder.js

http://datanucleus-appengine.googlecode.com/
JavaScript | 101 lines | 81 code | 8 blank | 12 comment | 24 complexity | 6b737ecca99b73e70149195653627e4a MD5 | raw file
Possible License(s): Apache-2.0
  1. // Copyright (c) 2005 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
  2. //
  3. // See scriptaculous.js for full license.
  4. var Builder = {
  5. NODEMAP: {
  6. AREA: 'map',
  7. CAPTION: 'table',
  8. COL: 'table',
  9. COLGROUP: 'table',
  10. LEGEND: 'fieldset',
  11. OPTGROUP: 'select',
  12. OPTION: 'select',
  13. PARAM: 'object',
  14. TBODY: 'table',
  15. TD: 'table',
  16. TFOOT: 'table',
  17. TH: 'table',
  18. THEAD: 'table',
  19. TR: 'table'
  20. },
  21. // note: For Firefox < 1.5, OPTION and OPTGROUP tags are currently broken,
  22. // due to a Firefox bug
  23. node: function(elementName) {
  24. elementName = elementName.toUpperCase();
  25. // try innerHTML approach
  26. var parentTag = this.NODEMAP[elementName] || 'div';
  27. var parentElement = document.createElement(parentTag);
  28. try { // prevent IE "feature": http://dev.rubyonrails.org/ticket/2707
  29. parentElement.innerHTML = "<" + elementName + "></" + elementName + ">";
  30. } catch(e) {}
  31. var element = parentElement.firstChild || null;
  32. // see if browser added wrapping tags
  33. if(element && (element.tagName != elementName))
  34. element = element.getElementsByTagName(elementName)[0];
  35. // fallback to createElement approach
  36. if(!element) element = document.createElement(elementName);
  37. // abort if nothing could be created
  38. if(!element) return;
  39. // attributes (or text)
  40. if(arguments[1])
  41. if(this._isStringOrNumber(arguments[1]) ||
  42. (arguments[1] instanceof Array)) {
  43. this._children(element, arguments[1]);
  44. } else {
  45. var attrs = this._attributes(arguments[1]);
  46. if(attrs.length) {
  47. try { // prevent IE "feature": http://dev.rubyonrails.org/ticket/2707
  48. parentElement.innerHTML = "<" +elementName + " " +
  49. attrs + "></" + elementName + ">";
  50. } catch(e) {}
  51. element = parentElement.firstChild || null;
  52. // workaround firefox 1.0.X bug
  53. if(!element) {
  54. element = document.createElement(elementName);
  55. for(attr in arguments[1])
  56. element[attr == 'class' ? 'className' : attr] = arguments[1][attr];
  57. }
  58. if(element.tagName != elementName)
  59. element = parentElement.getElementsByTagName(elementName)[0];
  60. }
  61. }
  62. // text, or array of children
  63. if(arguments[2])
  64. this._children(element, arguments[2]);
  65. return element;
  66. },
  67. _text: function(text) {
  68. return document.createTextNode(text);
  69. },
  70. _attributes: function(attributes) {
  71. var attrs = [];
  72. for(attribute in attributes)
  73. attrs.push((attribute=='className' ? 'class' : attribute) +
  74. '="' + attributes[attribute].toString().escapeHTML() + '"');
  75. return attrs.join(" ");
  76. },
  77. _children: function(element, children) {
  78. if(typeof children=='object') { // array can hold nodes and text
  79. children.flatten().each( function(e) {
  80. if(typeof e=='object')
  81. element.appendChild(e)
  82. else
  83. if(Builder._isStringOrNumber(e))
  84. element.appendChild(Builder._text(e));
  85. });
  86. } else
  87. if(Builder._isStringOrNumber(children))
  88. element.appendChild(Builder._text(children));
  89. },
  90. _isStringOrNumber: function(param) {
  91. return(typeof param=='string' || typeof param=='number');
  92. }
  93. }