Closed Bug 618379 Opened 14 years ago Closed 10 years ago

can't extend appendChild DOM function with Element.prototype

Categories

(Core :: DOM: Core & HTML, defect)

x86
macOS
defect
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla24

People

(Reporter: amwebdk, Unassigned)

References

(Depends on 1 open bug)

Details

(Whiteboard: DUPEME)

Attachments

(1 file)

User-Agent:       Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; da; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13
Build Identifier: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; da; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13

In Firefox you can't replace the appendChild function. I any other browsers this is possible just by typing: Element.prototype.appendChild = function () {};

There is no error, when you run the appendChild function, it is just the default function there run and not the user defined function.

TESTCASE:
HTMLDocument.prototype._appendChild = HTMLDocument.prototype.appendChild;
Element.prototype._appendChild = Element.prototype.appendChild;


Element.prototype.appendChild = HTMLDocument.prototype.appendChild = function () {
    document.body.style.background = "green";
    return this._appendChild.apply(this,arguments);
};

document.body.appendChild(document.createTextNode('Default AppendChild function did run'));

Reproducible: Always

Steps to Reproduce:
1. Open the testcase
Actual Results:  
The default appendChild function runs.

Expected Results:  
The user defined function runs.
Attached file testcase (deleted) —
red = bad
green = good
Known issue.... you can sort of work around for your testcase by setting it on HTMLBodyElement.prototype, but if you need to append to lots of different elements it's definitely a pain.
Whiteboard: DUPEME
Well I just need to know the size of a element, with only is possible to detect when the element is assigned to the document. And since there don't exist a "addedToDocument" event an extinction of apppendChild and insertBefore seams to be the only solution.

I would be very happy if you know a way to do it.

And could I see the original bug report.
I have created this function there do the job, and it was a pain:

function extendElementPrototypeFunction(name, func) {
    
    //Check if it is possible to extend the Element prototype
    Element.prototype['_'+name] = Element.prototype[name];
    Element.prototype[name] = function () {return 'unique'};
    
    var simpelExtend = false;
    try {
    	if (document.createElement('span')[name]() == 'unique') {
    		simpelExtend = true;
    	}
    } catch(err) {}
    
    //Roll back
    Element.prototype[name] = Element.prototype['_'+name];
    delete Element.prototype['_'+name];
    
    //Extend the DocumentFragment prototype, with is need anyway
    DocumentFragment.prototype['_'+name] = DocumentFragment.prototype[name];
    DocumentFragment.prototype[name] = func;
    
    //If it is possible to extend the Element prototype
    if (simpelExtend) {
    	Element.prototype['_'+name] = Element.prototype[name];
    	Element.prototype[name] = func;
    }
    
    //Else extend every singel Element prototype one by one
    else {
    
    	//All elements except object and embed
    	var elements = "unkownElement a abbr address area article aside audio b base bdi bdo blockquote body br button caption cite code col colgroup command datalist dd del details device dfn div dl dt em fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 head header hgroup hr html i iframe img input ins kbd keygen label legend li link map mark menu meta meter nav noscript ol optgroup option output p param pre progress q rp rt ruby s samp script section select small source span strong style sub summary sup table tbody td textarea tfoot th thead time title tr track ul var video wbr".split(" ");
    	
    	//Variabel to make sure the same Element prototype won't be extended twice
    	var dubbelControl = [];
    	
    	//Go through all elements
    	for (var i=0;i<elements.length;i++) {
    		
    		//Find the constructor object
    		var constructorObject = document.createElement(elements[i]).constructor;
    		
    		//Make sure the constructor pototype hasn't been extended before
    		var repeat = false;
    		for (var c=0;c<dubbelControl.length;c++) {
    			
    			if (constructorObject == dubbelControl[c]) {
    				repeat = true;
    				break;
    			}
    			
    		}
    		
    		//If the constructor pototype has been extended before, go to next element
    		if (repeat) {
    			continue;
    		}
    		//If not when add the constructor to the dubbelControl array
    		dubbelControl.push(constructorObject);
    		
    		//And extend the prototype
    		constructorObject.prototype['_'+name] = constructorObject.prototype[name];
    		constructorObject.prototype[name] = func;
    	}
    }
}

extendElementPrototypeFunction('appendChild',function () {
    if (arguments[0].nodeType == 1) {
        arguments[0].setAttribute('test','true');
    }
    
    return this._appendChild.apply(this,arguments);
});
This was fixed by bug
Status: UNCONFIRMED → RESOLVED
Closed: 10 years ago
Depends on: 826737
Resolution: --- → FIXED
Target Milestone: --- → mozilla24
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: