+ // Edge case: show the fake caret when the editor is focused for the first time
+ // and the first element is a view.
+ if ( firstFocus && ( view = getView( editor.getBody().firstChild ) ) ) {
+ setViewCursor( true, view );
+ }
+
+ firstFocus = false;
+ } );
+
+ editor.on( 'blur', function() {
+ focus = false;
+ editor.dom.removeClass( editor.getBody(), 'has-focus' );
+ } );
+
+ editor.on( 'NodeChange', function( event ) {
+ var dom = editor.dom,
+ views = editor.dom.select( '.wpview-wrap' ),
+ className = event.element.className,
+ view = getView( event.element ),
+ lKDN = lastKeyDownNode;
+
+ lastKeyDownNode = false;
+
+ clearInterval( cursorInterval );
+
+ // This runs a lot and is faster than replacing each class separately
+ tinymce.each( views, function ( view ) {
+ if ( view.className ) {
+ view.className = view.className.replace( / ?\bwpview-(?:selection-before|selection-after|cursor-hide)\b/g, '' );
+ }
+ });
+
+ if ( focus && view ) {
+ if ( ( className === 'wpview-selection-before' || className === 'wpview-selection-after' ) &&
+ editor.selection.isCollapsed() ) {
+
+ setViewCursorTries = 0;
+
+ deselect();
+
+ // Make sure the cursor arrived in the right node.
+ // This is necessary for Firefox.
+ if ( lKDN === view.previousSibling ) {
+ setViewCursor( true, view );
+ return;
+ } else if ( lKDN === view.nextSibling ) {
+ setViewCursor( false, view );
+ return;