Bug 166235: moz-user-select = 'none' should prevent copying to the clipboard. r=mats
authorGeoff Lankow <geoff@darktrojan.net>
Sat, 10 Apr 2010 16:05:58 -0400
changeset 40657 2b560e9560639bef5028dafa0e8d71c27b5745f8
parent 40656 7a792c959553fc526adeb1b0f293bf7318091faf
child 40658 40df35d082a7d9fe55cda1b13c6ba29c0c9dc11e
push id12732
push userme@kylehuey.com
push dateSat, 10 Apr 2010 21:55:14 +0000
treeherdermozilla-central@1a6aac2558e4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmats
bugs166235
milestone1.9.3a5pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
Bug 166235: moz-user-select = 'none' should prevent copying to the clipboard. r=mats
content/base/src/nsDocumentEncoder.cpp
content/base/test/Makefile.in
content/base/test/test_bug166235.html
--- a/content/base/src/nsDocumentEncoder.cpp
+++ b/content/base/src/nsDocumentEncoder.cpp
@@ -72,16 +72,17 @@
 #include "nsIParserService.h"
 #include "nsIScriptContext.h"
 #include "nsIScriptGlobalObject.h"
 #include "nsIScriptSecurityManager.h"
 #include "nsContentUtils.h"
 #include "nsUnicharUtils.h"
 #include "nsReadableUtils.h"
 #include "nsTArray.h"
+#include "nsIFrame.h"
 
 nsresult NS_NewDomSelection(nsISelection **aDomSelection);
 
 enum nsRangeIterationDirection {
   kDirectionOut = -1,
   kDirectionIn = 1
 };
 
@@ -359,16 +360,28 @@ nsDocumentEncoder::SerializeToStringRecu
   nsCOMPtr<nsIDOMNode> maybeFixedNode;
   
   if (mNodeFixup)
     mNodeFixup->FixupNode(aNode, &serializeClonedChildren, getter_AddRefs(maybeFixedNode));
 
   if (!maybeFixedNode)
     maybeFixedNode = aNode;
 
+  nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
+  if (content){
+    nsIFrame* frame = content->GetPrimaryFrame();
+    if (frame) {
+      PRBool isSelectable;
+      frame->IsSelectable(&isSelectable, nsnull);
+      if (!isSelectable){
+        aDontSerializeRoot = PR_TRUE;
+      }
+    }
+  }
+
   if (!aDontSerializeRoot) {
     rv = SerializeNodeStart(maybeFixedNode, 0, -1, aStr, aNode);
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   nsIDOMNode *node;
   if (serializeClonedChildren)
     node = maybeFixedNode;
--- a/content/base/test/Makefile.in
+++ b/content/base/test/Makefile.in
@@ -59,16 +59,17 @@ CPP_UNIT_TESTS = \
 XPCSHELL_TESTS = \
                unit \
                $(NULL)
 
 include $(topsrcdir)/config/rules.mk
 
 _TEST_FILES = 	test_bug5141.html \
 		test_bug51034.html \
+		test_bug166235.html \
 		test_bug199959.html \
 		test_bug218236.html \
 		file_bug218236_multipart.txt \
 		file_bug218236_multipart.txt^headers^ \
 		test_bug218277.html \
 		test_bug238409.html \
 		test_bug254337.html \
 		test_bug276037-1.html \
new file mode 100644
--- /dev/null
+++ b/content/base/test/test_bug166235.html
@@ -0,0 +1,60 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=166235
+-->
+<head>
+  <title>Test for Bug 166235</title>
+  <script type="application/javascript" src="/MochiKit/packed.js"></script>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.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=166235">Mozilla Bug 166235</a>
+<p id="test0">This text should be copied.</p>
+<p id="test1">This text should<span style="-moz-user-select: none;"> NOT</span> be copied.</p>
+<p id="test2">This<span style="-moz-user-select: -moz-none;"><span style="-moz-user-select: text"> text should</span> NOT</span> be copied.</p>
+<div id="content" style="display: none">
+  
+</div>
+<pre id="test">
+<script type="application/javascript">
+
+/** Test for Bug 166235 **/
+
+netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
+const de = Components.interfaces.nsIDocumentEncoder;
+var encoder = Components.classes["@mozilla.org/layout/htmlCopyEncoder;1"].createInstance(Components.interfaces.nsIDocumentEncoder);
+var selection = window.getSelection();
+
+// expected results for selection.toString()
+var originalStrings = [
+  'This text should be copied.',
+  'This text should be copied.',
+  'This text should be copied.',
+];
+
+// expected results when selection is copied and HTML tags removed
+var copiedStrings = [
+  'This text should be copied.',
+  'This text should be copied.',
+  'This text should be copied.'
+];
+
+for (var i = 0; i < 3; i++) {
+  var test = document.getElementById('test' + i);
+  selection.removeAllRanges();
+  selection.selectAllChildren(test);
+  is(selection.toString(), originalStrings [i], 'test ' + i + 'a');
+
+  selection.removeAllRanges();
+  selection.selectAllChildren(test);
+  encoder.init(document, "text/html", null);
+  encoder.setSelection(selection);
+  is(encoder.encodeToString().replace(/<[^>]*>/g, '').replace (/[\r\n]/g, ''), copiedStrings [i], 'test ' + i + 'b');
+}
+
+</script>
+</pre>
+</body>
+</html>