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

Fix multi line paste detection and filtering #8634

Merged
8 commits merged into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1973,7 +1973,8 @@ namespace winrt::TerminalApp::implementation
}
}

const bool hasNewLine = std::find(text.cbegin(), text.cend(), L'\n') != text.cend();
std::wregex newLine{ LR"((\r|\n))" };
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
const bool hasNewLine = std::regex_search(text.c_str(), newLine);
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
const bool warnMultiLine = hasNewLine && _settings.GlobalSettings().WarnAboutMultiLinePaste();

constexpr const std::size_t minimumSizeForWarning = 1024 * 5; // 5 KiB
Expand Down
24 changes: 4 additions & 20 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2025,29 +2025,13 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// - Pre-process text pasted (presumably from the clipboard)
// before sending it over the terminal's connection, converting
// Windows-space \r\n line-endings to \r line-endings
// - Also converts \n line-endings to \r line-endings
void TermControl::_SendPastedTextToConnection(const std::wstring& wstr)
{
// Some notes on this implementation:
//
// - std::regex can do this in a single line, but is somewhat
// overkill for a simple search/replace operation (and its
// performance guarantees aren't exactly stellar)
// - The STL doesn't have a simple string search/replace method.
// This fact is lamentable.
// - This line-ending conversion is intentionally fairly
// conservative, to avoid stripping out lone \n characters
// where they could conceivably be intentional.

std::wstring stripped{ wstr };

std::wstring::size_type pos = 0;

while ((pos = stripped.find(L"\r\n", pos)) != std::wstring::npos)
{
stripped.replace(pos, 2, L"\r");
}
std::wregex multiLines{ LR"(((\r)?)(\n))" };
auto res = std::regex_replace(wstr, multiLines, L"\r");
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved

_connection.WriteInput(stripped);
_connection.WriteInput(res);
_terminal->TrySnapOnInput();
}

Expand Down