hyperscript.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* megascript */
  2. _WIN.tx = {};
  3. function each(a, f) {
  4. for (var i = 0, l = a.length; i < l; i++) f(a[i]);
  5. }
  6. _WIN.installHyperscript = function(namespace, oDocument) {
  7. each('a big blockquote br b center code dd dl dt div em font form h1 h2 h3 h4 h5 h6 hr img iframe input i li ol option pre p script select small span strike strong style sub sup table tbody td textarea tr ul u'.split(' '),
  8. function(label) {
  9. namespace[label]=function(){
  10. var tag=oDocument.createElement(label);
  11. each(arguments, function(arg){
  12. if(arg.nodeType) {
  13. tag.appendChild(arg);
  14. } else if(typeof arg=='string' || typeof arg=='number') {
  15. if(label == "textarea") {
  16. if($tx.msie) {
  17. tag.value+=arg;
  18. } else {
  19. tag.text+=arg;
  20. }
  21. } else {
  22. tag.innerHTML+=arg;
  23. }
  24. } else if(typeof arg=='array') {
  25. for(var i=0; i<arg.length; i++) {
  26. tag.appendChild(arg[i]);
  27. }
  28. } else {
  29. for(var attr in arg) {
  30. if(attr=='style') {
  31. for(var sty in arg[attr]) {
  32. if((sty == 'float' || sty == 'cssFloat')) {
  33. tag[attr][tag[attr].styleFloat === _UNDEFINED ? 'cssFloat' : 'styleFloat'] = arg[attr][sty];
  34. } else {
  35. tag[attr][sty]=arg[attr][sty];
  36. }
  37. }
  38. } else if(["more", "less", "longDesc"].contains(attr)) { // custom attributes
  39. if (tag.setAttribute) {
  40. tag.setAttribute(attr, arg[attr]);
  41. }
  42. } else if (["colSpan", "rowSpan", "cellPadding", "cellSpacing"].contains(attr)) { // nonstandard attributes
  43. if (tag.setAttribute) {
  44. tag.setAttribute(attr, arg[attr]);
  45. }
  46. } else {
  47. if (arg[attr]) {
  48. tag[attr] = arg[attr];
  49. }
  50. }
  51. }
  52. }
  53. });
  54. return tag;
  55. };
  56. });
  57. };
  58. installHyperscript(_WIN.tx, _DOC);