2 Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
3 Available via Academic Free License >= 2.1 OR the modified BSD license.
4 see: http://dojotoolkit.org/license for details
8 if(!dojo
._hasResource
["dijit._editor.plugins.EnterKeyHandling"]){
9 dojo
._hasResource
["dijit._editor.plugins.EnterKeyHandling"]=true;
10 dojo
.provide("dijit._editor.plugins.EnterKeyHandling");
11 dojo
.require("dojo.window");
12 dojo
.declare("dijit._editor.plugins.EnterKeyHandling",dijit
._editor
._Plugin
,{blockNodeForEnter
:"BR",constructor:function(_1
){
16 },setEditor:function(_2
){
18 if(this.blockNodeForEnter
=="BR"){
20 _2
.contentDomPreFilters
.push(dojo
.hitch(this,"regularPsToSingleLinePs"));
21 _2
.contentDomPostFilters
.push(dojo
.hitch(this,"singleLinePsToRegularPs"));
22 _2
.onLoadDeferred
.addCallback(dojo
.hitch(this,"_fixNewLineBehaviorForIE"));
24 _2
.onLoadDeferred
.addCallback(dojo
.hitch(this,function(d
){
26 this.editor
.document
.execCommand("insertBrOnReturn",false,true);
34 if(this.blockNodeForEnter
){
35 dojo
["require"]("dijit._editor.range");
36 var h
=dojo
.hitch(this,this.handleEnterKey
);
37 _2
.addKeyHandler(13,0,0,h
);
38 _2
.addKeyHandler(13,0,1,h
);
39 this.connect(this.editor
,"onKeyPressed","onKeyPressed");
42 },onKeyPressed:function(e
){
43 if(this._checkListLater
){
44 if(dojo
.withGlobal(this.editor
.window
,"isCollapsed",dijit
)){
45 var _3
=dojo
.withGlobal(this.editor
.window
,"getAncestorElement",dijit
._editor
.selection
,["LI"]);
47 dijit
._editor
.RichText
.prototype.execCommand
.call(this.editor
,"formatblock",this.blockNodeForEnter
);
48 var _4
=dojo
.withGlobal(this.editor
.window
,"getAncestorElement",dijit
._editor
.selection
,[this.blockNodeForEnter
]);
50 _4
.innerHTML
=this.bogusHtmlContent
;
52 var r
=this.editor
.document
.selection
.createRange();
53 r
.move("character",-1);
57 console
.error("onKeyPressed: Cannot find the new block node");
61 if(_3
.parentNode
.parentNode
.nodeName
=="LI"){
62 _3
=_3
.parentNode
.parentNode
;
66 if(fc
&&fc
.nodeType
==1&&(fc
.nodeName
=="UL"||fc
.nodeName
=="OL")){
67 _3
.insertBefore(fc
.ownerDocument
.createTextNode(" "),fc
);
68 var _5
=dijit
.range
.create(this.editor
.window
);
69 _5
.setStart(_3
.firstChild
,0);
70 var _6
=dijit
.range
.getSelection(this.editor
.window
,true);
76 this._checkListLater
=false;
78 if(this._pressedEnterInBlock
){
79 if(this._pressedEnterInBlock
.previousSibling
){
80 this.removeTrailingBr(this._pressedEnterInBlock
.previousSibling
);
82 delete this._pressedEnterInBlock
;
84 },bogusHtmlContent
:" ",blockNodes
:/^(?:P|H1|H2|H3|H4|H5|H6|LI)$/,handleEnterKey:function(e
){
85 var _7
,_8
,_9
,_a
=this.editor
.document
,br
;
87 var _b
=dojo
.withGlobal(this.editor
.window
,"getParentElement",dijit
._editor
.selection
);
88 var _c
=dijit
.range
.getAncestor(_b
,this.blockNodes
);
90 if(!e
.shiftKey
&&_c
.tagName
=="LI"){
93 _7
=dijit
.range
.getSelection(this.editor
.window
);
97 _7
=dijit
.range
.getSelection(this.editor
.window
);
100 if(dijit
.range
.atBeginningOfContainer(_c
,_8
.startContainer
,_8
.startOffset
)){
102 br
=_a
.createElement("br");
103 _9
=dijit
.range
.create(this.editor
.window
);
104 _c
.insertBefore(br
,_c
.firstChild
);
105 _9
.setStartBefore(br
.nextSibling
);
106 _7
.removeAllRanges();
109 dojo
.place(br
,_c
,"before");
112 if(dijit
.range
.atEndOfContainer(_c
,_8
.startContainer
,_8
.startOffset
)){
113 _9
=dijit
.range
.create(this.editor
.window
);
114 br
=_a
.createElement("br");
117 _c
.appendChild(_a
.createTextNode(" "));
118 _9
.setStart(_c
.lastChild
,0);
120 dojo
.place(br
,_c
,"after");
121 _9
.setStartAfter(_c
);
123 _7
.removeAllRanges();
130 dijit
._editor
.RichText
.prototype.execCommand
.call(this.editor
,"inserthtml","<br>");
135 _7
=dijit
.range
.getSelection(this.editor
.window
);
139 _7
=dijit
.range
.getSelection(this.editor
.window
);
142 var _e
=dijit
.range
.getBlockAncestor(_8
.endContainer
,null,this.editor
.editNode
);
144 if((this._checkListLater
=(_f
&&(_f
.nodeName
=="LI"||_f
.parentNode
.nodeName
=="LI")))){
146 this._pressedEnterInBlock
=_f
;
148 if(/^(\s| |\xA0|<span\b[^>]*\bclass=['"]Apple-style-span['"][^>]*>(\s| |\xA0)<\/span>)?(<br>)?$/.test(_f
.innerHTML
)){
151 _9
=dijit
.range
.create(this.editor
.window
);
153 _7
.removeAllRanges();
156 this._checkListLater
=false;
160 if(!_e
.blockNode
||_e
.blockNode
===this.editor
.editNode
){
162 dijit
._editor
.RichText
.prototype.execCommand
.call(this.editor
,"formatblock",this.blockNodeForEnter
);
166 _e
={blockNode
:dojo
.withGlobal(this.editor
.window
,"getAncestorElement",dijit
._editor
.selection
,[this.blockNodeForEnter
]),blockContainer
:this.editor
.editNode
};
168 if(_e
.blockNode
!=this.editor
.editNode
&&(!(_e
.blockNode
.textContent
||_e
.blockNode
.innerHTML
).replace(/^\s+|\s+$/g,"").length
)){
169 this.removeTrailingBr(_e
.blockNode
);
173 _e
.blockNode
=this.editor
.editNode
;
175 _7
=dijit
.range
.getSelection(this.editor
.window
);
178 var _10
=_a
.createElement(this.blockNodeForEnter
);
179 _10
.innerHTML
=this.bogusHtmlContent
;
180 this.removeTrailingBr(_e
.blockNode
);
181 if(dijit
.range
.atEndOfContainer(_e
.blockNode
,_8
.endContainer
,_8
.endOffset
)){
182 if(_e
.blockNode
===_e
.blockContainer
){
183 _e
.blockNode
.appendChild(_10
);
185 dojo
.place(_10
,_e
.blockNode
,"after");
188 _9
=dijit
.range
.create(this.editor
.window
);
190 _7
.removeAllRanges();
192 if(this.editor
.height
){
193 dojo
.window
.scrollIntoView(_10
);
196 if(dijit
.range
.atBeginningOfContainer(_e
.blockNode
,_8
.startContainer
,_8
.startOffset
)){
197 dojo
.place(_10
,_e
.blockNode
,_e
.blockNode
===_e
.blockContainer
?"first":"before");
198 if(_10
.nextSibling
&&this.editor
.height
){
199 _9
=dijit
.range
.create(this.editor
.window
);
200 _9
.setStart(_10
.nextSibling
,0);
201 _7
.removeAllRanges();
203 dojo
.window
.scrollIntoView(_10
.nextSibling
);
207 if(_e
.blockNode
===_e
.blockContainer
){
208 _e
.blockNode
.appendChild(_10
);
210 dojo
.place(_10
,_e
.blockNode
,"after");
213 if(_e
.blockNode
.style
){
215 if(_e
.blockNode
.style
.cssText
){
216 _10
.style
.cssText
=_e
.blockNode
.style
.cssText
;
220 var rs
=_8
.startContainer
;
221 if(rs
&&rs
.nodeType
==3){
223 var txt
=rs
.nodeValue
;
224 var _13
=_a
.createTextNode(txt
.substring(0,_8
.startOffset
));
225 var _14
=_a
.createTextNode(txt
.substring(_8
.startOffset
,txt
.length
));
226 dojo
.place(_13
,rs
,"before");
227 dojo
.place(_14
,rs
,"after");
229 var _15
=_13
.parentNode
;
230 while(_15
!==_e
.blockNode
){
232 var _16
=_a
.createElement(tg
);
235 if(_15
.style
.cssText
){
236 _16
.style
.cssText
=_15
.style
.cssText
;
243 _16
.appendChild(_11
);
246 dojo
.place(_16
,_15
,"after");
252 if(_11
.nodeType
==1||(_11
.nodeType
==3&&_11
.nodeValue
)){
257 _10
.appendChild(_11
);
261 _9
=dijit
.range
.create(this.editor
.window
);
263 _7
.removeAllRanges();
265 if(this.editor
.height
){
266 dijit
.scrollIntoView(_10
);
269 this._pressedEnterInBlock
=_e
.blockNode
;
274 },removeTrailingBr:function(_17
){
275 var _18
=/P|DIV|LI/i.test(_17
.tagName
)?_17
:dijit
._editor
.selection
.getParentOfType(_17
,["P","DIV","LI"]);
280 if((_18
.childNodes
.length
>1&&_18
.lastChild
.nodeType
==3&&/^[\s\xAD]*$/.test(_18
.lastChild
.nodeValue
))||_18
.lastChild
.tagName
=="BR"){
281 dojo
.destroy(_18
.lastChild
);
284 if(!_18
.childNodes
.length
){
285 _18
.innerHTML
=this.bogusHtmlContent
;
287 },_fixNewLineBehaviorForIE:function(d
){
288 var doc
=this.editor
.document
;
289 if(doc
.__INSERTED_EDITIOR_NEWLINE_CSS
===undefined){
290 var _19
=dojo
.create("style",{type
:"text/css"},doc
.getElementsByTagName("head")[0]);
291 _19
.styleSheet
.cssText
="p{margin:0;}";
292 this.editor
.document
.__INSERTED_EDITIOR_NEWLINE_CSS
=true;
295 },regularPsToSingleLinePs:function(_1a
,_1b
){
298 var _1f
=_1e
[0].ownerDocument
.createElement("p");
299 _1e
[0].parentNode
.insertBefore(_1f
,_1e
[0]);
300 dojo
.forEach(_1e
,function(_20
){
301 _1f
.appendChild(_20
);
307 while(_21
<el
.childNodes
.length
){
308 _23
=el
.childNodes
[_21
];
309 if(_23
.nodeType
==3||(_23
.nodeType
==1&&_23
.nodeName
!="BR"&&dojo
.style(_23
,"display")!="block")){
312 var _24
=_23
.nextSibling
;
315 _21
=(_21
+1)-_22
.length
;
316 if(_23
.nodeName
=="BR"){
331 var _28
=el
.childNodes
.length
-1;
332 for(var i
=_28
;i
>=0;i
--){
333 _26
=el
.childNodes
[i
];
334 if(_26
.nodeName
=="BR"){
335 var _29
=_26
.ownerDocument
.createElement("p");
336 dojo
.place(_29
,el
,"after");
337 if(_27
.length
==0&&i
!=_28
){
338 _29
.innerHTML
=" ";
340 dojo
.forEach(_27
,function(_2a
){
341 _29
.appendChild(_2a
);
351 var ps
=_1a
.getElementsByTagName("p");
352 dojo
.forEach(ps
,function(p
){
355 dojo
.forEach(_2b
,function(p
){
356 var _2c
=p
.previousSibling
;
357 if((_2c
)&&(_2c
.nodeType
==1)&&(_2c
.nodeName
=="P"||dojo
.style(_2c
,"display")!="block")){
358 var _2d
=p
.parentNode
.insertBefore(this.document
.createElement("p"),p
);
359 _2d
.innerHTML
=_1b
?"":" ";
365 },singleLinePsToRegularPs:function(_2e
){
367 var ps
=_30
.getElementsByTagName("p");
369 for(var i
=0;i
<ps
.length
;i
++){
372 for(var k
=0;k
<_31
.length
;k
++){
373 if(_31
[k
]===p
.parentNode
){
379 _31
.push(p
.parentNode
);
385 return (!_34
.childNodes
.length
||_34
.innerHTML
==" ");
388 for(var i
=0;i
<_35
.length
;i
++){
391 var _38
=_36
.firstChild
;
394 if(_38
.nodeType
!=1||_38
.tagName
!="P"||(_38
.getAttributeNode("style")||{}).specified
){
404 if((!_37
.lastChild
||_37
.lastChild
.nodeName
!="BR")&&(_38
.firstChild
)&&(_38
.firstChild
.nodeName
!="BR")){
405 _37
.appendChild(this.editor
.document
.createElement("br"));
407 while(_38
.firstChild
){
408 _37
.appendChild(_38
.firstChild
);