Bug 801486 - Skip script elements in HTMLOptionElement.prototype.text; r=bz
authorMs2ger <ms2ger@gmail.com>
Sun, 21 Oct 2012 09:38:40 +0200
changeset 111026 16bf5abecb5b7417c6832fea4144f69b3f90aa23
parent 111025 84686086802ca5a134596a7f7d39384f1239a0f9
child 111027 fc9c3cc0a528c009573e62664973fc11a5ed57e2
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersbz
bugs801486
milestone19.0a1
Bug 801486 - Skip script elements in HTMLOptionElement.prototype.text; r=bz
content/html/content/src/nsHTMLOptionElement.cpp
content/html/content/test/forms/test_option_text.html
--- a/content/html/content/src/nsHTMLOptionElement.cpp
+++ b/content/html/content/src/nsHTMLOptionElement.cpp
@@ -255,17 +255,29 @@ nsHTMLOptionElement::BeforeSetAttr(int32
 
   return rv;
 }
 
 NS_IMETHODIMP
 nsHTMLOptionElement::GetText(nsAString& aText)
 {
   nsAutoString text;
-  nsContentUtils::GetNodeTextContent(this, true, text);
+
+  nsIContent* child = nsINode::GetFirstChild();
+  while (child) {
+    if (child->NodeType() == nsIDOMNode::TEXT_NODE ||
+        child->NodeType() == nsIDOMNode::CDATA_SECTION_NODE) {
+      child->AppendTextTo(text);
+    }
+    if (child->IsHTML(nsGkAtoms::script) || child->IsSVG(nsGkAtoms::script)) {
+      child = child->GetNextNonChildNode(this);
+    } else {
+      child = child->GetNextNode(this);
+    }
+  }
 
   // XXX No CompressWhitespace for nsAString.  Sad.
   text.CompressWhitespace(true, true);
   aText = text;
 
   return NS_OK;
 }
 
--- a/content/html/content/test/forms/test_option_text.html
+++ b/content/html/content/test/forms/test_option_text.html
@@ -7,10 +7,51 @@
 <script src="/resources/testharnessreport.js"></script>
 <div id=log></div>
 <script>
 test(function() {
   var option = document.createElement("option");
   option.appendChild(document.createElement("font"))
         .appendChild(document.createTextNode(" font "));
   assert_equals(option.text, "font");
-});
+}, "option.text should recurse");
+test(function() {
+  var option = document.createElement("option");
+  option.appendChild(document.createTextNode(" before "));
+  option.appendChild(document.createElement("script"))
+        .appendChild(document.createTextNode(" script "));
+  option.appendChild(document.createTextNode(" after "));
+  assert_equals(option.text, "before after");
+}, "option.text should not recurse into HTML script elements");
+test(function() {
+  var option = document.createElement("option");
+  option.appendChild(document.createTextNode(" before "));
+  option.appendChild(document.createElementNS("http://www.w3.org/2000/svg", "script"))
+        .appendChild(document.createTextNode(" script "));
+  option.appendChild(document.createTextNode(" after "));
+  assert_equals(option.text, "before after");
+}, "option.text should not recurse into SVG script elements");
+test(function() {
+  var option = document.createElement("option");
+  option.appendChild(document.createTextNode(" before "));
+  option.appendChild(document.createElementNS("http://www.w3.org/1998/Math/MathML", "script"))
+        .appendChild(document.createTextNode(" script "));
+  option.appendChild(document.createTextNode(" after "));
+  assert_equals(option.text, "before script after");
+}, "option.text should recurse into MathML script elements");
+test(function() {
+  var option = document.createElement("option");
+  option.appendChild(document.createTextNode(" before "));
+  option.appendChild(document.createElementNS(null, "script"))
+        .appendChild(document.createTextNode(" script "));
+  option.appendChild(document.createTextNode(" after "));
+  assert_equals(option.text, "before script after");
+}, "option.text should recurse into null script elements");
+test(function() {
+  var option = document.createElement("option");
+  var span = option.appendChild(document.createElement("span"));
+  span.appendChild(document.createTextNode(" Some "));
+  span.appendChild(document.createElement("script"))
+      .appendChild(document.createTextNode(" script "));
+  option.appendChild(document.createTextNode(" Text "));
+  assert_equals(option.text, "Some Text");
+}, "option.text should work if a child of the option ends with a script");
 </script>