Bug 116083 - Correctly handle the whitespace in all preformatted elements; r=roc

Previously this code only handled the special case of a <body> element that
had an explicit style attribute, which doesn't really make sense.  Now we
do the right thing based on the computed white-space style.
This commit is contained in:
Ehsan Akhgari 2014-12-01 06:38:11 -05:00
parent 5ec6bf8b88
commit e8eeae3474
3 changed files with 66 additions and 12 deletions

View File

@ -51,6 +51,7 @@
#include "nsIHTMLEditor.h"
#include "nsIDocShell.h"
#include "mozilla/dom/EncodingUtils.h"
#include "nsComputedDOMStyle.h"
using namespace mozilla;
using namespace mozilla::dom;
@ -1389,19 +1390,23 @@ nsHTMLCopyEncoder::SetSelection(nsISelection* aSelection)
mIsTextWidget = true;
break;
}
else if (atom == nsGkAtoms::body)
{
// check for moz prewrap style on body. If it's there we are
// in a plaintext editor. This is pretty cheezy but I haven't
// found a good way to tell if we are in a plaintext editor.
nsCOMPtr<nsIDOMElement> bodyElem = do_QueryInterface(selContent);
nsAutoString wsVal;
rv = bodyElem->GetAttribute(NS_LITERAL_STRING("style"), wsVal);
if (NS_SUCCEEDED(rv) && (kNotFound != wsVal.Find(NS_LITERAL_STRING("pre-wrap"))))
{
mIsTextWidget = true;
break;
else if (selContent->IsElement()) {
nsRefPtr<nsStyleContext> styleContext =
nsComputedDOMStyle::GetStyleContextForElementNoFlush(selContent->AsElement(),
nullptr, nullptr);
if (styleContext) {
const nsStyleText* textStyle = styleContext->StyleText();
switch (textStyle->mWhiteSpace) {
case NS_STYLE_WHITESPACE_PRE:
case NS_STYLE_WHITESPACE_PRE_WRAP:
case NS_STYLE_WHITESPACE_PRE_LINE:
case NS_STYLE_WHITESPACE_PRE_SPACE:
// Copy as plaintext for all preformatted elements
mIsTextWidget = true;
break;
}
}
break;
}
}

View File

@ -243,6 +243,7 @@ support-files =
[test_audioWindowUtils.html]
[test_audioNotification.html]
skip-if = buildapp == 'mulet'
[test_bug116083.html]
[test_bug793311.html]
[test_bug913761.html]
[test_bug976673.html]

View File

@ -0,0 +1,48 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=116083
-->
<head>
<title>Test for Bug 116083</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=116083">Mozilla Bug 116083</a>
<div id="content">
<div style="white-space: pre">foo bar</div>
<div style="white-space: pre-wrap">foo bar</div>
<div style="white-space: pre-line">foo bar</div>
<div style="white-space: -moz-pre-space">foo bar</div>
<div data-result="&#10;foo bar&#10;">foo bar</div>
</div>
<script type="application/javascript">
SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(function nextTest() {
var div = document.querySelector("#content>div");
if (!div) {
SimpleTest.finish();
return;
}
getSelection().selectAllChildren(div);
var expected = div.hasAttribute("data-result") ?
div.getAttribute("data-result") :
div.textContent;
SimpleTest.waitForClipboard(expected, function() {
synthesizeKey("C", {accelKey: true});
}, function() {
ok(true, div.getAttribute("style") + " passed");
div.parentNode.removeChild(div);
nextTest();
}, function() {
ok(false, "failed to copy the expected content to the clipboard");
SimpleTest.finish();
});
});
</script>
</body>
</html>