@@ -193,13 +193,22 @@ ZSSEditor.getField = function(fieldId) {
193193
194194ZSSEditor . getFocusedField = function ( ) {
195195 var currentField = $ ( this . closerParentNodeWithName ( 'div' ) ) ;
196- var currentFieldId = currentField . attr ( 'id' ) ;
196+ var currentFieldId ;
197197
198- while ( currentField
199- && ( ! currentFieldId || this . editableFields [ currentFieldId ] == null ) ) {
200- currentField = this . closerParentNodeStartingAtNode ( 'div' , currentField ) ;
198+ if ( currentField ) {
201199 currentFieldId = currentField . attr ( 'id' ) ;
200+ }
202201
202+ while ( currentField && ( ! currentFieldId || this . editableFields [ currentFieldId ] == null ) ) {
203+ currentField = this . closerParentNodeStartingAtNode ( 'div' , currentField ) ;
204+ if ( currentField ) {
205+ currentFieldId = currentField . attr ( 'id' ) ;
206+ }
207+ }
208+
209+ if ( ! currentFieldId ) {
210+ ZSSEditor . resetSelectionOnField ( 'zss_field_content' ) ;
211+ currentFieldId = 'zss_field_content' ;
203212 }
204213
205214 return this . editableFields [ currentFieldId ] ;
@@ -357,6 +366,9 @@ ZSSEditor.stylesCallback = function(stylesArray) {
357366
358367ZSSEditor . backupRange = function ( ) {
359368 var selection = window . getSelection ( ) ;
369+ if ( selection . rangeCount < 1 ) {
370+ return ;
371+ }
360372 var range = selection . getRangeAt ( 0 ) ;
361373
362374 ZSSEditor . currentSelection =
@@ -411,7 +423,6 @@ ZSSEditor.getCaretArguments = function() {
411423} ;
412424
413425ZSSEditor . getJoinedFocusedFieldIdAndCaretArguments = function ( ) {
414-
415426 var joinedArguments = ZSSEditor . getJoinedCaretArguments ( ) ;
416427 var idArgument = "id=" + ZSSEditor . getFocusedField ( ) . getNodeId ( ) ;
417428
@@ -430,6 +441,9 @@ ZSSEditor.getJoinedCaretArguments = function() {
430441
431442ZSSEditor . getCaretYPosition = function ( ) {
432443 var selection = window . getSelection ( ) ;
444+ if ( selection . rangeCount == 0 ) {
445+ return 0 ;
446+ }
433447 var range = selection . getRangeAt ( 0 ) ;
434448 var span = document . createElement ( "span" ) ;
435449 // Ensure span has dimensions and position by
@@ -543,7 +557,7 @@ ZSSEditor.setStrikeThrough = function() {
543557 var mustHandleWebKitIssue = ( isDisablingStrikeThrough
544558 && ZSSEditor . isCommandEnabled ( commandName ) ) ;
545559
546- if ( mustHandleWebKitIssue ) {
560+ if ( mustHandleWebKitIssue && window . getSelection ( ) . rangeCount > 0 ) {
547561 var troublesomeNodeNames = [ 'del' ] ;
548562
549563 var selection = window . getSelection ( ) ;
@@ -2730,6 +2744,9 @@ ZSSEditor.closerParentNode = function() {
27302744
27312745 var parentNode = null ;
27322746 var selection = window . getSelection ( ) ;
2747+ if ( selection . rangeCount < 1 ) {
2748+ return null ;
2749+ }
27332750 var range = selection . getRangeAt ( 0 ) . cloneRange ( ) ;
27342751
27352752 var currentNode = range . commonAncestorContainer ;
@@ -2752,15 +2769,15 @@ ZSSEditor.closerParentNodeStartingAtNode = function(nodeName, startingNode) {
27522769 nodeName = nodeName . toLowerCase ( ) ;
27532770
27542771 var parentNode = null ;
2755- var currentNode = startingNode , parentElement ;
2772+ var currentNode = startingNode . parentElement ;
27562773
27572774 while ( currentNode ) {
27582775
27592776 if ( currentNode . nodeName == document . body . nodeName ) {
27602777 break ;
27612778 }
27622779
2763- if ( currentNode . nodeName . toLowerCase ( ) == nodeName
2780+ if ( currentNode . nodeName && currentNode . nodeName . toLowerCase ( ) == nodeName
27642781 && currentNode . nodeType == document . ELEMENT_NODE ) {
27652782 parentNode = currentNode ;
27662783
@@ -2779,6 +2796,9 @@ ZSSEditor.closerParentNodeWithName = function(nodeName) {
27792796
27802797 var parentNode = null ;
27812798 var selection = window . getSelection ( ) ;
2799+ if ( selection . rangeCount < 1 ) {
2800+ return null ;
2801+ }
27822802 var range = selection . getRangeAt ( 0 ) . cloneRange ( ) ;
27832803
27842804 var referenceNode = range . commonAncestorContainer ;
@@ -2820,6 +2840,9 @@ ZSSEditor.parentTags = function() {
28202840
28212841 var parentTags = [ ] ;
28222842 var selection = window . getSelection ( ) ;
2843+ if ( selection . rangeCount < 1 ) {
2844+ return null ;
2845+ }
28232846 var range = selection . getRangeAt ( 0 ) ;
28242847
28252848 var currentNode = range . commonAncestorContainer ;
@@ -2937,21 +2960,29 @@ ZSSField.prototype.handleKeyDownEvent = function(e) {
29372960 } else if ( this . isMultiline ( ) ) {
29382961 this . wrapCaretInParagraphIfNecessary ( ) ;
29392962
2940- // If enter was pressed to end a UL or OL, let's double check and handle it accordingly if so
29412963 if ( wasEnterPressed ) {
2942- sel = window . getSelection ( ) ;
2943- node = $ ( sel . anchorNode ) ;
2944- children = $ ( sel . anchorNode . childNodes ) ;
2964+ var sel = window . getSelection ( ) ;
2965+ if ( sel . rangeCount < 1 ) {
2966+ return null ;
2967+ }
2968+ var node = $ ( sel . anchorNode ) ;
2969+ var children = $ ( sel . anchorNode . childNodes ) ;
2970+ var parentNode = rangy . getSelection ( ) . anchorNode . parentNode ;
29452971
2972+ // If enter was pressed to end a UL or OL, let's double check and handle it accordingly if so
29462973 if ( sel . isCollapsed && node . is ( NodeName . LI ) && ( ! children . length ||
29472974 ( children . length == 1 && children . first ( ) . is ( NodeName . BR ) ) ) ) {
29482975 e . preventDefault ( ) ;
2949- var parentNode = rangy . getSelection ( ) . anchorNode . parentNode ;
29502976 if ( parentNode && parentNode . nodeName === NodeName . OL ) {
29512977 ZSSEditor . setOrderedList ( ) ;
29522978 } else if ( parentNode && parentNode . nodeName === NodeName . UL ) {
29532979 ZSSEditor . setUnorderedList ( ) ;
29542980 }
2981+ // Exit blockquote when the user presses Enter inside a blockquote on a new line
2982+ // (main use case is to allow double Enter to exit blockquote)
2983+ } else if ( sel . isCollapsed && sel . baseOffset == 0 && parentNode && parentNode . nodeName == 'BLOCKQUOTE' ) {
2984+ e . preventDefault ( ) ;
2985+ ZSSEditor . setBlockquote ( ) ;
29552986 }
29562987 }
29572988 }
@@ -3211,7 +3242,7 @@ ZSSField.prototype.wrapCaretInParagraphIfNecessary = function()
32113242 if ( parentNodeShouldBeParagraph ) {
32123243 var selection = window . getSelection ( ) ;
32133244
3214- if ( selection ) {
3245+ if ( selection && selection . rangeCount > 0 ) {
32153246 var range = selection . getRangeAt ( 0 ) ;
32163247
32173248 if ( range . startContainer == range . endContainer ) {
0 commit comments