User:Gerbrant/gui/htmlEdit.js

From Wikipedia, the free encyclopedia

Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.

module("Gerbrant.gui.htmlEdit", function(html, width, height, onload)
{
	function handleEvent(element, event, handler)
	{
		if(element.addEventListener)
			element.addEventListener(event, handler, false);
		else if(element.attachEvent)
			element.attachEvent("on" + event, handler);
	}
 
	var self = this, iframe = document.createElement("IFRAME"), doc;
 
	iframe.src = "http://en.wikipedia.org/w/index.php?title=User:Gerbrant/EmptyPage&dontcountme=s";
 
	if(width) iframe.style.width = width;
	else iframe.style.width = "100%";
 
	if(height) iframe.style.height = height;
	else iframe.style.height = "10em";
 
	handleEvent(iframe, "load", function()
	{
		if(doc && doc.designMode);
		else
		{
			doc = iframe.contentWindow.document;
			doc.designMode = "on";
		}
 
		if(self.setHTML) return;
 
		/*
			In IE, we have to wait for the second load event.
			* The first load event will be for the uneditable document, you see.
			* When we turn editable on, a second load event is fired.
		*/
		try
		{
			doc.body.innerHTML = html;
			doc.body.style.cssText = "background:white;font-size:100%";
		}catch(e){return;}
 
		self.setHTML = function(newhtml)
		{
			doc.body.innerHTML = newhtml;
		}
		self.getHTML = function()
		{
			return doc.body.innerHTML;
		}
		if(onload) onload();
	});
 
	this.getElement = function()
	{
		return iframe;
	}
});