Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Matcher tests pass.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamil Piechaczek committed Mar 28, 2018
1 parent 31e71fb commit bcbfc34
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 59 deletions.
12 changes: 6 additions & 6 deletions src/view/matcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ export default class Matcher {
*
* // Single class.
* matcher.add( {
* class: 'foobar'
* classes: 'foobar'
* } );
*
* See {@link module:engine/view/matcher~MatcherPattern} for more examples.
*
* Multiple patterns can be added in one call:
*
* matcher.add( 'div', { class: 'foobar' } );
* matcher.add( 'div', { classes: 'foobar' } );
*
* @param {Object|String|RegExp|Function} pattern Object describing pattern details. If string or regular expression
* is provided it will be used to match element's name. Pattern can be also provided in a form
Expand All @@ -59,7 +59,7 @@ export default class Matcher {
* * `true` - then attribute is just required (can be empty),
* * a string - then attribute has to be equal, or
* * a regular expression - then attribute has to match the expression.
* @param {String|RegExp|Array} [pattern.class] Class name or array of class names to match. Each name can be
* @param {String|RegExp|Array} [pattern.classes] Class name or array of class names to match. Each name can be
* provided in a form of string or regular expression.
* @param {Object} [pattern.style] Object with key-value pairs representing styles to match. Each object key
* represents style name. Value under that key can be either a string or a regular expression and it will be used
Expand Down Expand Up @@ -106,9 +106,9 @@ export default class Matcher {
* @returns {Object|String|RegExp|Function} result.pattern Pattern that was used to find matched element.
* @returns {Object} result.match Object representing matched element parts.
* @returns {Boolean} [result.match.name] True if name of the element was matched.
* @returns {Array} [result.match.attribute] Array with matched attribute names.
* @returns {Array} [result.match.class] Array with matched class names.
* @returns {Array} [result.match.style] Array with matched style names.
* @returns {Array} [result.match.attributes] Array with matched attribute names.
* @returns {Array} [result.match.classes] Array with matched class names.
* @returns {Array} [result.match.styles] Array with matched style names.
*/
match( ...element ) {
for ( const singleElement of element ) {
Expand Down
106 changes: 53 additions & 53 deletions tests/view/matcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ describe( 'Matcher', () => {
const el = new Element( 'p', { title: 'foobar' } );

expect( matcher.match( el ) ).to.be.null;
const pattern = { name: 'p', attribute: { title: 'foobar' } };
const pattern = { name: 'p', attributes: { 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.match ).to.have.property( 'attributes' ).that.is.an( 'array' );
expect( result.match.attributes[ 0 ] ).to.equal( 'title' );
expect( result ).to.be.an( 'object' );
} );

Expand Down Expand Up @@ -77,7 +77,7 @@ describe( 'Matcher', () => {

it( 'should match element attributes', () => {
const pattern = {
attribute: {
attributes: {
title: 'foobar'
}
};
Expand All @@ -92,16 +92,16 @@ describe( 'Matcher', () => {
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 ).to.have.property( 'match' ).that.has.property( 'attributes' ).that.is.an( 'array' );

expect( result.match.attribute[ 0 ] ).equal( 'title' );
expect( result.match.attributes[ 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: {
attributes: {
title: /fooba./
}
};
Expand All @@ -114,21 +114,21 @@ describe( 'Matcher', () => {
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' );
expect( result ).to.have.property( 'match' ).that.has.property( 'attributes' ).that.is.an( 'array' );
expect( result.match.attributes[ 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( result ).to.have.property( 'match' ).that.has.property( 'attributes' ).that.is.an( 'array' );
expect( result.match.attributes[ 0 ] ).equal( 'title' );
expect( matcher.match( el3 ) ).to.be.null;
} );

it( 'should match if element has given attribute', () => {
const pattern = {
attribute: {
attributes: {
title: true
}
};
Expand All @@ -141,35 +141,35 @@ describe( 'Matcher', () => {
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' );
expect( result ).to.have.property( 'match' ).that.has.property( 'attributes' ).that.is.an( 'array' );
expect( result.match.attributes[ 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( result ).to.have.property( 'match' ).that.has.property( 'attributes' ).that.is.an( 'array' );
expect( result.match.attributes[ 0 ] ).equal( 'title' );

expect( matcher.match( el3 ) ).to.be.null;
} );

it( 'should match element class names', () => {
const pattern = { class: 'foobar' };
const pattern = { classes: '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;
expect( result ).to.have.property( 'match' ).that.has.property( 'classes' ).that.is.an( 'array' );
expect( result.match.classes[ 0 ] ).equal( 'foobar' );
expect( new Matcher( { classes: 'baz' } ).match( el1 ) ).to.be.null;
} );

it( 'should match element class names using RegExp', () => {
const pattern = { class: /fooba./ };
const pattern = { classes: /fooba./ };
const matcher = new Matcher( pattern );
const el1 = new Element( 'p', { class: 'foobar' } );
const el2 = new Element( 'p', { class: 'foobaz' } );
Expand All @@ -179,21 +179,21 @@ describe( 'Matcher', () => {
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( result ).to.have.property( 'match' ).that.has.property( 'classes' ).that.is.an( 'array' );
expect( result.match.classes[ 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( result ).to.have.property( 'match' ).that.has.property( 'classes' ).that.is.an( 'array' );
expect( result.match.classes[ 0 ] ).equal( 'foobaz' );
expect( matcher.match( el3 ) ).to.be.null;
} );

it( 'should match element styles', () => {
const pattern = {
style: {
styles: {
color: 'red'
}
};
Expand All @@ -205,15 +205,15 @@ describe( 'Matcher', () => {
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( result ).to.have.property( 'match' ).that.has.property( 'styles' ).that.is.an( 'array' );
expect( result.match.styles[ 0 ] ).equal( 'color' );
expect( matcher.match( el2 ) ).to.be.null;
expect( new Matcher( { style: { color: 'blue' } } ).match( el1 ) ).to.be.null;
expect( new Matcher( { styles: { color: 'blue' } } ).match( el1 ) ).to.be.null;
} );

it( 'should match element styles using RegExp', () => {
const pattern = {
style: {
styles: {
color: /^.*blue$/
}
};
Expand All @@ -226,15 +226,15 @@ describe( 'Matcher', () => {
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' );
expect( result ).to.have.property( 'match' ).that.has.property( 'styles' ).that.is.an( 'array' );
expect( result.match.styles[ 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( result ).to.have.property( 'match' ).that.has.property( 'styles' ).that.is.an( 'array' );
expect( result.match.styles[ 0 ] ).to.equal( 'color' );
expect( matcher.match( el3 ) ).to.be.null;
} );

Expand Down Expand Up @@ -278,7 +278,7 @@ describe( 'Matcher', () => {
it( 'should match multiple attributes', () => {
const pattern = {
name: 'a',
attribute: {
attributes: {
name: 'foo',
title: 'bar'
}
Expand All @@ -294,15 +294,15 @@ describe( 'Matcher', () => {
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' );
expect( result.match ).to.have.property( 'attributes' ).that.is.an( 'array' );
expect( result.match.attributes[ 0 ] ).to.equal( 'name' );
expect( result.match.attributes[ 1 ] ).to.equal( 'title' );
} );

it( 'should match multiple classes', () => {
const pattern = {
name: 'a',
class: [ 'foo', 'bar' ]
classes: [ 'foo', 'bar' ]
};
const matcher = new Matcher( pattern );
const el = new Element( 'a' );
Expand All @@ -313,15 +313,15 @@ describe( 'Matcher', () => {
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' );
expect( result.match ).to.have.property( 'classes' ).that.is.an( 'array' );
expect( result.match.classes[ 0 ] ).to.equal( 'foo' );
expect( result.match.classes[ 1 ] ).to.equal( 'bar' );
} );

it( 'should match multiple styles', () => {
const pattern = {
name: 'a',
style: {
styles: {
color: 'red',
position: 'relative'
}
Expand All @@ -338,9 +338,9 @@ describe( 'Matcher', () => {
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' );
expect( result.match ).to.have.property( 'styles' ).that.is.an( 'array' );
expect( result.match.styles[ 0 ] ).to.equal( 'color' );
expect( result.match.styles[ 1 ] ).to.equal( 'position' );
} );
} );

Expand Down Expand Up @@ -370,7 +370,7 @@ describe( 'Matcher', () => {
} );

it( 'should return all matched elements when using RegExp pattern', () => {
const pattern = { class: /^red-.*/ };
const pattern = { classes: /^red-.*/ };
const matcher = new Matcher( pattern );
const el1 = new Element( 'p' );
const el2 = new Element( 'p' );
Expand All @@ -386,14 +386,14 @@ describe( 'Matcher', () => {
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[ 0 ].match ).to.have.property( 'classes' ).that.is.an( 'array' );
expect( result[ 0 ].match.classes[ 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( result[ 1 ].match ).to.have.property( 'classes' ).that.is.an( 'array' );
expect( result[ 1 ].match.classes[ 0 ] ).to.equal( 'red-background' );

expect( matcher.matchAll( el3 ) ).to.be.null;
} );
Expand All @@ -407,7 +407,7 @@ describe( 'Matcher', () => {
} );

it( 'should return null if pattern has no name property', () => {
const matcher = new Matcher( { class: 'foo' } );
const matcher = new Matcher( { classes: 'foo' } );

expect( matcher.getElementName() ).to.be.null;
} );
Expand All @@ -425,7 +425,7 @@ describe( 'Matcher', () => {
} );

it( 'should return null if matcher has more than one pattern', () => {
const matcher = new Matcher( { name: 'div' }, { class: 'foo' } );
const matcher = new Matcher( { name: 'div' }, { classes: 'foo' } );

expect( matcher.getElementName() ).to.be.null;
} );
Expand Down

0 comments on commit bcbfc34

Please sign in to comment.