Bug 1408312 - Part 1: Add ServoCSSParser utility class. r=xidorn draft
authorCameron McCormack <cam@mcc.id.au>
Mon, 16 Oct 2017 08:54:47 +0800
changeset 681162 cee890ae943bdd855528522ef774bc95adfd4245
parent 680782 c6a2643362a67cdf7a87ac165454fce4b383debb
child 681163 decfb82dfa419b5a57edb9e2f1d4cd04ed7d5599
push id84782
push userbmo:cam@mcc.id.au
push dateTue, 17 Oct 2017 03:35:29 +0000
reviewersxidorn
bugs1408312
milestone58.0a1
Bug 1408312 - Part 1: Add ServoCSSParser utility class. r=xidorn MozReview-Commit-ID: KzM9332hBSx
layout/style/ServoBindingList.h
layout/style/ServoCSSParser.cpp
layout/style/ServoCSSParser.h
layout/style/moz.build
--- a/layout/style/ServoBindingList.h
+++ b/layout/style/ServoBindingList.h
@@ -677,14 +677,22 @@ SERVO_BINDING_FUNC(Servo_HasPendingResty
 
 SERVO_BINDING_FUNC(Servo_GetArcStringData, void,
                    const RustString*, uint8_t const** chars, uint32_t* len);
 SERVO_BINDING_FUNC(Servo_ReleaseArcStringData, void,
                    const mozilla::ServoRawOffsetArc<RustString>* string);
 SERVO_BINDING_FUNC(Servo_CloneArcStringData, mozilla::ServoRawOffsetArc<RustString>,
                    const mozilla::ServoRawOffsetArc<RustString>* string);
 
+// CSS parsing utility functions.
+SERVO_BINDING_FUNC(Servo_IsValidCSSColor, bool, const nsAString* value);
+SERVO_BINDING_FUNC(Servo_ComputeColor, bool,
+                   RawServoStyleSetBorrowedOrNull set,
+                   nscolor current_color,
+                   const nsAString* value,
+                   nscolor* result_color);
+
 // AddRef / Release functions
 #define SERVO_ARC_TYPE(name_, type_)                                \
   SERVO_BINDING_FUNC(Servo_##name_##_AddRef, void, type_##Borrowed) \
   SERVO_BINDING_FUNC(Servo_##name_##_Release, void, type_##Borrowed)
 #include "mozilla/ServoArcTypeList.h"
 #undef SERVO_ARC_TYPE
new file mode 100644
--- /dev/null
+++ b/layout/style/ServoCSSParser.cpp
@@ -0,0 +1,27 @@
+/* -*- 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/. */
+
+/* CSS parsing utility functions */
+
+#include "ServoCSSParser.h"
+
+using namespace mozilla;
+
+/* static */ bool
+ServoCSSParser::IsValidCSSColor(const nsAString& aValue)
+{
+  return Servo_IsValidCSSColor(&aValue);
+}
+
+/* static */ bool
+ServoCSSParser::ComputeColor(ServoStyleSet* aStyleSet,
+                             nscolor aCurrentColor,
+                             const nsAString& aValue,
+                             nscolor* aResultColor)
+{
+  return Servo_ComputeColor(aStyleSet ? aStyleSet->RawSet() : nullptr,
+                            aCurrentColor, &aValue, aResultColor);
+}
new file mode 100644
--- /dev/null
+++ b/layout/style/ServoCSSParser.h
@@ -0,0 +1,45 @@
+/* -*- 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/. */
+
+/* CSS parsing utility functions */
+
+#ifndef mozilla_ServoCSSParser_h
+#define mozilla_ServoCSSParser_h
+
+#include "mozilla/ServoBindings.h"
+
+namespace mozilla {
+
+class ServoCSSParser
+{
+public:
+  /**
+   * Returns whether the specified string can be parsed as a valid CSS
+   * <color> value.
+   *
+   * This includes Mozilla-specific keywords such as -moz-default-color.
+   */
+  static bool IsValidCSSColor(const nsAString& aValue);
+
+  /**
+   * Computes an nscolor from the given CSS <color> value.
+   *
+   * @param aStyleSet The style set whose nsPresContext will be used to
+   *   compute system colors and other special color values.
+   * @param aCurrentColor The color value that currentcolor should compute to.
+   * @param aValue The CSS <color> value.
+   * @param aResultColor The resulting computed color value.
+   * @return Whether aValue was successfully parsed and aResultColor was set.
+   */
+  static bool ComputeColor(ServoStyleSet* aStyleSet,
+                           nscolor aCurrentColor,
+                           const nsAString& aValue,
+                           nscolor* aResultColor);
+};
+
+} // namespace mozilla
+
+#endif // mozilla_ServoCSSParser_h
--- a/layout/style/moz.build
+++ b/layout/style/moz.build
@@ -98,16 +98,17 @@ EXPORTS.mozilla += [
     'PostTraversalTask.h',
     'PreloadedStyleSheet.h',
     'RuleNodeCacheConditions.h',
     'RuleProcessorCache.h',
     'ServoArcTypeList.h',
     'ServoBindingList.h',
     'ServoBindings.h',
     'ServoBindingTypes.h',
+    'ServoCSSParser.h',
     'ServoCSSRuleList.h',
     'ServoDeclarationBlock.h',
     'ServoDocumentRule.h',
     'ServoElementSnapshot.h',
     'ServoElementSnapshotTable.h',
     'ServoFontFeatureValuesRule.h',
     'ServoImportRule.h',
     'ServoKeyframeRule.h',
@@ -244,16 +245,17 @@ UNIFIED_SOURCES += [
     'nsStyleTransformMatrix.cpp',
     'nsStyleUtil.cpp',
     'nsTransitionManager.cpp',
     'PostTraversalTask.cpp',
     'PreloadedStyleSheet.cpp',
     'RuleNodeCacheConditions.cpp',
     'RuleProcessorCache.cpp',
     'ServoBindings.cpp',
+    'ServoCSSParser.cpp',
     'ServoCSSRuleList.cpp',
     'ServoDeclarationBlock.cpp',
     'ServoDocumentRule.cpp',
     'ServoElementSnapshot.cpp',
     'ServoFontFeatureValuesRule.cpp',
     'ServoImportRule.cpp',
     'ServoKeyframeRule.cpp',
     'ServoKeyframesRule.cpp',