A lightweight (1kb), cross-browser library for DOM selections and ranges. IE9+
$ npm install --save dom-selection
Multiple module systems are included:
dist/dom-selection.js) - This is the main/default entry. Use as a regular Node module.dist/dom-selection.min.js) - This is exposed as the umd:main entry. It's also minified & can be used in the browser.dist/dom-selection.es.js) - This is exposed as the module entry. Easier for code-splitting bundlers like Rollup or Webpack.This package is available as a UMD and a CommonJS module, so you may import it directly into your JavaScript:
const domSel = require('dom-selection');
Or you may import it directly into your HTML markup:
<script src="dist/dom-selection.min.js"></script>
<script>
// the 'DOMSelection' global is now available
var range = DOMSelection.getRange();
</script>
The parameter [sel] indicates that an existing Selection object may be provided. By default, the current, active Selection will be used if available.
Get a Selection's Range.
Returns: Range
Restore a Range to a Selection.
saved
Type:
Range
The
Rangeto apply.
Get a Selection's rectangular bounds.
Returns: Object or false
Get all Nodes within a Selection.
Returns: Array
Get the inner HTML content of a Selection.
Returns: String
Is there a Selection active?
Returns: Boolean
Collapse a Selection to its beginning.
Collapse a Selection to its endpoint.
Check if the direction of the Selection was RTL.
Returns: Boolean
Is the Selection within given container Node?
Returns: Boolean
container
Type:
Node
The DOM element to check.
Force/Restrict a Selection to the container & its children only.
container
Type:
Node
The DOM element that should be the boundary.
Expand the Selection to include the full word that the Caret is (partially) within.
Get the full word that the Caret is within.
Returns: String
Force the Selection to include entire words. Can be thought of as "snap to words". No partially-selected words will occur.
MIT © Luke Edwards