]>
git.immae.eu Git - perso/Immae/Projets/packagist/connexionswing-ckeditor-component.git/blob - sources/core/dom/document.js
f287245e64d820d3281d57d1641f8d16ebfdde6f
2 * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
3 * For licensing, see LICENSE.md 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 * var document = new CKEDITOR.dom.document( document );
17 * @extends CKEDITOR.dom.domObject
18 * @constructor Creates a document class instance.
19 * @param {Object} domDocument A native DOM document.
21 CKEDITOR
.dom
.document = function( domDocument
) {
22 CKEDITOR
.dom
.domObject
.call( this, domDocument
);
25 // PACKAGER_RENAME( CKEDITOR.dom.document )
27 CKEDITOR
.dom
.document
.prototype = new CKEDITOR
.dom
.domObject();
29 CKEDITOR
.tools
.extend( CKEDITOR
.dom
.document
.prototype, {
31 * The node type. This is a constant value set to {@link CKEDITOR#NODE_DOCUMENT}.
34 * @property {Number} [=CKEDITOR.NODE_DOCUMENT]
36 type: CKEDITOR
.NODE_DOCUMENT
,
39 * Appends a CSS file to the document.
41 * CKEDITOR.document.appendStyleSheet( '/mystyles.css' );
43 * @param {String} cssFileUrl The CSS file URL.
45 appendStyleSheet: function( cssFileUrl
) {
46 if ( this.$.createStyleSheet
)
47 this.$.createStyleSheet( cssFileUrl
);
49 var link
= new CKEDITOR
.dom
.element( 'link' );
56 this.getHead().append( link
);
61 * Creates a CSS stylesheet and inserts it into the document.
63 * @param cssStyleText {String} CSS style text.
64 * @returns {Object} The created DOM native stylesheet object.
66 appendStyleText: function( cssStyleText
) {
67 if ( this.$.createStyleSheet
) {
68 var styleSheet
= this.$.createStyleSheet( '' );
69 styleSheet
.cssText
= cssStyleText
;
71 var style
= new CKEDITOR
.dom
.element( 'style', this );
72 style
.append( new CKEDITOR
.dom
.text( cssStyleText
, this ) );
73 this.getHead().append( style
);
76 return styleSheet
|| style
.$.sheet
;
80 * Creates a {@link CKEDITOR.dom.element} instance in this document.
82 * @param {String} name The name of the element.
83 * @param {Object} [attributesAndStyles]
84 * @param {Object} [attributesAndStyles.attributes] Attributes that will be set.
85 * @param {Object} [attributesAndStyles.styles] Styles that will be set.
86 * @returns {CKEDITOR.dom.element}
88 createElement: function( name
, attribsAndStyles
) {
89 var element
= new CKEDITOR
.dom
.element( name
, this );
91 if ( attribsAndStyles
) {
92 if ( attribsAndStyles
.attributes
)
93 element
.setAttributes( attribsAndStyles
.attributes
);
95 if ( attribsAndStyles
.styles
)
96 element
.setStyles( attribsAndStyles
.styles
);
103 * Creates a {@link CKEDITOR.dom.text} instance in this document.
105 * @param {String} text Value of the text node.
106 * @returns {CKEDITOR.dom.element}
108 createText: function( text
) {
109 return new CKEDITOR
.dom
.text( text
, this );
113 * Moves the selection focus to this document's window.
116 this.getWindow().focus();
120 * Returns the element that is currently designated as the active element in the document.
122 * **Note:** Only one element can be active at a time in a document.
123 * An active element does not necessarily have focus,
124 * but an element with focus is always the active element in a document.
126 * @returns {CKEDITOR.dom.element} Active element or `null` if an IE8-9 bug is encountered.
127 * See [#10030](http://dev.ckeditor.com/ticket/10030).
129 getActive: function() {
132 $active
= this.$.activeElement
;
136 return new CKEDITOR
.dom
.element( $active
);
140 * Gets an element based on its ID.
142 * var element = CKEDITOR.document.getById( 'myElement' );
143 * alert( element.getId() ); // 'myElement'
145 * @param {String} elementId The element ID.
146 * @returns {CKEDITOR.dom.element} The element instance, or `null` if not found.
148 getById: function( elementId
) {
149 var $ = this.$.getElementById( elementId
);
150 return $ ? new CKEDITOR
.dom
.element( $ ) : null;
154 * Gets a node based on its address. See {@link CKEDITOR.dom.node#getAddress}.
156 * @param {Array} address
157 * @param {Boolean} [normalized=false]
159 getByAddress: function( address
, normalized
) {
160 var $ = this.$.documentElement
;
162 for ( var i
= 0; $ && i
< address
.length
; i
++ ) {
163 var target
= address
[ i
];
166 $ = $.childNodes
[ target
];
170 var currentIndex
= -1;
172 for ( var j
= 0; j
< $.childNodes
.length
; j
++ ) {
173 var candidate
= $.childNodes
[ j
];
175 if ( normalized
=== true && candidate
.nodeType
== 3 && candidate
.previousSibling
&& candidate
.previousSibling
.nodeType
== 3 )
180 if ( currentIndex
== target
) {
187 return $ ? new CKEDITOR
.dom
.node( $ ) : null;
191 * Gets elements list based on a given tag name.
193 * @param {String} tagName The element tag name.
194 * @returns {CKEDITOR.dom.nodeList} The nodes list.
196 getElementsByTag: function( tagName
, namespace ) {
197 if ( !( CKEDITOR
.env
.ie
&& ( document
.documentMode
<= 8 ) ) && namespace )
198 tagName
= namespace + ':' + tagName
;
199 return new CKEDITOR
.dom
.nodeList( this.$.getElementsByTagName( tagName
) );
203 * Gets the `<head>` element for this document.
205 * var element = CKEDITOR.document.getHead();
206 * alert( element.getName() ); // 'head'
208 * @returns {CKEDITOR.dom.element} The `<head>` element.
210 getHead: function() {
211 var head
= this.$.getElementsByTagName( 'head' )[ 0 ];
213 head
= this.getDocumentElement().append( new CKEDITOR
.dom
.element( 'head' ), true );
215 head
= new CKEDITOR
.dom
.element( head
);
221 * Gets the `<body>` element for this document.
223 * var element = CKEDITOR.document.getBody();
224 * alert( element.getName() ); // 'body'
226 * @returns {CKEDITOR.dom.element} The `<body>` element.
228 getBody: function() {
229 return new CKEDITOR
.dom
.element( this.$.body
);
233 * Gets the DOM document element for this document.
235 * @returns {CKEDITOR.dom.element} The DOM document element.
237 getDocumentElement: function() {
238 return new CKEDITOR
.dom
.element( this.$.documentElement
);
242 * Gets the window object that stores this document.
244 * @returns {CKEDITOR.dom.window} The window object.
246 getWindow: function() {
247 return new CKEDITOR
.dom
.window( this.$.parentWindow
|| this.$.defaultView
);
251 * Defines the document content through `document.write`. Note that the
252 * previous document content will be lost (cleaned).
256 * '<head><title>Sample Document</title></head>' +
257 * '<body>Document content created by code.</body>' +
262 * @param {String} html The HTML defining the document content.
264 write: function( html
) {
265 // Don't leave any history log in IE. (#5657)
266 this.$.open( 'text/html', 'replace' );
268 // Support for custom document.domain in IE.
270 // The script must be appended because if placed before the
271 // doctype, IE will go into quirks mode and mess with
272 // the editable, e.g. by changing its default height.
273 if ( CKEDITOR
.env
.ie
)
274 html
= html
.replace( /(?:^\s*<!DOCTYPE[^>]*?>)|^/i, '$&\n<script data-cke-temp="1">(' + CKEDITOR
.tools
.fixDomain
+ ')();</script>' );
276 this.$.write( html
);
281 * Wrapper for `querySelectorAll`. Returns a list of elements within this document that match
282 * the specified `selector`.
284 * **Note:** The returned list is not a live collection (like the result of native `querySelectorAll`).
287 * @param {String} selector
288 * @returns {CKEDITOR.dom.nodeList}
290 find: function( selector
) {
291 return new CKEDITOR
.dom
.nodeList( this.$.querySelectorAll( selector
) );
295 * Wrapper for `querySelector`. Returns the first element within this document that matches
296 * the specified `selector`.
299 * @param {String} selector
300 * @returns {CKEDITOR.dom.element}
302 findOne: function( selector
) {
303 var el
= this.$.querySelector( selector
);
305 return el
? new CKEDITOR
.dom
.element( el
) : null;
309 * Internet Explorer 8 only method. It returns a document fragment which has all HTML5 elements enabled.
313 * @returns DocumentFragment
315 _getHtml5ShivFrag: function() {
316 var $frag
= this.getCustomData( 'html5ShivFrag' );
319 $frag
= this.$.createDocumentFragment();
320 CKEDITOR
.tools
.enableHtml5Elements( $frag
, true );
321 this.setCustomData( 'html5ShivFrag', $frag
);