1 /*
  2  * CKEditor - The text editor for Internet - http://ckeditor.com
  3  * Copyright (C) 2003-2008 Frederico Caldeira Knabben
  4  *
  5  * == BEGIN LICENSE ==
  6  *
  7  * Licensed under the terms of any of the following licenses at your
  8  * choice:
  9  *
 10  *  - GNU General Public License Version 2 or later (the "GPL")
 11  *    http://www.gnu.org/licenses/gpl.html
 12  *
 13  *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 14  *    http://www.gnu.org/licenses/lgpl.html
 15  *
 16  *  - Mozilla Public License Version 1.1 or later (the "MPL")
 17  *    http://www.mozilla.org/MPL/MPL-1.1.html
 18  *
 19  * == END LICENSE ==
 20  */
 21
 22 /**
 23  * @fileOverview Defines the {@link CKEDITOR.dom.document} class, which
 24  *		represents a DOM document.
 25  */
 26
 27 /**
 28  * Represents a DOM document.
 29  * @constructor
 30  * @param {Object} document A native DOM document.
 31  * @example
 32  * // Create a new <span> element.
 33  * var document = new CKEDITOR.dom.document( document );
 34  */
 35 CKEDITOR.dom.document = function( document )
 36 {
 37 	this.$ = document;
 38 };
 39
 40 CKEDITOR.dom.document.prototype =
 41 {
 42 	/**
 43 	 * Appends a CSS file to the document.
 44 	 * @param {String} cssFileUrl The CSS file URL.
 45 	 * @type undefined
 46 	 * @example
 47 	 * <b>CKEDITOR.document.appendStyleSheet( '/mystyles.css' )</b>;
 48 	 */
 49 	appendStyleSheet : function( cssFileUrl )
 50 	{
 51 		if ( this.$.createStyleSheet )
 52 			this.$.createStyleSheet( cssFileUrl );
 53 		else
 54 		{
 55 			var link = new CKEDITOR.dom.element( 'link' );
 56 			link.setAttributes(
 57 				{
 58 					rel		:'stylesheet',
 59 					type	: 'text/css',
 60 					href	: cssFileUrl
 61 				});
 62
 63 			this.getHead().append( link );
 64 		}
 65 	},
 66
 67 	/**
 68 	 * Determines whether the specified object is equal to the current object.
 69 	 * @param {Object} object The object to compare with the current object.
 70 	 * @returns {Boolean} "true" if the object is equal.
 71 	 * @example
 72 	 * var doc = new CKEDITOR.dom.document( document );
 73 	 * alert( doc.equals( CKEDITOR.document ) );  // "true"
 74 	 * alert( doc == CKEDITOR.document );         // "false"
 75 	 */
 76 	equals : function( object )
 77 	{
 78 		return ( object && object.$ === this.$ );
 79 	},
 80
 81 	/**
 82 	 * Gets and element based on its id.
 83 	 * @param {String} elementId The element id.
 84 	 * @returns {CKEDITOR.dom.element} The element instance, or null if not found.
 85 	 * @example
 86 	 * var element = <b>CKEDITOR.document.getById( 'myElement' )</b>;
 87 	 * alert( element.getId() );  // "myElement"
 88 	 */
 89 	getById : function( elementId )
 90 	{
 91 		var $ = this.$.getElementById( elementId );
 92 		return $ ? new CKEDITOR.dom.element( $ ) : null;
 93 	},
 94
 95 	/**
 96 	 * Gets the <head> element for this document.
 97 	 * @returns {CKEDITOR.dom.element} The <head> element.
 98 	 * @example
 99 	 * var element = <b>CKEDITOR.document.getHead()</b>;
100 	 * alert( element.getName() );  // "head"
101 	 */
102 	getHead : function()
103 	{
104 		var head = this.$.getElementsByTagName( 'head' )[0];
105 		head = new CKEDITOR.dom.element( head );
106
107 		return (
108 		/** @ignore */
109 		this.getHead = function()
110 			{
111 				return head;
112 			})();
113 	},
114
115 	/**
116 	 * Gets the <body> element for this document.
117 	 * @returns {CKEDITOR.dom.element} The <body> element.
118 	 * @example
119 	 * var element = <b>CKEDITOR.document.getBody()</b>;
120 	 * alert( element.getName() );  // "body"
121 	 */
122 	getBody : function()
123 	{
124 		var body = new CKEDITOR.dom.element( this.$.body );
125
126 		return (
127 		/** @ignore */
128 		this.getBody = function()
129 			{
130 				return body;
131 			})();
132 	}
133 };
134