function putHTMLEnts(s, rev)
{
var i;
var chrs=new Array(' ','¡','¢','£','¤','¥','¦','§','¨','©','ª','«','¬','­','®','¯','°','±','²','³','´','µ','¶','·','¸','¹','º','»','¼','½','¾','¿','À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','×','Ø','Ù','Ú','Û','Ü','Ý','Þ','ß','à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô','õ','ö','÷','ø','ù','ú','û','ü','ý','þ','ÿ','"','<','>','&','€','“','”','‘','’','–','…'), ents=new Array('&nbsp;','&iexcl;','&cent;','&pound;','&curren;','&yen;','&brvbar;','&sect;','&uml;','&copy;','&ordf;','&laquo;','&not;','&shy;','&reg;','&macr;','&deg;','&plusmn;','&sup2;','&sup3;','&acute;','&micro;','&para;','&middot;','&cedil;','&sup1;','&ordm;','&raquo;','&frac14;','&frac12;','&frac34;','&iquest;','&Agrave;','&Aacute;','&Acirc;','&Atilde;','&Auml;','&Aring;','&AElig;','&Ccedil;','&Egrave;','&Eacute;','&Ecirc;','&Euml;','&Igrave;','&Iacute;','&Icirc;','&Iuml;','&ETH;','&Ntilde;','&Ograve;','&Oacute;','&Ocirc;','&Otilde;','&Ouml;','&times;','&Oslash;','&Ugrave;','&Uacute;','&Ucirc;','&Uuml;','&Yacute;','&THORN;','&szlig;','&agrave;','&aacute;','&acirc;','&atilde;','&auml;','&aring;','&aelig;','&ccedil;','&egrave;','&eacute;','&ecirc;','&euml;','&igrave;','&iacute;','&icirc;','&iuml;','&eth;','&ntilde;','&ograve;','&oacute;','&ocirc;','&otilde;','&ouml;','&divide;','&oslash;','&ugrave;','&uacute;','&ucirc;','&uuml;','&yacute;','&thorn;','&yuml;','&quot;','&lt;','&gt;','&amp;','&euro;','&quot;','&quot;','\'','\'','-','...');if (rev)
{
for (i=0; i<ents.length; i++)
{
s=s.replace(eval('/'+ents[i]+'/g'), chrs[i]);
}
}
else
{
i=chrs.length-1;
while (i > 0)
{
if (chrs[i] == '&')
{
break;
}
i--;
}
ents[i]='&';
s=s.replace(/&/g, '&amp;');
for (i=0; i<chrs.length; i++)
{
s=s.replace(eval('/'+chrs[i]+'/g'), ents[i]);
}
}
return s;
}
function parseAttrsHTML(n, rmFrmt)
{
var attrsTxt='', attrName=''; attrValue='';
if (n.nodeType == 1)
{
attrsTxt=(n.style.cssText != '' && ! rmFrmt ? ' style="'+n.style.cssText.toLowerCase()+'"' : '');
for (var i=0; i<n.attributes.length; i++)
{
if (n.attributes.item(i).nodeValue)
{
attrName=n.attributes.item(i).nodeName.toLowerCase();
attrValue=String(n.attributes.item(i).nodeValue);
switch (n.nodeName.toLowerCase())
{
case 'pre':
switch (attrName)
{
case 'xml:space':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue)+'"';
break;
}
case 'p': case 'h1': case 'h2': case 'h3': case 'h4': case 'h5': case 'h6': case 'div':
case 'ul': case 'ol': case 'li': case 'dl': case 'dt': case 'dd':
case 'address': case 'hr': case 'span': case 'bdo': case 'em': case 'strong': case 'dfn':
case 'code': case 'samp': case 'kbd': case 'var': case 'cite': case 'abbr': case 'acronym':
case 'sub': case 'sup': case 'tt': case 'i': case 'b': case 'big': case 'small':
case 'noscript': case 'u': case 'strike':
switch (attrName)
{
case 'id': case 'class': case 'title':
case 'lang': case 'xml:lang': case 'dir':
case 'onclick': case 'ondblclick': case 'onmousedown': case 'onmouseup':
case 'onmouseover': case 'onmousemove': case 'onmouseout':
case 'onkeypress': case 'onkeydown': case 'onkeyup':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue)+'"';
break;
case "align":
if (attrsTxt.search(/\s?style=\"/i) >= 0)
{
attrsTxt=attrsTxt.replace(/(\s?style=\"[^\";]*);?\s*\"/i, '$1; text-align: '+putHTMLEnts(attrValue)+';"');
}
else
{
attrsTxt+=' style="'+putHTMLEnts('text-align: '+attrValue)+';"';
}
break;
}
break;
case 'script':
switch (attrName)
{
case 'src':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue.replace(/^(https?:\/\/www.confindustriaravenna.it)?\/(.*)/i, '../$2'))+'"';
break;
case 'id': case 'charset': case 'type': case 'defer': case 'xml:space':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue)+'"';
break;
}
break;
case 'ins':
case 'del':
switch (attrName)
{
case 'datetime':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue)+'"';
break;
}
case 'blockquote': case 'q':
switch (attrName)
{
case 'id': case 'class': case 'title':
case 'lang': case 'xml:lang': case 'dir':
case 'onclick': case 'ondblclick': case 'onmousedown': case 'onmouseup':
case 'onmouseover': case 'onmousemove': case 'onmouseout':
case 'onkeypress': case 'onkeydown': case 'onkeyup':
case 'cite':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue)+'"';
break;
}
break;
case 'a':
switch (attrName)
{
case 'href':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue.replace(/^(https?:\/\/www.confindustriaravenna.it)?\/(.*)/i, '../$2'))+'"';
break;
case 'id': case 'class': case 'title':
case 'lang': case 'xml:lang': case 'dir':
case 'onclick': case 'ondblclick': case 'onmousedown': case 'onmouseup':
case 'onmouseover': case 'onmousemove': case 'onmouseout':
case 'onkeypress': case 'onkeydown': case 'onkeyup':
case 'accesskey': case 'tabindex': case 'onfocus': case 'onblur':
case 'charset': case 'type': case 'name': case 'hreflang': case 'rel':
case 'rev': case 'shape': case 'coords':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue)+'"';
break;
}
break;
case 'br':
switch (attrName)
{
case 'id': case 'class': case 'title':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue)+'"';
break;
}
break;
case 'object':
switch (attrName)
{
case 'id': case 'class': case 'title':
case 'lang': case 'xml:lang': case 'dir':
case 'onclick': case 'ondblclick': case 'onmousedown': case 'onmouseup':
case 'onmouseover': case 'onmousemove': case 'onmouseout':
case 'onkeypress': case 'onkeydown': case 'onkeyup':
case 'declare': case 'classid': case 'codebase': case 'data': case 'type':
case 'codetype': case 'archive': case 'standby': case 'height': case 'width':
case 'usemap': case 'name': case 'tabindex':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue)+'"';
break;
}
break;
case 'param':
switch (attrName)
{
case 'id': case 'name': case 'value': case 'valuetype': case 'type':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue)+'"';
break;
}
break;
case 'img':
switch (attrName)
{
case 'src':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue.replace(/^(https?:\/\/www.confindustriaravenna.it)?\/(.*)/i, '../$2'))+'"';
break;
case 'id': case 'class': case 'title':
case 'lang': case 'xml:lang': case 'dir':
case 'onclick': case 'ondblclick': case 'onmousedown': case 'onmouseup':
case 'onmouseover': case 'onmousemove': case 'onmouseout':
case 'onkeypress': case 'onkeydown': case 'onkeyup':
case 'alt': case 'longdesc': case 'height': case 'width':
case 'usemap': case 'ismap':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue)+'"';
break;
}
break;
case 'map':
switch (attrName)
{
case 'id': case 'class': case 'title':
case 'lang': case 'xml:lang': case 'dir':
case 'onclick': case 'ondblclick': case 'onmousedown': case 'onmouseup':
case 'onmouseover': case 'onmousemove': case 'onmouseout':
case 'onkeypress': case 'onkeydown': case 'onkeyup':
case 'name':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue)+'"';
break;
}
break;
case 'area':
switch (attrName)
{
case 'href':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue.replace(/^(https?:\/\/www.confindustriaravenna.it)?\/(.*)/i, '../$2'))+'"';
break;
case 'id': case 'class': case 'title':
case 'lang': case 'xml:lang': case 'dir':
case 'onclick': case 'ondblclick': case 'onmousedown': case 'onmouseup':
case 'onmouseover': case 'onmousemove': case 'onmouseout':
case 'onkeypress': case 'onkeydown': case 'onkeyup':
case 'accesskey': case 'tabindex': case 'onfocus': case 'onblur':
case 'shape': case 'coords': case 'nohref': case 'alt':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue)+'"';
break;
}
break;
case 'form':
switch (attrName)
{
case 'id': case 'class': case 'title':
case 'lang': case 'xml:lang': case 'dir':
case 'onclick': case 'ondblclick': case 'onmousedown': case 'onmouseup':
case 'onmouseover': case 'onmousemove': case 'onmouseout':
case 'onkeypress': case 'onkeydown': case 'onkeyup':
case 'action': case 'method': case 'enctype': case 'onsubmit': case 'onreset':
case 'accept': case 'accept-charset':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue)+'"';
break;
}
break;
case 'label':
switch (attrName)
{
case 'id': case 'class': case 'title':
case 'lang': case 'xml:lang': case 'dir':
case 'onclick': case 'ondblclick': case 'onmousedown': case 'onmouseup':
case 'onmouseover': case 'onmousemove': case 'onmouseout':
case 'onkeypress': case 'onkeydown': case 'onkeyup':
case 'for': case 'accesskey': case 'onfocus': case 'onblur':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue)+'"';
break;
}
break;
case 'input':
switch (attrName)
{
case 'src':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue.replace(/^(https?:\/\/www.confindustriaravenna.it)?\/(.*)/i, '../$2'))+'"';
break;
case 'id': case 'class': case 'title':
case 'lang': case 'xml:lang': case 'dir':
case 'onclick': case 'ondblclick': case 'onmousedown': case 'onmouseup':
case 'onmouseover': case 'onmousemove': case 'onmouseout':
case 'onkeypress': case 'onkeydown': case 'onkeyup':
case 'accesskey': case 'tabindex': case 'onfocus': case 'onblur':
case 'type': case 'name': case 'value': case 'checked': case 'disabled':
case 'readonly': case 'size': case 'maxlength': case 'alt':
case 'usemap': case 'onselect': case 'onchange': case 'accept':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue)+'"';
break;
}
break;
case 'select':
switch (attrName)
{
case 'id': case 'class': case 'title':
case 'lang': case 'xml:lang': case 'dir':
case 'onclick': case 'ondblclick': case 'onmousedown': case 'onmouseup':
case 'onmouseover': case 'onmousemove': case 'onmouseout':
case 'onkeypress': case 'onkeydown': case 'onkeyup':
case 'name': case 'size': case 'multiple': case 'disabled': case 'tabindex':
case 'onfocus': case 'onblur': case 'onchange':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue)+'"';
break;
}
break;
case 'option':
switch (attrName)
{
case 'selected': case 'value':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue)+'"';
break;
}
case 'optgroup':
switch (attrName)
{
case 'id': case 'class': case 'title':
case 'lang': case 'xml:lang': case 'dir':
case 'onclick': case 'ondblclick': case 'onmousedown': case 'onmouseup':
case 'onmouseover': case 'onmousemove': case 'onmouseout':
case 'onkeypress': case 'onkeydown': case 'onkeyup':
case 'disabled': case 'label':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue)+'"';
break;
}
break;
case 'textarea':
switch (attrName)
{
case 'id': case 'class': case 'title':
case 'lang': case 'xml:lang': case 'dir':
case 'onclick': case 'ondblclick': case 'onmousedown': case 'onmouseup':
case 'onmouseover': case 'onmousemove': case 'onmouseout':
case 'onkeypress': case 'onkeydown': case 'onkeyup':
case 'accesskey': case 'tabindex': case 'onfocus': case 'onblur':
case 'name': case 'rows': case 'cols': case 'disabled': case 'readonly':
case 'onselect': case 'onchange':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue)+'"';
break;
}
break;
case 'legend':
switch (attrName)
{
case 'accesskey':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue)+'"';
break;
}
case 'fieldset':
switch (attrName)
{
case 'id': case 'class': case 'title':
case 'lang': case 'xml:lang': case 'dir':
case 'onclick': case 'ondblclick': case 'onmousedown': case 'onmouseup':
case 'onmouseover': case 'onmousemove': case 'onmouseout':
case 'onkeypress': case 'onkeydown': case 'onkeyup':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue)+'"';
break;
}
break;
case 'table':
switch (attrName)
{
case 'summary': case 'width': case 'border': case 'frame':
case 'rules': case 'cellspacing': case 'cellpadding':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue)+'"';
break;
}
case 'caption':
switch (attrName)
{
case 'id': case 'class': case 'title':
case 'lang': case 'xml:lang': case 'dir':
case 'onclick': case 'ondblclick': case 'onmousedown': case 'onmouseup':
case 'onmouseover': case 'onmousemove': case 'onmouseout':
case 'onkeypress': case 'onkeydown': case 'onkeyup':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue)+'"';
break;
}
break;
case 'th': case 'td':
switch (attrName)
{
case 'abbr': case 'axis': case 'headers': case 'scope':
case 'rowspan': case 'colspan':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue)+'"';
break;
}
case 'colgroup': case 'col': case 'thead': case 'tfoot': case 'tbody': case 'tr':
switch (attrName)
{
case 'id': case 'class': case 'title':
case 'lang': case 'xml:lang': case 'dir':
case 'onclick': case 'ondblclick': case 'onmousedown': case 'onmouseup':
case 'onmouseover': case 'onmousemove': case 'onmouseout':
case 'onkeypress': case 'onkeydown': case 'onkeyup':
case 'span': case 'width': case 'align': case 'char': case 'charoff': case 'valign':
attrsTxt+=' '+attrName+'="'+putHTMLEnts(attrValue)+'"';
break;
}
break;
}
}
}
}
return attrsTxt;
}
function parseHTML(n, rmFrmt)
{
var HTMLTxt='', tagBeg='', tagEnd='';
var tagName='', tagAttrs='';
tagAttrs=parseAttrsHTML(n, rmFrmt);
for (var i=0; i<n.childNodes.length; i++)
{
HTMLTxt+=parseHTML(n.childNodes.item(i), rmFrmt);
}
switch (n.nodeType)
{
case 1:
tagName=n.nodeName.toLowerCase();
switch (tagName)
{
case 'hr': case 'br': case 'param': case 'img': case 'area': case 'input': case 'col':
tagBeg='<'+tagName+tagAttrs+' />';
break;
case 'div': case 'ul': case 'ol': case 'dl':
case 'form': case 'fieldset': case 'table': case 'tr':
if (! rmFrmt || HTMLTxt != '' || tagAttrs != '')
{
tagBeg='<'+tagName+tagAttrs+'>\n';
tagEnd='</'+tagName+'>\n';
}
break;
case 'h1': case 'h2': case 'h3': case 'h4': case 'h5': case 'h6':
case 'pre':	case 'p':
case 'li': case 'address': case 'script': case 'noscript': case 'ins': case 'del':
case 'blockquote': case 'th': case 'td':
if (! rmFrmt || HTMLTxt != '' || tagAttrs != '')
{
tagBeg='<'+tagName+tagAttrs+'>';
tagEnd='</'+tagName+'>\n';
}
break;
case 'b':
if (! rmFrmt || HTMLTxt != '' || tagAttrs != '')
{
tagBeg='<strong'+tagAttrs+'>';
tagEnd='</strong>';
}
break;
case 'i':
if (! rmFrmt || HTMLTxt != '' || tagAttrs != '')
{
tagBeg='<em'+tagAttrs+'>';
tagEnd='</em>';
}
break;
case 'span':
if (! rmFrmt)
{
tagBeg='<'+tagName+tagAttrs+'>';
tagEnd='</'+tagName+'>';
}
break;
case 'em': case 'strong':
if (rmFrmt)
{
break;
}
case 'dt': case 'dd': case 'bdo': case 'dfn':
case 'code': case 'samp': case 'kbd': case 'var': case 'cite': case 'abbr': case 'acronym':
case 'sub': case 'sup': case 'tt': case 'big': case 'small': case 'q':
case 'a': case 'object': case 'map':
case 'label': case 'select': case 'option': case 'optgroup': case 'textarea': case 'legend':
case 'caption': case 'colgroup': case 'thead': case 'tfoot': case 'tbody':
if (! rmFrmt || HTMLTxt != '' || tagAttrs != '')
{
tagBeg='<'+tagName+tagAttrs+'>';
tagEnd='</'+tagName+'>';
}
break;
case 'u':
if (! rmFrmt)
{
if (tagAttrs.search(/\s?style=\"/i) >= 0)
{
tagAttrs=tagAttrs.replace(/(\s?style=\"[^\";]*);?\s*\"/i, '$1; text-decoration: underline;"');
}
else
{
tagAttrs+=' style="text-decoration: underline;"';
}
tagBeg='<span'+tagAttrs+'>';
tagEnd='</span>';
}
break;
case 'strike':
if (! rmFrmt)
{
if (tagAttrs.search(/\s?style=\"/i) >= 0)
{
tagAttrs=tagAttrs.replace(/(\s?style=\"[^\";]*);?\s*\"/i, '$1; text-decoration: line-through;"');
}
else
{
tagAttrs+=' style="text-decoration: line-through;"';
}
tagBeg='<span'+tagAttrs+'>';
tagEnd='</span>';
}
break;
default:
remTagList+=', '+n.nodeName;
break;
}
HTMLTxt=tagBeg+HTMLTxt+tagEnd;
break;
case 3:
HTMLTxt+=putHTMLEnts(n.nodeValue.replace(/^[\n\r]*/, ''));
break;
case 8:
HTMLTxt+='<!-- '+n.nodeValue+' -->';
break;
}
return HTMLTxt;
}
var remTagList='';
function cleanHTML(n)
{
var HTMLTxt='';
n.normalize();
for (var i=0; i<n.childNodes.length; i++)
{
HTMLTxt+=parseHTML(n.childNodes.item(i), false);
}
if (remTagList != '')
{
alert('Attenzione, sono stati rimossi alcuni tag non validi: '+remTagList.substr(2)+'.\nControlla il risultato!');
remTagList='';
}
if (HTMLTxt.search(/^[\n\r]*<br \/>[\n\r]*$/i) >= 0 ||
HTMLTxt.search(/^[\n\r]*(<p>)?&nbsp;[\n\r]*(<\/p>)?$/i) >= 0)
{
HTMLTxt='';
}
return HTMLTxt;
}
