Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix (engine): detecting restricted objects in firefox #14706

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/ckeditor5-engine/src/view/observer/selectionobserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ export default class SelectionObserver extends Observer {

const domSelection = domDocument.defaultView!.getSelection()!;

if ( isGeckoRestrictedDomSelection( domSelection ) ) {
return;
}
illia-stv marked this conversation as resolved.
Show resolved Hide resolved

if ( this.checkShouldIgnoreEventFromTarget( domSelection.anchorNode! ) ) {
return;
}
Expand Down Expand Up @@ -363,3 +367,21 @@ export type ViewDocumentSelectionChangeDoneEvent = {
name: 'selectionChangeDone';
args: [ ViewDocumentSelectionEventData ];
};

// I certain cases, Firefox mysteriously assigns so called "restricted objects" to native DOM Range properties.
// Any attempt at accessing restricted object's properties causes errors.
// https://github.com/ckeditor/ckeditor5/issues/9635
function isGeckoRestrictedDomSelection( domSelection: DomSelection ): boolean {
if ( !env.isGecko ) {
return false;
}

try {
const container = domSelection.getRangeAt( 0 ).startContainer;
Object.prototype.toString.call( container );
} catch ( error ) {
return true;
}

return false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

/* globals setTimeout, document, console, Event */
/* globals setTimeout, document, console, Event, Selection */

import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';

Expand Down Expand Up @@ -162,6 +162,34 @@ describe( 'SelectionObserver', () => {
changeDomSelection();
} );

describe( 'Restricted objects handling in Gecko', () => {
beforeEach( () => {
testUtils.sinon.stub( env, 'isGecko' ).value( true );
} );

it( 'should detect "restricted objects" in Firefox DOM ranges and prevent an error being thrown', done => {
const domRangeStubWithRestrictedObject = domDocument.createRange();

sinon.stub( domRangeStubWithRestrictedObject, 'startContainer' ).get( () => {
throw new Error( 'Permission denied to access property Symbol.toStringTag' );
} );

testUtils.sinon.stub( Selection.prototype, 'getRangeAt' ).returns( domRangeStubWithRestrictedObject );

expect( () => {
changeDomSelection();
setTimeout( done, 100 );
} ).to.not.throw();
} );

it( 'should do nothing in Firefox if the DOM selection is correct', done => {
expect( () => {
changeDomSelection();
setTimeout( done, 100 );
} ).to.not.throw();
} );
} );

it( 'should add only one #selectionChange listener to one document', done => {
// Add second roots to ensure that listener is added once.
createViewRoot( viewDocument, 'div', 'additional' );
Expand Down