Matcher
View matcher class. Instance of this class can be used to find elements that match given pattern.
Properties
_patterns : Array<MatcherFunctionPattern | MatcherObjectPattern>privatereadonlymodule:engine/view/matcher~Matcher#_patterns
Methods
constructor( pattern )module:engine/view/matcher~Matcher#constructorCreates new instance of Matcher.
Parameters
pattern : Array<MatcherPattern>Match patterns. See add method for more information.
add( pattern ) → voidmodule:engine/view/matcher~Matcher#addAdds pattern or patterns to matcher instance.
// String. matcher.add( 'div' ); // Regular expression. matcher.add( /^\w/ ); // Single class. matcher.add( { classes: 'foobar' } );Copy codeSee
MatcherPatternfor more examples.Multiple patterns can be added in one call:
matcher.add( 'div', { classes: 'foobar' } );Copy codeParameters
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
matchkey of the object returned from match or matchAll methods.
Returns
void
getElementName() → null | stringmodule:engine/view/matcher~Matcher#getElementNameReturns 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(notRegExp). Otherwise, returnsnull.Returns
null | stringElement name trying to match.
match( element ) → null | MatchResultmodule:engine/view/matcher~Matcher#matchMatches 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' ], [ 'class', 'foo' ], [ 'style', 'color' ], [ 'style', 'position' ] ] } }Copy codeYou could use the
matchfield from the above returned object as an input for theViewConsumable#test()andViewConsumable#consume()methods.Parameters
element : Array<ViewElement>View element to match against stored patterns.
Returns
null | MatchResultThe match information about found element or
null.
Related:
matchAll( element ) → null | Array<MatchResult>module:engine/view/matcher~Matcher#matchAllMatches elements for currently stored patterns. Returns array of match information with all found elements. If no element is found - returns
null.Parameters
element : Array<ViewElement>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:
_isElementMatching( element, pattern ) → null | Matchprivatemodule:engine/view/matcher~Matcher#_isElementMatchingReturns match information if element is matching provided pattern. If element cannot be matched to provided pattern - returns
null.Parameters
element : ViewElementpattern : MatcherFunctionPattern | MatcherObjectPattern
Returns
null | MatchReturns object with match information or null if element is not matching.