Don't parse "style" attributes in data documents unless someone asks for

.style.  Bug 418214, r+sr=peterv, a=schrep


git-svn-id: svn://10.0.0.236/trunk@245987 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
bzbarsky%mit.edu
2008-02-19 17:52:01 +00:00
parent 96250bbfc2
commit 92b2b69ed3
6 changed files with 105 additions and 15 deletions

View File

@@ -86,7 +86,7 @@ nsStyledElement::ParseAttribute(PRInt32 aNamespaceID, nsIAtom* aAttribute,
if (aNamespaceID == kNameSpaceID_None) {
if (aAttribute == nsGkAtoms::style) {
SetFlags(NODE_MAY_HAVE_STYLE);
ParseStyleAttribute(this, aValue, aResult);
ParseStyleAttribute(this, aValue, aResult, PR_FALSE);
return PR_TRUE;
}
if (aAttribute == nsGkAtoms::_class) {
@@ -165,7 +165,7 @@ nsStyledElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
// XXXbz if we already have a style attr parsed, this won't do
// anything... need to fix that.
ReparseStyleAttribute();
ReparseStyleAttribute(PR_FALSE);
return rv;
}
@@ -184,7 +184,7 @@ nsStyledElement::GetStyle(nsIDOMCSSStyleDeclaration** aStyle)
if (!slots->mStyle) {
// Just in case...
ReparseStyleAttribute();
ReparseStyleAttribute(PR_TRUE);
nsresult rv;
if (!gCSSOMFactory) {
@@ -204,7 +204,7 @@ nsStyledElement::GetStyle(nsIDOMCSSStyleDeclaration** aStyle)
}
nsresult
nsStyledElement::ReparseStyleAttribute()
nsStyledElement::ReparseStyleAttribute(PRBool aForceInDataDoc)
{
if (!HasFlag(NODE_MAY_HAVE_STYLE)) {
return NS_OK;
@@ -215,7 +215,7 @@ nsStyledElement::ReparseStyleAttribute()
nsAttrValue attrValue;
nsAutoString stringValue;
oldVal->ToString(stringValue);
ParseStyleAttribute(this, stringValue, attrValue);
ParseStyleAttribute(this, stringValue, attrValue, aForceInDataDoc);
// Don't bother going through SetInlineStyleRule, we don't want to fire off
// mutation events or document notifications anyway
nsresult rv = mAttrsAndChildren.SetAndTakeAttr(nsGkAtoms::style, attrValue);
@@ -228,12 +228,13 @@ nsStyledElement::ReparseStyleAttribute()
void
nsStyledElement::ParseStyleAttribute(nsIContent* aContent,
const nsAString& aValue,
nsAttrValue& aResult)
nsAttrValue& aResult,
PRBool aForceInDataDoc)
{
nsresult result = NS_OK;
nsIDocument* doc = aContent->GetOwnerDoc();
if (doc) {
if (doc && (aForceInDataDoc || !doc->IsLoadedAsData())) {
PRBool isCSS = PR_TRUE; // assume CSS until proven otherwise
if (!aContent->IsNativeAnonymous()) { // native anonymous content

View File

@@ -76,11 +76,6 @@ public:
nsIContent* aBindingParent,
PRBool aCompileEventHandlers);
/**
* Create the style struct from the style attr. Used when an element is first
* put into a document. Only has an effect if the old value is a string.
*/
nsresult ReparseStyleAttribute(void);
/**
* Parse a style attr value into a CSS rulestruct (or, if there is no
* document, leave it as a string) and return as nsAttrValue.
@@ -91,7 +86,8 @@ public:
*/
static void ParseStyleAttribute(nsIContent* aContent,
const nsAString& aValue,
nsAttrValue& aResult);
nsAttrValue& aResult,
PRBool aForceInDataDoc);
static void Shutdown();
@@ -102,6 +98,13 @@ protected:
nsresult GetStyle(nsIDOMCSSStyleDeclaration** aStyle);
/**
* Create the style struct from the style attr. Used when an element is
* first put into a document. Only has an effect if the old value is a
* string. If aForceInDataDoc is true, will reparse even if we're in a data
* document.
*/
nsresult ReparseStyleAttribute(PRBool aForceInDataDoc);
};
#endif // __NS_STYLEDELEMENT_H_

View File

@@ -144,6 +144,7 @@ _TEST_FILES = test_bug5141.html \
test_bug414796.html \
test_bug416383.html \
test_bug417384.html \
test_bug418214.html \
$(NULL)
libs:: $(_TEST_FILES)

View File

@@ -0,0 +1,84 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=418214
-->
<head>
<title>Test for Bug 418214</title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="text/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=418214">Mozilla Bug 418214</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
var str = '<root xmlns:html="http://www.w3.org/1999/xhtml" xmlns:svg="http://www.w3.org/2000/svg" xmlns:math="http://www.w3.org/1998/Math/MathML"><html:div id="d" style="border:: invalid"/><svg:svg id="s" style="border:: invalid"/><math:math id="m" style="border:: invalid"/></root>';
/** Test for Bug 418214 **/
var doc = (new DOMParser()).parseFromString(str, "text/xml");
var d = doc.getElementById("d");
var s = doc.getElementById("s");
var m = doc.getElementById("m");
is(d.getAttribute("style"), "border:: invalid",
"Shouldn't be parsing style on HTML in data documents");
is(s.getAttribute("style"), "border:: invalid",
"Shouldn't be parsing style on SVG in data documents");
is(m.getAttribute("style"), "border:: invalid",
"Shouldn't be parsing style on MathML in data documents");
var d2 = d.cloneNode(true);
var s2 = s.cloneNode(true);
var m2 = m.cloneNode(true);
is(d2.getAttribute("style"), "border:: invalid",
"Shouldn't be parsing style on HTML on clone");
is(s2.getAttribute("style"), "border:: invalid",
"Shouldn't be parsing style on SVG on clone");
is(m2.getAttribute("style"), "border:: invalid",
"Shouldn't be parsing style on MathML on clone");
d2.style;
s2.style;
m2.style;
is(d2.getAttribute("style"), "",
"Getting .style should parse style on HTML");
is(s2.getAttribute("style"), "",
"Getting .style should parse style on SVG");
is(m2.getAttribute("style"), "border:: invalid",
"Getting .style shouldn't parse style on MathML");
d = document.adoptNode(d);
s = document.adoptNode(s);
m = document.adoptNode(m);
is(d.getAttribute("style"), "border:: invalid",
"Adopting should not parse style on HTML");
is(s.getAttribute("style"), "border:: invalid",
"Adopting should not parse style on SVG");
is(m.getAttribute("style"), "border:: invalid",
"Adopting should not parse style on MathML");
$("display").appendChild(d);
$("display").appendChild(s);
$("display").appendChild(m);
is(d.getAttribute("style"), "",
"Insertion should parse style on HTML");
is(s.getAttribute("style"), "",
"Insertion should parse style on SVG");
is(m.getAttribute("style"), "",
"Insertion should parse style on MathML");
</script>
</pre>
</body>
</html>

View File

@@ -195,7 +195,8 @@ nsSVGElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
nsAttrValue attrValue;
nsAutoString stringValue;
oldVal->ToString(stringValue);
ParseStyleAttribute(this, stringValue, attrValue);
// Force in data doc, since we already have a style rule
ParseStyleAttribute(this, stringValue, attrValue, PR_TRUE);
// Don't bother going through SetInlineStyleRule, we don't want to fire off
// mutation events or document notifications anyway
rv = mAttrsAndChildren.SetAndTakeAttr(nsGkAtoms::style, attrValue);

View File

@@ -1088,7 +1088,7 @@ nsXULElement::ParseAttribute(PRInt32 aNamespaceID,
if (aNamespaceID == kNameSpaceID_None) {
if (aAttribute == nsGkAtoms::style) {
SetFlags(NODE_MAY_HAVE_STYLE);
nsStyledElement::ParseStyleAttribute(this, aValue, aResult);
nsStyledElement::ParseStyleAttribute(this, aValue, aResult, PR_FALSE);
return PR_TRUE;
}