1 /*
  2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
  3 For licensing, see LICENSE.html or http://ckeditor.com/license
  4 */
  5
  6 /**
  7  * @fileOverview Defines the {@link CKEDITOR.dom.document} class, which
  8  *		represents a DOM document.
  9  */
 10
 11 /**
 12  * Represents a DOM document.
 13  * @constructor
 14  * @augments CKEDITOR.dom.domObject
 15  * @param {Object} domDocument A native DOM document.
 16  * @example
 17  * var document = new CKEDITOR.dom.document( document );
 18  */
 19 CKEDITOR.dom.document = function( domDocument )
 20 {
 21 	CKEDITOR.dom.domObject.call( this, domDocument );
 22 };
 23
 24 // PACKAGER_RENAME( CKEDITOR.dom.document )
 25
 26 CKEDITOR.dom.document.prototype = new CKEDITOR.dom.domObject();
 27
 28 CKEDITOR.tools.extend( CKEDITOR.dom.document.prototype,
 29 	/** @lends CKEDITOR.dom.document.prototype */
 30 	{
 31 		/**
 32 		 * Appends a CSS file to the document.
 33 		 * @param {String} cssFileUrl The CSS file URL.
 34 		 * @example
 35 		 * <b>CKEDITOR.document.appendStyleSheet( '/mystyles.css' )</b>;
 36 		 */
 37 		appendStyleSheet : function( cssFileUrl )
 38 		{
 39 			if ( this.$.createStyleSheet )
 40 				this.$.createStyleSheet( cssFileUrl );
 41 			else
 42 			{
 43 				var link = new CKEDITOR.dom.element( 'link' );
 44 				link.setAttributes(
 45 					{
 46 						rel		:'stylesheet',
 47 						type	: 'text/css',
 48 						href	: cssFileUrl
 49 					});
 50
 51 				this.getHead().append( link );
 52 			}
 53 		},
 54
 55 		createElement : function( name, attribsAndStyles )
 56 		{
 57 			var element = new CKEDITOR.dom.element( name, this );
 58
 59 			if ( attribsAndStyles )
 60 			{
 61 				if ( attribsAndStyles.attributes )
 62 					element.setAttributes( attribsAndStyles.attributes );
 63
 64 				if ( attribsAndStyles.styles )
 65 					element.setStyles( attribsAndStyles.styles );
 66 			}
 67
 68 			return element;
 69 		},
 70
 71 		createText : function( text )
 72 		{
 73 			return new CKEDITOR.dom.text( '', this );
 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 		 * Gets 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