mark() method
Syntax
// javascript
const instance = new Mark(context);
instance.mark(search[, options]);
// jQuery
$(context).mark(search[, options]);
Parameters:
search
string or string[] - string or array of stringsoptions
object - Optional options:
Option | Type | Default | Description |
---|---|---|---|
element | string | mark | A custom mark element e.g. span . |
className | string | '' | A class to be added to mark elements. |
exclude | string or string[] | [] | A string or an array of selectors. Specifies DOM elements that should be excluded from searching. |
separateWordSearch | boolean | true | Whether to break term into separate words and search for each individual word |
diacritics | boolean | true | Whether to match diacritic characters |
caseSensitive | boolean | false | Whether to search case sensitive |
accuracy | string or object | 'partially' |
|
wildcards | string | disabled | Two characters ? and * used as wildcards unless thay are escaped
|
ignoreJoiners | boolean | false | Whether to find matches that contain soft hyphen, zero width space, zero width non-joiner and zero width joiner |
ignorePunctuation | string[] | [] | An array of punctuation characters |
synonyms | object | {} | An object with synonyms
e.g. |
acrossElements | boolean | false | Whether to search for matches across elements |
combinePatterns | number or boolean | 10 | Combine a specified number of individual term patterns into one
See Performance for more details. |
cacheTextNodes | boolean | undefined | Caching information to improve performance
See Performance for more details. |
blockElementsBoundary | boolean or object | undefined | Whether to limit matches within default HTML block elements and/or custom elements
See Elements boundaries for more details.
|
shadowDOM | boolean | undefined | Whether to mark inside shadow DOMs
See Highlighting in shadow DOM for more details. |
iframes | boolean | false | Whether to mark inside iframes |
iframesTimeout | number | 5000 ms | The max time to wait for iframe(s) to load before skipping |
debug | boolean | false | Whether to log messages |
log | object | console | Log messages to a specific object |
filter | function | A callback to filter matches. It calls for each match (with acrossElements option, if the match is located across several elements, it calls for each text node which is part of the match)
filter : (textNode, term, marksSoFar, termMarksSoFar, filterInfo) => {}
| |
each | function | A callback for each marked element
each : (markElement, eachInfo) => {}
| |
done | function | A callback on finish
done : (totalMarks, totalMatches, termStats) => {}
| |
noMatch | function | A callback that is called when a term has no match at all
noMatch : (term) => {}
|
Available properties of the filterInfo
object depending on options
options | match | matchStart | execution | offset |
---|---|---|---|---|
acrossElements: true | + | + | + | + |
acrossElements: false | + | - | + | + |
Available properties of the eachInfo
object depending on options
options | match | matchStart | count |
---|---|---|---|
acrossElements: true | + | + | + |
acrossElements: false | + | - | + |
Example with default options values
const options = {
element : 'mark',
className : '',
separateWordSearch : true,
diacritics : true,
exclude : [],
caseSensitive : false,
accuracy : 'partially',
synonyms : {},
ignoreJoiners : false,
ignorePunctuation : [],
wildcards : 'disabled',
acrossElements : false,
combinePatterns : false,
cacheTextNodes : false,
blockElementsBoundary : false,
shadowDOM : false,
iframes : false,
iframesTimeout : 5000,
filter : (textNode, term, marksSoFar, termMarksSoFar, filterInfo) => {
return true; // must return either true or false
},
each : (markElement, eachInfo) => {},
done : (totalMarks, totalMatches, termStats) => {},
noMatch : (term) => {},
debug : false,
log : window.console
};
JavaScript:
var instance = new Mark(document.querySelector('selector'));
instance.mark('test', options);
jQuery:
$('selector').mark('test', options);
- AE - only available when
acrossElements
option is set totrue