03. juni 2006 - 02:11
#25
Første beta ser sådan ud:
<script type="text/JavaScript">
var OC_MarkUpLaundry = { // Version 1.0 Beta
/*****************************************/
/***** OC_MarkUpLaundry 1.0 Beta *****/
/*****************************************/
/* (C) Copyright - Ole Clausen - 2006 */
/* Free to use if you leave this notice */
/*****************************************/
/*****************************************/
/* The object cleans up markup from an */
/* editor-app in Internet Explorer. */
/* Call getCleanMarkUp and supply a ref */
/* to the editor's content element. */
/* A clean markup-string is returned. */
/*****************************************/
/*****************************************/
/***** PUBLIC PROPERTIES *****/
/***** May be edited here *****/
/***** - or set in code *****/
/*****************************************/
aAttributes: ["id", "name", "class", "style", "href", "type", "value", "src", "target", "action", "method", "enctype",
"rowspan", "colspan", "for", "usemap", "area", "coords", "onmouseover", "onmouseout", "onmousemove",
"onmousedown", "onmouseup", "onclick", "onkeydown", "onkeypress", "onkeyup", "onchange", "onfocus",
"onblur", "onsubmit", "onload", "onerror"
],
bTagsAsXHTML: false,
bFormatMarkUp: true,
aNoBreakElms: ["b", "strong", "i", "em", "u", "a", "img"],
aBreakBeforeEndElms: ["table", "thead", "tbody", "tfoot", "tr", "ul", "ol", "pre", "form", "fieldset", "select"],
aHalfTabs: ["thead", "tbody", "tfoot", "tr"],
oTabChars: {half:" ", full:" "},
/*****************************************/
/***** DON'T EDIT BELOW THIS *****/
/*****************************************/
_inited: false,
_aNoEndTagElms: ["area", "base", "br", "col", "hr", "img", "input", "isindex", "param"],
_oDoc: null, _oDocElm: null,
_layout: {nb:{}, bbe:{}, fT:{}, hT:{}, ne:{}},
_hshAttr: {},
_init: function() {
for (var i=0,j=this.aAttributes.length; i<j; i++) this._hshAttr[this.aAttributes[i]] = 1;
for (var i=0,j=this.aNoBreakElms.length; i<j; i++) this._layout.nb[this.aNoBreakElms[i]] = 1;
for (var i=0,j=this.aBreakBeforeEndElms.length; i<j; i++) this._layout.bbe[this.aBreakBeforeEndElms[i]] = 1;
for (var i=0,j=this.aHalfTabs.length; i<j; i++) this._layout.hT[this.aHalfTabs[i]] = 1;
for (var i=0,j=this._aNoEndTagElms.length; i<j; i++) this._layout.ne[this._aNoEndTagElms[i]] = 1;
this._inited = true;
},
_getLineBreak: function() {
return this._oDoc.createElement("lineBreak");
},
_getTab: function(str) {
var o = this._oDoc.createElement("tab");
o.appendChild( this._oDoc.createTextNode(str!=""?str:"-") );
return o;
},
_cloneElm: function(contHtml, contXml) {
var aN, n, nN, o, oB, cD, tx, sParTab, sTab, lnBr;
aN = contHtml.childNodes;
for (var i=0,j=aN.length; i<j; i++) {
n = aN[i];
nN = n.nodeName.toLowerCase();
sParTab = sTab = "";
switch (n.nodeType) {
case 1: // Element
o = this._oDoc.createElement(nN);
contXml.appendChild(o);
if (!this._layout.nb[nN] && this.bFormatMarkUp) {
contXml.insertBefore(this._getLineBreak(), o);
if (o.parentNode.previousSibling && o.parentNode.previousSibling.nodeName=="tab") {
sParTab = o.parentNode.previousSibling.firstChild.nodeValue;
}
sParTab = sParTab=="-" ? "" : sParTab;
if (!this._layout.nb[nN] && !this._layout.hT[nN]) sTab = sParTab + this.oTabChars.full;
else if (this._layout.hT[nN]) sTab = sParTab + this.oTabChars.half;
else sTab = sParTab;
oTab = this._getTab(sTab);
contXml.insertBefore(oTab, o);
}
if (nN=="script") {
cD = this._oDoc.createCDATASection(n.text);
o.appendChild(cD);
}
else if (n.childNodes && n.childNodes.length>0) this._cloneElm(n, o);
else if ( (!n.childNodes || n.childNodes.length<1) && !this._layout.ne[nN]) {
tx = this._oDoc.createEntityReference("nbsp");
try { o.appendChild(tx) };
catch(e) { tx = null };
}
if (this._layout.bbe[nN] && this.bFormatMarkUp) {
o.appendChild(this._getLineBreak());
o.appendChild(this._getTab(sTab));
}
if (n.style&&n.style.cssText!="") o.setAttribute("style", n.style.cssText.toLowerCase());
var att = n.attributes;
for (x=0,y=this.aAttributes.length; x<y; x++) {
attTmp = att.getNamedItem(this.aAttributes[x]);
if ( attTmp && typeof attTmp.nodeValue=="string" && attTmp.nodeValue!="" ) {
o.setAttribute(this.aAttributes[x], attTmp.nodeValue);
}
}
break;
case 3: //Text
nV = n.nodeValue;
if (nV.match(/[^\s]+/)) {
tx = this._oDoc.createCDATASection(nV);
}
else tx = this._oDoc.createTextNode(nV);
contXml.appendChild(tx);
break;
case 8: // Comment
nV = n.nodeValue;
o = this._oDoc.createElement("comment");
tx = this._oDoc.createCDATASection(nV);
o.appendChild(tx);
contXml.appendChild(o);
break
}
}
},
_cleanTables: function() {
var aTbls, aTds;
aTbls = this._oDoc.getElementsByTagName("table");
for (var i=0,j=aTbls.length; i<j; i++) {
aTds = aTbls[i].getElementsByTagName("td");
for (var x=0,y=aTds.length; x<y; x++) {
if (aTds[x].getAttribute("colspan")=="1") aTds[x].attributes.removeNamedItem("colspan");
if (aTds[x].getAttribute("rowspan")=="1") aTds[x].attributes.removeNamedItem("rowspan");
}
}
},
_replaceTabs: function() {
var o, aTabs = this._oDocElm.getElementsByTagName("tab");
for (var i=aTabs.length-1; i>=0; i--) {
if (aTabs[i].firstChild.nodeValue=="-") {
aTabs[i].parentNode.removeChild(aTabs[i]);
} else {
o = this._oDoc.createTextNode( aTabs[i].firstChild.nodeValue );
aTabs[i].parentNode.replaceChild(o, aTabs[i]);
}
}
},
getCleanMarkUp: function(contHtml) {
var xml, aTabs;
var startT = new Date().getTime();
if (!this._inited) this._init();
this._oDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
this._oDocElm = this._oDoc.createElement("markupFragment");
this._oDoc.appendChild(this._oDocElm);
this._cloneElm(contHtml, this._oDocElm);
this._cleanTables();
this._replaceTabs();
xml = this._oDocElm.xml;
xml = xml.replace(/<\/?markupFragment>/g, "");
xml = xml.replace(/<lineBreak\s?\/>/g, "\r\n");
xml = xml.replace(/<comment>([^\/]*)<\/comment>/g, "\r\n\r\n<!-- $1 -->\r\n");
xml = xml.replace(/<!\[CDATA\[/g, "");
xml = xml.replace(/\]\]>/g, "");
if (!this.bTagsAsXHTML) xml = xml.replace(/\s?\/>/g, ">");
this._oDoc = null;
return xml + "\r\n";
}
};
</script>
Man kan ændre i de properties, der er anført som 'public', eller sætte dem i kode på formen:
OC_MarkUpLaundry.bTagsAsXHTML = true;
De properties, der står efter 'bFormatMarkUp' er kun aktuelle, hvis denne er sat til true.
'aBreakBeforeEndElms' indeholder tegNames på de elementer, hvor slut-tagget skal stå på en ny linje.
'oTabChars' indeholder to forskellige tabs - full og half. Mange foretrækker at bruge indrykning med f.eks. to mellemrum ved nogle tags - og en 'TAB' ved andre.
Resten skulle være til at regne ud ... kik på navnene ;o)