Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
gitlab-ce
Commits
6089ece0
Commit
6089ece0
authored
Jan 17, 2017
by
Douwe Maan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix ShortcutsIssuable#replyWithSelectedText tests
parent
72620ea1
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
67 additions
and
34 deletions
+67
-34
app/assets/javascripts/copy_as_gfm.js.es6
app/assets/javascripts/copy_as_gfm.js.es6
+49
-15
app/assets/javascripts/shortcuts_issuable.js
app/assets/javascripts/shortcuts_issuable.js
+6
-10
spec/javascripts/shortcuts_issuable_spec.js
spec/javascripts/shortcuts_issuable_spec.js
+12
-9
No files found.
app/assets/javascripts/copy_as_gfm.js.es6
View file @
6089ece0
...
...
@@ -231,20 +231,13 @@
let clipboardData = e.originalEvent.clipboardData;
if (!clipboardData) return;
if (!window.getSelection) return;
let selection = window.getSelection();
if (!selection.rangeCount || selection.rangeCount === 0) return;
let selectedDocument = selection.getRangeAt(0).cloneContents();
if (!selectedDocument) return;
if (selectedDocument.textContent.length === 0) return;
let documentFragment = CopyAsGFM.getSelectedFragment();
if (!documentFragment) return;
e.preventDefault();
clipboardData.setData('text/plain',
selectedDocu
ment.textContent);
clipboardData.setData('text/plain',
documentFrag
ment.textContent);
let gfm = CopyAsGFM.nodeToGFM(
selectedDocu
ment);
let gfm = CopyAsGFM.nodeToGFM(
documentFrag
ment);
clipboardData.setData('text/x-gfm', gfm);
}
...
...
@@ -257,11 +250,25 @@
e.preventDefault();
this
.insertText(e.target, gfm);
CopyAsGFM
.insertText(e.target, gfm);
}
insertText(target, text) {
// Firefox doesn't support `document.execCommand('insertText', false, text);` on textareas
static getSelectedFragment() {
if (!window.getSelection) return null;
let selection = window.getSelection();
if (!selection.rangeCount || selection.rangeCount === 0) return null;
let documentFragment = selection.getRangeAt(0).cloneContents();
if (!documentFragment) return null;
if (documentFragment.textContent.length === 0) return null;
return documentFragment;
}
static insertText(target, text) {
// Firefox doesn't support `document.execCommand('insertText', false, text)` on textareas
let selectionStart = target.selectionStart;
let selectionEnd = target.selectionEnd;
...
...
@@ -292,7 +299,7 @@
for (let selector in rules) {
let func = rules[selector];
if (!
node.matches(
selector)) continue;
if (!
CopyAsGFM.nodeMatchesSelector(node,
selector)) continue;
let result = func(node, text);
if (result === false) continue;
...
...
@@ -315,11 +322,38 @@
let clonedNode = clonedNodes[i];
let text = this.nodeToGFM(node);
// `clonedNode.replaceWith(text)` is not yet widely supported
clonedNode.parentNode.replaceChild(document.createTextNode(text), clonedNode);
}
return clonedParentNode.innerText || clonedParentNode.textContent;
}
static nodeMatchesSelector(node, selector) {
let matches = Element.prototype.matches ||
Element.prototype.matchesSelector ||
Element.prototype.mozMatchesSelector ||
Element.prototype.msMatchesSelector ||
Element.prototype.oMatchesSelector ||
Element.prototype.webkitMatchesSelector;
if (matches) {
return matches.call(node, selector);
}
// IE11 doesn't support `node.matches(selector)`
let parentNode = node.parentNode;
if (!parentNode) {
parentNode = document.createElement('div');
node = node.cloneNode(true);
parentNode.appendChild(node);
}
let matchingNodes = parentNode.querySelectorAll(selector);
return Array.prototype.indexOf.call(matchingNodes, node) !== -1;
}
}
window.gl = window.gl || {};
...
...
app/assets/javascripts/shortcuts_issuable.js
View file @
6089ece0
...
...
@@ -39,26 +39,22 @@
}
ShortcutsIssuable
.
prototype
.
replyWithSelectedText
=
function
()
{
var
quote
,
replyField
,
selectedDocument
,
selected
,
selection
,
separator
;
if
(
!
window
.
getSelection
)
return
;
var
quote
,
replyField
,
documentFragment
,
selected
,
separator
;
selection
=
window
.
getSelection
();
if
(
!
selection
.
rangeCount
||
selection
.
rangeCount
===
0
)
return
;
documentFragment
=
window
.
gl
.
CopyAsGFM
.
getSelectedFragment
();
if
(
!
documentFragment
)
return
;
selectedDocument
=
selection
.
getRangeAt
(
0
).
cloneContents
();
if
(
!
selectedDocument
)
return
;
selected
=
window
.
gl
.
CopyAsGFM
.
nodeToGFM
(
selectedDocument
);
selected
=
window
.
gl
.
CopyAsGFM
.
nodeToGFM
(
documentFragment
);
replyField
=
$
(
'
.js-main-target-form #note_note
'
);
if
(
selected
.
trim
()
===
""
)
{
return
;
}
quote
=
_
.
map
(
selected
.
split
(
"
\n
"
),
function
(
val
)
{
return
"
>
"
+
val
+
"
\n
"
;
return
(
"
>
"
+
val
).
trim
()
+
"
\n
"
;
});
// If replyField already has some content, add a newline before our quote
separator
=
replyField
.
val
().
trim
()
!==
""
&&
"
\n
"
||
''
;
separator
=
replyField
.
val
().
trim
()
!==
""
&&
"
\n
\n
"
||
''
;
replyField
.
val
(
function
(
_
,
current
)
{
return
current
+
separator
+
quote
.
join
(
''
)
+
"
\n
"
;
});
...
...
spec/javascripts/shortcuts_issuable_spec.js
View file @
6089ece0
/* eslint-disable space-before-function-paren, no-return-assign, no-var, quotes, padded-blocks */
/* global ShortcutsIssuable */
/*= require copy_as_gfm */
/*= require shortcuts_issuable */
(
function
()
{
...
...
@@ -14,10 +15,12 @@
});
return
describe
(
'
#replyWithSelectedText
'
,
function
()
{
var
stubSelection
;
// Stub window.getSelection to return the provided String.
stubSelection
=
function
(
text
)
{
return
window
.
getSelection
=
function
()
{
return
text
;
// Stub window.gl.CopyAsGFM.getSelectedFragment to return a node with the provided HTML.
stubSelection
=
function
(
html
)
{
window
.
gl
.
CopyAsGFM
.
getSelectedFragment
=
function
()
{
var
node
=
document
.
createElement
(
'
div
'
);
node
.
innerHTML
=
html
;
return
node
;
};
};
beforeEach
(
function
()
{
...
...
@@ -32,13 +35,13 @@
});
describe
(
'
with any selection
'
,
function
()
{
beforeEach
(
function
()
{
return
stubSelection
(
'
Selected text.
'
);
return
stubSelection
(
'
<p>Selected text.</p>
'
);
});
it
(
'
leaves existing input intact
'
,
function
()
{
$
(
this
.
selector
).
val
(
'
This text was already here.
'
);
expect
(
$
(
this
.
selector
).
val
()).
toBe
(
'
This text was already here.
'
);
this
.
shortcut
.
replyWithSelectedText
();
return
expect
(
$
(
this
.
selector
).
val
()).
toBe
(
"
This text was already here.
\n
> Selected text.
\n\n
"
);
return
expect
(
$
(
this
.
selector
).
val
()).
toBe
(
"
This text was already here.
\n
\n
> Selected text.
\n\n
"
);
});
it
(
'
triggers `input`
'
,
function
()
{
var
triggered
;
...
...
@@ -61,16 +64,16 @@
});
describe
(
'
with a one-line selection
'
,
function
()
{
return
it
(
'
quotes the selection
'
,
function
()
{
stubSelection
(
'
This text has been selected.
'
);
stubSelection
(
'
<p>This text has been selected.</p>
'
);
this
.
shortcut
.
replyWithSelectedText
();
return
expect
(
$
(
this
.
selector
).
val
()).
toBe
(
"
> This text has been selected.
\n\n
"
);
});
});
return
describe
(
'
with a multi-line selection
'
,
function
()
{
return
it
(
'
quotes the selected lines as a group
'
,
function
()
{
stubSelection
(
"
Selected line one.
\n\n
Selected line two.
\n
Selected line three.
\n
"
);
stubSelection
(
"
<p>Selected line one.</p>
\n\n
<p>Selected line two.</p>
\n\n
<p>Selected line three.</p>
"
);
this
.
shortcut
.
replyWithSelectedText
();
return
expect
(
$
(
this
.
selector
).
val
()).
toBe
(
"
> Selected line one.
\n
>
Selected line two.
\n
> Selected line three.
\n\n
"
);
return
expect
(
$
(
this
.
selector
).
val
()).
toBe
(
"
> Selected line one.
\n
>
\n
> Selected line two.
\n
>
\n
> Selected line three.
\n\n
"
);
});
});
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment