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 1 commit
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
16 changes: 11 additions & 5 deletions packages/ckeditor5-engine/src/view/domconverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,7 @@ 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( [] );
}
Expand Down Expand Up @@ -1792,17 +1793,22 @@ 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
/**
* 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 ) {
return false;
}

try {
const container = domSelection.getRangeAt( 0 ).startContainer;
Object.prototype.toString.call( container );
if ( domSelection.rangeCount ) {
illia-stv marked this conversation as resolved.
Show resolved Hide resolved
const container = domSelection.getRangeAt( 0 ).startContainer;

Object.prototype.toString.call( container );
}
} catch ( error ) {
return true;
}
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();
} );
} );
} );