mirror of
https://github.com/Foundry376/Mailspring.git
synced 2024-12-29 11:52:34 +08:00
fix(paste): Don't paste html with surrounding line breaks
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
This commit is contained in:
parent
e15066d5f0
commit
dc9e8dc8af
2 changed files with 37 additions and 3 deletions
|
@ -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(/(<br\s*\/?>\s*){3,}/g, "<br/><br/>")
|
||||
|
||||
# We never want to keep leading and trailing <brs>, since the user
|
||||
# would have started a new paragraph themselves if they wanted space
|
||||
# before what they paste.
|
||||
# BAD: "<p>begins at<br>12AM</p>" => "<br><br>begins at<br>12AM<br><br>"
|
||||
# Better: "<p>begins at<br>12AM</p>" => "begins at<br>12"
|
||||
inputText = inputText.replace(/^(<br ?\/>)+/, '')
|
||||
inputText = inputText.replace(/(<br ?\/>)+$/, '')
|
||||
|
||||
return inputText
|
||||
|
||||
|
||||
|
|
|
@ -97,16 +97,42 @@ describe "ContenteditableComponent", ->
|
|||
{
|
||||
in: "<style>Yo</style> Foo Bar <div>Baz</div>"
|
||||
# Strip bad tags
|
||||
sanitizedAsHTML: " Foo Bar Baz<br/>"
|
||||
sanitizedAsHTML: " Foo Bar Baz"
|
||||
# HTML encode tags for literal display
|
||||
sanitizedAsPlain: "<style>Yo</style> Foo Bar <div>Baz</div>"
|
||||
}
|
||||
},
|
||||
{
|
||||
in: "<script>Bah</script> 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: """
|
||||
<!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>"""
|
||||
# Strip non white-list tags and encode malformed ones.
|
||||
sanitizedAsHTML: "<ul><br /><li><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><br /></ul>"
|
||||
# HTML encode tags for literal display
|
||||
sanitizedAsPlain: "<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><br/><html><br/><head><br/><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><br/><meta http-equiv="Content-Style-Type" content="text/css"><br/><title></title><br/><meta name="Generator" content="Cocoa HTML Writer"><br/><meta name="CocoaVersion" content="1265.21"><br/><style type="text/css"><br/>li.li1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}<br/>ul.ul1 {list-style-type: disc}<br/></style><br/></head><br/><body><br/><ul class="ul1"><br/><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><br/></ul><br/></body><br/></html>"
|
||||
}
|
||||
]
|
||||
|
||||
|
|
Loading…
Reference in a new issue