Bug 1383650 - Add conversion utilities to get CSS counterpart of SVG unit and AttrEnum r=longsonr

We need some utilities to convert SVG unit and attrenum to CSS unit and property id.
This is useful when we need to pass parsed geometry property directly to CSS.

Differential Revision: https://phabricator.services.mozilla.com/D30777

--HG--
extra : moz-landing-system : lando
This commit is contained in:
violet 2019-05-16 13:21:21 +00:00
parent 00adbe98b0
commit b081f29f41
11 changed files with 157 additions and 0 deletions

View File

@ -150,5 +150,20 @@ bool SVGCircleElement::IsLengthChangedViaCSS(const ComputedStyle& aNewStyle,
newSVGReset->mR != oldSVGReset->mR;
}
nsCSSPropertyID SVGCircleElement::GetCSSPropertyIdForAttrEnum(
uint8_t aAttrEnum) {
switch (aAttrEnum) {
case ATTR_CX:
return eCSSProperty_cx;
case ATTR_CY:
return eCSSProperty_cy;
case ATTR_R:
return eCSSProperty_r;
default:
MOZ_ASSERT_UNREACHABLE("Unknown attr enum");
return eCSSProperty_UNKNOWN;
}
}
} // namespace dom
} // namespace mozilla

View File

@ -7,6 +7,7 @@
#ifndef mozilla_dom_SVGCircleElement_h
#define mozilla_dom_SVGCircleElement_h
#include "nsCSSPropertyID.h"
#include "SVGGeometryElement.h"
#include "SVGAnimatedLength.h"
@ -47,6 +48,7 @@ class SVGCircleElement final : public SVGCircleElementBase {
static bool IsLengthChangedViaCSS(const ComputedStyle& aNewStyle,
const ComputedStyle& aOldStyle);
static nsCSSPropertyID GetCSSPropertyIdForAttrEnum(uint8_t aAttrEnum);
// WebIDL
already_AddRefed<DOMSVGAnimatedLength> Cx();

View File

@ -163,5 +163,22 @@ bool SVGEllipseElement::IsLengthChangedViaCSS(const ComputedStyle& aNewStyle,
newSVGReset->mRy != oldSVGReset->mRy;
}
nsCSSPropertyID SVGEllipseElement::GetCSSPropertyIdForAttrEnum(
uint8_t aAttrEnum) {
switch (aAttrEnum) {
case CX:
return eCSSProperty_cx;
case CY:
return eCSSProperty_cy;
case RX:
return eCSSProperty_rx;
case RY:
return eCSSProperty_ry;
default:
MOZ_ASSERT_UNREACHABLE("Unknown attr enum");
return eCSSProperty_UNKNOWN;
}
}
} // namespace dom
} // namespace mozilla

View File

@ -7,6 +7,7 @@
#ifndef mozilla_dom_SVGEllipseElement_h
#define mozilla_dom_SVGEllipseElement_h
#include "nsCSSPropertyID.h"
#include "SVGAnimatedLength.h"
#include "SVGGeometryElement.h"
@ -47,6 +48,7 @@ class SVGEllipseElement final : public SVGEllipseElementBase {
static bool IsLengthChangedViaCSS(const ComputedStyle& aNewStyle,
const ComputedStyle& aOldStyle);
static nsCSSPropertyID GetCSSPropertyIdForAttrEnum(uint8_t aAttrEnum);
// WebIDL
already_AddRefed<DOMSVGAnimatedLength> Cx();

View File

@ -135,5 +135,22 @@ SVGElement::LengthAttributesInfo SVGForeignObjectElement::GetLengthInfo() {
ArrayLength(sLengthInfo));
}
nsCSSPropertyID SVGForeignObjectElement::GetCSSPropertyIdForAttrEnum(
uint8_t aAttrEnum) {
switch (aAttrEnum) {
case ATTR_X:
return eCSSProperty_x;
case ATTR_Y:
return eCSSProperty_y;
case ATTR_WIDTH:
return eCSSProperty_width;
case ATTR_HEIGHT:
return eCSSProperty_height;
default:
MOZ_ASSERT_UNREACHABLE("Unknown attr enum");
return eCSSProperty_UNKNOWN;
}
}
} // namespace dom
} // namespace mozilla

View File

@ -8,6 +8,7 @@
#define mozilla_dom_SVGForeignObjectElement_h
#include "mozilla/dom/SVGGraphicsElement.h"
#include "nsCSSPropertyID.h"
#include "SVGAnimatedLength.h"
nsresult NS_NewSVGForeignObjectElement(
@ -42,6 +43,8 @@ class SVGForeignObjectElement final : public SVGGraphicsElement {
virtual nsresult Clone(dom::NodeInfo*, nsINode** aResult) const override;
static nsCSSPropertyID GetCSSPropertyIdForAttrEnum(uint8_t aAttrEnum);
// WebIDL
already_AddRefed<DOMSVGAnimatedLength> X();
already_AddRefed<DOMSVGAnimatedLength> Y();

View File

@ -0,0 +1,74 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "SVGGeometryProperty.h"
#include "SVGCircleElement.h"
#include "SVGEllipseElement.h"
#include "SVGForeignObjectElement.h"
#include "SVGRectElement.h"
namespace mozilla {
namespace dom {
namespace SVGGeometryProperty {
nsCSSUnit SpecifiedUnitTypeToCSSUnit(uint8_t aSpecifiedUnit) {
switch (aSpecifiedUnit) {
case SVGLength_Binding::SVG_LENGTHTYPE_NUMBER:
case SVGLength_Binding::SVG_LENGTHTYPE_PX:
return nsCSSUnit::eCSSUnit_Pixel;
case SVGLength_Binding::SVG_LENGTHTYPE_MM:
return nsCSSUnit::eCSSUnit_Millimeter;
case SVGLength_Binding::SVG_LENGTHTYPE_CM:
return nsCSSUnit::eCSSUnit_Centimeter;
case SVGLength_Binding::SVG_LENGTHTYPE_IN:
return nsCSSUnit::eCSSUnit_Inch;
case SVGLength_Binding::SVG_LENGTHTYPE_PT:
return nsCSSUnit::eCSSUnit_Point;
case SVGLength_Binding::SVG_LENGTHTYPE_PC:
return nsCSSUnit::eCSSUnit_Pica;
case SVGLength_Binding::SVG_LENGTHTYPE_PERCENTAGE:
return nsCSSUnit::eCSSUnit_Percent;
case SVGLength_Binding::SVG_LENGTHTYPE_EMS:
return nsCSSUnit::eCSSUnit_EM;
case SVGLength_Binding::SVG_LENGTHTYPE_EXS:
return nsCSSUnit::eCSSUnit_XHeight;
default:
MOZ_ASSERT_UNREACHABLE("Unknown unit type");
return nsCSSUnit::eCSSUnit_Pixel;
}
}
nsCSSPropertyID AttrEnumToCSSPropId(const SVGElement* aElement,
uint8_t aAttrEnum) {
// This is a very trivial function only applied to a few elements,
// so we want to avoid making it virtual.
if (aElement->IsSVGElement(nsGkAtoms::rect)) {
return SVGRectElement::GetCSSPropertyIdForAttrEnum(aAttrEnum);
}
if (aElement->IsSVGElement(nsGkAtoms::circle)) {
return SVGCircleElement::GetCSSPropertyIdForAttrEnum(aAttrEnum);
}
if (aElement->IsSVGElement(nsGkAtoms::ellipse)) {
return SVGEllipseElement::GetCSSPropertyIdForAttrEnum(aAttrEnum);
}
if (aElement->IsSVGElement(nsGkAtoms::foreignObject)) {
return SVGForeignObjectElement::GetCSSPropertyIdForAttrEnum(aAttrEnum);
}
return eCSSProperty_UNKNOWN;
}
} // namespace SVGGeometryProperty
} // namespace dom
} // namespace mozilla

View File

@ -148,6 +148,10 @@ bool ResolveAll(const SVGElement* aElement,
return false;
}
nsCSSUnit SpecifiedUnitTypeToCSSUnit(uint8_t aSpecifiedUnit);
nsCSSPropertyID AttrEnumToCSSPropId(const SVGElement* aElement,
uint8_t aAttrEnum);
} // namespace SVGGeometryProperty
} // namespace dom
} // namespace mozilla

View File

@ -241,5 +241,25 @@ bool SVGRectElement::IsLengthChangedViaCSS(const ComputedStyle& aNewStyle,
newSVGReset->mRy != oldSVGReset->mRy;
}
nsCSSPropertyID SVGRectElement::GetCSSPropertyIdForAttrEnum(uint8_t aAttrEnum) {
switch (aAttrEnum) {
case ATTR_X:
return eCSSProperty_x;
case ATTR_Y:
return eCSSProperty_y;
case ATTR_WIDTH:
return eCSSProperty_width;
case ATTR_HEIGHT:
return eCSSProperty_height;
case ATTR_RX:
return eCSSProperty_rx;
case ATTR_RY:
return eCSSProperty_ry;
default:
MOZ_ASSERT_UNREACHABLE("Unknown attr enum");
return eCSSProperty_UNKNOWN;
}
}
} // namespace dom
} // namespace mozilla

View File

@ -7,6 +7,7 @@
#ifndef mozilla_dom_SVGRectElement_h
#define mozilla_dom_SVGRectElement_h
#include "nsCSSPropertyID.h"
#include "SVGAnimatedLength.h"
#include "SVGGeometryElement.h"
@ -48,6 +49,7 @@ class SVGRectElement final : public SVGRectElementBase {
static bool IsLengthChangedViaCSS(const ComputedStyle& aNewStyle,
const ComputedStyle& aOldStyle);
static nsCSSPropertyID GetCSSPropertyIdForAttrEnum(uint8_t aAttrEnum);
// WebIDL
already_AddRefed<DOMSVGAnimatedLength> X();

View File

@ -181,6 +181,7 @@ UNIFIED_SOURCES += [
'SVGFragmentIdentifier.cpp',
'SVGGElement.cpp',
'SVGGeometryElement.cpp',
'SVGGeometryProperty.cpp',
'SVGGradientElement.cpp',
'SVGGraphicsElement.cpp',
'SVGImageElement.cpp',