8880de8a4d8d1e48dfe71231552489823671c05c
2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
7 * @fileOverview Defines the {@link CKEDITOR.dom.document} class, which
8 * represents a DOM document.
12 * Represents a DOM document.
14 * @augments CKEDITOR.dom.domObject
15 * @param {Object} domDocument A native DOM document.
17 * var document = new CKEDITOR.dom.document( document );
19 CKEDITOR
.dom
.document = function( domDocument
)
21 CKEDITOR
.dom
.domObject
.call( this, domDocument
);
24 // PACKAGER_RENAME( CKEDITOR.dom.document )
26 CKEDITOR
.dom
.document
.prototype = new CKEDITOR
.dom
.domObject();
28 CKEDITOR
.tools
.extend( CKEDITOR
.dom
.document
.prototype,
29 /** @lends CKEDITOR.dom.document.prototype */
32 * Appends a CSS file to the document.
33 * @param {String} cssFileUrl The CSS file URL.
35 * <b>CKEDITOR.document.appendStyleSheet( '/mystyles.css' )</b>;
37 appendStyleSheet : function( cssFileUrl
)
39 if ( this.$.createStyleSheet
)
40 this.$.createStyleSheet( cssFileUrl
);
43 var link
= new CKEDITOR
.dom
.element( 'link' );
51 this.getHead().append( link
);
55 appendStyleText : function( cssStyleText
)
57 if ( this.$.createStyleSheet
)
59 var styleSheet
= this.$.createStyleSheet( "" );
60 styleSheet
.cssText
= cssStyleText
;
64 var style
= new CKEDITOR
.dom
.element( 'style', this );
65 style
.append( new CKEDITOR
.dom
.text( cssStyleText
, this ) );
66 this.getHead().append( style
);
70 createElement : function( name
, attribsAndStyles
)
72 var element
= new CKEDITOR
.dom
.element( name
, this );
74 if ( attribsAndStyles
)
76 if ( attribsAndStyles
.attributes
)
77 element
.setAttributes( attribsAndStyles
.attributes
);
79 if ( attribsAndStyles
.styles
)
80 element
.setStyles( attribsAndStyles
.styles
);
86 createText : function( text
)
88 return new CKEDITOR
.dom
.text( text
, this );
93 this.getWindow().focus();
97 * Gets and element based on its id.
98 * @param {String} elementId The element id.
99 * @returns {CKEDITOR.dom.element} The element instance, or null if not found.
101 * var element = <b>CKEDITOR.document.getById( 'myElement' )</b>;
102 * alert( element.getId() ); // "myElement"
104 getById : function( elementId
)
106 var $ = this.$.getElementById( elementId
);
107 return $ ? new CKEDITOR
.dom
.element( $ ) : null;
110 getByAddress : function( address
, normalized
)
112 var $ = this.$.documentElement
;
114 for ( var i
= 0 ; $ && i
< address
.length
; i
++ )
116 var target
= address
[ i
];
120 $ = $.childNodes
[ target
];
124 var currentIndex
= -1;
126 for (var j
= 0 ; j
< $.childNodes
.length
; j
++ )
128 var candidate
= $.childNodes
[ j
];
130 if ( normalized
=== true &&
131 candidate
.nodeType
== 3 &&
132 candidate
.previousSibling
&&
133 candidate
.previousSibling
.nodeType
== 3 )
140 if ( currentIndex
== target
)
148 return $ ? new CKEDITOR
.dom
.node( $ ) : null;
151 getElementsByTag : function( tagName
, namespace )
153 if ( !( CKEDITOR
.env
.ie
&& ! ( document
.documentMode
> 8 ) ) && namespace )
154 tagName
= namespace + ':' + tagName
;
155 return new CKEDITOR
.dom
.nodeList( this.$.getElementsByTagName( tagName
) );
159 * Gets the <head> element for this document.
160 * @returns {CKEDITOR.dom.element} The <head> element.
162 * var element = <b>CKEDITOR.document.getHead()</b>;
163 * alert( element.getName() ); // "head"
167 var head
= this.$.getElementsByTagName( 'head' )[0];
169 head
= this.getDocumentElement().append( new CKEDITOR
.dom
.element( 'head' ), true );
171 head
= new CKEDITOR
.dom
.element( head
);
174 this.getHead = function()
181 * Gets the <body> element for this document.
182 * @returns {CKEDITOR.dom.element} The <body> element.
184 * var element = <b>CKEDITOR.document.getBody()</b>;
185 * alert( element.getName() ); // "body"
189 var body
= new CKEDITOR
.dom
.element( this.$.body
);
192 this.getBody = function()
199 * Gets the DOM document element for this document.
200 * @returns {CKEDITOR.dom.element} The DOM document element.
202 getDocumentElement : function()
204 var documentElement
= new CKEDITOR
.dom
.element( this.$.documentElement
);
207 this.getDocumentElement = function()
209 return documentElement
;
214 * Gets the window object that holds this document.
215 * @returns {CKEDITOR.dom.window} The window object.
217 getWindow : function()
219 var win
= new CKEDITOR
.dom
.window( this.$.parentWindow
|| this.$.defaultView
);
222 this.getWindow = function()
229 * Defines the document contents through document.write. Note that the
230 * previous document contents will be lost (cleaned).
232 * @param {String} html The HTML defining the document contents.
236 * '<head><title>Sample Doc</title></head>' +
237 * '<body>Document contents created by code</body>' +
240 write : function( html
)
242 // Don't leave any history log in IE. (#5657)
243 this.$.open( 'text/html', 'replace' );
245 // Support for custom document.domain in IE.
246 CKEDITOR
.env
.isCustomDomain() && ( this.$.domain
= document
.domain
);
248 this.$.write( html
);