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