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( 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 getByAddress : function( address, normalized ) 91 { 92 var $ = this.$.documentElement; 93 94 for ( var i = 0 ; $ && i < address.length ; i++ ) 95 { 96 var target = address[ i ]; 97 98 if ( !normalized ) 99 { 100 $ = $.childNodes[ target ]; 101 continue; 102 } 103 104 var currentIndex = -1; 105 106 for (var j = 0 ; j < $.childNodes.length ; j++ ) 107 { 108 var candidate = $.childNodes[ j ]; 109 110 if ( normalized === true && 111 candidate.nodeType == 3 && 112 candidate.previousSibling && 113 candidate.previousSibling.nodeType == 3 ) 114 { 115 continue; 116 } 117 118 currentIndex++; 119 120 if ( currentIndex == target ) 121 { 122 $ = candidate; 123 break; 124 } 125 } 126 } 127 128 return $ ? new CKEDITOR.dom.node( $ ) : null; 129 }, 130 131 /** 132 * Gets the <head> element for this document. 133 * @returns {CKEDITOR.dom.element} The <head> element. 134 * @example 135 * var element = <b>CKEDITOR.document.getHead()</b>; 136 * alert( element.getName() ); // "head" 137 */ 138 getHead : function() 139 { 140 var head = this.$.getElementsByTagName( 'head' )[0]; 141 head = new CKEDITOR.dom.element( head ); 142 143 return ( 144 /** @ignore */ 145 this.getHead = function() 146 { 147 return head; 148 })(); 149 }, 150 151 /** 152 * Gets the <body> element for this document. 153 * @returns {CKEDITOR.dom.element} The <body> element. 154 * @example 155 * var element = <b>CKEDITOR.document.getBody()</b>; 156 * alert( element.getName() ); // "body" 157 */ 158 getBody : function() 159 { 160 var body = new CKEDITOR.dom.element( this.$.body ); 161 162 return ( 163 /** @ignore */ 164 this.getBody = function() 165 { 166 return body; 167 })(); 168 }, 169 170 getDocumentElement : function() 171 { 172 var documentElement = new CKEDITOR.dom.element( this.$.documentElement ); 173 174 return ( 175 /** @ignore */ 176 this.getDocumentElement = function() 177 { 178 return documentElement; 179 })(); 180 }, 181 182 /** 183 * Gets the window object that holds this document. 184 * @returns {CKEDITOR.dom.window} The window object. 185 * @example 186 */ 187 getWindow : function() 188 { 189 var win = new CKEDITOR.dom.window( this.$.parentWindow || this.$.defaultView ); 190 191 return ( 192 /** @ignore */ 193 this.getWindow = function() 194 { 195 return win; 196 })(); 197 } 198 }); 199