Bug 441339 - Simplify number-optional-number parsing. r+sr=roc

This commit is contained in:
Robert Longson 2008-06-24 13:54:11 +01:00
parent f5c57c867f
commit 343cf22257
5 changed files with 93 additions and 190 deletions

View File

@ -316,7 +316,15 @@ nsSVGElement::ParseAttribute(PRInt32 aNamespaceID,
NumberAttributesInfo numberInfo = GetNumberInfo(); NumberAttributesInfo numberInfo = GetNumberInfo();
for (i = 0; i < numberInfo.mNumberCount; i++) { for (i = 0; i < numberInfo.mNumberCount; i++) {
if (aAttribute == *numberInfo.mNumberInfo[i].mName) { if (aAttribute == *numberInfo.mNumberInfo[i].mName) {
rv = numberInfo.mNumbers[i].SetBaseValueString(aValue, this, PR_FALSE); if (i + 1 < numberInfo.mNumberCount &&
aAttribute == *numberInfo.mNumberInfo[i + 1].mName) {
rv = ParseNumberOptionalNumber(aValue, i, i + 1);
if (NS_FAILED(rv)) {
numberInfo.Reset(i + 1);
}
} else {
rv = numberInfo.mNumbers[i].SetBaseValueString(aValue, this, PR_FALSE);
}
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
numberInfo.Reset(i); numberInfo.Reset(i);
} }
@ -331,7 +339,15 @@ nsSVGElement::ParseAttribute(PRInt32 aNamespaceID,
IntegerAttributesInfo integerInfo = GetIntegerInfo(); IntegerAttributesInfo integerInfo = GetIntegerInfo();
for (i = 0; i < integerInfo.mIntegerCount; i++) { for (i = 0; i < integerInfo.mIntegerCount; i++) {
if (aAttribute == *integerInfo.mIntegerInfo[i].mName) { if (aAttribute == *integerInfo.mIntegerInfo[i].mName) {
rv = integerInfo.mIntegers[i].SetBaseValueString(aValue, this, PR_FALSE); if (i + 1 < integerInfo.mIntegerCount &&
aAttribute == *integerInfo.mIntegerInfo[i + 1].mName) {
rv = ParseIntegerOptionalInteger(aValue, i, i + 1);
if (NS_FAILED(rv)) {
integerInfo.Reset(i + 1);
}
} else {
rv = integerInfo.mIntegers[i].SetBaseValueString(aValue, this, PR_FALSE);
}
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
integerInfo.Reset(i); integerInfo.Reset(i);
} }
@ -443,6 +459,7 @@ nsSVGElement::UnsetAttr(PRInt32 aNamespaceID, nsIAtom* aName,
lenInfo.Reset(i); lenInfo.Reset(i);
DidChangeLength(i, PR_FALSE); DidChangeLength(i, PR_FALSE);
foundMatch = PR_TRUE; foundMatch = PR_TRUE;
break;
} }
} }
} }
@ -453,9 +470,16 @@ nsSVGElement::UnsetAttr(PRInt32 aNamespaceID, nsIAtom* aName,
for (PRUint32 i = 0; i < numInfo.mNumberCount; i++) { for (PRUint32 i = 0; i < numInfo.mNumberCount; i++) {
if (aName == *numInfo.mNumberInfo[i].mName) { if (aName == *numInfo.mNumberInfo[i].mName) {
if (i + 1 < numInfo.mNumberCount &&
aName == *numInfo.mNumberInfo[i + 1].mName) {
// found a number-optional-number
numInfo.Reset(i + 1);
DidChangeNumber(i + 1, PR_FALSE);
}
numInfo.Reset(i); numInfo.Reset(i);
DidChangeNumber(i, PR_FALSE); DidChangeNumber(i, PR_FALSE);
foundMatch = PR_TRUE; foundMatch = PR_TRUE;
break;
} }
} }
} }
@ -466,9 +490,16 @@ nsSVGElement::UnsetAttr(PRInt32 aNamespaceID, nsIAtom* aName,
for (PRUint32 i = 0; i < intInfo.mIntegerCount; i++) { for (PRUint32 i = 0; i < intInfo.mIntegerCount; i++) {
if (aName == *intInfo.mIntegerInfo[i].mName) { if (aName == *intInfo.mIntegerInfo[i].mName) {
if (i + 1 < intInfo.mIntegerCount &&
aName == *intInfo.mIntegerInfo[i + 1].mName) {
// found a number-optional-number
intInfo.Reset(i + 1);
DidChangeNumber(i + 1, PR_FALSE);
}
intInfo.Reset(i); intInfo.Reset(i);
DidChangeInteger(i, PR_FALSE); DidChangeInteger(i, PR_FALSE);
foundMatch = PR_TRUE; foundMatch = PR_TRUE;
break;
} }
} }
} }
@ -482,6 +513,7 @@ nsSVGElement::UnsetAttr(PRInt32 aNamespaceID, nsIAtom* aName,
angleInfo.Reset(i); angleInfo.Reset(i);
DidChangeAngle(i, PR_FALSE); DidChangeAngle(i, PR_FALSE);
foundMatch = PR_TRUE; foundMatch = PR_TRUE;
break;
} }
} }
} }
@ -508,6 +540,7 @@ nsSVGElement::UnsetAttr(PRInt32 aNamespaceID, nsIAtom* aName,
enumInfo.Reset(i); enumInfo.Reset(i);
DidChangeEnum(i, PR_FALSE); DidChangeEnum(i, PR_FALSE);
foundMatch = PR_TRUE; foundMatch = PR_TRUE;
break;
} }
} }
} }
@ -523,6 +556,7 @@ nsSVGElement::UnsetAttr(PRInt32 aNamespaceID, nsIAtom* aName,
stringInfo.Reset(i); stringInfo.Reset(i);
DidChangeString(i, PR_FALSE); DidChangeString(i, PR_FALSE);
foundMatch = PR_TRUE; foundMatch = PR_TRUE;
break;
} }
} }
} }
@ -1375,104 +1409,87 @@ nsSVGElement::DidChangeString(PRUint8 aAttrEnum, PRBool aDoSetAttr)
info.mStrings[aAttrEnum].GetBaseValue(), PR_TRUE); info.mStrings[aAttrEnum].GetBaseValue(), PR_TRUE);
} }
PRBool nsresult
nsSVGElement::ParseNumberOptionalNumber(nsIAtom* aAttribute, const nsAString& aValue, nsSVGElement::ParseNumberOptionalNumber(const nsAString& aValue,
PRUint32 aIndex1, PRUint32 aIndex2, PRUint32 aIndex1, PRUint32 aIndex2)
nsAttrValue& aResult)
{ {
NS_ConvertUTF16toUTF8 value(aValue); NS_ConvertUTF16toUTF8 value(aValue);
const char *str = value.get(); const char *str = value.get();
PRBool parseError = NS_IsAsciiWhitespace(*str); if (NS_IsAsciiWhitespace(*str))
float x, y; return NS_ERROR_FAILURE;
if (!parseError) { char *rest;
char *rest; float x = float(PR_strtod(str, &rest));
x = y = float(PR_strtod(str, &rest)); float y = x;
if (str == rest) { if (str == rest) {
//first value was illformed //first value was illformed
parseError = PR_TRUE; return NS_ERROR_FAILURE;
} else if (*rest != '\0') { }
while (NS_IsAsciiWhitespace(*rest)) {
++rest; if (*rest != '\0') {
} while (NS_IsAsciiWhitespace(*rest)) {
if (*rest == ',') { ++rest;
++rest; }
} if (*rest == ',') {
++rest;
}
y = float(PR_strtod(rest, &rest)); y = float(PR_strtod(rest, &rest));
if (*rest != '\0') { if (*rest != '\0') {
//second value was illformed or there was trailing content //second value was illformed or there was trailing content
parseError = PR_TRUE; return NS_ERROR_FAILURE;
}
} }
} }
NumberAttributesInfo numberInfo = GetNumberInfo(); NumberAttributesInfo numberInfo = GetNumberInfo();
if (parseError) {
ReportAttributeParseFailure(GetOwnerDoc(), aAttribute, aValue);
x = numberInfo.mNumberInfo[aIndex1].mDefaultValue;
y = numberInfo.mNumberInfo[aIndex2].mDefaultValue;
} else {
aResult.SetTo(aValue);
}
numberInfo.mNumbers[aIndex1].SetBaseValue(x, this, PR_FALSE); numberInfo.mNumbers[aIndex1].SetBaseValue(x, this, PR_FALSE);
numberInfo.mNumbers[aIndex2].SetBaseValue(y, this, PR_FALSE); numberInfo.mNumbers[aIndex2].SetBaseValue(y, this, PR_FALSE);
return NS_OK;
return (!parseError);
} }
PRBool nsresult
nsSVGElement::ParseIntegerOptionalInteger(nsIAtom* aAttribute, const nsAString& aValue, nsSVGElement::ParseIntegerOptionalInteger(const nsAString& aValue,
PRUint32 aIndex1, PRUint32 aIndex2, PRUint32 aIndex1, PRUint32 aIndex2)
nsAttrValue& aResult)
{ {
NS_ConvertUTF16toUTF8 value(aValue); NS_ConvertUTF16toUTF8 value(aValue);
const char *str = value.get(); const char *str = value.get();
PRBool parseError = NS_IsAsciiWhitespace(*str); if (NS_IsAsciiWhitespace(*str))
PRInt32 x, y; return NS_ERROR_FAILURE;
if (!parseError) { char *rest;
char *rest; PRInt32 x = strtol(str, &rest, 10);
x = y = strtol(str, &rest, 10); PRInt32 y = x;
if (str == rest) { if (str == rest) {
//first value was illformed //first value was illformed
parseError = PR_TRUE; return NS_ERROR_FAILURE;
} else if (*rest != '\0') { }
while (NS_IsAsciiWhitespace(*rest)) {
++rest; if (*rest != '\0') {
} while (NS_IsAsciiWhitespace(*rest)) {
if (*rest == ',') { ++rest;
++rest; }
} if (*rest == ',') {
++rest;
}
y = strtol(rest, &rest, 10); y = strtol(rest, &rest, 10);
if (*rest != '\0') { if (*rest != '\0') {
//second value was illformed or there was trailing content //second value was illformed or there was trailing content
parseError = PR_TRUE; return NS_ERROR_FAILURE;
}
} }
} }
IntegerAttributesInfo integerInfo = GetIntegerInfo(); IntegerAttributesInfo integerInfo = GetIntegerInfo();
if (parseError) {
ReportAttributeParseFailure(GetOwnerDoc(), aAttribute, aValue);
x = integerInfo.mIntegerInfo[aIndex1].mDefaultValue;
y = integerInfo.mIntegerInfo[aIndex2].mDefaultValue;
} else {
aResult.SetTo(aValue);
}
integerInfo.mIntegers[aIndex1].SetBaseValue(x, this, PR_FALSE); integerInfo.mIntegers[aIndex1].SetBaseValue(x, this, PR_FALSE);
integerInfo.mIntegers[aIndex2].SetBaseValue(y, this, PR_FALSE); integerInfo.mIntegers[aIndex2].SetBaseValue(y, this, PR_FALSE);
return (!parseError); return NS_OK;
} }
nsresult nsresult

View File

@ -310,23 +310,21 @@ protected:
static nsSVGEnumMapping sSVGUnitTypesMap[]; static nsSVGEnumMapping sSVGUnitTypesMap[];
private:
/* read <number-optional-number> */ /* read <number-optional-number> */
PRBool nsresult
ParseNumberOptionalNumber(nsIAtom* aAttribute, const nsAString& aValue, ParseNumberOptionalNumber(const nsAString& aValue,
PRUint32 aIndex1, PRUint32 aIndex2, PRUint32 aIndex1, PRUint32 aIndex2);
nsAttrValue& aResult);
/* read <integer-optional-integer> */ /* read <integer-optional-integer> */
PRBool nsresult
ParseIntegerOptionalInteger(nsIAtom* aAttribute, const nsAString& aValue, ParseIntegerOptionalInteger(const nsAString& aValue,
PRUint32 aIndex1, PRUint32 aIndex2, PRUint32 aIndex1, PRUint32 aIndex2);
nsAttrValue& aResult);
static nsresult ReportAttributeParseFailure(nsIDocument* aDocument, static nsresult ReportAttributeParseFailure(nsIDocument* aDocument,
nsIAtom* aAttribute, nsIAtom* aAttribute,
const nsAString& aValue); const nsAString& aValue);
private:
void ResetOldStyleBaseType(nsISVGValue *svg_value); void ResetOldStyleBaseType(nsISVGValue *svg_value);
nsCOMPtr<nsICSSStyleRule> mContentStyleRule; nsCOMPtr<nsICSSStyleRule> mContentStyleRule;

View File

@ -175,20 +175,6 @@ nsSVGFilterElement::GetHref(nsIDOMSVGAnimatedString * *aHref)
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// nsIContent methods // nsIContent methods
PRBool
nsSVGFilterElement::ParseAttribute(PRInt32 aNameSpaceID, nsIAtom* aName,
const nsAString& aValue,
nsAttrValue& aResult)
{
if (aName == nsGkAtoms::filterRes && aNameSpaceID == kNameSpaceID_None) {
return ParseIntegerOptionalInteger(aName, aValue,
FILTERRES_X, FILTERRES_Y,
aResult);
}
return nsSVGFilterElementBase::ParseAttribute(aNameSpaceID, aName,
aValue, aResult);
}
NS_IMETHODIMP_(PRBool) NS_IMETHODIMP_(PRBool)
nsSVGFilterElement::IsAttributeMapped(const nsIAtom* name) const nsSVGFilterElement::IsAttributeMapped(const nsIAtom* name) const
{ {

View File

@ -85,10 +85,6 @@ public:
protected: protected:
virtual PRBool ParseAttribute(PRInt32 aNameSpaceID, nsIAtom* aName,
const nsAString& aValue,
nsAttrValue& aResult);
virtual LengthAttributesInfo GetLengthInfo(); virtual LengthAttributesInfo GetLengthInfo();
virtual IntegerAttributesInfo GetIntegerInfo(); virtual IntegerAttributesInfo GetIntegerInfo();
virtual EnumAttributesInfo GetEnumInfo(); virtual EnumAttributesInfo GetEnumInfo();

View File

@ -528,9 +528,6 @@ public:
NS_FORWARD_NSIDOMNODE(nsSVGFEGaussianBlurElementBase::) NS_FORWARD_NSIDOMNODE(nsSVGFEGaussianBlurElementBase::)
NS_FORWARD_NSIDOMELEMENT(nsSVGFEGaussianBlurElementBase::) NS_FORWARD_NSIDOMELEMENT(nsSVGFEGaussianBlurElementBase::)
virtual PRBool ParseAttribute(PRInt32 aNameSpaceID, nsIAtom* aName,
const nsAString& aValue,
nsAttrValue& aResult);
virtual nsresult Clone(nsINodeInfo *aNodeInfo, nsINode **aResult) const; virtual nsresult Clone(nsINodeInfo *aNodeInfo, nsINode **aResult) const;
protected: protected:
@ -631,20 +628,6 @@ nsSVGFEGaussianBlurElement::SetStdDeviation(float stdDeviationX, float stdDeviat
return NS_OK; return NS_OK;
} }
PRBool
nsSVGFEGaussianBlurElement::ParseAttribute(PRInt32 aNameSpaceID, nsIAtom* aName,
const nsAString& aValue,
nsAttrValue& aResult)
{
if (aName == nsGkAtoms::stdDeviation && aNameSpaceID == kNameSpaceID_None) {
return ParseNumberOptionalNumber(aName, aValue,
STD_DEV_X, STD_DEV_Y,
aResult);
}
return nsSVGFEGaussianBlurElementBase::ParseAttribute(aNameSpaceID, aName,
aValue, aResult);
}
void void
nsSVGFEGaussianBlurElement::BoxBlurH(PRUint8 *aInput, PRUint8 *aOutput, nsSVGFEGaussianBlurElement::BoxBlurH(PRUint8 *aInput, PRUint8 *aOutput,
PRInt32 aStride, const nsRect &aRegion, PRInt32 aStride, const nsRect &aRegion,
@ -3110,9 +3093,6 @@ public:
NS_FORWARD_NSIDOMNODE(nsSVGFETurbulenceElementBase::) NS_FORWARD_NSIDOMNODE(nsSVGFETurbulenceElementBase::)
NS_FORWARD_NSIDOMELEMENT(nsSVGFETurbulenceElementBase::) NS_FORWARD_NSIDOMELEMENT(nsSVGFETurbulenceElementBase::)
virtual PRBool ParseAttribute(PRInt32 aNameSpaceID, nsIAtom* aName,
const nsAString& aValue,
nsAttrValue& aResult);
virtual nsresult Clone(nsINodeInfo *aNodeInfo, nsINode **aResult) const; virtual nsresult Clone(nsINodeInfo *aNodeInfo, nsINode **aResult) const;
protected: protected:
@ -3307,20 +3287,6 @@ NS_IMETHODIMP nsSVGFETurbulenceElement::GetType(nsIDOMSVGAnimatedEnumeration * *
return mEnumAttributes[TYPE].ToDOMAnimatedEnum(aType, this); return mEnumAttributes[TYPE].ToDOMAnimatedEnum(aType, this);
} }
PRBool
nsSVGFETurbulenceElement::ParseAttribute(PRInt32 aNameSpaceID, nsIAtom* aName,
const nsAString& aValue,
nsAttrValue& aResult)
{
if (aName == nsGkAtoms::baseFrequency && aNameSpaceID == kNameSpaceID_None) {
return ParseNumberOptionalNumber(aName, aValue,
BASE_FREQ_X, BASE_FREQ_Y,
aResult);
}
return nsSVGFETurbulenceElementBase::ParseAttribute(aNameSpaceID, aName,
aValue, aResult);
}
nsresult nsresult
nsSVGFETurbulenceElement::Filter(nsSVGFilterInstance *instance) nsSVGFETurbulenceElement::Filter(nsSVGFilterInstance *instance)
{ {
@ -3638,9 +3604,6 @@ public:
NS_FORWARD_NSIDOMNODE(nsSVGFEMorphologyElementBase::) NS_FORWARD_NSIDOMNODE(nsSVGFEMorphologyElementBase::)
NS_FORWARD_NSIDOMELEMENT(nsSVGFEMorphologyElementBase::) NS_FORWARD_NSIDOMELEMENT(nsSVGFEMorphologyElementBase::)
virtual PRBool ParseAttribute(PRInt32 aNameSpaceID, nsIAtom* aName,
const nsAString& aValue,
nsAttrValue& aResult);
virtual nsresult Clone(nsINodeInfo *aNodeInfo, nsINode **aResult) const; virtual nsresult Clone(nsINodeInfo *aNodeInfo, nsINode **aResult) const;
protected: protected:
@ -3751,20 +3714,6 @@ nsSVGFEMorphologyElement::SetRadius(float rx, float ry)
return NS_OK; return NS_OK;
} }
PRBool
nsSVGFEMorphologyElement::ParseAttribute(PRInt32 aNameSpaceID, nsIAtom* aName,
const nsAString& aValue,
nsAttrValue& aResult)
{
if (aName == nsGkAtoms::radius && aNameSpaceID == kNameSpaceID_None) {
return ParseNumberOptionalNumber(aName, aValue,
RADIUS_X, RADIUS_Y,
aResult);
}
return nsSVGFEMorphologyElementBase::ParseAttribute(aNameSpaceID, aName,
aValue, aResult);
}
void void
nsSVGFEMorphologyElement::GetSourceImageNames(nsTArray<nsSVGString*>* aSources) nsSVGFEMorphologyElement::GetSourceImageNames(nsTArray<nsSVGString*>* aSources)
{ {
@ -3972,9 +3921,6 @@ public:
NS_FORWARD_NSIDOMNODE(nsSVGFEConvolveMatrixElementBase::) NS_FORWARD_NSIDOMNODE(nsSVGFEConvolveMatrixElementBase::)
NS_FORWARD_NSIDOMELEMENT(nsSVGFEConvolveMatrixElementBase::) NS_FORWARD_NSIDOMELEMENT(nsSVGFEConvolveMatrixElementBase::)
virtual PRBool ParseAttribute(PRInt32 aNameSpaceID, nsIAtom* aName,
const nsAString& aValue,
nsAttrValue& aResult);
virtual nsresult Clone(nsINodeInfo *aNodeInfo, nsINode **aResult) const; virtual nsresult Clone(nsINodeInfo *aNodeInfo, nsINode **aResult) const;
protected: protected:
@ -4197,28 +4143,6 @@ nsSVGFEConvolveMatrixElement::ComputeNeededSourceBBoxes(const nsRect& aTargetBBo
// source's output bounding box. // source's output bounding box.
} }
PRBool
nsSVGFEConvolveMatrixElement::ParseAttribute(PRInt32 aNameSpaceID, nsIAtom* aName,
const nsAString& aValue,
nsAttrValue& aResult)
{
if (aNameSpaceID == kNameSpaceID_None) {
if (aName == nsGkAtoms::order) {
return ParseIntegerOptionalInteger(aName, aValue,
ORDER_X, ORDER_Y,
aResult);
}
if (aName == nsGkAtoms::kernelUnitLength) {
return ParseNumberOptionalNumber(aName, aValue,
KERNEL_UNIT_LENGTH_X, KERNEL_UNIT_LENGTH_Y,
aResult);
}
}
return nsSVGFEConvolveMatrixElementBase::ParseAttribute(aNameSpaceID, aName,
aValue, aResult);
}
static PRInt32 BoundInterval(PRInt32 aVal, PRInt32 aMax) static PRInt32 BoundInterval(PRInt32 aVal, PRInt32 aMax)
{ {
aVal = PR_MAX(aVal, 0); aVal = PR_MAX(aVal, 0);
@ -4770,9 +4694,6 @@ public:
NS_IMETHOD_(PRBool) IsAttributeMapped(const nsIAtom* aAttribute) const; NS_IMETHOD_(PRBool) IsAttributeMapped(const nsIAtom* aAttribute) const;
virtual PRBool ParseAttribute(PRInt32 aNameSpaceID, nsIAtom* aName,
const nsAString& aValue,
nsAttrValue& aResult);
protected: protected:
virtual void virtual void
LightPixel(const float *N, const float *L, LightPixel(const float *N, const float *L,
@ -4830,21 +4751,6 @@ nsSVGFELightingElement::IsAttributeMapped(const nsIAtom* name) const
nsSVGFELightingElementBase::IsAttributeMapped(name); nsSVGFELightingElementBase::IsAttributeMapped(name);
} }
PRBool
nsSVGFELightingElement::ParseAttribute(PRInt32 aNameSpaceID, nsIAtom* aName,
const nsAString& aValue,
nsAttrValue& aResult)
{
if (aName == nsGkAtoms::kernelUnitLength && aNameSpaceID == kNameSpaceID_None) {
return ParseNumberOptionalNumber(aName, aValue,
KERNEL_UNIT_LENGTH_X, KERNEL_UNIT_LENGTH_Y,
aResult);
}
return nsSVGFELightingElementBase::ParseAttribute(aNameSpaceID, aName,
aValue, aResult);
}
void void
nsSVGFELightingElement::GetSourceImageNames(nsTArray<nsSVGString*>* aSources) nsSVGFELightingElement::GetSourceImageNames(nsTArray<nsSVGString*>* aSources)
{ {