阅读:5944回复:2
Madfox 补丁解析--IE DOM部分函数的实现
这一部分是通过xbl来实现了一些IE DOM的函数。会在每个网页载入的时候加入。函数并不多,实现的方式也多是通过调用已有的标准方法。
Index: layout/html/document/src/html.css =================================================================== --- layout/html/document/src/html.css (revision 11) +++ layout/html/document/src/html.css (revision 12) @@ -438,6 +438,11 @@ -moz-binding: url('chrome://xbl-marquee/content/xbl-marquee.xml#marquee-vertical'); } +/* IE compatibility */ +head { + -moz-binding: url('chrome://iecompat/content/iecompat.xml#iecompat-head'); +} + /* PRINT ONLY rules follow */ @media print { Index: layout/html/document/src/iecompat/resources/content/iecompat.xml =================================================================== --- layout/html/document/src/iecompat/resources/content/iecompat.xml (revision 0) +++ layout/html/document/src/iecompat/resources/content/iecompat.xml (revision 12) @@ -0,0 +1,320 @@ +<?xml version="1.0"?> +<!-- ***** BEGIN LICENSE BLOCK ***** + - Version: MPL 1.1/GPL 2.0/LGPL 2.1 + - + - The contents of this file are subject to the Mozilla Public License Version + - 1.1 (the "License"); you may not use this file except in compliance with + - the License. You may obtain a copy of the License at + - http://www.mozilla.org/MPL/ + - + - Software distributed under the License is distributed on an "AS IS" basis, + - WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + - for the specific language governing rights and limitations under the + - License. + - + - The Initial Developer of the Original Code is + - Sun Microsystems Inc. + - Erick Arvidsson + - + - Contributor(s): + - Sun Microsystems Inc. + - Erik Arvidsson <erik@eae.net> http://webfx.eae.net/dhtml/ieemu/ + - + - Alternatively, the contents of this file may be used under the terms of + - either the GNU General Public License Version 2 or later (the "GPL"), or + - the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + - in which case the provisions of the GPL or the LGPL are applicable instead + - of those above. If you wish to allow use of your version of this file only + - under the terms of either the GPL or the LGPL, and not to allow others to + - use your version of this file under the terms of the MPL, indicate your + - decision by deleting the provisions above and replace them with the notice + - and other provisions required by the LGPL or the GPL. If you do not delete + - the provisions above, a recipient may use your version of this file under + - the terms of any one of the MPL, the GPL or the LGPL. + - + - ***** END LICENSE BLOCK ***** --> + +<bindings id="iecompats" + xmlns="http://www.mozilla.org/xbl" + xmlns:html="http://www.w3.org/1999/xhtml" + xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" + xmlns:xbl="http://www.mozilla.org/xbl"> + + <binding id="iecompat-head"> + <implementation> + <constructor> + <![CDATA[ + // set up ie environment for Moz + function init() { + if(document.all) { + extendEventObject(); + emulateAttachEvent(); + emulateEventHandlers(["click", "dblclick", "mouseover", "mouseout", "mousedown", "mouseup", "mousemove", "keydown", "keypress", "keyup"]); + } + emulateHTMLModel(); + extendElementModel(); + emulateCurrentStyle(["left", "right", "top", "bottom", "width", "height"]); + emulateWindowNavigate(); + } + + /* + * Extends the event object with srcElement, cancelBubble, returnValue, + * fromElement and toElement + */ + function extendEventObject() { + Event.prototype.__defineSetter__("returnValue", function (b) { + if (!b) this.preventDefault(); + return b; + }); + + Event.prototype.__defineSetter__("cancelBubble", function (b) { + if (b) this.stopPropagation(); + return b; + }); + + Event.prototype.__defineGetter__("srcElement", function () { + var node = this.target; + while (node.nodeType != 1) node = node.parentNode; + return node; + }); + + Event.prototype.__defineGetter__("fromElement", function () { + var node; + if (this.type == "mouseover") + node = this.relatedTarget; + else if (this.type == "mouseout") + node = this.target; + if (!node) return; + while (node.nodeType != 1) node = node.parentNode; + return node; + }); + + Event.prototype.__defineGetter__("toElement", function () { + var node; + if (this.type == "mouseout") + node = this.relatedTarget; + else if (this.type == "mouseover") + node = this.target; + if (!node) return; + while (node.nodeType != 1) node = node.parentNode; + return node; + }); + + Event.prototype.__defineGetter__("offsetX", function () { + return this.layerX; + }); + Event.prototype.__defineGetter__("offsetY", function () { + return this.layerY; + }); + } + + /* + * Emulates element.attachEvent as well as detachEvent + */ + function emulateAttachEvent() { + HTMLDocument.prototype.attachEvent = + HTMLElement.prototype.attachEvent = function (sType, fHandler) { + var shortTypeName = sType.replace(/on/, ""); + fHandler._ieEmuEventHandler = function (e) { + window.event = e; + return fHandler(); + }; + this.addEventListener(shortTypeName, fHandler._ieEmuEventHandler, false); + }; + + HTMLDocument.prototype.detachEvent = + HTMLElement.prototype.detachEvent = function (sType, fHandler) { + var shortTypeName = sType.replace(/on/, ""); + if (typeof fHandler._ieEmuEventHandler == "function") + this.removeEventListener(shortTypeName, fHandler._ieEmuEventHandler, false); + else + this.removeEventListener(shortTypeName, fHandler, true); + }; + } + + /* + * This function binds the event object passed along in an + * event to window.event + */ + function emulateEventHandlers(eventNames) { + for (var i = 0; i < eventNames.length; i++) { + document.addEventListener(eventNames[i], function (e) { + window.event = e; + }, true); // using capture + } + } + + function extendElementModel() { + HTMLElement.prototype.__defineGetter__("parentElement", function () { + if (this.parentNode == this.ownerDocument) return null; + return this.parentNode; + }); + + HTMLElement.prototype.__defineGetter__("children", function () { + var tmp = []; + var j = 0; + var n; + for (var i = 0; i < this.childNodes.length; i++) { + n = this.childNodes[i]; + if (n.nodeType == 1) { + tmp[j++] = n; + if (n.name) { // named children + if (!tmp[n.name]) + tmp[n.name] = []; + tmp[n.name][tmp[n.name].length] = n; + } + if (n.id) // child with id + tmp[n.id] = n + } + } + return tmp; + }); + + HTMLElement.prototype.contains = function (oEl) { + if (oEl == this) return true; + if (oEl == null) return false; + return this.contains(oEl.parentNode); + }; + } + + function emulateCurrentStyle(properties) { + HTMLElement.prototype.__defineGetter__("currentStyle", function () { + var cs = {}; + var el = this; + for (var i = 0; i < properties.length; i++) { + cs.__defineGetter__(properties[i], encapsulateObjects(el, properties[i])); + } + return cs; + }); + } + // used internally for emualteCurrentStyle + function encapsulateObjects(el, sProperty) { + return function () { + return document.defaultView.getComputedStyle(el, null).getPropertyValue(sProperty); + }; + } + + function emulateHTMLModel() { + + // This function is used to generate a html string for the text properties/methods + // It replaces '\n' with "<BR"> as well as fixes consecutive white spaces + // It also repalaces some special characters + function convertTextToHTML(s) { + s = s.replace(/\&/g, "&").replace(/</g, "&").replace(/>/g, "&").replace(/\n/g, "<BR>"); + while (/\s\s/.test(s)) + s = s.replace(/\s\s/, "& "); + return s.replace(/\s/g, " "); + } + + HTMLElement.prototype.insertAdjacentHTML = function (sWhere, sHTML) { + var df; // : DocumentFragment + var r = this.ownerDocument.createRange(); + + switch (String(sWhere).toLowerCase()) { + case "beforebegin": + r.setStartBefore(this); + df = r.createContextualFragment(sHTML); + this.parentNode.insertBefore(df, this); + break; + + case "afterbegin": + r.selectNodeContents(this); + r.collapse(true); + df = r.createContextualFragment(sHTML); + this.insertBefore(df, this.firstChild); + break; + + case "beforeend": + r.selectNodeContents(this); + r.collapse(false); + df = r.createContextualFragment(sHTML); + this.appendChild(df); + break; + + case "afterend": + r.setStartAfter(this); + df = r.createContextualFragment(sHTML); + this.parentNode.insertBefore(df, this.nextSibling); + break; + } + }; + + HTMLElement.prototype.__defineSetter__("outerHTML", function (sHTML) { + var r = this.ownerDocument.createRange(); + r.setStartBefore(this); + var df = r.createContextualFragment(sHTML); + this.parentNode.replaceChild(df, this); + + return sHTML; + }); + + HTMLElement.prototype.__defineGetter__("canHaveChildren", function () { + switch (this.tagName) { + case "AREA": + case "BASE": + case "BASEFONT": + case "COL": + case "FRAME": + case "HR": + case "IMG": + case "BR": + case "INPUT": + case "ISINDEX": + case "LINK": + case "META": + case "PARAM": + return false; + } + return true; + }); + + HTMLElement.prototype.__defineGetter__("outerHTML", function () { + var attr, attrs = this.attributes; + var str = "<" + this.tagName; + for (var i = 0; i < attrs.length; i++) { + attr = attrs[i]; + if (attr.specified) + str += " " + attr.name + '="' + attr.value + '"'; + } + if (!this.canHaveChildren) + return str + ">"; + + return str + ">" + this.innerHTML + "</" + this.tagName + ">"; + }); + + + HTMLElement.prototype.__defineSetter__("innerText", function (sText) { + this.innerHTML = convertTextToHTML(sText); + return sText; + }); + + var tmpGet; + HTMLElement.prototype.__defineGetter__("innerText", tmpGet = function () { + var r = this.ownerDocument.createRange(); + r.selectNodeContents(this); + return r.toString(); + }); + + HTMLElement.prototype.__defineSetter__("outerText", function (sText) { + this.outerHTML = convertTextToHTML(sText); + return sText; + }); + HTMLElement.prototype.__defineGetter__("outerText", tmpGet); + + HTMLElement.prototype.insertAdjacentText = function (sWhere, sText) { + this.insertAdjacentHTML(sWhere, convertTextToHTML(sText)); + }; + + } + + function emulateWindowNavigate() { + window.__proto__.navigate = + function(sURL){ this.location.href = sURL; }; + } + + init(); + ]]> + </constructor> + </implementation> + </binding> +</bindings> |
|
1楼#
发布于:2005-04-01 11:25
版主您好,firefox的JS控制显示
错误: unterminated character class A 源文件:file:///E:/openhost/alai_js/menu_xp.htm 行:119,列:21 源代码: sCode=sCode.replace(/([A-Za-z0-9><_\)\(\]\[\}\{\. \$/;=\+\?\"&:-]{2,})/g,"<font color='green'>$1</font>"); 这行代码在IE下是正常的,应该怎么改才能在firefox运行呢? sCode=sCode.replace(/([A-Za-z0-9><_\)\(\]\[\}\{\. \$/;=\+\?\"&:-]{2,})/g,"<font color='green'>$1</font>"); |
|
2楼#
发布于:2005-04-01 11:25
实在是太精彩了~
难道没有人发现它的 价值吗?
虽然有些部分不是完全实现 但是已经和 IE 很接近了~ 只是 调用 XBL 调用有些麻烦~ |
|