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