Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Pathier <tpxp@live.fr>2018-11-20 21:18:29 +0300
committerPhil Hughes <me@iamphill.com>2018-11-20 21:18:29 +0300
commit7ed62abf7edbdd987fa7a983e06398f4c515aa3e (patch)
tree39829881e5edb024846c8571d867622172c93772 /app/assets/javascripts/lib/utils/common_utils.js
parentac6673ddcfda0a0229b86f22543d31ddccc1dc55 (diff)
Resolve "The reply shortcut can add any text of the page to the "comment" text area"
Diffstat (limited to 'app/assets/javascripts/lib/utils/common_utils.js')
-rw-r--r--app/assets/javascripts/lib/utils/common_utils.js24
1 files changed, 21 insertions, 3 deletions
diff --git a/app/assets/javascripts/lib/utils/common_utils.js b/app/assets/javascripts/lib/utils/common_utils.js
index e14fff7a610..3186ae9c133 100644
--- a/app/assets/javascripts/lib/utils/common_utils.js
+++ b/app/assets/javascripts/lib/utils/common_utils.js
@@ -226,7 +226,17 @@ export const getParameterByName = (name, urlToParse) => {
return decodeURIComponent(results[2].replace(/\+/g, ' '));
};
-const handleSelectedRange = range => {
+const handleSelectedRange = (range, restrictToNode) => {
+ // Make sure this range is within the restricting container
+ if (restrictToNode && !range.intersectsNode(restrictToNode)) return null;
+
+ // If only a part of the range is within the wanted container, we need to restrict the range to it
+ if (restrictToNode && !restrictToNode.contains(range.commonAncestorContainer)) {
+ if (!restrictToNode.contains(range.startContainer)) range.setStart(restrictToNode, 0);
+ if (!restrictToNode.contains(range.endContainer))
+ range.setEnd(restrictToNode, restrictToNode.childNodes.length);
+ }
+
const container = range.commonAncestorContainer;
// add context to fragment if needed
if (container.tagName === 'OL') {
@@ -237,14 +247,22 @@ const handleSelectedRange = range => {
return range.cloneContents();
};
-export const getSelectedFragment = () => {
+export const getSelectedFragment = restrictToNode => {
const selection = window.getSelection();
if (selection.rangeCount === 0) return null;
+ // Most usages of the selection only want text from a part of the page (e.g. discussion)
+ if (restrictToNode && !selection.containsNode(restrictToNode, true)) return null;
+
const documentFragment = document.createDocumentFragment();
+ documentFragment.originalNodes = [];
for (let i = 0; i < selection.rangeCount; i += 1) {
const range = selection.getRangeAt(i);
- documentFragment.appendChild(handleSelectedRange(range));
+ const handledRange = handleSelectedRange(range, restrictToNode);
+ if (handledRange) {
+ documentFragment.appendChild(handledRange);
+ documentFragment.originalNodes.push(range.commonAncestorContainer);
+ }
}
if (documentFragment.textContent.length === 0) return null;