markRanges() method
Syntax
// javascript
const instance = new Mark(context);
instance.markRanges(ranges[, options]);
// jQuery
$(context).markRanges(ranges[, options]);
Parameters:
ranges
object[] - An array of objects withstart
andlength
properties with integer type values.options
object - Optional options:
Option | Type | Default | Description |
---|---|---|---|
element | string | mark | A custom mark element e.g. span . |
className | string | '' | A custom 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.
See exclude option for more details. |
wrapAllRanges | boolean | undefined | Mark nesting/overlapping capturing groups
See Marking nesting and overlapping ranges and match groups for more details. |
shadowDOM | boolean | undefined | Whether to mark inside shadow DOMs
See Highlighting in shadow DOM for more details. |
markLines | boolean | undefined | Whether to mark ranges of lines instead of ranges of texts
See Highlighting line ranges 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 range (if a range is located across several elements, it calls for each text node which is part of the range)
filter : (textNode, range, matchString, index) => {}
The function must return either | |
each | function | A callback for each marked element
each : (markElement, range, rangeInfo) => {}
| |
done | function | A callback on finish
done : (totalMarks, totalRanges) => {}
| |
noMatch | function | A callback that is called on non-valid range
noMatch : (range) => {}
|
Example with default options values
const options = {
element : 'mark',
className : '',
exclude : [],
wrapAllRanges : false,
markLines : false,
shadowDOM : false,
iframes : false,
iframesTimeout : 5000,
filter : (textNode, range, matchingString, index) => {
return true; // must return either true or false
},
each : (markElement, range, rangeInfo) => {},
done : (totalMarks, totalMatches) => {},
noMatch : (range) => {},
debug : false,
log : window.console
};
JavaScript:
const instance = new Mark(document.querySelector('selector')),
ranges = [{ start: 0, length: 5 }, { start: 6, length: 5 }];
instance.markRanges(ranges, options);
jQuery:
$('selector').markRanges(ranges, options);