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  * @augments CKEDITOR.dom.domObject
 31  * @param {Object} domDocument A native DOM document.
 32  * @example
 33  * var document = new CKEDITOR.dom.document( document );
 34  */
 35 CKEDITOR.dom.document = function( domDocument )
 36 {
 37 	CKEDITOR.dom.domObject.call( this, domDocument );
 38
 39 	/**
 40 	 * The native DOM document represented by this class instance.
 41 	 * @type Object
 42 	 * @example
 43 	 */
 44 	this.$ = domDocument;
 45 };
 46
 47 CKEDITOR.dom.document.prototype = new CKEDITOR.dom.domObject();
 48
 49 CKEDITOR.tools.extend( CKEDITOR.dom.document.prototype,
 50 	/** @lends CKEDITOR.dom.document.prototype */
 51 	{
 52 		/**
 53 		 * Appends a CSS file to the document.
 54 		 * @param {String} cssFileUrl The CSS file URL.
 55 		 * @example
 56 		 * <b>CKEDITOR.document.appendStyleSheet( '/mystyles.css' )</b>;
 57 		 */
 58 		appendStyleSheet : function( cssFileUrl )
 59 		{
 60 			if ( this.$.createStyleSheet )
 61 				this.$.createStyleSheet( cssFileUrl );
 62 			else
 63 			{
 64 				var link = new CKEDITOR.dom.element( 'link' );
 65 				link.setAttributes(
 66 					{
 67 						rel		:'stylesheet',
 68 						type	: 'text/css',
 69 						href	: cssFileUrl
 70 					});
 71
 72 				this.getHead().append( link );
 73 			}
 74 		},
 75
 76 		/**
 77 		 * Gets and element based on its id.
 78 		 * @param {String} elementId The element id.
 79 		 * @returns {CKEDITOR.dom.element} The element instance, or null if not found.
 80 		 * @example
 81 		 * var element = <b>CKEDITOR.document.getById( 'myElement' )</b>;
 82 		 * alert( element.getId() );  // "myElement"
 83 		 */
 84 		getById : function( elementId )
 85 		{
 86 			var $ = this.$.getElementById( elementId );
 87 			return $ ? new CKEDITOR.dom.element( $ ) : null;
 88 		},
 89
 90 		/**
 91 		 * Gets the <head> element for this document.
 92 		 * @returns {CKEDITOR.dom.element} The <head> element.
 93 		 * @example
 94 		 * var element = <b>CKEDITOR.document.getHead()</b>;
 95 		 * alert( element.getName() );  // "head"
 96 		 */
 97 		getHead : function()
 98 		{
 99 			var head = this.$.getElementsByTagName( 'head' )[0];
100 			head = new CKEDITOR.dom.element( head );
101
102 			return (
103 			/** @ignore */
104 			this.getHead = function()
105 				{
106 					return head;
107 				})();
108 		},
109
110 		/**
111 		 * Gets the <body> element for this document.
112 		 * @returns {CKEDITOR.dom.element} The <body> element.
113 		 * @example
114 		 * var element = <b>CKEDITOR.document.getBody()</b>;
115 		 * alert( element.getName() );  // "body"
116 		 */
117 		getBody : function()
118 		{
119 			var body = new CKEDITOR.dom.element( this.$.body );
120
121 			return (
122 			/** @ignore */
123 			this.getBody = function()
124 				{
125 					return body;
126 				})();
127 		},
128
129 		/**
130 		 * Get's the window object that holds this document.
131 		 * @returns {CKEDITOR.dom.window} The window object.
132 		 * @example
133 		 */
134 		getWindow : function()
135 		{
136 			var win = new CKEDITOR.dom.window( this.$.parentWindow || this.$.defaultView );
137
138 			return (
139 			/** @ignore */
140 			this.getWindow = function()
141 				{
142 					return win;
143 				})();
144 		}
145 	});
146