codice:
//variables of html parser
this.B=0;
this.I=0;
this.U=0;
this.HREF='';
this.fontlist=new Array("arial","times","courier","helvetica","symbol");
this.issetfont=false;
this.issetcolor=false;
//////////////////////////////////////
//html parser
function isset(o){return (o?true:false)}
// funzioni da sviluppare : ereg
this.strip_tags=function(html,noStripList){
if (noStripList)noStripList = noStripList.toLowerCase()
var test = /<(.*?)>/ig;
html = html.replace(
test,
function($0){
if (noStripList){
var s=$0.toLowerCase();
if(s.substr(1,1)=="/"){
var st="<" + s.substr(2)
if (noStripList.indexOf(st)>-1)return s
}
else
{
var p = s.indexOf(" ")
if (p>0){
st=s.substring(0,p) + ">"
if(noStripList.indexOf(st)>-1)return s;
}
else if(noStripList.indexOf(s)>-1)return s;
}
}
}
)
return html;
}
this.stripslashes=function(s){
return this.str_replace("\\'","\'",s)
}
this.GetAttrArray=function(value){
var i,strCh,ch
var key="";
var tmpValue="";
var valueKey="";
var inString = false
var ar=[];
for(i=0;i<value.length;i++){
ch = value.charAt(i);
if (inString&&(ch==strCh||i==value.length-1)){
valueKey = tmpValue;
if(ch!=strCh&&i==value.length-1)valueKey+=ch
tmpValue = ""
inString=false;
ar[key]=valueKey
}
else if (ch=="="){
key = this.trim(tmpValue).toUpperCase()
tmpValue = ""
while(value.charAt(i+1)==" ")i++
strCh = value.charAt(i+1)
inString=true;
if (strCh!="'"&&strCh!="\"")strCh=" "
else i++
}
else tmpValue+=ch;
}
return ar;
}
function preg_split(src){
var Res;
var arRes=[];
var re = /[<>]/g;
while ((Res = re.exec(src)) != null){
arRes.push(src.substr(0,Res.index))
src = src.substr(Res.lastIndex)
re = /[<>]/g;
}
arRes.push(src)
return arRes
}
this.WriteHTML=function(html)
{
var a,i,e,a2,tag;
html=this.strip_tags(html,"[b]<u>[i]<a><img>
[b][i]<font><tr><blockquote><dir>"); //remove all unsupported tags
html=this.str_replace("\n",' ',html); //replace carriage returns by spaces
a=preg_split(html); //explodes the string
for(i in a)
{
e=a[i];
if(i%2==0)
{
//Text
if(this.HREF)
this.PutLink(this.HREF,e);
else
this.Write(this.stripslashes(this.txtentities(e)));
}
else
{
//Tag
if(e.charAt(0)=='/')
this.CloseTag(e.substr(1).toUpperCase());
else
{
//Extract attributes
var attr;
var p = e.indexOf(" ")
if(p>-1){
tag = e.substring(0,p)
var attr=this.GetAttrArray(e.substr(p+1))
}else tag = e
this.OpenTag(tag.toUpperCase() ,attr);
}
}
}
}
function px2mm(px){
if(!px)return 0;
return px*25.4/72;
}
this.OpenTag=function(tag,attr)
{
//Opening tag
switch(tag){
case 'STRONG':
this.SetStyle('B',true);
break;
case 'EM':
this.SetStyle('I',true);
break;
case 'B':
case 'I':
case 'U':
this.SetStyle(tag,true);
break;
case 'A':
this.HREF=attr['HREF'];
break;
case 'IMG':
if(attr['SRC'])this.Image(attr['SRC'], px2mm(attr['WIDTH']), px2mm(attr['HEIGHT']),this.HREF);
break;
case 'TR':
case 'BLOCKQUOTE':
case 'BR':
this.Ln(5);
break;
case 'P':
this.Ln(10);
break;
case 'FONT':
if (isset(attr['COLOR']) && attr['COLOR']!='') {
var coul=this.HexToRgb(attr['COLOR']);
this.SetTextColor(coul['R'],coul['G'],coul['B']);
this.issetcolor=true;
}
if (isset(attr['FACE']) && this.InArray(attr['FACE'].toLowerCase(), this.fontlist)) {
this.SetFont(attr['FACE'].toLowerCase());
this.issetfont=true;
}
break;
}
}
this.CloseTag=function(tag)
{
//Closing tag
if(tag=='STRONG')
tag='B';
if(tag=='EM')
tag='I';
if(tag=='B'||tag=='I'||tag=='U')
this.SetStyle(tag,false);
if(tag=='A')
this.HREF='';
if(tag=='FONT'){
if (this.issetcolor==true) {
this.SetTextColor(0);
}
if (this.issetfont) {
this.SetFont('arial');
this.issetfont=false;
}
}
if(tag=='DIR')
this.margin.left -= 5
}
this.arStyle = {'B':0,'U':0,'I':0}
this.SetStyle=function(tag,enable)
{
//Modify style and select corresponding font
this.arStyle[tag] += (enable ? 1 : -1);
var style = (this.arStyle['I']>0?"I":"") + (this.arStyle['B']>0?"B":"") + (this.arStyle['U']>0?"U":"")
this.SetFont("",style);
}
this.PutLink=function(URL,txt)
{
//Put a hyperlink
this.SetTextColor(0,0,255);
this.SetStyle('U',true);
this.Write(txt,0,URL);
this.SetStyle('U',false);
this.SetTextColor(0);
}