Skip to content

Commit

Permalink
Test (engine): test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
illia-stv committed Aug 30, 2023
1 parent 7b953f1 commit 95afdeb
Showing 1 changed file with 11 additions and 71 deletions.
82 changes: 11 additions & 71 deletions packages/ckeditor5-engine/tests/view/observer/selectionobserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ import SelectionObserver from '../../../src/view/observer/selectionobserver';
import FocusObserver from '../../../src/view/observer/focusobserver';
import MutationObserver from '../../../src/view/observer/mutationobserver';
import createViewRoot from '../_utils/createroot';
import { parse, stringify } from '../../../src/dev-utils/view';
import { parse } from '../../../src/dev-utils/view';
import { StylesProcessor } from '../../../src/view/stylesmap';
import createElement from '@ckeditor/ckeditor5-utils/src/dom/createelement';
import env from '@ckeditor/ckeditor5-utils/src/env';

describe( 'SelectionObserver', () => {
Expand Down Expand Up @@ -162,81 +161,23 @@ 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', () => {
testUtils.sinon.stub( env, 'isGecko' ).value( true );

const domFoo = document.createTextNode( 'foo' );
const domP = createElement( document, 'p', null, [ domFoo ] );
const converter = view.domConverter;

const viewP = parse( '<p>foo</p>' );

converter.bindElements( domP, viewP );

document.body.appendChild( domP );

const domRange = document.createRange();
domRange.setStart( domFoo, 1 );
domRange.setEnd( domFoo, 2 );

const domSelection = document.getSelection();
domSelection.removeAllRanges();
domSelection.addRange( domRange );

const viewSelection = converter.domSelectionToView( domSelection );

expect( viewSelection.rangeCount ).to.equal( 1 );
expect( stringify( viewP, viewSelection.getFirstRange() ) ).to.equal( '<p>f{o}o</p>' );

// Now we know that there should be a valid view range. So let's test if the DOM node throws an error.
sinon.stub( domFoo, Symbol.toStringTag ).get( () => {
throw new Error( 'Permission denied to access property Symbol.toStringTag' );
} );
it( 'should detect "restricted objects" in Firefox DOM ranges and prevent an error being thrown', () => {
testUtils.sinon.stub( env, 'isGecko' ).value( true );

let result = null;
changeDomSelection();
domDocument.dispatchEvent( new Event( 'selectionchange' ) );

expect( () => {
result = converter.domSelectionToView( domSelection );
} ).to.not.throw();
expect( view.hasDomSelection ).to.be.true;

expect( result instanceof ViewSelection ).to.be.true;
expect( result.rangeCount ).to.equal( 0 );
const domFoo = domDocument.getSelection().anchorNode;

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

it( 'should do nothing in Firefox if the DOM selection is correct', done => {
const domFoo = document.createTextNode( 'foo' );
const domP = createElement( document, 'p', null, [ domFoo ] );
const converter = view.domConverter;

const viewP = parse( '<p>foo</p>' );

converter.bindElements( domP, viewP );

document.body.appendChild( domP );

const domRange = document.createRange();
domRange.setStart( domFoo, 1 );
domRange.setEnd( domFoo, 2 );

const domSelection = document.getSelection();
domSelection.removeAllRanges();
domSelection.addRange( domRange );

expect( () => {
converter.domSelectionToView( domSelection );

setTimeout( done, 100 );
} ).to.not.throw();
domDocument.dispatchEvent( new Event( 'selectionchange' ) );

domP.remove();
} );
expect( view.hasDomSelection ).to.be.false;
} );

it( 'should add only one #selectionChange listener to one document', done => {
Expand Down Expand Up @@ -547,7 +488,6 @@ describe( 'SelectionObserver', () => {
selectionObserver.listenTo( domMain, 'selectstart', selectStartChangedSpy, { priority: 'highest' } );

// The event was fired somewhere else in DOM.
domDocument.dispatchEvent( new Event( 'selectstart' ) );

expect( viewDocument.isSelecting ).to.be.false;
sinon.assert.notCalled( selectStartChangedSpy );
Expand Down

0 comments on commit 95afdeb

Please sign in to comment.