Jump to content

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. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
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;
	}
});