国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

286 lines
7.8KB

  1. /**
  2. *
  3. * @version $Id: ieemu.js 1 22:28 2010年7月20日Z tianya $
  4. * @package DedeBIZ.Administrator
  5. * @copyright Copyright (c) 2020, DedeBIZ.COM
  6. * @license https://www.dedebiz.com/license
  7. * @link https://www.dedebiz.com
  8. */
  9. var ie = document.all != null;
  10. var moz = !ie && document.getElementById != null && document.layers == null;
  11. /*
  12. * Extends the event object with srcElement, cancelBubble, returnValue,
  13. * fromElement and toElement
  14. */
  15. function extendEventObject() {
  16. Event.prototype.__defineSetter__("returnValue", function (b) {
  17. if (!b) this.preventDefault();
  18. });
  19. Event.prototype.__defineSetter__("cancelBubble", function (b) {
  20. if (b) this.stopPropagation();
  21. });
  22. Event.prototype.__defineGetter__("srcElement", function () {
  23. var node = this.target;
  24. while (node.nodeType != 1) node = node.parentNode;
  25. return node;
  26. });
  27. Event.prototype.__defineGetter__("fromElement", function () {
  28. var node;
  29. if (this.type == "mouseover")
  30. node = this.relatedTarget;
  31. else if (this.type == "mouseout")
  32. node = this.target;
  33. if (!node) return;
  34. while (node.nodeType != 1) node = node.parentNode;
  35. return node;
  36. });
  37. Event.prototype.__defineGetter__("toElement", function () {
  38. var node;
  39. if (this.type == "mouseout")
  40. node = this.relatedTarget;
  41. else if (this.type == "mouseover")
  42. node = this.target;
  43. if (!node) return;
  44. while (node.nodeType != 1) node = node.parentNode;
  45. return node;
  46. });
  47. Event.prototype.__defineGetter__("offsetX", function () {
  48. return this.layerX;
  49. });
  50. Event.prototype.__defineGetter__("offsetY", function () {
  51. return this.layerY;
  52. });
  53. }
  54. /*
  55. * Emulates element.attachEvent as well as detachEvent
  56. */
  57. function emulateAttachEvent() {
  58. HTMLDocument.prototype.attachEvent =
  59. HTMLElement.prototype.attachEvent = function (sType, fHandler) {
  60. var shortTypeName = sType.replace(/on/, "");
  61. fHandler._ieEmuEventHandler = function (e) {
  62. window.event = e;
  63. return fHandler();
  64. };
  65. this.addEventListener(shortTypeName, fHandler._ieEmuEventHandler, false);
  66. };
  67. HTMLDocument.prototype.detachEvent =
  68. HTMLElement.prototype.detachEvent = function (sType, fHandler) {
  69. var shortTypeName = sType.replace(/on/, "");
  70. if (typeof fHandler._ieEmuEventHandler == "function")
  71. this.removeEventListener(shortTypeName, fHandler._ieEmuEventHandler, false);
  72. else
  73. this.removeEventListener(shortTypeName, fHandler, true);
  74. };
  75. }
  76. /*
  77. * This function binds the event object passed along in an
  78. * event to window.event
  79. */
  80. function emulateEventHandlers(eventNames) {
  81. for (var i = 0; i < eventNames.length; i++) {
  82. document.addEventListener(eventNames[i], function (e) {
  83. window.event = e;
  84. }, true); // using capture
  85. }
  86. }
  87. /*
  88. * Simple emulation of document.all
  89. * this one is far from complete. Be cautious
  90. */
  91. function emulateAllModel() {
  92. var allGetter = function () {
  93. var a = this.getElementsByTagName("*");
  94. var node = this;
  95. a.tags = function (sTagName) {
  96. return node.getElementsByTagName(sTagName);
  97. };
  98. return a;
  99. };
  100. HTMLDocument.prototype.__defineGetter__("all", allGetter);
  101. HTMLElement.prototype.__defineGetter__("all", allGetter);
  102. }
  103. function extendElementModel() {
  104. HTMLElement.prototype.__defineGetter__("parentElement", function () {
  105. if (this.parentNode == this.ownerDocument) return null;
  106. return this.parentNode;
  107. });
  108. HTMLElement.prototype.__defineGetter__("children", function () {
  109. var tmp = [];
  110. var j = 0;
  111. var n;
  112. for (var i = 0; i < this.childNodes.length; i++) {
  113. n = this.childNodes[i];
  114. if (n.nodeType == 1) {
  115. tmp[j++] = n;
  116. if (n.name) { // named children
  117. if (!tmp[n.name])
  118. tmp[n.name] = [];
  119. tmp[n.name][tmp[n.name].length] = n;
  120. }
  121. if (n.id) // child with id
  122. tmp[n.id] = n
  123. }
  124. }
  125. return tmp;
  126. });
  127. HTMLElement.prototype.contains = function (oEl) {
  128. if (oEl == this) return true;
  129. if (oEl == null) return false;
  130. return this.contains(oEl.parentNode);
  131. };
  132. }
  133. /*
  134. document.defaultView.getComputedStyle(el1,<BR>null).getPropertyValue('top');
  135. */
  136. function emulateCurrentStyle(properties) {
  137. HTMLElement.prototype.__defineGetter__("currentStyle", function () {
  138. var cs = {};
  139. var el = this;
  140. for (var i = 0; i < properties.length; i++) {
  141. //cs.__defineGetter__(properties[i], function () {
  142. // window.status = "i: " + i ;
  143. // return document.defaultView.getComputedStyle(el, null).getPropertyValue(properties[i]);
  144. //});
  145. cs.__defineGetter__(properties[i], encapsulateObjects(el, properties[i]));
  146. }
  147. return cs;
  148. });
  149. }
  150. // used internally for emualteCurrentStyle
  151. function encapsulateObjects(el, sProperty) {
  152. return function () {
  153. return document.defaultView.getComputedStyle(el, null).getPropertyValue(sProperty);
  154. };
  155. }
  156. function emulateHTMLModel() {
  157. // This function is used to generate a html string for the text properties/methods
  158. // It replaces '\n' with "<BR"> as well as fixes consecutive white spaces
  159. // It also repalaces some special characters
  160. function convertTextToHTML(s) {
  161. s = s.replace(/\&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\n/g, "<BR>");
  162. while (/\s\s/.test(s))
  163. s = s.replace(/\s\s/, "&nbsp; ");
  164. return s.replace(/\s/g, " ");
  165. }
  166. HTMLElement.prototype.insertAdjacentHTML = function (sWhere, sHTML) {
  167. var df; // : DocumentFragment
  168. var r = this.ownerDocument.createRange();
  169. switch (String(sWhere).toLowerCase()) {
  170. case "beforebegin":
  171. r.setStartBefore(this);
  172. df = r.createContextualFragment(sHTML);
  173. this.parentNode.insertBefore(df, this);
  174. break;
  175. case "afterbegin":
  176. r.selectNodeContents(this);
  177. r.collapse(true);
  178. df = r.createContextualFragment(sHTML);
  179. this.insertBefore(df, this.firstChild);
  180. break;
  181. case "beforeend":
  182. r.selectNodeContents(this);
  183. r.collapse(false);
  184. df = r.createContextualFragment(sHTML);
  185. this.appendChild(df);
  186. break;
  187. case "afterend":
  188. r.setStartAfter(this);
  189. df = r.createContextualFragment(sHTML);
  190. this.parentNode.insertBefore(df, this.nextSibling);
  191. break;
  192. }
  193. };
  194. HTMLElement.prototype.__defineSetter__("outerHTML", function (sHTML) {
  195. var r = this.ownerDocument.createRange();
  196. r.setStartBefore(this);
  197. var df = r.createContextualFragment(sHTML);
  198. this.parentNode.replaceChild(df, this);
  199. return sHTML;
  200. });
  201. HTMLElement.prototype.__defineGetter__("canHaveChildren", function () {
  202. switch (this.tagName) {
  203. case "AREA":
  204. case "BASE":
  205. case "BASEFONT":
  206. case "COL":
  207. case "FRAME":
  208. case "HR":
  209. case "IMG":
  210. case "BR":
  211. case "INPUT":
  212. case "ISINDEX":
  213. case "LINK":
  214. case "META":
  215. case "PARAM":
  216. return false;
  217. }
  218. return true;
  219. });
  220. HTMLElement.prototype.__defineGetter__("outerHTML", function () {
  221. var attr, attrs = this.attributes;
  222. var str = "<" + this.tagName;
  223. for (var i = 0; i < attrs.length; i++) {
  224. attr = attrs[i];
  225. if (attr.specified)
  226. str += " " + attr.name + '="' + attr.value + '"';
  227. }
  228. if (!this.canHaveChildren)
  229. return str + ">";
  230. return str + ">" + this.innerHTML + "</" + this.tagName + ">";
  231. });
  232. HTMLElement.prototype.__defineSetter__("innerText", function (sText) {
  233. this.innerHTML = convertTextToHTML(sText);
  234. return sText;
  235. });
  236. var tmpGet;
  237. HTMLElement.prototype.__defineGetter__("innerText", tmpGet = function () {
  238. var r = this.ownerDocument.createRange();
  239. r.selectNodeContents(this);
  240. return r.toString();
  241. });
  242. HTMLElement.prototype.__defineSetter__("outerText", function (sText) {
  243. this.outerHTML = convertTextToHTML(sText);
  244. return sText;
  245. });
  246. HTMLElement.prototype.__defineGetter__("outerText", tmpGet);
  247. HTMLElement.prototype.insertAdjacentText = function (sWhere, sText) {
  248. this.insertAdjacentHTML(sWhere, convertTextToHTML(sText));
  249. };
  250. }