User:Martijn Hoekstra/parsoidclean.js

(function($, mw){	var savebuttonselector = "#wpSave";	var previewbuttonselector = "#wpPreview";	var thisrevision = mw.config.values.wgCurRevisionId;	var prefix = mw.config.values.wgDBname; //this is identical to the canonical `wikiid` from meta info (is it?)	var parsoidserver = "//parsoid-prod.wmflabs.org/";	var thispage = mw.config.values.wgRelevantPageName;	var previewselector = "#wikiPreview";	var contentmodel = mw.config.values.wgPageContentModel;	var api = new mw.Api;	var editorselector = "#wpTextbox1";	var action = mw.config.values.wgAction;	function roundtrip(newcontent){		var html_p = parse(newcontent);		var newwikitext_p = html_p.then(function(html){ console.debug("parsoid returned html", html); return serialize(html, thisrevision).then(function(newwikitext){ 		 	console.debug("parsoid returned wikitext", newwikitext);		  	return [newwikitext, html];		  }); });		var diff_p = newwikitext_p.then(function(parseresult) { var newwikitext = parseresult[0]; var html = parseresult[1]; if (newwikitext === newcontent){ console.debug("we're parsoid clean"); return { diff: null, preview: html };			} else { console.debug("we're parsoid unclean"); return { diff: "Unclean parse, but I'm not telling you what", preview: html };			}		});		return diff_p;	}	function point_promise(value){		var d = $.Deferred;		d.resolve(value);		return d.promise;	}	function diff(left, right){		if (left === right) {			return point_promise(null);		} else {		}	}	function parse(wikitext){		return $.ajax(parsoidserver + prefix + "/" + thispage, { type: "POST", data: { wt: wikitext, body: 1 }		});	}	function serialize(html, oldid){		return $.ajax(parsoidserver + prefix + "/" + thispage, { type: "POST", data: { oldid: oldid, html: html }		});	}	function previewaction(clickevent){		console.debug("this", this);		clickevent.preventDefault;		var newcontent = $(editorselector).val;		roundtrip(newcontent).then(function(diff){ if (diff.diff === null){ $(previewselector + ":last").html(diff.preview); } else { $(previewselector + ":last").text(newcontent); }		});	}	$(function{ console.debug("document ready"); if(contentmodel === "wikitext" && action === "submit"){ console.debug("attach action"); $(previewbuttonselector).text("Show preview (paroid clean)"); $(previewbuttonselector).click(previewaction); } else { console.debug(contentmodel, action); }	}); })(window.$, window.mw);