I need to select all nodes in an HTML document using the DOM selection API getSelection
.
Nodes that are not selectable by the user (i.e. with mouse) should be excluded.
So, if an element has the CSS rule user-select: none
or -moz-user-select: none
applied to it, my programmatical selection should exclude those elements.
If I select text manually (via mouse) those elements won't be selected. If I apply window.getSelection().selectAllChildren
on one of its parent elements the non-selectable element is getting selected as well.
I tried different methods both of the Selection
and the Range
objects, but haven't found a way to only select those elements programmatically that are selectable manually.
<body>
<div>Selectable</div>
<div style="-moz-user-select:none">
<span id="span">Non-Selectable</span>
</div>
<script>
const sel = window.getSelection();
sel.selectAllChildren(document.body);
console.log(sel.containsNode(document.getElementById('span')));
// outputs true
</script>
</body>
Does anyone know a way to programmatically select only those elements that are selectable manually?
EDIT So what I need is a function that receives a node as argument and returns a Boolean on wether this node is selectable:
function isSelectable(node) {
// determine if node is selectable
}