国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

240 satır
7.1KB

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