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 9 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
29 changes: 28 additions & 1 deletion packages/ckeditor5-engine/src/view/domconverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import {
isText,
isComment,
isValidAttributeName,
first
first,
env
} from '@ckeditor/ckeditor5-utils';

import type ViewNode from './node';
Expand Down Expand Up @@ -732,6 +733,11 @@ export default class DomConverter {
* @returns View selection.
*/
public domSelectionToView( domSelection: DomSelection ): ViewSelection {
// https://github.com/ckeditor/ckeditor5/issues/9635
illia-stv marked this conversation as resolved.
Show resolved Hide resolved
if ( isGeckoRestrictedDomSelection( domSelection ) ) {
return new ViewSelection( [] );
}

illia-stv marked this conversation as resolved.
Show resolved Hide resolved
// DOM selection might be placed in fake selection container.
// If container contains fake selection - return corresponding view selection.
if ( domSelection.rangeCount === 1 ) {
Expand Down Expand Up @@ -1787,6 +1793,27 @@ function _logUnsafeElement( elementName: string ): void {
}
}

/**
* In 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
illia-stv marked this conversation as resolved.
Show resolved Hide resolved
*/
function isGeckoRestrictedDomSelection( domSelection: DomSelection ): boolean {
if ( !env.isGecko || !domSelection.rangeCount ) {
return false;
}

try {
const container = domSelection.getRangeAt( 0 ).startContainer;
illia-stv marked this conversation as resolved.
Show resolved Hide resolved

Object.prototype.toString.call( container );
illia-stv marked this conversation as resolved.
Show resolved Hide resolved
} catch ( error ) {
return true;
}

return false;
}

/**
* Enum representing the type of the block filler.
*
Expand Down
19 changes: 19 additions & 0 deletions packages/ckeditor5-engine/tests/view/domconverter/dom-to-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { parse, stringify } from '../../../src/dev-utils/view';
import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
import count from '@ckeditor/ckeditor5-utils/src/count';
import createElement from '@ckeditor/ckeditor5-utils/src/dom/createelement';
import env from '@ckeditor/ckeditor5-utils/src/env';

describe( 'DomConverter', () => {
let converter, viewDocument;
Expand Down Expand Up @@ -1287,5 +1288,23 @@ describe( 'DomConverter', () => {

domContainer.remove();
} );

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

testUtils.sinon.stub( env, 'isGecko' ).value( true );

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

const domSelection = document.getSelection();

testUtils.sinon.stub( domSelection, 'getRangeAt' ).withArgs( 0 ).returns( domRangeStubWithRestrictedObject );

expect( () => {
converter.domSelectionToView( domSelection );
} ).to.not.throw();
} );
} );
} );
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