Jsdom: Implement DOM selection apis

Created on 5 Nov 2014  ·  4Comments  ·  Source: jsdom/jsdom

https://developer.mozilla.org/en-US/docs/Web/API/Window.getSelection

Even a nonfunctioning shim would go a long way. Currently I just do the following:

window.document.getSelection = function() { return { addRange: function() {}, removeAllRanges:function() { } } };

Just to avoid "cannot call undefined" when running getSelection().

feature

Most helpful comment

@domenic thanks, got the shim I needed by adding a version of @tolmasky's code in a Jest helper:

// spec/javascripts/helpers/jest-env.js
// window.getSelection isn't in jsdom
// https://github.com/tmpvar/jsdom/issues/937
window.getSelection = function() { 
  return { 
    addRange: function() {}, 
    removeAllRanges:function() {} 
  };
};

and linking to the above file via Jest's config.setupEnvScriptFile.

All 4 comments

We're not really in the business of nonfunctioning shims, but you're welcome to add that to your own jsdom windows.

Any ideas on how to bake this into how Jest uses jsdom?

Not sure myself; @Sebmaster is a bit more familiar with jest, but I think the Jest issue tracker will be your best bet.

@domenic thanks, got the shim I needed by adding a version of @tolmasky's code in a Jest helper:

// spec/javascripts/helpers/jest-env.js
// window.getSelection isn't in jsdom
// https://github.com/tmpvar/jsdom/issues/937
window.getSelection = function() { 
  return { 
    addRange: function() {}, 
    removeAllRanges:function() {} 
  };
};

and linking to the above file via Jest's config.setupEnvScriptFile.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

josephrexme picture josephrexme  ·  4Comments

mitar picture mitar  ·  4Comments

jacekpl picture jacekpl  ·  4Comments

philipwalton picture philipwalton  ·  4Comments

vsemozhetbyt picture vsemozhetbyt  ·  4Comments