Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude unselectable text from being copied #138

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion docs/index.rst
Expand Up @@ -372,6 +372,22 @@ In this case, all elements that match ``your.selector`` will have a copy button
added to them.


Exclude unselectable text from copied text
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

By default, ``sphinx-copybutton`` will exclude text that is not selectable by the user. Unselectable text is determined with the CSS style
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
By default, ``sphinx-copybutton`` will exclude text that is not selectable by the user. Unselectable text is determined with the CSS style
By default, ``sphinx-copybutton`` will exclude text that is not selectable by the user.
Unselectable text is determined with the CSS style
Suggested change
By default, ``sphinx-copybutton`` will exclude text that is not selectable by the user. Unselectable text is determined with the CSS style
By default, ``sphinx-copybutton`` will exclude text that is not selectable by the user. Unselectable text is determined with the CSS style

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you provide an example of what un-selectable text generally is, so that users understand when they'll likely use this?


.. code-block:: css

user-select: none

To change this behavior, use the following configuration in ``conf.py``:

.. code-block:: python

copybutton_exclude_unselectable = False

choldgraf marked this conversation as resolved.
Show resolved Hide resolved

Development
===========

Expand Down Expand Up @@ -441,7 +457,7 @@ Then run the docs build:

.. code-block:: console

$ cd doc
$ cd docs
$ make html

.. toctree::
Expand Down
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -18,5 +18,8 @@
"require": [
"esm"
]
},
"dependencies": {
"ci": "^2.1.1"
}
}
4 changes: 4 additions & 0 deletions sphinx_copybutton/__init__.py
Expand Up @@ -50,6 +50,9 @@ def add_to_context(app, config):
.replace("export function", "function")
}
)
config.html_context.update(
{"copybutton_exclude_unselectable": config.copybutton_exclude_unselectable}
)


def setup(app):
Expand All @@ -68,6 +71,7 @@ def setup(app):
app.add_config_value("copybutton_here_doc_delimiter", "", "html")
app.add_config_value("copybutton_image_path", "copy-button.svg", "html")
app.add_config_value("copybutton_selector", "div.highlight pre", "html")
app.add_config_value("copybutton_exclude_unselectable", True, "html")

# Add configuration value to the template
app.connect("config-inited", add_to_context)
Expand Down
6 changes: 5 additions & 1 deletion sphinx_copybutton/_static/copybutton.js_t
Expand Up @@ -118,7 +118,11 @@ const addCopyButtonToCodeCells = () => {

var copyTargetText = (trigger) => {
var target = document.querySelector(trigger.attributes['data-clipboard-target'].value);
return formatCopyText(target.innerText, {{ "{!r}".format(copybutton_prompt_text) }}, {{ copybutton_prompt_is_regexp | lower }}, {{ copybutton_only_copy_prompt_lines | lower }}, {{ copybutton_remove_prompts | lower }}, {{ copybutton_copy_empty_lines | lower }}, {{ "{!r}".format(copybutton_line_continuation_character) }}, {{ "{!r}".format(copybutton_here_doc_delimiter) }})
let text = (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you provide a comment about what this is doing? This uses some JS syntax that I haven't seen before (with ? and :) and we want this theme to be maintainable for non-JS developers so it'd help to have extra explanation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The p ? t : f syntax is the ternary operator that returns t if p is true and f otherwise.

{{ copybutton_exclude_unselectable | lower }} ?
filterUnselectableText( target ) : target.innerText
);
return formatCopyText(text, {{ "{!r}".format(copybutton_prompt_text) }}, {{ copybutton_prompt_is_regexp | lower }}, {{ copybutton_only_copy_prompt_lines | lower }}, {{ copybutton_remove_prompts | lower }}, {{ copybutton_copy_empty_lines | lower }}, {{ "{!r}".format(copybutton_line_continuation_character) }}, {{ "{!r}".format(copybutton_here_doc_delimiter) }})
}

// Initialize with a callback so we can modify the text before copy
Expand Down
46 changes: 45 additions & 1 deletion sphinx_copybutton/_static/copybutton_funcs.js
Expand Up @@ -2,10 +2,54 @@ function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}

function getUnselectable(target) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide a high-level docstring that describes what this function does

let unselectable = [];

// get all unselectable elements of children
for (let child of target.children) {
unselectable = unselectable.concat(getUnselectable(child));
}

// add self to unselectable if needed
if (getComputedStyle(target)["userSelect"] === "none") {
unselectable.push( target);
}

return unselectable;
}

function getFilteredText(target, exclude) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here - please provide a docstring

let text = '';
for (let child of target.childNodes) {
if (exclude.indexOf(child) > -1) {
// child should be filtered out
continue;
}

if (child.nodeType === Node.TEXT_NODE) {
// child is a text node, add its contents
text += child.textContent
}
else {
// recurse on non-text nodes
text += getFilteredText(child, exclude);
}
}

return text;
}

export function filterUnselectableText(target) {
// get unselectable elements
const unselectable = getUnselectable(target);

// get text from selectable elements
return getFilteredText(target, unselectable);
}

// Callback when a copy button is clicked. Will be passed the node that was clicked
// should then grab the text and replace pieces of text that shouldn't be used in output
export function formatCopyText(textContent, copybuttonPromptText, isRegexp = false, onlyCopyPromptLines = true, removePrompts = true, copyEmptyLines = true, lineContinuationChar = "", hereDocDelim = "") {

var regexp;
var match;

Expand Down