all files / src/ sputils.ui.js

80% Statements 12/15
87.5% Branches 7/8
100% Functions 2/2
80% Lines 12/15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33                                          
(function () {
  /**
   * Selects all text in the element `el` visually
   * as though selected with the mouse, to be ready
   * for copying or something like that.
   * @function sputils.ui.selectElementText
   * @param {DOMElement} el the DOM element in which to select the text.
   * @param {Window} win @optional the relevant browsing context.
   * @example
   * sputils.selectElementText(document.querySelector('#container));
   */
  var selectElementText = function (el, win) {
    win = win || global;
    var doc = win.document, sel, range;
    if (win.getSelection && doc.createRange) {
      sel = win.getSelection();
      range = doc.createRange();
      range.selectNodeContents(el);
      sel.removeAllRanges();
      sel.addRange(range);
    } else Iif (doc.body.createTextRange) {
      range = doc.body.createTextRange();
      range.moveToElementText(el);
      range.select();
    }
  };
 
  /** @namespace */
  sputils.ui = {
    selectElementText: selectElementText
  };
})();