JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.3
[ckeditor.git] / _source / core / dom / document.js
1 /*\r
2 Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 /**\r
7  * @fileOverview Defines the {@link CKEDITOR.dom.document} class, which\r
8  *              represents a DOM document.\r
9  */\r
10 \r
11 /**\r
12  * Represents a DOM document.\r
13  * @constructor\r
14  * @augments CKEDITOR.dom.domObject\r
15  * @param {Object} domDocument A native DOM document.\r
16  * @example\r
17  * var document = new CKEDITOR.dom.document( document );\r
18  */\r
19 CKEDITOR.dom.document = function( domDocument )\r
20 {\r
21         CKEDITOR.dom.domObject.call( this, domDocument );\r
22 };\r
23 \r
24 // PACKAGER_RENAME( CKEDITOR.dom.document )\r
25 \r
26 CKEDITOR.dom.document.prototype = new CKEDITOR.dom.domObject();\r
27 \r
28 CKEDITOR.tools.extend( CKEDITOR.dom.document.prototype,\r
29         /** @lends CKEDITOR.dom.document.prototype */\r
30         {\r
31                 /**\r
32                  * Appends a CSS file to the document.\r
33                  * @param {String} cssFileUrl The CSS file URL.\r
34                  * @example\r
35                  * <b>CKEDITOR.document.appendStyleSheet( '/mystyles.css' )</b>;\r
36                  */\r
37                 appendStyleSheet : function( cssFileUrl )\r
38                 {\r
39                         if ( this.$.createStyleSheet )\r
40                                 this.$.createStyleSheet( cssFileUrl );\r
41                         else\r
42                         {\r
43                                 var link = new CKEDITOR.dom.element( 'link' );\r
44                                 link.setAttributes(\r
45                                         {\r
46                                                 rel             :'stylesheet',\r
47                                                 type    : 'text/css',\r
48                                                 href    : cssFileUrl\r
49                                         });\r
50 \r
51                                 this.getHead().append( link );\r
52                         }\r
53                 },\r
54 \r
55                 appendStyleText : function( cssStyleText )\r
56                 {\r
57                         if ( this.$.createStyleSheet )\r
58                         {\r
59                                 var styleSheet = this.$.createStyleSheet( "" );\r
60                                 styleSheet.cssText = cssStyleText ;\r
61                         }\r
62                         else\r
63                         {\r
64                                 var style = new CKEDITOR.dom.element( 'style', this );\r
65                                 style.append( new CKEDITOR.dom.text( cssStyleText, this ) );\r
66                                 this.getHead().append( style );\r
67                         }\r
68                 },\r
69 \r
70                 createElement : function( name, attribsAndStyles )\r
71                 {\r
72                         var element = new CKEDITOR.dom.element( name, this );\r
73 \r
74                         if ( attribsAndStyles )\r
75                         {\r
76                                 if ( attribsAndStyles.attributes )\r
77                                         element.setAttributes( attribsAndStyles.attributes );\r
78 \r
79                                 if ( attribsAndStyles.styles )\r
80                                         element.setStyles( attribsAndStyles.styles );\r
81                         }\r
82 \r
83                         return element;\r
84                 },\r
85 \r
86                 createText : function( text )\r
87                 {\r
88                         return new CKEDITOR.dom.text( text, this );\r
89                 },\r
90 \r
91                 focus : function()\r
92                 {\r
93                         this.getWindow().focus();\r
94                 },\r
95 \r
96                 /**\r
97                  * Gets and element based on its id.\r
98                  * @param {String} elementId The element id.\r
99                  * @returns {CKEDITOR.dom.element} The element instance, or null if not found.\r
100                  * @example\r
101                  * var element = <b>CKEDITOR.document.getById( 'myElement' )</b>;\r
102                  * alert( element.getId() );  // "myElement"\r
103                  */\r
104                 getById : function( elementId )\r
105                 {\r
106                         var $ = this.$.getElementById( elementId );\r
107                         return $ ? new CKEDITOR.dom.element( $ ) : null;\r
108                 },\r
109 \r
110                 getByAddress : function( address, normalized )\r
111                 {\r
112                         var $ = this.$.documentElement;\r
113 \r
114                         for ( var i = 0 ; $ && i < address.length ; i++ )\r
115                         {\r
116                                 var target = address[ i ];\r
117 \r
118                                 if ( !normalized )\r
119                                 {\r
120                                         $ = $.childNodes[ target ];\r
121                                         continue;\r
122                                 }\r
123 \r
124                                 var currentIndex = -1;\r
125 \r
126                                 for (var j = 0 ; j < $.childNodes.length ; j++ )\r
127                                 {\r
128                                         var candidate = $.childNodes[ j ];\r
129 \r
130                                         if ( normalized === true &&\r
131                                                         candidate.nodeType == 3 &&\r
132                                                         candidate.previousSibling &&\r
133                                                         candidate.previousSibling.nodeType == 3 )\r
134                                         {\r
135                                                 continue;\r
136                                         }\r
137 \r
138                                         currentIndex++;\r
139 \r
140                                         if ( currentIndex == target )\r
141                                         {\r
142                                                 $ = candidate;\r
143                                                 break;\r
144                                         }\r
145                                 }\r
146                         }\r
147 \r
148                         return $ ? new CKEDITOR.dom.node( $ ) : null;\r
149                 },\r
150 \r
151                 getElementsByTag : function( tagName, namespace )\r
152                 {\r
153                         if ( !( CKEDITOR.env.ie && ! ( document.documentMode > 8 ) ) && namespace )\r
154                                 tagName = namespace + ':' + tagName;\r
155                         return new CKEDITOR.dom.nodeList( this.$.getElementsByTagName( tagName ) );\r
156                 },\r
157 \r
158                 /**\r
159                  * Gets the &lt;head&gt; element for this document.\r
160                  * @returns {CKEDITOR.dom.element} The &lt;head&gt; element.\r
161                  * @example\r
162                  * var element = <b>CKEDITOR.document.getHead()</b>;\r
163                  * alert( element.getName() );  // "head"\r
164                  */\r
165                 getHead : function()\r
166                 {\r
167                         var head = this.$.getElementsByTagName( 'head' )[0];\r
168                         if ( !head )\r
169                                 head = this.getDocumentElement().append( new CKEDITOR.dom.element( 'head' ), true );\r
170                         else\r
171                         head = new CKEDITOR.dom.element( head );\r
172 \r
173                         return (\r
174                         this.getHead = function()\r
175                                 {\r
176                                         return head;\r
177                                 })();\r
178                 },\r
179 \r
180                 /**\r
181                  * Gets the &lt;body&gt; element for this document.\r
182                  * @returns {CKEDITOR.dom.element} The &lt;body&gt; element.\r
183                  * @example\r
184                  * var element = <b>CKEDITOR.document.getBody()</b>;\r
185                  * alert( element.getName() );  // "body"\r
186                  */\r
187                 getBody : function()\r
188                 {\r
189                         var body = new CKEDITOR.dom.element( this.$.body );\r
190 \r
191                         return (\r
192                         this.getBody = function()\r
193                                 {\r
194                                         return body;\r
195                                 })();\r
196                 },\r
197 \r
198                 /**\r
199                  * Gets the DOM document element for this document.\r
200                  * @returns {CKEDITOR.dom.element} The DOM document element.\r
201                  */\r
202                 getDocumentElement : function()\r
203                 {\r
204                         var documentElement = new CKEDITOR.dom.element( this.$.documentElement );\r
205 \r
206                         return (\r
207                         this.getDocumentElement = function()\r
208                                 {\r
209                                         return documentElement;\r
210                                 })();\r
211                 },\r
212 \r
213                 /**\r
214                  * Gets the window object that holds this document.\r
215                  * @returns {CKEDITOR.dom.window} The window object.\r
216                  */\r
217                 getWindow : function()\r
218                 {\r
219                         var win = new CKEDITOR.dom.window( this.$.parentWindow || this.$.defaultView );\r
220 \r
221                         return (\r
222                         this.getWindow = function()\r
223                                 {\r
224                                         return win;\r
225                                 })();\r
226                 },\r
227 \r
228                 /**\r
229                  * Defines the document contents through document.write. Note that the\r
230                  * previous document contents will be lost (cleaned).\r
231                  * @since 3.5\r
232                  * @param {String} html The HTML defining the document contents.\r
233                  * @example\r
234                  * document.write(\r
235                  *     '&lt;html&gt;' +\r
236                  *         '&lt;head&gt;&lt;title&gt;Sample Doc&lt;/title&gt;&lt;/head&gt;' +\r
237                  *         '&lt;body&gt;Document contents created by code&lt;/body&gt;' +\r
238                  *      '&lt;/html&gt;' );\r
239                  */\r
240                 write : function( html )\r
241                 {\r
242                         // Don't leave any history log in IE. (#5657)\r
243                         this.$.open( 'text/html', 'replace' );\r
244 \r
245                         // Support for custom document.domain in IE.\r
246                         CKEDITOR.env.isCustomDomain() &&  ( this.$.domain = document.domain );\r
247 \r
248                         this.$.write( html );\r
249                         this.$.close();\r
250                 }\r
251         });\r