From dc9e8dc8afbe168bb93e58f7c00c75f41c4b3cb2 Mon Sep 17 00:00:00 2001 From: Ben Gotow Date: Fri, 15 May 2015 10:56:40 -0700 Subject: [PATCH] fix(paste): Don't paste html with surrounding line breaks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: This resolves T1187 — pasting plain text and html now work pretty well Test Plan: Run new test case Reviewers: evan Reviewed By: evan Maniphest Tasks: T1187 Differential Revision: https://review.inboxapp.com/D1511 --- .../lib/contenteditable-component.cjsx | 10 ++++++- .../spec/contenteditable-component-spec.cjsx | 30 +++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/internal_packages/composer/lib/contenteditable-component.cjsx b/internal_packages/composer/lib/contenteditable-component.cjsx index 15e66fbc8..5658f7f3f 100644 --- a/internal_packages/composer/lib/contenteditable-component.cjsx +++ b/internal_packages/composer/lib/contenteditable-component.cjsx @@ -339,7 +339,7 @@ class ContenteditableComponent extends React.Component menu.append(new MenuItem({ label: 'Copy', click:copy})) menu.append(new MenuItem({ label: 'Paste', click:paste})) menu.popup(remote.getCurrentWindow()) - + _onMouseDown: (event) => @_mouseDownEvent = event @_mouseHasMoved = false @@ -723,6 +723,14 @@ class ContenteditableComponent extends React.Component # https://regex101.com/r/gF6bF4/4 inputText = inputText.replace(/(\s*){3,}/g, "

") + # We never want to keep leading and trailing , since the user + # would have started a new paragraph themselves if they wanted space + # before what they paste. + # BAD: "

begins at
12AM

" => "

begins at
12AM

" + # Better: "

begins at
12AM

" => "begins at
12" + inputText = inputText.replace(/^(
)+/, '') + inputText = inputText.replace(/(
)+$/, '') + return inputText diff --git a/internal_packages/composer/spec/contenteditable-component-spec.cjsx b/internal_packages/composer/spec/contenteditable-component-spec.cjsx index 3bc4af042..da3d49255 100644 --- a/internal_packages/composer/spec/contenteditable-component-spec.cjsx +++ b/internal_packages/composer/spec/contenteditable-component-spec.cjsx @@ -97,16 +97,42 @@ describe "ContenteditableComponent", -> { in: " Foo Bar
Baz
" # Strip bad tags - sanitizedAsHTML: " Foo Bar Baz
" + sanitizedAsHTML: " Foo Bar Baz" # HTML encode tags for literal display sanitizedAsPlain: "<style>Yo</style> Foo Bar <div>Baz</div>" - } + }, { in: " Yo < script>Boo! < / script >" # Strip non white-list tags and encode malformed ones. sanitizedAsHTML: " Yo < script>Boo! < / script >" # HTML encode tags for literal display sanitizedAsPlain: "<script>Bah</script> Yo < script>Boo! < / script >" + }, + { + in: """ + + + + + + + + + + + +
    +
  • Packet pickup: I'll pick up my packet at some point on Saturday at Fort Mason. Let me know if you'd like me to get yours. I'll need a photo of your ID and your confirmation number. Also, shirt color preference, I believe. Gray or black? Can't remember...
  • +
+ + """ + # Strip non white-list tags and encode malformed ones. + sanitizedAsHTML: "

  • Packet pickup: I'll pick up my packet at some point on Saturday at Fort Mason. Let me know if you'd like me to get yours. I'll need a photo of your ID and your confirmation number. Also, shirt color preference, I believe. Gray or black? Can't remember...

" + # HTML encode tags for literal display + sanitizedAsPlain: "<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<meta name="CocoaVersion" content="1265.21">
<style type="text/css">
li.li1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
ul.ul1 {list-style-type: disc}
</style>
</head>
<body>
<ul class="ul1">
<li class="li1"><b>Packet pickup: </b>I'll pick up my packet at some point on Saturday at Fort Mason. Let me know if you'd like me to get yours. I'll need a photo of your ID and your confirmation number. Also, shirt color preference, I believe. Gray or black? Can't remember...</li>
</ul>
</body>
</html>" } ]