wrap-range-text
v1.0.1
Published
Safely wrap all selected text contained within a DOM Range
Downloads
120
Readme
wrap-range-text
Safely wrap all selected text contained within a DOM Range.
Installation
npm install wrap-range-text
Usage
wrapRange([wrapperEl, [range]])
Arguments
wrapperEl
- (optional) a DOMElement
orstring
tag name used to wrap each text node (this element will be cloned for each node). Default:'span'
.range
- a DOMRange
object. Default: the current selection (document.getSelection().getRangeAt(0)
).
Returns
wrapRange
returns an object with the following properties:
nodes
- an array of wrapper Nodesunwrap
- a function that unwraps the text when called
var wrapRange = require('wrap-range-text')
var wrapper = document.createElement('span')
wrapper.classList.add('fancy-text')
var range = document.createRange()
range.selectNodeContents(document.body)
var wrap = wrapRange(wrapper, range)
// nodes should be wrapped with `span.fancy-text` elements
wrap.unwrap()
// nodes should be unwrapped
License
Copyright 2015 Cameron Lakenen