0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 07:13:08 -05:00

Fix copying plain text in Firefox

Resolves #244
This commit is contained in:
Neil Jenkins 2016-11-02 15:32:06 +11:00
parent e517f1505b
commit c656be7e65
3 changed files with 34 additions and 16 deletions

View file

@ -1770,7 +1770,7 @@ var styleToSemantic = {
}
},
fontWeight: {
regexp: /^bold/i,
regexp: /^bold|^700/i,
replace: function ( doc ) {
return createElement( doc, 'B' );
}
@ -1918,7 +1918,7 @@ var stylesRewriters = {
}
};
var allowedBlock = /^(?:A(?:DDRESS|RTICLE|SIDE|UDIO)|BLOCKQUOTE|CAPTION|D(?:[DLT]|IV)|F(?:IGURE|IGCAPTION|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|IGCAPTION|OOTER)|H[1-6]|HEADER|L(?:ABEL|EGEND|I)|O(?:L|UTPUT)|P(?:RE)?|SECTION|T(?:ABLE|BODY|D|FOOT|H|HEAD|R)|COL(?:GROUP)?|UL)$/;
var blacklist = /^(?:HEAD|META|STYLE)/;
@ -2096,6 +2096,19 @@ var cleanupBRs = function ( node, root ) {
}
};
// The (non-standard but supported enough) innerText property is based on the
// render tree in Firefox and possibly other browsers, so we must insert the
// DOM node into the document to ensure the text part is correct.
var setClipboardData = function ( clipboardData, node ) {
var body = node.ownerDocument.body;
node.setAttribute( 'style',
'position:fixed;overflow:hidden;bottom:100%;right:100%;' );
body.appendChild( node );
clipboardData.setData( 'text/html', node.innerHTML );
clipboardData.setData( 'text/plain', node.innerText || node.textContent );
body.removeChild( node );
};
var onCut = function ( event ) {
var clipboardData = event.clipboardData;
var range = this.getSelection();
@ -2112,9 +2125,7 @@ var onCut = function ( event ) {
if ( !isEdge && !isIOS && clipboardData ) {
moveRangeBoundariesUpTree( range, root );
node.appendChild( deleteContentsOfRange( range, root ) );
clipboardData.setData( 'text/html', node.innerHTML );
clipboardData.setData( 'text/plain',
node.innerText || node.textContent );
setClipboardData( clipboardData, node );
event.preventDefault();
} else {
setTimeout( function () {
@ -2160,9 +2171,7 @@ var onCopy = function ( event ) {
}
node.appendChild( contents );
clipboardData.setData( 'text/html', node.innerHTML );
clipboardData.setData( 'text/plain',
node.innerText || node.textContent );
setClipboardData( clipboardData, node );
event.preventDefault();
}
};

File diff suppressed because one or more lines are too long

View file

@ -1,5 +1,18 @@
/*jshint strict:false, undef:false, unused:false */
// The (non-standard but supported enough) innerText property is based on the
// render tree in Firefox and possibly other browsers, so we must insert the
// DOM node into the document to ensure the text part is correct.
var setClipboardData = function ( clipboardData, node ) {
var body = node.ownerDocument.body;
node.setAttribute( 'style',
'position:fixed;overflow:hidden;bottom:100%;right:100%;' );
body.appendChild( node );
clipboardData.setData( 'text/html', node.innerHTML );
clipboardData.setData( 'text/plain', node.innerText || node.textContent );
body.removeChild( node );
};
var onCut = function ( event ) {
var clipboardData = event.clipboardData;
var range = this.getSelection();
@ -16,9 +29,7 @@ var onCut = function ( event ) {
if ( !isEdge && !isIOS && clipboardData ) {
moveRangeBoundariesUpTree( range, root );
node.appendChild( deleteContentsOfRange( range, root ) );
clipboardData.setData( 'text/html', node.innerHTML );
clipboardData.setData( 'text/plain',
node.innerText || node.textContent );
setClipboardData( clipboardData, node );
event.preventDefault();
} else {
setTimeout( function () {
@ -64,9 +75,7 @@ var onCopy = function ( event ) {
}
node.appendChild( contents );
clipboardData.setData( 'text/html', node.innerHTML );
clipboardData.setData( 'text/plain',
node.innerText || node.textContent );
setClipboardData( clipboardData, node );
event.preventDefault();
}
};