FCKeditor

TestDomAbstraction.html

From FCKeditor Docs

Jump to: navigation, search

Script Block

window.onload = function()
{
	var repetitions = 1000 ;

	var t = new Date() ;
	
	for ( var i = 0 ; i < repetitions ; i++ )
		PureDom() ;
	
	t = ( ( new Date ) - t ) ;
	
	var html = '<p>Pure DOM: ' + t + ' ms.</p>' ;

	t = new Date() ;
	
	for ( var i = 0 ; i < repetitions ; i++ )
		DomAbstraction() ;
	
	t = ( ( new Date ) - t ) ;
	
	html += '<p>DOM Abstraction: ' + t + ' ms.</p>' ;

	document.body.innerHTML = html ;
}

function PureDom()
{
	var p = ( document.parentWindow || document.defaultView ).document.body.firstChild ;
//	var p = document.body.firstChild ;
	
	var current = p.firstChild ;
	
	var name ;
	while ( current )
	{
		if ( current.nodeType == 1 )
		{
			name = current.nodeName.toLowerCase() ;
			name = current.ownerDocument.body.firstChild.nodeName.toLowerCase() ;
		}
		else if ( current.nodeType == 3 )
			name = current.nodeValue ;

		current = current.nextSibling ;
	}		
}

function DomAbstraction()
{
	var p = (new DOM.Document(document)).getWindow().getDocument().getBody().getFirst() ;
//	var p = new DOM.Element(document.body.firstChild) ;
	
	var current = p.getFirst() ;
	
	var name ;
	while ( current )
	{
		if ( current.isElement )
		{
			name = current.getName() ;
			name = current.getDocument().getBody().getFirst().getName() ;
		}
		else if ( current.isText )
			name = current.getText() ;

		current = current.getNext() ;
	}		
}

var DOM =
{
	Window : (function()
	{
		var classImpl = function( domWindow )
		{
			this._W = domWindow ;
		}
		
		classImpl.prototype =
		{
			getDocument : function()
			{
				return this._document || ( this._document = new DOM.Document( this._W.document ) ) ;
			}
		}

		return classImpl ;
	})(),

	Document : (function()
	{
		var classImpl = function( domDocument )
		{
			this._D = domDocument ;
		}
		
		classImpl.prototype =
		{
			getWindow : function()
			{
				return this._window || ( this._window = new DOM.Window( this._D.parentWindow || this._D.defaultView ) ) ;
			},

			getBody : function()
			{
				return this._body || ( this._body = new DOM.Element( this._D.body ) ) ;
			}
		}

		return classImpl ;
	})(),
	
	Element : (function()
	{
		var classImpl = function( domElement )
		{
			this._E = domElement ;
		}
		
		classImpl.prototype =
		{
			isElement : true,
			
			getDocument : function()
			{
				return this._document || ( this._document = new DOM.Document( this._E.ownerDocument ) ) ;
			},

			getFirst : function()
			{
				return DOM.getNode( this._E.firstChild ) ;
			},

			getNext : function()
			{
				return DOM.getNode( this._E.nextSibling ) ;
			},

			getName : function()
			{
				return this._name || ( this._name = this._E.nodeName.toLowerCase() ) ;
			}
		}

		return classImpl ;
	})(),

	Text : (function()
	{
		var classImpl = function( domTextNode )
		{
			this._T = domTextNode ;
		}
		
		classImpl.prototype =
		{
			isText : true,

			getNext : function()
			{
				return DOM.getNode( this._T.nextSibling ) ;
			},

			getText : function()
			{
				return this._T.nodeValue ;
			}
		}

		return classImpl ;
	})(),
	
	getNode : function( domNode )
	{
		if ( domNode )
		{		
			if ( domNode.nodeType == 1 )
				return new DOM.Element( domNode ) ;
			else
				return new DOM.Text( domNode ) ;
		}
		
		return null ;
	}
}

Body Contents

<body><p>This is <b>some</b> sample <i>text</i>.</p></body>
Personal tools
Powered by MediaWiki