Bug 1408303 - Part 3: Move several Servo parsers into ServoCSSParser helper class. r=heycam

We have ServoCSSParser class, and I think it's better to move those
Servo FFI into this class to avoid including ServoBindings.h everywhere.

MozReview-Commit-ID: 6orXtddp9ZU

--HG--
extra : rebase_source : 6da4158c4fec606aaee49fddee3192f94d6c85a3
This commit is contained in:
Boris Chiou 2017-12-01 17:35:47 +08:00
Родитель c27700de6c
Коммит 570057e075
12 изменённых файлов: 243 добавлений и 102 удалений

Просмотреть файл

@ -11,6 +11,7 @@
#include "mozilla/RangedArray.h"
#include "mozilla/ServoBindings.h"
#include "mozilla/ServoBindingTypes.h"
#include "mozilla/ServoCSSParser.h"
#include "mozilla/StyleAnimationValue.h"
#include "mozilla/TimingParams.h"
#include "mozilla/dom/BaseKeyframeTypesBinding.h" // For FastBaseKeyframe etc.
@ -560,26 +561,6 @@ KeyframeUtils::IsAnimatableProperty(nsCSSPropertyID aProperty,
return false;
}
/* static */ already_AddRefed<RawServoDeclarationBlock>
KeyframeUtils::ParseProperty(nsCSSPropertyID aProperty,
const nsAString& aValue,
nsIDocument* aDocument)
{
MOZ_ASSERT(aDocument);
NS_ConvertUTF16toUTF8 value(aValue);
// FIXME this is using the wrong base uri (bug 1343919)
RefPtr<URLExtraData> data = new URLExtraData(aDocument->GetDocumentURI(),
aDocument->GetDocumentURI(),
aDocument->NodePrincipal());
return Servo_ParseProperty(aProperty,
&value,
data,
ParsingMode::Default,
aDocument->GetCompatibilityMode(),
aDocument->CSSLoader()).Consume();
}
// ------------------------------------------------------------------
//
// Internal helpers
@ -903,8 +884,10 @@ MakePropertyValuePair(nsCSSPropertyID aProperty, const nsAString& aStringValue,
Maybe<PropertyValuePair> result;
if (aDocument->GetStyleBackendType() == StyleBackendType::Servo) {
ServoCSSParser::ParsingEnvironment env =
ServoCSSParser::GetParsingEnvironment(aDocument);
RefPtr<RawServoDeclarationBlock> servoDeclarationBlock =
KeyframeUtils::ParseProperty(aProperty, aStringValue, aDocument);
ServoCSSParser::ParseProperty(aProperty, aStringValue, env);
if (servoDeclarationBlock) {
result.emplace(aProperty, Move(servoDeclarationBlock));

Просмотреть файл

@ -107,22 +107,6 @@ public:
*/
static bool IsAnimatableProperty(nsCSSPropertyID aProperty,
StyleBackendType aBackend);
/**
* Parse a string representing a CSS property value into a
* RawServoDeclarationBlock.
*
* @param aProperty The property to be parsed.
* @param aValue The specified value.
* @param aDocument The current document.
* @return The parsed value as a RawServoDeclarationBlock. We put the value
* in a declaration block since that is how we represent specified values
* in Servo.
*/
static already_AddRefed<RawServoDeclarationBlock> ParseProperty(
nsCSSPropertyID aProperty,
const nsAString& aValue,
nsIDocument* aDocument);
};
} // namespace mozilla

Просмотреть файл

@ -10,7 +10,7 @@
#include "mozilla/dom/AnimatableBinding.h"
#include "mozilla/dom/KeyframeAnimationOptionsBinding.h"
#include "mozilla/dom/KeyframeEffectBinding.h"
#include "mozilla/ServoBindings.h"
#include "mozilla/ServoCSSParser.h"
#include "nsCSSParser.h" // For nsCSSParser
#include "nsIDocument.h"
#include "nsRuleNode.h"
@ -119,11 +119,8 @@ TimingParams::ParseEasing(const nsAString& aEasing,
if (aDocument->IsStyledByServo()) {
nsTimingFunction timingFunction;
// FIXME this is using the wrong base uri (bug 1343919)
RefPtr<URLExtraData> data = new URLExtraData(aDocument->GetDocumentURI(),
aDocument->GetDocumentURI(),
aDocument->NodePrincipal());
if (!Servo_ParseEasing(&aEasing, data, &timingFunction)) {
RefPtr<URLExtraData> url = ServoCSSParser::GetURLExtraData(aDocument);
if (!ServoCSSParser::ParseEasing(aEasing, url, timingFunction)) {
aRv.ThrowTypeError<dom::MSG_INVALID_EASING_ERROR>(aEasing);
return Nothing();
}

Просмотреть файл

@ -12,7 +12,7 @@
#include "mozilla/dom/DOMPointBinding.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/ToJSValue.h"
#include "mozilla/ServoBindings.h"
#include "mozilla/ServoCSSParser.h"
#include "nsCSSParser.h"
#include "nsStyleTransformMatrix.h"
@ -676,10 +676,9 @@ DOMMatrix::SetMatrixValue(const nsAString& aTransformList, ErrorResult& aRv)
gfx::Matrix4x4 transform;
bool contains3dTransform = false;
if (mIsServo) {
bool status = Servo_ParseTransformIntoMatrix(&aTransformList,
&contains3dTransform,
&transform.components);
if (!status) {
if (!ServoCSSParser::ParseTransformIntoMatrix(aTransformList,
contains3dTransform,
transform.components)) {
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
return nullptr;
}

Просмотреть файл

@ -2841,15 +2841,11 @@ CreateDeclarationForServo(nsCSSPropertyID aProperty,
aDocument->GetDocumentURI(),
aDocument->NodePrincipal());
NS_ConvertUTF16toUTF8 value(aPropertyValue);
ServoCSSParser::ParsingEnvironment env(data,
aDocument->GetCompatibilityMode(),
aDocument->CSSLoader());
RefPtr<RawServoDeclarationBlock> servoDeclarations =
Servo_ParseProperty(aProperty,
&value,
data,
ParsingMode::Default,
aDocument->GetCompatibilityMode(),
aDocument->CSSLoader()).Consume();
ServoCSSParser::ParseProperty(aProperty, aPropertyValue, env);
if (!servoDeclarations) {
// We got a syntax error. The spec says this value must be ignored.

Просмотреть файл

@ -18,6 +18,7 @@
#include "nsPresContext.h"
#include "mozilla/ServoBindings.h"
#include "mozilla/StyleAnimationValue.h" // For AnimationValue
#include "mozilla/ServoCSSParser.h"
#include "mozilla/StyleSetHandleInlines.h"
#include "mozilla/dom/BaseKeyframeTypesBinding.h" // For CompositeOperation
#include "mozilla/dom/Element.h"
@ -712,19 +713,12 @@ ValueFromStringHelper(nsCSSPropertyID aPropID,
}
// Parse property
// FIXME this is using the wrong base uri (bug 1343919)
RefPtr<URLExtraData> data = new URLExtraData(doc->GetDocumentURI(),
doc->GetDocumentURI(),
doc->NodePrincipal());
NS_ConvertUTF16toUTF8 value(aString);
ServoCSSParser::ParsingEnvironment env =
ServoCSSParser::GetParsingEnvironment(doc);
RefPtr<RawServoDeclarationBlock> servoDeclarationBlock =
Servo_ParseProperty(aPropID,
&value,
data,
ParsingMode::AllowUnitlessLength |
ParsingMode::AllowAllNumericValues,
doc->GetCompatibilityMode(),
doc->CSSLoader()).Consume();
ServoCSSParser::ParseProperty(aPropID, aString, env,
ParsingMode::AllowUnitlessLength |
ParsingMode::AllowAllNumericValues);
if (!servoDeclarationBlock) {
return result;
}

Просмотреть файл

@ -13,6 +13,7 @@
#include "mozilla/dom/TypedArray.h"
#include "mozilla/dom/UnionTypes.h"
#include "mozilla/CycleCollectedJSContext.h"
#include "mozilla/ServoCSSParser.h"
#include "mozilla/ServoStyleSet.h"
#include "mozilla/ServoUtils.h"
#include "nsCSSFontFaceRule.h"
@ -522,7 +523,7 @@ FontFace::ParseDescriptor(nsCSSFontDesc aDescID,
if (mFontFaceSet->Document()->IsStyledByServo()) {
RefPtr<URLExtraData> url = new URLExtraData(base, docURI, principal);
return Servo_ParseFontDescriptor(aDescID, &aString, url, &aResult);
return ServoCSSParser::ParseFontDescriptor(aDescID, aString, url, aResult);
}
nsCSSParser parser;

Просмотреть файл

@ -20,6 +20,7 @@
#include "mozilla/AsyncEventDispatcher.h"
#include "mozilla/Logging.h"
#include "mozilla/Preferences.h"
#include "mozilla/ServoCSSParser.h"
#include "mozilla/ServoStyleSet.h"
#include "mozilla/ServoUtils.h"
#include "mozilla/Sprintf.h"
@ -210,12 +211,9 @@ FontFaceSet::ParseFontShorthandForMatching(
nsCSSValue style;
nsCSSValue stretch;
nsCSSValue weight;
// Bug 1343919: The Base URI is not correct.
RefPtr<URLExtraData> url = new URLExtraData(mDocument->GetDocumentURI(),
mDocument->GetDocumentURI(),
mDocument->NodePrincipal());
if (!Servo_ParseFontShorthandForMatching(
&aFont, url, &aFamilyList, &style, &stretch, &weight)) {
RefPtr<URLExtraData> url = ServoCSSParser::GetURLExtraData(mDocument);
if (!ServoCSSParser::ParseFontShorthandForMatching(
aFont, url, aFamilyList, style, stretch, weight)) {
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
return;
}

Просмотреть файл

@ -8,6 +8,9 @@
#include "ServoCSSParser.h"
#include "mozilla/ServoBindings.h"
#include "nsIDocument.h"
using namespace mozilla;
/* static */ bool
@ -54,3 +57,77 @@ ServoCSSParser::ParseCounterStyleDescriptor(nsCSSCounterDesc aDescriptor,
return Servo_ParseCounterStyleDescriptor(aDescriptor, &value, aURLExtraData,
&aResult);
}
/* static */ already_AddRefed<RawServoDeclarationBlock>
ServoCSSParser::ParseProperty(nsCSSPropertyID aProperty,
const nsAString& aValue,
const ParsingEnvironment& aParsingEnvironment,
ParsingMode aParsingMode)
{
NS_ConvertUTF16toUTF8 value(aValue);
return Servo_ParseProperty(aProperty,
&value,
aParsingEnvironment.mUrlExtraData,
aParsingMode,
aParsingEnvironment.mCompatMode,
aParsingEnvironment.mLoader).Consume();
}
/* static */ bool
ServoCSSParser::ParseEasing(const nsAString& aValue,
URLExtraData* aUrl,
nsTimingFunction& aResult)
{
return Servo_ParseEasing(&aValue, aUrl, &aResult);
}
/* static */ bool
ServoCSSParser::ParseTransformIntoMatrix(const nsAString& aValue,
bool& aContains3DTransform,
RawGeckoGfxMatrix4x4& aResult)
{
return Servo_ParseTransformIntoMatrix(&aValue,
&aContains3DTransform,
&aResult);
}
/* static */ bool
ServoCSSParser::ParseFontDescriptor(nsCSSFontDesc aDescID,
const nsAString& aValue,
URLExtraData* aUrl,
nsCSSValue& aResult)
{
return Servo_ParseFontDescriptor(aDescID, &aValue, aUrl, &aResult);
}
/* static */ bool
ServoCSSParser::ParseFontShorthandForMatching(const nsAString& aValue,
URLExtraData* aUrl,
RefPtr<SharedFontList>& aList,
nsCSSValue& aStyle,
nsCSSValue& aStretch,
nsCSSValue& aWeight)
{
return Servo_ParseFontShorthandForMatching(&aValue, aUrl, &aList,
&aStyle, &aStretch, &aWeight);
}
/* static */ already_AddRefed<URLExtraData>
ServoCSSParser::GetURLExtraData(nsIDocument* aDocument)
{
MOZ_ASSERT(aDocument);
// FIXME this is using the wrong base uri (bug 1343919)
RefPtr<URLExtraData> url = new URLExtraData(aDocument->GetDocumentURI(),
aDocument->GetDocumentURI(),
aDocument->NodePrincipal());
return url.forget();
}
/* static */ ServoCSSParser::ParsingEnvironment
ServoCSSParser::GetParsingEnvironment(nsIDocument* aDocument)
{
return ParsingEnvironment(GetURLExtraData(aDocument),
aDocument->GetCompatibilityMode(),
aDocument->CSSLoader());
}

Просмотреть файл

@ -9,7 +9,19 @@
#ifndef mozilla_ServoCSSParser_h
#define mozilla_ServoCSSParser_h
#include "mozilla/ServoBindings.h"
#include "mozilla/gfx/Types.h"
#include "mozilla/RefPtr.h"
#include "mozilla/ServoTypes.h"
#include "nsColor.h"
#include "nsCSSPropertyID.h"
#include "nsDOMCSSDeclaration.h"
#include "nsString.h"
class nsCSSValue;
class nsIDocument;
struct nsCSSRect;
using RawGeckoGfxMatrix4x4 = mozilla::gfx::Float[16];
namespace mozilla {
namespace css {
@ -19,9 +31,15 @@ class Loader;
namespace mozilla {
class ServoStyleSet;
class SharedFontList;
struct URLExtraData;
class ServoCSSParser
{
public:
using ParsingEnvironment = nsDOMCSSDeclaration::ServoCSSParsingEnvironment;
/**
* Returns whether the specified string can be parsed as a valid CSS
* <color> value.
@ -87,6 +105,98 @@ public:
const nsAString& aValue,
URLExtraData* aURLExtraData,
nsCSSValue& aResult);
/**
* Parse a string representing a CSS property value into a
* RawServoDeclarationBlock.
*
* @param aProperty The property to be parsed.
* @param aValue The specified value.
* @param aParsingEnvironment All the parsing environment data we need.
* @param aParsingMode The paring mode we apply.
* @return The parsed value as a RawServoDeclarationBlock. We put the value
* in a declaration block since that is how we represent specified values
* in Servo.
*/
static already_AddRefed<RawServoDeclarationBlock> ParseProperty(
nsCSSPropertyID aProperty,
const nsAString& aValue,
const ParsingEnvironment& aParsingEnvironment,
ParsingMode aParsingMode = ParsingMode::Default);
/**
* Parse a animation timing function.
*
* @param aValue The specified value.
* @param aUrl The parser url extra data.
* @param aResult The output timing function. (output)
* @return Whether the value was successfully parsed.
*/
static bool ParseEasing(const nsAString& aValue,
URLExtraData* aUrl,
nsTimingFunction& aResult);
/**
* Parse a specified transform list into a gfx matrix.
*
* @param aValue The specified value.
* @param aContains3DTransform The output flag indicates whether this is any
* 3d transform function. (output)
* @param aResult The output matrix. (output)
* @return Whether the value was successfully parsed.
*/
static bool ParseTransformIntoMatrix(const nsAString& aValue,
bool& aContains3DTransform,
RawGeckoGfxMatrix4x4& aResult);
/**
* Parse a font descriptor.
*
* @param aDescID The font descriptor id.
* @param aValue The specified value.
* @param aUrl The parser url extra data.
* @param aResult The parsed result. (output)
* @return Whether the value was successfully parsed.
*/
static bool ParseFontDescriptor(nsCSSFontDesc aDescID,
const nsAString& aValue,
URLExtraData* aUrl,
nsCSSValue& aResult);
/**
* Parse a font shorthand for FontFaceSet matching, so we only care about
* FontFamily, FontStyle, FontStretch, and FontWeight.
*
* @param aValue The specified value.
* @param aUrl The parser url extra data.
* @param aList The parsed FontFamily list. (output)
* @param aStyle The parsed FontStyle. (output)
* @param aStretch The parsed FontStretch. (output)
* @param aWeight The parsed FontWeight. (output)
* @return Whether the value was successfully parsed.
*/
static bool ParseFontShorthandForMatching(const nsAString& aValue,
URLExtraData* aUrl,
RefPtr<SharedFontList>& aList,
nsCSSValue& aStyle,
nsCSSValue& aStretch,
nsCSSValue& aWeight);
/**
* Get a URLExtraData from |nsIDocument|.
*
* @param aDocument The current document.
* @return The URLExtraData object.
*/
static already_AddRefed<URLExtraData> GetURLExtraData(nsIDocument* aDocument);
/**
* Get a ParsingEnvironment from |nsIDocument|.
*
* @param aDocument The current document.
* @return The ParsingEnvironment object.
*/
static ParsingEnvironment GetParsingEnvironment(nsIDocument* aDocument);
};
} // namespace mozilla

Просмотреть файл

@ -30,9 +30,9 @@
#include "mozilla/css/Declaration.h"
#include "mozilla/dom/Element.h"
#include "mozilla/FloatingPoint.h"
#include "mozilla/KeyframeUtils.h" // KeyframeUtils::ParseProperty
#include "mozilla/Likely.h"
#include "mozilla/ServoBindings.h" // RawServoDeclarationBlock
#include "mozilla/ServoCSSParser.h"
#include "gfxMatrix.h"
#include "gfxQuaternion.h"
#include "nsIDocument.h"
@ -5477,7 +5477,8 @@ AnimationValue::FromString(nsCSSPropertyID aProperty,
}
RefPtr<RawServoDeclarationBlock> declarations =
KeyframeUtils::ParseProperty(aProperty, aValue, doc);
ServoCSSParser::ParseProperty(aProperty, aValue,
ServoCSSParser::GetParsingEnvironment(doc));
if (!declarations) {
return result;

Просмотреть файл

@ -107,6 +107,31 @@ public:
virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
// Information needed to parse a declaration for Servo side.
// Put this in public so other Servo parsing functions can reuse this.
struct MOZ_STACK_CLASS ServoCSSParsingEnvironment
{
RefPtr<mozilla::URLExtraData> mUrlExtraData;
nsCompatibility mCompatMode;
mozilla::css::Loader* mLoader;
ServoCSSParsingEnvironment(mozilla::URLExtraData* aUrlData,
nsCompatibility aCompatMode,
mozilla::css::Loader* aLoader)
: mUrlExtraData(aUrlData)
, mCompatMode(aCompatMode)
, mLoader(aLoader)
{}
ServoCSSParsingEnvironment(already_AddRefed<mozilla::URLExtraData> aUrlData,
nsCompatibility aCompatMode,
mozilla::css::Loader* aLoader)
: mUrlExtraData(aUrlData)
, mCompatMode(aCompatMode)
, mLoader(aLoader)
{}
};
protected:
// The reason for calling GetCSSDeclaration.
enum Operation {
@ -154,30 +179,6 @@ protected:
"performance overhead (see bug 649163)") mCSSLoader;
};
// Information neded to parse a declaration for Servo side.
struct MOZ_STACK_CLASS ServoCSSParsingEnvironment
{
RefPtr<mozilla::URLExtraData> mUrlExtraData;
nsCompatibility mCompatMode;
mozilla::css::Loader* mLoader;
ServoCSSParsingEnvironment(mozilla::URLExtraData* aUrlData,
nsCompatibility aCompatMode,
mozilla::css::Loader* aLoader)
: mUrlExtraData(aUrlData)
, mCompatMode(aCompatMode)
, mLoader(aLoader)
{}
ServoCSSParsingEnvironment(already_AddRefed<mozilla::URLExtraData> aUrlData,
nsCompatibility aCompatMode,
mozilla::css::Loader* aLoader)
: mUrlExtraData(aUrlData)
, mCompatMode(aCompatMode)
, mLoader(aLoader)
{}
};
// On failure, mPrincipal should be set to null in aCSSParseEnv.
// If mPrincipal is null, the other members may not be set to
// anything meaningful.