Bug 1360063 - Expose the parsing code for nsSVGViewBoxRect as a method. r=longsonr

MozReview-Commit-ID: J3xv6VyvYgg

--HG--
extra : rebase_source : 007fbb2a151fd1d846fef4522c8ade002267427f
This commit is contained in:
Jonathan Watt 2017-04-10 17:02:34 +01:00
parent a696cd2ae6
commit 0ddff2c13d
2 changed files with 39 additions and 36 deletions

View File

@ -32,6 +32,41 @@ nsSVGViewBoxRect::operator==(const nsSVGViewBoxRect& aOther) const
height == aOther.height);
}
/* static */ nsresult
nsSVGViewBoxRect::FromString(const nsAString& aStr, nsSVGViewBoxRect *aViewBox)
{
if (aStr.EqualsLiteral("none")) {
aViewBox->none = true;
return NS_OK;
}
nsCharSeparatedTokenizerTemplate<IsSVGWhitespace>
tokenizer(aStr, ',',
nsCharSeparatedTokenizer::SEPARATOR_OPTIONAL);
float vals[NUM_VIEWBOX_COMPONENTS];
uint32_t i;
for (i = 0; i < NUM_VIEWBOX_COMPONENTS && tokenizer.hasMoreTokens(); ++i) {
if (!SVGContentUtils::ParseNumber(tokenizer.nextToken(), vals[i])) {
return NS_ERROR_DOM_SYNTAX_ERR;
}
}
if (i != NUM_VIEWBOX_COMPONENTS || // Too few values.
tokenizer.hasMoreTokens() || // Too many values.
tokenizer.separatorAfterCurrentToken()) { // Trailing comma.
return NS_ERROR_DOM_SYNTAX_ERR;
}
aViewBox->x = vals[0];
aViewBox->y = vals[1];
aViewBox->width = vals[2];
aViewBox->height = vals[3];
aViewBox->none = false;
return NS_OK;
}
/* Cycle collection macros for nsSVGViewBox */
NS_SVG_VAL_IMPL_CYCLE_COLLECTION_WRAPPERCACHED(nsSVGViewBox::DOMBaseVal, mSVGElement)
@ -132,40 +167,6 @@ nsSVGViewBox::SetBaseValue(const nsSVGViewBoxRect& aRect,
}
}
static nsresult
ToSVGViewBoxRect(const nsAString& aStr, nsSVGViewBoxRect *aViewBox)
{
if (aStr.EqualsLiteral("none")) {
aViewBox->none = true;
return NS_OK;
}
nsCharSeparatedTokenizerTemplate<IsSVGWhitespace>
tokenizer(aStr, ',',
nsCharSeparatedTokenizer::SEPARATOR_OPTIONAL);
float vals[NUM_VIEWBOX_COMPONENTS];
uint32_t i;
for (i = 0; i < NUM_VIEWBOX_COMPONENTS && tokenizer.hasMoreTokens(); ++i) {
if (!SVGContentUtils::ParseNumber(tokenizer.nextToken(), vals[i])) {
return NS_ERROR_DOM_SYNTAX_ERR;
}
}
if (i != NUM_VIEWBOX_COMPONENTS || // Too few values.
tokenizer.hasMoreTokens() || // Too many values.
tokenizer.separatorAfterCurrentToken()) { // Trailing comma.
return NS_ERROR_DOM_SYNTAX_ERR;
}
aViewBox->x = vals[0];
aViewBox->y = vals[1];
aViewBox->width = vals[2];
aViewBox->height = vals[3];
aViewBox->none = false;
return NS_OK;
}
nsresult
nsSVGViewBox::SetBaseValueString(const nsAString& aValue,
nsSVGElement *aSVGElement,
@ -173,7 +174,7 @@ nsSVGViewBox::SetBaseValueString(const nsAString& aValue,
{
nsSVGViewBoxRect viewBox;
nsresult rv = ToSVGViewBoxRect(aValue, &viewBox);
nsresult rv = nsSVGViewBoxRect::FromString(aValue, &viewBox);
if (NS_FAILED(rv)) {
return rv;
}
@ -318,7 +319,7 @@ nsSVGViewBox::SMILViewBox
bool& aPreventCachingOfSandwich) const
{
nsSVGViewBoxRect viewBox;
nsresult res = ToSVGViewBoxRect(aStr, &viewBox);
nsresult res = nsSVGViewBoxRect::FromString(aStr, &viewBox);
if (NS_FAILED(res)) {
return res;
}

View File

@ -38,6 +38,8 @@ struct nsSVGViewBoxRect
nsSVGViewBoxRect(const nsSVGViewBoxRect& rhs) :
x(rhs.x), y(rhs.y), width(rhs.width), height(rhs.height), none(rhs.none) {}
bool operator==(const nsSVGViewBoxRect& aOther) const;
static nsresult FromString(const nsAString& aStr, nsSVGViewBoxRect *aViewBox);
};
class nsSVGViewBox