Class

Matcher (engine/view)

@ckeditor/ckeditor5-engine/src/view/matcher

class

View matcher class. Instance of this class can be used to find elements that match given pattern.

Filtering

Properties

Methods

  • constructor( pattern )

    Creates new instance of Matcher.

    Parameters

    pattern : Array<MatcherPattern>

    Match patterns. See add method for more information.

  • add( pattern ) → void

    Adds pattern or patterns to matcher instance.

    // String.
    matcher.add( 'div' );
    
    // Regular expression.
    matcher.add( /^\w/ );
    
    // Single class.
    matcher.add( {
    	classes: 'foobar'
    } );
    

    See MatcherPattern for more examples.

    Multiple patterns can be added in one call:

    matcher.add( 'div', { classes: 'foobar' } );
    

    Parameters

    pattern : Array<MatcherPattern>

    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 of a function - then this function will be called with each element as a parameter. Function's return value will be stored under match key of the object returned from match or matchAll methods.

    Returns

    void
  • getElementName() → null | string

    Returns the name of the element to match if there is exactly one pattern added to the matcher instance and it matches element name defined by string (not RegExp). Otherwise, returns null.

    Returns

    null | string

    Element name trying to match.

  • match( element ) → null | MatchResult

    Matches elements for currently stored patterns. Returns match information about first found element, otherwise returns null.

    Example of returned object:

    {
    	element: <instance of found element>,
    	pattern: <pattern used to match found element>,
    	match: {
    		name: true,
    		attributes: [ 'title', 'href' ],
    		classes: [ 'foo' ],
    		styles: [ 'color', 'position' ]
    	}
    }
    

    Parameters

    element : Array<Element>

    View element to match against stored patterns.

    Returns

    null | MatchResult

    Related:

  • matchAll( element ) → null | Array<MatchResult>

    Matches elements for currently stored patterns. Returns array of match information with all found elements. If no element is found - returns null.

    Parameters

    element : Array<Element>

    View element to match against stored patterns.

    Returns

    null | Array<MatchResult>

    Array with match information about found elements or null. For more information see match method description.

    Related: