| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- /**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import Matcher from '../../src/view/matcher';
- import Element from '../../src/view/element';
- describe( 'Matcher', () => {
- describe( 'add', () => {
- it( 'should allow to add pattern to matcher', () => {
- const matcher = new Matcher( 'div' );
- const el = new Element( 'p', { title: 'foobar' } );
- expect( matcher.match( el ) ).to.be.null;
- const pattern = { name: 'p', attribute: { title: 'foobar' } };
- matcher.add( pattern );
- const result = matcher.match( el );
- expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
- expect( result ).to.have.property( 'element' ).that.equal( el );
- expect( result ).to.have.property( 'match' ).that.has.property( 'name' ).that.is.true;
- expect( result.match ).to.have.property( 'attribute' ).that.is.an( 'array' );
- expect( result.match.attribute[ 0 ] ).to.equal( 'title' );
- expect( result ).to.be.an( 'object' );
- } );
- it( 'should allow to add more than one pattern', () => {
- const matcher = new Matcher();
- const el1 = new Element( 'p' );
- const el2 = new Element( 'div' );
- matcher.add( 'p', 'div' );
- let result = matcher.match( el1 );
- expect( result ).to.be.an( 'object' );
- expect( result ).to.have.property( 'element' ).that.equal( el1 );
- expect( result ).to.have.property( 'pattern' ).that.have.property( 'name' ).that.equal( 'p' );
- result = matcher.match( el2 );
- expect( result ).to.be.an( 'object' );
- expect( result ).to.have.property( 'element' ).that.equal( el2 );
- expect( result ).to.have.property( 'pattern' ).that.have.property( 'name' ).that.equal( 'div' );
- } );
- } );
- describe( 'match', () => {
- it( 'should match element name', () => {
- const matcher = new Matcher( 'p' );
- const el = new Element( 'p' );
- const el2 = new Element( 'div' );
- const result = matcher.match( el );
- expect( result ).to.be.an( 'object' );
- expect( result ).to.have.property( 'element' ).and.equal( el );
- expect( result ).to.have.property( 'pattern' );
- expect( result.pattern.name ).to.equal( 'p' );
- expect( result ).to.have.property( 'match' );
- expect( result.match.name ).to.be.true;
- expect( matcher.match( el2 ) ).to.be.null;
- } );
- it( 'should match element name with RegExp', () => {
- const pattern = /^text...a$/;
- const matcher = new Matcher( pattern );
- const el1 = new Element( 'textarea' );
- const el2 = new Element( 'div' );
- const result = matcher.match( el1 );
- expect( result ).to.be.an( 'object' );
- expect( result ).to.have.property( 'element' ).that.equal( el1 );
- expect( result ).to.have.property( 'pattern' ).that.has.property( 'name' ).that.equal( pattern );
- expect( result ).to.have.property( 'match' ).that.has.property( 'name' ).that.is.true;
- expect( matcher.match( el2 ) ).to.be.null;
- } );
- it( 'should match element attributes', () => {
- const pattern = {
- attribute: {
- title: 'foobar'
- }
- };
- const matcher = new Matcher( pattern );
- const el1 = new Element( 'p', { title: 'foobar' } );
- const el2 = new Element( 'p', { title: 'foobaz' } );
- const el3 = new Element( 'p', { name: 'foobar' } );
- const result = matcher.match( el1 );
- expect( result ).to.be.an( 'object' );
- expect( result ).to.have.property( 'element' ).and.equal( el1 );
- expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
- expect( result ).to.have.property( 'match' ).that.has.property( 'attribute' ).that.is.an( 'array' );
- expect( result.match.attribute[ 0 ] ).equal( 'title' );
- expect( matcher.match( el2 ) ).to.be.null;
- expect( matcher.match( el3 ) ).to.be.null;
- } );
- it( 'should match element attributes using RegExp', () => {
- const pattern = {
- attribute: {
- title: /fooba./
- }
- };
- const matcher = new Matcher( pattern );
- const el1 = new Element( 'p', { title: 'foobar' } );
- const el2 = new Element( 'p', { title: 'foobaz' } );
- const el3 = new Element( 'p', { title: 'qux' } );
- let result = matcher.match( el1 );
- expect( result ).to.be.an( 'object' );
- expect( result ).to.have.property( 'element' ).that.equal( el1 );
- expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
- expect( result ).to.have.property( 'match' ).that.has.property( 'attribute' ).that.is.an( 'array' );
- expect( result.match.attribute[ 0 ] ).equal( 'title' );
- result = matcher.match( el2 );
- expect( result ).to.be.an( 'object' );
- expect( result ).to.have.property( 'element' ).that.equal( el2 );
- expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
- expect( result ).to.have.property( 'match' ).that.has.property( 'attribute' ).that.is.an( 'array' );
- expect( result.match.attribute[ 0 ] ).equal( 'title' );
- expect( matcher.match( el3 ) ).to.be.null;
- } );
- it( 'should match if element has given attribute', () => {
- const pattern = {
- attribute: {
- title: true
- }
- };
- const matcher = new Matcher( pattern );
- const el1 = new Element( 'p', { title: 'foobar' } );
- const el2 = new Element( 'p', { title: '' } );
- const el3 = new Element( 'p' );
- let result = matcher.match( el1 );
- expect( result ).to.be.an( 'object' );
- expect( result ).to.have.property( 'element' ).that.equal( el1 );
- expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
- expect( result ).to.have.property( 'match' ).that.has.property( 'attribute' ).that.is.an( 'array' );
- expect( result.match.attribute[ 0 ] ).equal( 'title' );
- result = matcher.match( el2 );
- expect( result ).to.be.an( 'object' );
- expect( result ).to.have.property( 'element' ).that.equal( el2 );
- expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
- expect( result ).to.have.property( 'match' ).that.has.property( 'attribute' ).that.is.an( 'array' );
- expect( result.match.attribute[ 0 ] ).equal( 'title' );
- expect( matcher.match( el3 ) ).to.be.null;
- } );
- it( 'should match element class names', () => {
- const pattern = { class: 'foobar' };
- const matcher = new Matcher( pattern );
- const el1 = new Element( 'p', { class: 'foobar' } );
- const result = matcher.match( el1 );
- expect( result ).to.be.an( 'object' );
- expect( result ).to.have.property( 'element' ).that.equal( el1 );
- expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
- expect( result ).to.have.property( 'match' ).that.has.property( 'class' ).that.is.an( 'array' );
- expect( result.match.class[ 0 ] ).equal( 'foobar' );
- expect( new Matcher( { class: 'baz' } ).match( el1 ) ).to.be.null;
- } );
- it( 'should match element class names using RegExp', () => {
- const pattern = { class: /fooba./ };
- const matcher = new Matcher( pattern );
- const el1 = new Element( 'p', { class: 'foobar' } );
- const el2 = new Element( 'p', { class: 'foobaz' } );
- const el3 = new Element( 'p', { class: 'qux' } );
- let result = matcher.match( el1 );
- expect( result ).to.be.an( 'object' );
- expect( result ).to.have.property( 'element' ).that.equal( el1 );
- expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
- expect( result ).to.have.property( 'match' ).that.has.property( 'class' ).that.is.an( 'array' );
- expect( result.match.class[ 0 ] ).equal( 'foobar' );
- result = matcher.match( el2 );
- expect( result ).to.be.an( 'object' );
- expect( result ).to.have.property( 'element' ).that.equal( el2 );
- expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
- expect( result ).to.have.property( 'match' ).that.has.property( 'class' ).that.is.an( 'array' );
- expect( result.match.class[ 0 ] ).equal( 'foobaz' );
- expect( matcher.match( el3 ) ).to.be.null;
- } );
- it( 'should match element styles', () => {
- const pattern = {
- style: {
- color: 'red'
- }
- };
- const matcher = new Matcher( pattern );
- const el1 = new Element( 'p', { style: 'color: red' } );
- const el2 = new Element( 'p', { style: 'position: absolute' } );
- const result = matcher.match( el1 );
- expect( result ).to.be.an( 'object' );
- expect( result ).to.have.property( 'element' ).that.equal( el1 );
- expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
- expect( result ).to.have.property( 'match' ).that.has.property( 'style' ).that.is.an( 'array' );
- expect( result.match.style[ 0 ] ).equal( 'color' );
- expect( matcher.match( el2 ) ).to.be.null;
- expect( new Matcher( { style: { color: 'blue' } } ).match( el1 ) ).to.be.null;
- } );
- it( 'should match element styles using RegExp', () => {
- const pattern = {
- style: {
- color: /^.*blue$/
- }
- };
- const matcher = new Matcher( pattern );
- const el1 = new Element( 'p', { style: 'color: blue' } );
- const el2 = new Element( 'p', { style: 'color: darkblue' } );
- const el3 = new Element( 'p', { style: 'color: red' } );
- let result = matcher.match( el1 );
- expect( result ).to.be.an( 'object' );
- expect( result ).to.have.property( 'element' ).that.equal( el1 );
- expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
- expect( result ).to.have.property( 'match' ).that.has.property( 'style' ).that.is.an( 'array' );
- expect( result.match.style[ 0 ] ).to.equal( 'color' );
- result = matcher.match( el2 );
- expect( result ).to.be.an( 'object' );
- expect( result ).to.have.property( 'element' ).that.equal( el2 );
- expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
- expect( result ).to.have.property( 'match' ).that.has.property( 'style' ).that.is.an( 'array' );
- expect( result.match.style[ 0 ] ).to.equal( 'color' );
- expect( matcher.match( el3 ) ).to.be.null;
- } );
- it( 'should allow to use function as a pattern', () => {
- const match = { name: true };
- const pattern = element => {
- if ( element.name === 'div' && element.childCount > 0 ) {
- return match;
- }
- return null;
- };
- const matcher = new Matcher( pattern );
- const el1 = new Element( 'p' );
- const el2 = new Element( 'div', null, [ el1 ] );
- expect( matcher.match( el1 ) ).to.be.null;
- const result = matcher.match( el2 );
- expect( result ).to.be.an( 'object' );
- expect( result ).to.have.property( 'element' ).that.equal( el2 );
- expect( result ).to.have.property( 'match' ).that.equal( match );
- } );
- it( 'should return first matched element', () => {
- const pattern = {
- name: 'p'
- };
- const el1 = new Element( 'div' );
- const el2 = new Element( 'p' );
- const el3 = new Element( 'span' );
- const matcher = new Matcher( pattern );
- const result = matcher.match( el1, el2, el3 );
- expect( result ).to.be.an( 'object' );
- expect( result ).to.have.property( 'element' ).that.equal( el2 );
- expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
- expect( result ).to.have.property( 'match' ).that.is.an( 'object' );
- expect( result.match ).to.have.property( 'name' ).that.is.true;
- } );
- it( 'should match multiple attributes', () => {
- const pattern = {
- name: 'a',
- attribute: {
- name: 'foo',
- title: 'bar'
- }
- };
- const matcher = new Matcher( pattern );
- const el = new Element( 'a', {
- name: 'foo',
- title: 'bar'
- } );
- const result = matcher.match( el );
- expect( result ).to.be.an( 'object' );
- expect( result ).to.have.property( 'element' ).that.equal( el );
- expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
- expect( result ).to.have.property( 'match' ).that.is.an( 'object' );
- expect( result.match ).to.have.property( 'attribute' ).that.is.an( 'array' );
- expect( result.match.attribute[ 0 ] ).to.equal( 'name' );
- expect( result.match.attribute[ 1 ] ).to.equal( 'title' );
- } );
- it( 'should match multiple classes', () => {
- const pattern = {
- name: 'a',
- class: [ 'foo', 'bar' ]
- };
- const matcher = new Matcher( pattern );
- const el = new Element( 'a' );
- el._addClass( [ 'foo', 'bar', 'baz' ] );
- const result = matcher.match( el );
- expect( result ).to.be.an( 'object' );
- expect( result ).to.have.property( 'element' ).that.equal( el );
- expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
- expect( result ).to.have.property( 'match' ).that.is.an( 'object' );
- expect( result.match ).to.have.property( 'class' ).that.is.an( 'array' );
- expect( result.match.class[ 0 ] ).to.equal( 'foo' );
- expect( result.match.class[ 1 ] ).to.equal( 'bar' );
- } );
- it( 'should match multiple styles', () => {
- const pattern = {
- name: 'a',
- style: {
- color: 'red',
- position: 'relative'
- }
- };
- const matcher = new Matcher( pattern );
- const el = new Element( 'a' );
- el._setStyle( {
- color: 'red',
- position: 'relative'
- } );
- const result = matcher.match( el );
- expect( result ).to.be.an( 'object' );
- expect( result ).to.have.property( 'element' ).that.equal( el );
- expect( result ).to.have.property( 'pattern' ).that.equal( pattern );
- expect( result ).to.have.property( 'match' ).that.is.an( 'object' );
- expect( result.match ).to.have.property( 'style' ).that.is.an( 'array' );
- expect( result.match.style[ 0 ] ).to.equal( 'color' );
- expect( result.match.style[ 1 ] ).to.equal( 'position' );
- } );
- } );
- describe( 'matchAll', () => {
- it( 'should return all matched elements with correct patterns', () => {
- const matcher = new Matcher( 'p', 'div' );
- const el1 = new Element( 'p' );
- const el2 = new Element( 'div' );
- const el3 = new Element( 'span' );
- const result = matcher.matchAll( el1, el2, el3 );
- expect( result ).to.be.an( 'array' );
- expect( result.length ).to.equal( 2 );
- expect( result[ 0 ] ).to.have.property( 'element' ).that.equal( el1 );
- expect( result[ 0 ] ).to.have.property( 'pattern' ).that.is.an( 'object' );
- expect( result[ 0 ].pattern ).to.have.property( 'name' ).that.is.equal( 'p' );
- expect( result[ 0 ] ).to.have.property( 'match' ).that.is.an( 'object' );
- expect( result[ 0 ].match ).to.have.property( 'name' ).that.is.true;
- expect( result[ 1 ] ).to.have.property( 'element' ).that.equal( el2 );
- expect( result[ 1 ] ).to.have.property( 'pattern' ).that.is.an( 'object' );
- expect( result[ 1 ].pattern ).to.have.property( 'name' ).that.is.equal( 'div' );
- expect( result[ 1 ] ).to.have.property( 'match' ).that.is.an( 'object' );
- expect( result[ 1 ].match ).to.have.property( 'name' ).that.is.true;
- expect( matcher.matchAll( el3 ) ).to.be.null;
- } );
- it( 'should return all matched elements when using RegExp pattern', () => {
- const pattern = { class: /^red-.*/ };
- const matcher = new Matcher( pattern );
- const el1 = new Element( 'p' );
- const el2 = new Element( 'p' );
- const el3 = new Element( 'p' );
- el1._addClass( 'red-foreground' );
- el2._addClass( 'red-background' );
- el3._addClass( 'blue-text' );
- const result = matcher.matchAll( el1, el2, el3 );
- expect( result ).to.be.an( 'array' );
- expect( result.length ).to.equal( 2 );
- expect( result[ 0 ] ).to.have.property( 'element' ).that.equal( el1 );
- expect( result[ 0 ] ).to.have.property( 'pattern' ).that.is.equal( pattern );
- expect( result[ 0 ] ).to.have.property( 'match' ).that.is.an( 'object' );
- expect( result[ 0 ].match ).to.have.property( 'class' ).that.is.an( 'array' );
- expect( result[ 0 ].match.class[ 0 ] ).to.equal( 'red-foreground' );
- expect( result[ 1 ] ).to.have.property( 'element' ).that.equal( el2 );
- expect( result[ 1 ] ).to.have.property( 'pattern' ).that.is.equal( pattern );
- expect( result[ 1 ] ).to.have.property( 'match' ).that.is.an( 'object' );
- expect( result[ 1 ].match ).to.have.property( 'class' ).that.is.an( 'array' );
- expect( result[ 1 ].match.class[ 0 ] ).to.equal( 'red-background' );
- expect( matcher.matchAll( el3 ) ).to.be.null;
- } );
- } );
- describe( 'getElementName', () => {
- it( 'should return null if there are no patterns in the matcher instance', () => {
- const matcher = new Matcher();
- expect( matcher.getElementName() ).to.be.null;
- } );
- it( 'should return null if pattern has no name property', () => {
- const matcher = new Matcher( { class: 'foo' } );
- expect( matcher.getElementName() ).to.be.null;
- } );
- it( 'should return null if pattern has name property specified as RegExp', () => {
- const matcher = new Matcher( { name: /foo.*/ } );
- expect( matcher.getElementName() ).to.be.null;
- } );
- it( 'should return element name if matcher has one patter with name property specified as string', () => {
- const matcher = new Matcher( { name: 'div' } );
- expect( matcher.getElementName() ).to.equal( 'div' );
- } );
- it( 'should return null if matcher has more than one pattern', () => {
- const matcher = new Matcher( { name: 'div' }, { class: 'foo' } );
- expect( matcher.getElementName() ).to.be.null;
- } );
- it( 'should return null for matching function', () => {
- const matcher = new Matcher( () => {} );
- expect( matcher.getElementName() ).to.be.null;
- } );
- it( 'should return null for matching named function', () => {
- const matcher = new Matcher( function matchFunction() {} );
- expect( matcher.getElementName() ).to.be.null;
- } );
- } );
- } );
|