0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2025-01-03 05:00:13 -05:00

Use default block settings when fixCursor called on body

This commit is contained in:
Neil Jenkins 2015-03-29 18:35:03 +11:00
parent 61d5004373
commit 3c8eaa1fcc
4 changed files with 46 additions and 32 deletions

View file

@ -291,12 +291,14 @@ function fixCursor ( node ) {
// cursor to appear.
var doc = node.ownerDocument,
root = node,
fixer, child,
l, instance;
fixer, child, instance;
if ( node.nodeName === 'BODY' ) {
if ( !( child = node.firstChild ) || child.nodeName === 'BR' ) {
fixer = doc.createElement( 'DIV' );
instance = getSquireInstance( doc );
fixer = instance ?
instance.createDefaultBlock() :
createElement( doc, 'DIV' );
if ( child ) {
node.replaceChild( fixer, child );
}
@ -318,14 +320,7 @@ function fixCursor ( node ) {
if ( !child ) {
if ( cantFocusEmptyTextNodes ) {
fixer = doc.createTextNode( ZWS );
// Find the relevant Squire instance and notify
l = instances.length;
while ( l-- ) {
instance = instances[l];
if ( instance._doc === doc ) {
instance._didAddZWS();
}
}
getSquireInstance( doc )._didAddZWS();
} else {
fixer = doc.createTextNode( '' );
}
@ -351,7 +346,7 @@ function fixCursor ( node ) {
}
}
else if ( !node.querySelector( 'BR' ) ) {
fixer = doc.createElement( 'BR' );
fixer = createElement( doc, 'BR' );
while ( ( child = node.lastElementChild ) && !isInline( child ) ) {
node = child;
}
@ -566,7 +561,7 @@ function mergeContainers ( node ) {
if ( prev && areAlike( prev, node ) ) {
if ( !isContainer( prev ) ) {
if ( isListItem ) {
block = doc.createElement( 'DIV' );
block = createElement( doc, 'DIV' );
block.appendChild( empty( prev ) );
prev.appendChild( block );
} else {
@ -583,7 +578,7 @@ function mergeContainers ( node ) {
mergeContainers( first );
}
} else if ( isListItem ) {
prev = doc.createElement( 'DIV' );
prev = createElement( doc, 'DIV' );
node.insertBefore( prev, first );
fixCursor( prev );
}
@ -810,7 +805,7 @@ var insertTreeFragmentIntoRange = function ( range, frag ) {
deleteContentsOfRange( range );
}
// Move range down into text ndoes
// Move range down into text nodes
moveRangeBoundariesDownTree( range );
// If inline, just insert at the current position.
@ -1097,6 +1092,18 @@ var expandRangeToBlockBoundaries = function ( range ) {
var instances = [];
function getSquireInstance ( doc ) {
var l = instances.length,
instance;
while ( l-- ) {
instance = instances[l];
if ( instance._doc === doc ) {
return instance;
}
}
return null;
}
function Squire ( doc ) {
var win = doc.defaultView;
var body = doc.body;
@ -2213,7 +2220,7 @@ var addLinks = function ( frag ) {
}
};
var allowedBlock = /^(?:A(?:DDRESS|RTICLE|SIDE)|BLOCKQUOTE|CAPTION|D(?:[DLT]|IV)|F(?:IGURE|OOTER)|H[1-6]|HEADER|L(?:ABEL|EGEND|I)|O(?:L|UTPUT)|P(?:RE)?|SECTION|T(?:ABLE|BODY|D|FOOT|H|HEAD|R)|UL)$/;
var allowedBlock = /^(?:A(?:DDRESS|RTICLE|SIDE|UDIO)|BLOCKQUOTE|CAPTION|D(?:[DLT]|IV)|F(?:IGURE|OOTER)|H[1-6]|HEADER|L(?:ABEL|EGEND|I)|O(?:L|UTPUT)|P(?:RE)?|SECTION|T(?:ABLE|BODY|D|FOOT|H|HEAD|R)|UL)$/;
var fontSizes = {
1: 10,

File diff suppressed because one or more lines are too long

View file

@ -2,6 +2,18 @@
var instances = [];
function getSquireInstance ( doc ) {
var l = instances.length,
instance;
while ( l-- ) {
instance = instances[l];
if ( instance._doc === doc ) {
return instance;
}
}
return null;
}
function Squire ( doc ) {
var win = doc.defaultView;
var body = doc.body;

View file

@ -160,12 +160,14 @@ function fixCursor ( node ) {
// cursor to appear.
var doc = node.ownerDocument,
root = node,
fixer, child,
l, instance;
fixer, child, instance;
if ( node.nodeName === 'BODY' ) {
if ( !( child = node.firstChild ) || child.nodeName === 'BR' ) {
fixer = doc.createElement( 'DIV' );
instance = getSquireInstance( doc );
fixer = instance ?
instance.createDefaultBlock() :
createElement( doc, 'DIV' );
if ( child ) {
node.replaceChild( fixer, child );
}
@ -187,14 +189,7 @@ function fixCursor ( node ) {
if ( !child ) {
if ( cantFocusEmptyTextNodes ) {
fixer = doc.createTextNode( ZWS );
// Find the relevant Squire instance and notify
l = instances.length;
while ( l-- ) {
instance = instances[l];
if ( instance._doc === doc ) {
instance._didAddZWS();
}
}
getSquireInstance( doc )._didAddZWS();
} else {
fixer = doc.createTextNode( '' );
}
@ -220,7 +215,7 @@ function fixCursor ( node ) {
}
}
else if ( !node.querySelector( 'BR' ) ) {
fixer = doc.createElement( 'BR' );
fixer = createElement( doc, 'BR' );
while ( ( child = node.lastElementChild ) && !isInline( child ) ) {
node = child;
}
@ -435,7 +430,7 @@ function mergeContainers ( node ) {
if ( prev && areAlike( prev, node ) ) {
if ( !isContainer( prev ) ) {
if ( isListItem ) {
block = doc.createElement( 'DIV' );
block = createElement( doc, 'DIV' );
block.appendChild( empty( prev ) );
prev.appendChild( block );
} else {
@ -452,7 +447,7 @@ function mergeContainers ( node ) {
mergeContainers( first );
}
} else if ( isListItem ) {
prev = doc.createElement( 'DIV' );
prev = createElement( doc, 'DIV' );
node.insertBefore( prev, first );
fixCursor( prev );
}