﻿//------------------------------------------------------------------------------
//
// ADOBE SYSTEMS INCORPORATED
// Copyright 2003 Adobe Systems Incorporated 
// All Rights Reserved
//
// NOTICE: Adobe permits you to use, modify, and distribute 
// this file in accordance with the terms of the Adobe license 
// agreement accompanying it. If you have received this file 
// from a source other than Adobe, then your use, modification, 
// or distribution of it requires the prior written permission 
// of Adobe.
//
//------------------------------------------------------------------------------


///////////////////////////////////////////////////////////////////////////////
//
// HTML Tag Class
//

//-----------------------------------------------------------------------------
// 
// HTMLItem(...)
// 
// Purpose: ctor
// 
// Author : pwollek
// 
//-----------------------------------------------------------------------------

function HTMLItem( rtfEntry, mapEntry, start )
{
	this.type		= '';					// type string, e.g. 'paragraph', 'character', 'style'
	this.text		= '';					// text if type == 'character'
	this.rtfEntry	= rtfEntry;				// the original RTFEntry instance
	this.mapEntry	= mapEntry;				// the corresponding map entry
	this.startTag	= start;				// is it a start tag
	this.subItems	= new Array;			// sub items
	this.styleStack = null;					// stack of open styles if type == 'paragraph'
	this.alignment  = 'left';				// alignment of paragraph if type == 'paragraph'
	
	// set type
	if( this.mapEntry != null && this.mapEntry != undefined )
		this.type = mapEntry[3];
	else
		this.type = 'character';
	
	// create style stack
	if( this.type == 'paragraph' )
	{
		this.styleStack = new Array;
	}
}

//-----------------------------------------------------------------------------
// 
// function(...)
// 
// Purpose: add a sub item
// 
// Author : pwollek
// 
//-----------------------------------------------------------------------------

HTMLItem.prototype.addItem = function( rtfEntry, mapEntry, start )
{
	var item = new HTMLItem( rtfEntry, mapEntry, start );
	
	this.subItems.push( item );
	
	if( item.type == 'style' )
	{
		if( start )
		{
			this.styleStack.push( item );
		}
		else
		{
			var found = false;
			
			for( var i=this.styleStack.length-1; i>=0 && !found; i-- )
			{
				if( this.styleStack[i].rtfEntry.name == rtfEntry.name )
				{
					this.styleStack.splice( i, 1);
					found = true;
				}
			}
		}
	}
}

//-----------------------------------------------------------------------------
// 
// HTMLItem.prototype.addEndTagFor(...)
// 
// Purpose: add endtag for given item
// 
// Author : pwollek
// 
//-----------------------------------------------------------------------------

HTMLItem.prototype.addEndTagFor = function( item )
{
	if( item.startTag )
		this.addItem( item.rtfEntry, item.mapEntry, false );
}

//-----------------------------------------------------------------------------
// 
// function(...)
// 
// Purpose: add text
// 
// Author : pwollek
// 
//-----------------------------------------------------------------------------

HTMLItem.prototype.addText = function( text )
{
	var textItem = null;
	
	if( this.subItems.length > 0 && this.subItems[this.subItems.length-1].type == 'character' )
		this.subItems[this.subItems.length-1].text += text;
	else
	{
		textItem = new HTMLItem( null, null, true );
		textItem.text += text;
		this.subItems.push( textItem );
	}
}

//-----------------------------------------------------------------------------
// 
// function(...)
// 
// Purpose: open style tag (with respect of style stack of current paragraph)
// 
// Author : pwollek
// 
//-----------------------------------------------------------------------------

HTMLItem.prototype.openStyle = function( rtfEntry, mapEntry )
{
	if( this.styleOnStack( rtfEntry ) )
	{
		var reStyles = this.closeStyle( rtfEntry, mapEntry, true );
	}	

	this.addItem( rtfEntry, mapEntry, true );
}

//-----------------------------------------------------------------------------
// 
// function(...)
// 
// Purpose: close given style (with respect of style stack)
// 
// Author : pwollek
// 
//-----------------------------------------------------------------------------

HTMLItem.prototype.closeStyle = function( rtfEntry, mapEntry, reopen )
{
	var ret = new Array;
	
	if( this.styleOnStack( rtfEntry ) )
	{
		var style = null;
		
		for( var i=this.styleStack.length-1; i>=0 && style==null; i-- )
		{
			if( this.styleStack[i].rtfEntry.name == rtfEntry.name )
				style = this.styleStack[i];
			else
				ret.push( this.styleStack[i] );
		}
		
		if( style != null )
		{
			for( var i=0; i<ret.length; i++ )
				this.addEndTagFor( ret[i] );
				
			this.addEndTagFor( style );
		}
	}
	
	if( reopen && ret.length > 0 )
	{
		while( ret.length > 0 )
		{
			var rstyle = ret.pop();
			this.addItem( rstyle.rtfEntry, rstyle.mapEntry, true );
		}
	}
	
	return ret;
}

//-----------------------------------------------------------------------------
// 
// function(...)
// 
// Purpose: close all open styles
// 
// Author : pwollek
// 
//-----------------------------------------------------------------------------

HTMLItem.prototype.closeAllStyles = function()
{
	var ret = new Array;
	
	while( this.styleStack.length > 0 )
	{
		var style = this.styleStack[this.styleStack.length-1];
		this.addEndTagFor( style );
		ret.unshift( style );
	}
	
	return ret;
}

//-----------------------------------------------------------------------------
// 
// function(...)
// 
// Purpose: is style on my stack
// 
// Author : pwollek
// 
//-----------------------------------------------------------------------------

HTMLItem.prototype.styleOnStack = function( rtfEntry )
{
	var ret = false;
	
	if( this.type == 'paragraph' && this.styleStack.length > 0 )
	{
		for( var i=this.styleStack.length-1; i>=0 && !ret; i-- )
			ret = ( this.styleStack[i].rtfEntry.name == rtfEntry.name );//&& this.styleStack[i].rtfEntry.value == rtfEntry.value );
	}
	
	return ret;
}

//-----------------------------------------------------------------------------
// 
// function(...)
// 
// Purpose: stream out my source code
// 
// Author : pwollek
// 
//-----------------------------------------------------------------------------

HTMLItem.prototype.getSource = function()
{
	var source = '';
	
	switch( this.type )
	{
		case 'character':
			source += this.text;
			break;
			
		case 'style':
		case 'tag':
		{
			if( this.startTag )
			{
				var attr = '';
				
				if( this.mapEntry[5] != '' && this.rtfEntry.mlvalue != undefined )
					attr = ' ' + this.mapEntry[5] + '=\"' + this.rtfEntry.mlvalue + '\"';
					
				source += '<' + this.mapEntry[1] + attr + '>';
			}
			else
			{
				if( this.mapEntry[2] )
				{
					source += '</' + this.mapEntry[1] + '>';
				}
			}
		}
		break;
		
		case 'paragraph':
		{
			var attr = '';
			
			if( this.alignment != 'left' )
				attr = ' align=\"' + this.alignment + '\"';
				
			source += '<p' + attr + '>';
			
			for( var i=0; i<this.subItems.length; i++ )
				source += this.subItems[i].getSource();
				
			source += '</p>';
		}
		break;
	}
	
	return source;
}

//-----------------------------------------------------------------------------
// 
// function(...)
// 
// Purpose: remove empty style tags which mean styles without any contents like e.g. '...<b></b>...'
// 
// Author : pwollek
// 
//-----------------------------------------------------------------------------

HTMLItem.prototype.removeEmptyStyles = function()
{
	var remIndex = new Array;
	
	for( var si=0; si<this.subItems.length; si++ )
	{
		var item = this.subItems[si];
		
		if( item.type == 'style' && item.startTag )
		{
			var endIndex = -1;
			var content = false;
			
			for( var fe=si+1; fe<this.subItems.length && endIndex == -1 && !content; fe++ )
			{
				var desitem = this.subItems[fe];
				
				content = ( desitem.type == 'tag' || desitem.type == 'character' );
				
				if( desitem.type == 'style' && desitem.rtfEntry == item.rtfEntry && !desitem.startTag )
					endIndex = fe;
			}
			
			if( endIndex > -1 && !content )
			{
				remIndex.push( si );
				remIndex.push( endIndex );
			}
		}
	}
	
	if( remIndex.length > 0 )
	{
		for( var i=0; i<remIndex.length; i++ )
		{
			var index = remIndex[i];
			this.subItems.splice( index, 1 );
			
			for( var s=i+1; s<remIndex.length; s++ )
			{
				if( remIndex[s] > index )
					remIndex[s]--;
			}
		}
	}
}

HTMLItem.prototype.replaceSpaces = function()
{
	for( var i=0; i<this.subItems.length; i++ )
	{
		if( this.subItems[i].type == 'character' )
		{
			var nText = '';
			var sp = false;
			
			for( var t=0; t<this.subItems[i].text.length; t++ )
			{
				var ch = this.subItems[i].text[t];
				
				if( !sp )
				{
					nText += ch;
					
					sp = ch == ' ';
						
				}
				else
				{
					if( ch == ' ' )
						nText += '&nbsp;';
					else
					{
						nText += ch;
						sp = false;
					}
				}
			}			
		}
		else
			this.subItems[i].replaceSpaces();
	}
}