tools.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * <ul>
  3. * 不存在的标准
  4. * <li>元素不存在,元素无父节点
  5. * <li>元素被隐藏了
  6. * <li>元素的父节点被隐藏
  7. * <li>存在的标准 - display = 'block' - display = ''
  8. *
  9. * 参数: jquery object
  10. */
  11. function isShown(o) {
  12. if (!o || (!o.parentNode && o != document))
  13. return false;
  14. if (o instanceof String || 'string' == typeof o)
  15. o = document.getElementById(o);
  16. if (o == document.body)
  17. return true;
  18. if (o.style && "none" == o.style.display)
  19. return false;
  20. if (parseInt(o.style.marginLeft) < -2000 || parseInt(o.style.left) < -2000)
  21. return false;
  22. if (parseInt(o.offsetHeight) == 0 || parseInt(o.offsetWidth) == 0)
  23. return false;
  24. if (o.parentNode && !isShown(o.parentNode))
  25. return false;
  26. return true;
  27. }
  28. var testingElement = {}, te = testingElement;
  29. (function() {
  30. function mySetup() {
  31. te.dom = [];
  32. te.obj = [];
  33. }
  34. /**
  35. * 添加一个通用接口,用于支持类似dispose等通用方法
  36. */
  37. te.checkUI = {
  38. eventLength : 0,
  39. /**
  40. * 校验对象的dispose方法,主要是dom和event支持
  41. *
  42. * @param ui
  43. * 需要调用dispose的ui对象
  44. * @param eventLength
  45. * ui启动前在baidu.event._listeners中的事件总数
  46. * @param dom 需要确认dom被干掉了
  47. * @param callback 回调函数,在dispose之后
  48. */
  49. dispose : function(ui, eventLength, dom, callback) {
  50. if (!ui.dispose || 'function' != typeof ui.dispose) {
  51. ok(false, 'ui do not have a dispose function');
  52. return false;
  53. }
  54. var m = ui.getMain();
  55. ui.dispose();
  56. ok(ui.disposed === true, 'ui.disposed is true');
  57. ok(!isShown(m), 'main is not shown');
  58. if (baidu.event && baidu.event._listeners)
  59. equals(baidu.event._listeners.length, eventLength
  60. || this.eventLength,
  61. 'event all should be un after dispose');
  62. if (dom)
  63. ok(!isShown(dom), 'dom not shown');
  64. callback && callback(ui);
  65. }
  66. };
  67. function myTeardown() {
  68. if (te) {
  69. if (te.dom && te.dom.length) {
  70. for ( var i = 0; i < te.dom.length; i++)
  71. if (te.dom[i] && te.dom[i].parentNode)
  72. te.dom[i].parentNode.removeChild(te.dom[i]);
  73. }
  74. if (te.obj && te.obj.length) {
  75. while (te.obj.length > 0)
  76. try {
  77. te.obj.shift().dispose();
  78. } catch (e) {
  79. }
  80. }
  81. }
  82. }
  83. var s = QUnit.testStart, e = QUnit.testDone, ms = QUnit.moduleStart, me = QUnit.moduleEnd, d = QUnit.done;
  84. QUnit.testStart = function() {
  85. mySetup();
  86. s.apply(this, arguments);
  87. ;
  88. };
  89. QUnit.testDone = function() {
  90. e.call(this, arguments);
  91. myTeardown();
  92. };
  93. // QUnit.moduleStart = function() {
  94. // var h = setInterval(function() {
  95. // if (window && window['baidu'] && window.document && window.document.body)
  96. // {
  97. // clearInterval(h);
  98. // start();
  99. // }
  100. // }, 20);
  101. // stop();
  102. // ms.apply(this, arguments);;
  103. // };
  104. // QUnit.moduleEnd = function() {
  105. // me.call(this, arguments);
  106. // };
  107. // QUnit.done = function(fail,total) {
  108. // // d.call(this, arguments);
  109. // d(fail,total);
  110. // };
  111. })();
  112. // function Include(src) {
  113. // var url = "http://"
  114. // + location.host
  115. // + location.pathname.substring(0, location.pathname.substring(1)
  116. // .indexOf('/') + 1);
  117. // document.write("<script type='text/javascript' src='" + url
  118. // + "/src/Import.php?f=" + src + "'></script>");
  119. // }