This repository has been archived on 2026-03-06. You can view files and clone it, but cannot push or open issues or pull requests.
todo/client/node_modules/@testing-library/user-event/dist/esm/document/copySelection.js

28 lines
906 B
JavaScript
Raw Normal View History

2025-06-13 06:04:40 +00:00
import { createDataTransfer } from '../utils/dataTransfer/DataTransfer.js';
import '../utils/dataTransfer/Clipboard.js';
import { getWindow } from '../utils/misc/getWindow.js';
import { hasOwnSelection } from '../utils/focus/selection.js';
import { getUISelection, getUIValue } from './UI.js';
function copySelection(target) {
const data = hasOwnSelection(target) ? {
'text/plain': readSelectedValueFromInput(target)
} : {
'text/plain': String(target.ownerDocument.getSelection())
};
const dt = createDataTransfer(getWindow(target));
for(const type in data){
if (data[type]) {
dt.setData(type, data[type]);
}
}
return dt;
}
function readSelectedValueFromInput(target) {
const sel = getUISelection(target);
const val = getUIValue(target);
return val.substring(sel.startOffset, sel.endOffset);
}
export { copySelection };