Bug 1330041 - Basic handling framework for presentation attributes in Stylo, with handling for font-size and color; r?emilio,bz draft
authorManish Goregaokar <manishearth@gmail.com>
Thu, 19 Jan 2017 15:56:53 -0800
changeset 469143 c7da2d9131802df4dbf691ece58e492f4d0ae5ed
parent 466816 d92fd6b6d6bfc5b566222ae2957e55772d60151a
child 544104 283e7cde3e7f5a067f2593f9854a70781326de0a
push id43626
push userbmo:manishearth@gmail.com
push dateWed, 01 Feb 2017 19:04:48 +0000
reviewersemilio, bz
bugs1330041
milestone54.0a1
Bug 1330041 - Basic handling framework for presentation attributes in Stylo, with handling for font-size and color; r?emilio,bz This introduces a basic framework for servo's style system to be able to query the style of presentation attributes which it can then insert into the cascade. It uses that framework to implement the size and color attributes on <font>. There are a number of improvements that can be done on top of this: - Implement all other properties - Abstractify the ruledata parameter of the mappers using templates or virtual dispatch so that it can be a Servo decl block instead - Implement aforementiond abstraction over Servo decl blocks (this obsoletes the code in the first item above, so it might just be better to skip that and directly do this) - Replace uses of nsHTMLStyleSheet with an abstract base class containing common elements between Servo and Gecko I'd prefer for these to be done in separate steps. MozReview-Commit-ID: GO60qfeZOfl
dom/base/Element.cpp
dom/base/Element.h
dom/base/nsAttrAndChildArray.cpp
dom/base/nsAttrAndChildArray.h
dom/base/nsMappedAttributes.cpp
dom/base/nsMappedAttributes.h
layout/style/ServoBindingList.h
layout/style/ServoBindingTypes.h
layout/style/ServoBindings.cpp
layout/style/ServoBindings.h
layout/style/ServoStyleSet.cpp
layout/style/ServoStyleSet.h
layout/style/nsHTMLStyleSheet.cpp
layout/style/nsHTMLStyleSheet.h
layout/style/nsRuleNode.cpp
layout/style/nsRuleNode.h
--- a/dom/base/Element.cpp
+++ b/dom/base/Element.cpp
@@ -2018,16 +2018,22 @@ Element::GetInlineStyleDeclaration() con
 
   if (attrVal && attrVal->Type() == nsAttrValue::eCSSDeclaration) {
     return attrVal->GetCSSDeclarationValue();
   }
 
   return nullptr;
 }
 
+const nsMappedAttributes*
+Element::GetMappedAttributes() const
+{
+  return mAttrsAndChildren.GetMapped();
+}
+
 nsresult
 Element::SetInlineStyleDeclaration(DeclarationBlock* aDeclaration,
                                    const nsAString* aSerialized,
                                    bool aNotify)
 {
   NS_NOTYETIMPLEMENTED("Element::SetInlineStyleDeclaration");
   return NS_ERROR_NOT_IMPLEMENTED;
 }
--- a/dom/base/Element.h
+++ b/dom/base/Element.h
@@ -277,16 +277,21 @@ public:
   void ClearStyleStateLocks();
 
   /**
    * Get the inline style declaration, if any, for this element.
    */
   DeclarationBlock* GetInlineStyleDeclaration() const;
 
   /**
+   * Get the mapped attributes, if any, for this element.
+   */
+  const nsMappedAttributes* GetMappedAttributes() const;
+
+  /**
    * Set the inline style declaration for this element. This will send
    * an appropriate AttributeChanged notification if aNotify is true.
    */
   virtual nsresult SetInlineStyleDeclaration(DeclarationBlock* aDeclaration,
                                              const nsAString* aSerialized,
                                              bool aNotify);
 
   /**
--- a/dom/base/nsAttrAndChildArray.cpp
+++ b/dom/base/nsAttrAndChildArray.cpp
@@ -767,16 +767,21 @@ nsAttrAndChildArray::MakeMappedUnique(ns
     // nature not modifiable).
     aAttributes->DropStyleSheetReference();
   }
   mapped.swap(mImpl->mMappedAttrs);
 
   return NS_OK;
 }
 
+const nsMappedAttributes*
+nsAttrAndChildArray::GetMapped() const
+{
+  return mImpl ? mImpl->mMappedAttrs : nullptr;
+}
 
 bool
 nsAttrAndChildArray::GrowBy(uint32_t aGrowSize)
 {
   CheckedUint32 size = 0;
   if (mImpl) {
     size += mImpl->mBufferSize;
     size += NS_IMPL_EXTRA_SIZE;
--- a/dom/base/nsAttrAndChildArray.h
+++ b/dom/base/nsAttrAndChildArray.h
@@ -129,16 +129,17 @@ public:
            !AttrSlotIsTaken(ATTRCHILD_ARRAY_MAX_ATTR_COUNT - 1);
   }
 
   size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
   bool HasMappedAttrs() const
   {
     return MappedAttrCount();
   }
+  const nsMappedAttributes* GetMapped() const;
 
 private:
   nsAttrAndChildArray(const nsAttrAndChildArray& aOther) = delete;
   nsAttrAndChildArray& operator=(const nsAttrAndChildArray& aOther) = delete;
 
   void Clear();
 
   uint32_t NonMappedAttrCount() const;
--- a/dom/base/nsMappedAttributes.cpp
+++ b/dom/base/nsMappedAttributes.cpp
@@ -7,33 +7,39 @@
 /*
  * A unique per-element set of attributes that is used as an
  * nsIStyleRule; used to implement presentational attributes.
  */
 
 #include "nsMappedAttributes.h"
 #include "nsHTMLStyleSheet.h"
 #include "nsRuleWalker.h"
+#include "nsRuleData.h"
 #include "mozilla/HashFunctions.h"
 #include "mozilla/MemoryReporting.h"
+#include "mozilla/ServoDeclarationBlock.h"
 
 using namespace mozilla;
 
 nsMappedAttributes::nsMappedAttributes(nsHTMLStyleSheet* aSheet,
                                        nsMapRuleToAttributesFunc aMapRuleFunc)
   : mAttrCount(0),
     mSheet(aSheet),
-    mRuleMapper(aMapRuleFunc)
+    mRuleMapper(aMapRuleFunc),
+    mServoStyle(nullptr)
 {
 }
 
 nsMappedAttributes::nsMappedAttributes(const nsMappedAttributes& aCopy)
   : mAttrCount(aCopy.mAttrCount),
     mSheet(aCopy.mSheet),
-    mRuleMapper(aCopy.mRuleMapper)
+    mRuleMapper(aCopy.mRuleMapper),
+    // This is only called by ::Clone, which is used to create independent
+    // nsMappedAttributes objects which should not share a ServoDeclarationBlock
+    mServoStyle(nullptr)
 {
   NS_ASSERTION(mBufferSize >= aCopy.mAttrCount, "can't fit attributes");
 
   uint32_t i;
   for (i = 0; i < mAttrCount; ++i) {
     new (&Attrs()[i]) InternalAttr(aCopy.Attrs()[i]);
   }
 }
@@ -276,8 +282,25 @@ nsMappedAttributes::SizeOfIncludingThis(
 
   size_t n = aMallocSizeOf(this);
   for (uint16_t i = 0; i < mAttrCount; ++i) {
     n += Attrs()[i].mValue.SizeOfExcludingThis(aMallocSizeOf);
   }
   return n;
 }
 
+void
+nsMappedAttributes::LazilyResolveServoDeclaration(nsRuleData* aRuleData,
+                                                  nsCSSPropertyID* aIndexToIdMapping,
+                                                  size_t aRuleDataSize)
+{
+  MapRuleInfoInto(aRuleData);
+
+  MOZ_ASSERT(!mServoStyle,
+             "LazilyResolveServoDeclaration should not be called if mServoStyle is already set");
+  mServoStyle = Servo_DeclarationBlock_CreateEmpty().Consume();
+  for (size_t i = 0; i < aRuleDataSize; i++) {
+    nsCSSValue& val = aRuleData->mValueStorage[i];
+    if (val.GetUnit() != eCSSUnit_Null) {
+      Servo_DeclarationBlock_AddPresValue(mServoStyle.get(), aIndexToIdMapping[i], &val);
+    }
+  }
+}
--- a/dom/base/nsMappedAttributes.h
+++ b/dom/base/nsMappedAttributes.h
@@ -11,16 +11,17 @@
 
 #ifndef nsMappedAttributes_h___
 #define nsMappedAttributes_h___
 
 #include "nsAttrAndChildArray.h"
 #include "nsMappedAttributeElement.h"
 #include "nsIStyleRule.h"
 #include "mozilla/Attributes.h"
+#include "mozilla/ServoBindings.h"
 #include "mozilla/MemoryReporting.h"
 
 class nsIAtom;
 class nsHTMLStyleSheet;
 
 class nsMappedAttributes final : public nsIStyleRule
 {
 public:
@@ -65,19 +66,37 @@ public:
     NS_ASSERTION(aPos < mAttrCount, "out-of-bounds");
     return &Attrs()[aPos].mValue;
   }
   // Remove the attr at position aPos.  The value of the attr is placed in
   // aValue; any value that was already in aValue is destroyed.
   void RemoveAttrAt(uint32_t aPos, nsAttrValue& aValue);
   const nsAttrName* GetExistingAttrNameFromQName(const nsAString& aName) const;
   int32_t IndexOfAttr(nsIAtom* aLocalName) const;
-  
 
-  // nsIStyleRule 
+  // Apply the contained mapper to an empty nsRuleData object
+  // that is able to contain all properties. Set contained servo declaration block
+  // to result of this computation.
+  // aIndexToIdMapping is
+  // a table that maps from an index in the rule data to the corresponding
+  // property ID. aRuleDataSize is the length of the backing storage
+  // of the rule data.
+  void LazilyResolveServoDeclaration(nsRuleData* aRuleData,
+                                     nsCSSPropertyID* aIndexToIdMapping,
+                                     size_t aRuleDataSize);
+
+  // Obtain the contained servo declaration block
+  // May return null if called before the inner block
+  // has been (lazily) resolved
+  const RefPtr<RawServoDeclarationBlock>& GetServoStyle() const
+  {
+    return mServoStyle;
+  }
+
+  // nsIStyleRule
   virtual void MapRuleInfoInto(nsRuleData* aRuleData) override;
   virtual bool MightMapInheritedStyleData() override;
   virtual bool GetDiscretelyAnimatedCSSValue(nsCSSPropertyID aProperty,
                                              nsCSSValue* aValue) override;
 #ifdef DEBUG
   virtual void List(FILE* out = stdout, int32_t aIndent = 0) const override;
 #endif
 
@@ -110,12 +129,13 @@ private:
   }
 
   uint16_t mAttrCount;
 #ifdef DEBUG
   uint16_t mBufferSize;
 #endif
   nsHTMLStyleSheet* mSheet; //weak
   nsMapRuleToAttributesFunc mRuleMapper;
+  RefPtr<RawServoDeclarationBlock> mServoStyle;
   void* mAttrs[1];
 };
 
 #endif /* nsMappedAttributes_h___ */
--- a/layout/style/ServoBindingList.h
+++ b/layout/style/ServoBindingList.h
@@ -161,16 +161,20 @@ SERVO_BINDING_FUNC(Servo_DeclarationBloc
                    nsCSSPropertyID property,
                    nsACString* value, bool is_important)
 SERVO_BINDING_FUNC(Servo_DeclarationBlock_RemoveProperty, void,
                    RawServoDeclarationBlockBorrowed declarations,
                    const nsACString* property)
 SERVO_BINDING_FUNC(Servo_DeclarationBlock_RemovePropertyById, void,
                    RawServoDeclarationBlockBorrowed declarations,
                    nsCSSPropertyID property)
+SERVO_BINDING_FUNC(Servo_DeclarationBlock_AddPresValue, void,
+                   RawServoDeclarationBlockBorrowed declarations,
+                   nsCSSPropertyID property,
+                   nsCSSValueBorrowedMut css_value)
 
 // CSS supports()
 SERVO_BINDING_FUNC(Servo_CSSSupports2, bool,
                    const nsACString* name, const nsACString* value)
 SERVO_BINDING_FUNC(Servo_CSSSupports, bool,
                    const nsACString* cond)
 
 // Computed style data
@@ -211,16 +215,18 @@ SERVO_BINDING_FUNC(Servo_ResolvePseudoSt
 //
 // The tree must be in a consistent state such that a normal traversal could be
 // performed, and this function maintains that invariant.
 SERVO_BINDING_FUNC(Servo_ResolveStyleLazily, ServoComputedValuesStrong,
                    RawGeckoElementBorrowed element, nsIAtom* pseudo_tag,
                    RawServoStyleSetBorrowed set)
 
 // Restyle the given subtree.
+// Use ServoStyleSet::PrepareAndTraverseSubtree instead of calling this
+// directly
 SERVO_BINDING_FUNC(Servo_TraverseSubtree, void,
                    RawGeckoElementBorrowed root, RawServoStyleSetBorrowed set,
                    mozilla::TraversalRootBehavior root_behavior)
 
 // Assert that the tree has no pending or unconsumed restyles.
 SERVO_BINDING_FUNC(Servo_AssertTreeIsClean, void, RawGeckoElementBorrowed root)
 
 // Style-struct management.
--- a/layout/style/ServoBindingTypes.h
+++ b/layout/style/ServoBindingTypes.h
@@ -61,16 +61,17 @@ typedef nsTArray<const RawServoAnimation
 #define DECL_BORROWED_REF_TYPE_FOR(type_) typedef type_ const* type_##Borrowed;
 #define DECL_NULLABLE_BORROWED_REF_TYPE_FOR(type_) typedef type_ const* type_##BorrowedOrNull;
 #define DECL_BORROWED_MUT_REF_TYPE_FOR(type_) typedef type_* type_##BorrowedMut;
 #define DECL_NULLABLE_BORROWED_MUT_REF_TYPE_FOR(type_) typedef type_* type_##BorrowedMutOrNull;
 
 #define SERVO_ARC_TYPE(name_, type_)         \
   DECL_NULLABLE_BORROWED_REF_TYPE_FOR(type_) \
   DECL_BORROWED_REF_TYPE_FOR(type_)          \
+  DECL_BORROWED_MUT_REF_TYPE_FOR(type_)      \
   struct MOZ_MUST_USE_TYPE type_##Strong     \
   {                                          \
     type_* mPtr;                             \
     already_AddRefed<type_> Consume();       \
   };
 #include "mozilla/ServoArcTypeList.h"
 #undef SERVO_ARC_TYPE
 
--- a/layout/style/ServoBindings.cpp
+++ b/layout/style/ServoBindings.cpp
@@ -16,16 +16,17 @@
 #include "nsContentUtils.h"
 #include "nsDOMTokenList.h"
 #include "nsIContentInlines.h"
 #include "nsIDOMNode.h"
 #include "nsIDocument.h"
 #include "nsIFrame.h"
 #include "nsINode.h"
 #include "nsIPrincipal.h"
+#include "nsMappedAttributes.h"
 #include "nsMediaFeatures.h"
 #include "nsNameSpaceManager.h"
 #include "nsNetUtil.h"
 #include "nsRuleNode.h"
 #include "nsString.h"
 #include "nsStyleStruct.h"
 #include "nsStyleUtil.h"
 #include "nsTArray.h"
@@ -306,32 +307,47 @@ Gecko_DropElementSnapshot(ServoElementSn
     nsCOMPtr<nsIRunnable> task = NS_NewRunnableFunction([=]() { delete aSnapshot; });
     NS_DispatchToMainThread(task.forget());
   } else {
     delete aSnapshot;
   }
 }
 
 RawServoDeclarationBlockStrongBorrowedOrNull
-Gecko_GetServoDeclarationBlock(RawGeckoElementBorrowed aElement)
+Gecko_GetStyleAttrDeclarationBlock(RawGeckoElementBorrowed aElement)
 {
   DeclarationBlock* decl = aElement->GetInlineStyleDeclaration();
   if (!decl) {
     return nullptr;
   }
   if (decl->IsGecko()) {
     // XXX This can happen when nodes are adopted from a Gecko-style-backend
     //     document into a Servo-style-backend document.  See bug 1330051.
     NS_WARNING("stylo: requesting a Gecko declaration block?");
     return nullptr;
   }
   return reinterpret_cast<const RawServoDeclarationBlockStrong*>
     (decl->AsServo()->RefRaw());
 }
 
+RawServoDeclarationBlockStrongBorrowedOrNull
+Gecko_GetHTMLPresentationAttrDeclarationBlock(RawGeckoElementBorrowed aElement)
+{
+  static_assert(sizeof(RefPtr<RawServoDeclarationBlock>) ==
+                sizeof(RawServoDeclarationBlockStrong),
+                "RefPtr should just be a pointer");
+  const nsMappedAttributes* attrs = aElement->GetMappedAttributes();
+  if (!attrs) {
+    return nullptr;
+  }
+
+  const RefPtr<RawServoDeclarationBlock>& servo = attrs->GetServoStyle();
+  return reinterpret_cast<const RawServoDeclarationBlockStrong*>(&servo);
+}
+
 RawServoDeclarationBlockStrong
 Gecko_GetAnimationRule(RawGeckoElementBorrowed aElement,
                        nsIAtom* aPseudoTag,
                        EffectCompositor::CascadeLevel aCascadeLevel)
 {
   MOZ_ASSERT(aElement, "Invalid GeckoElement");
 
   const RawServoDeclarationBlockStrong emptyDeclarationBlock{ nullptr };
--- a/layout/style/ServoBindings.h
+++ b/layout/style/ServoBindings.h
@@ -152,17 +152,19 @@ nsIAtom* Gecko_GetElementId(RawGeckoElem
 SERVO_DECLARE_ELEMENT_ATTR_MATCHING_FUNCTIONS(Gecko_, RawGeckoElementBorrowed)
 SERVO_DECLARE_ELEMENT_ATTR_MATCHING_FUNCTIONS(Gecko_Snapshot,
                                               const ServoElementSnapshot*)
 
 #undef SERVO_DECLARE_ELEMENT_ATTR_MATCHING_FUNCTIONS
 
 // Style attributes.
 RawServoDeclarationBlockStrongBorrowedOrNull
-Gecko_GetServoDeclarationBlock(RawGeckoElementBorrowed element);
+Gecko_GetStyleAttrDeclarationBlock(RawGeckoElementBorrowed element);
+RawServoDeclarationBlockStrongBorrowedOrNull
+Gecko_GetHTMLPresentationAttrDeclarationBlock(RawGeckoElementBorrowed element);
 
 // Animations
 RawServoDeclarationBlockStrong
 Gecko_GetAnimationRule(RawGeckoElementBorrowed aElement,
                        nsIAtom* aPseudoTag,
                        mozilla::EffectCompositor::CascadeLevel aCascadeLevel);
 
 // Atoms.
--- a/layout/style/ServoStyleSet.cpp
+++ b/layout/style/ServoStyleSet.cpp
@@ -8,16 +8,17 @@
 
 #include "mozilla/DocumentStyleRootIterator.h"
 #include "mozilla/ServoRestyleManager.h"
 #include "mozilla/dom/ChildIterator.h"
 #include "mozilla/dom/Element.h"
 #include "mozilla/dom/ElementInlines.h"
 #include "nsCSSAnonBoxes.h"
 #include "nsCSSPseudoElements.h"
+#include "nsHTMLStyleSheet.h"
 #include "nsIDocumentInlines.h"
 #include "nsPrintfCString.h"
 #include "nsStyleContext.h"
 #include "nsStyleSet.h"
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
@@ -121,16 +122,18 @@ ServoStyleSet::GetContext(nsIContent* aC
                           nsStyleContext* aParentContext,
                           nsIAtom* aPseudoTag,
                           CSSPseudoElementType aPseudoType,
                           LazyComputeBehavior aMayCompute)
 {
   MOZ_ASSERT(aContent->IsElement());
   Element* element = aContent->AsElement();
 
+
+  ResolveMappedAttrDeclarationBlocks();
   RefPtr<ServoComputedValues> computedValues;
   if (aMayCompute == LazyComputeBehavior::Allow) {
     computedValues =
       Servo_ResolveStyleLazily(element, nullptr, mRawSet.get()).Consume();
   } else {
     computedValues = ResolveServoStyle(element);
   }
 
@@ -149,16 +152,31 @@ ServoStyleSet::GetContext(already_AddRef
   // XXXbholley: Figure out the correct thing to pass here. Does this fixup
   // duplicate something that servo already does?
   bool skipFixup = false;
 
   return NS_NewStyleContext(aParentContext, mPresContext, aPseudoTag,
                             aPseudoType, Move(aComputedValues), skipFixup);
 }
 
+void
+ServoStyleSet::ResolveMappedAttrDeclarationBlocks()
+{
+  if (nsHTMLStyleSheet* sheet = mPresContext->Document()->GetAttributeStyleSheet()) {
+    sheet->CalculateMappedServoDeclarations();
+  }
+}
+
+void
+ServoStyleSet::PrepareAndTraverseSubtree(RawGeckoElementBorrowed aRoot,
+                                         mozilla::TraversalRootBehavior aRootBehavior) {
+  ResolveMappedAttrDeclarationBlocks();
+  Servo_TraverseSubtree(aRoot, mRawSet.get(), aRootBehavior);
+}
+
 already_AddRefed<nsStyleContext>
 ServoStyleSet::ResolveStyleFor(Element* aElement,
                                nsStyleContext* aParentContext,
                                LazyComputeBehavior aMayCompute,
                                TreeMatchContext& aTreeMatchContext)
 {
   // aTreeMatchContext is used to speed up selector matching,
   // but if the element already has a ServoComputedValues computed in
@@ -507,33 +525,32 @@ ServoStyleSet::HasStateDependentStyle(do
 void
 ServoStyleSet::StyleDocument()
 {
   // Restyle the document from the root element and each of the document level
   // NAC subtree roots.
   DocumentStyleRootIterator iter(mPresContext->Document());
   while (Element* root = iter.GetNextStyleRoot()) {
     if (root->ShouldTraverseForServo()) {
-      Servo_TraverseSubtree(root, mRawSet.get(), TraversalRootBehavior::Normal);
+      PrepareAndTraverseSubtree(root, TraversalRootBehavior::Normal);
     }
   }
 }
 
 void
 ServoStyleSet::StyleNewSubtree(Element* aRoot)
 {
   MOZ_ASSERT(!aRoot->HasServoData());
-  Servo_TraverseSubtree(aRoot, mRawSet.get(), TraversalRootBehavior::Normal);
+  PrepareAndTraverseSubtree(aRoot, TraversalRootBehavior::Normal);
 }
 
 void
 ServoStyleSet::StyleNewChildren(Element* aParent)
 {
-  Servo_TraverseSubtree(aParent, mRawSet.get(),
-                        TraversalRootBehavior::UnstyledChildrenOnly);
+  PrepareAndTraverseSubtree(aParent, TraversalRootBehavior::UnstyledChildrenOnly);
 }
 
 void
 ServoStyleSet::NoteStyleSheetsChanged()
 {
   Servo_StyleSet_NoteStyleSheetsChanged(mRawSet.get());
 }
 
--- a/layout/style/ServoStyleSet.h
+++ b/layout/style/ServoStyleSet.h
@@ -186,16 +186,30 @@ private:
                                               CSSPseudoElementType aPseudoType);
 
   already_AddRefed<nsStyleContext> GetContext(nsIContent* aContent,
                                               nsStyleContext* aParentContext,
                                               nsIAtom* aPseudoTag,
                                               CSSPseudoElementType aPseudoType,
                                               LazyComputeBehavior aMayCompute);
 
+  /**
+   * Resolve all ServoDeclarationBlocks attached to mapped
+   * presentation attributes cached on the document.
+   * Call this before jumping into Servo's style system.
+   */
+  void ResolveMappedAttrDeclarationBlocks();
+
+  /**
+   * Perform all lazy operations required before traversing
+   * a subtree.
+   */
+  void PrepareAndTraverseSubtree(RawGeckoElementBorrowed aRoot,
+                                 mozilla::TraversalRootBehavior aRootBehavior);
+
   nsPresContext* mPresContext;
   UniquePtr<RawServoStyleSet> mRawSet;
   EnumeratedArray<SheetType, SheetType::Count,
                   nsTArray<RefPtr<ServoStyleSheet>>> mSheets;
   int32_t mBatching;
 };
 
 } // namespace mozilla
--- a/layout/style/nsHTMLStyleSheet.cpp
+++ b/layout/style/nsHTMLStyleSheet.cpp
@@ -284,16 +284,17 @@ static const PLDHashTableOps LangRuleTab
 
 // -----------------------------------------------------------
 
 nsHTMLStyleSheet::nsHTMLStyleSheet(nsIDocument* aDocument)
   : mDocument(aDocument)
   , mTableQuirkColorRule(new TableQuirkColorRule())
   , mTableTHRule(new TableTHRule())
   , mMappedAttrTable(&MappedAttrTable_Ops, sizeof(MappedAttrTableEntry))
+  , mMappedAttrsDirty(false)
   , mLangRuleTable(&LangRuleTable_Ops, sizeof(LangRuleTableEntry))
 {
   MOZ_ASSERT(aDocument);
 }
 
 NS_IMPL_ISUPPORTS(nsHTMLStyleSheet, nsIStyleRuleProcessor)
 
 /* virtual */ void
@@ -473,16 +474,17 @@ void
 nsHTMLStyleSheet::Reset()
 {
   mLinkRule          = nullptr;
   mVisitedRule       = nullptr;
   mActiveRule        = nullptr;
 
   mLangRuleTable.Clear();
   mMappedAttrTable.Clear();
+  mMappedAttrsDirty = false;
 }
 
 nsresult
 nsHTMLStyleSheet::ImplLinkColorSetter(RefPtr<HTMLColorRule>& aRule, nscolor aColor)
 {
   if (aRule && aRule->mColor == aColor) {
     return NS_OK;
   }
@@ -521,42 +523,133 @@ nsresult
 nsHTMLStyleSheet::SetVisitedLinkColor(nscolor aColor)
 {
   return ImplLinkColorSetter(mVisitedRule, aColor);
 }
 
 already_AddRefed<nsMappedAttributes>
 nsHTMLStyleSheet::UniqueMappedAttributes(nsMappedAttributes* aMapped)
 {
+  mMappedAttrsDirty = true;
   auto entry = static_cast<MappedAttrTableEntry*>
                           (mMappedAttrTable.Add(aMapped, fallible));
   if (!entry)
     return nullptr;
   if (!entry->mAttributes) {
     // We added a new entry to the hashtable, so we have a new unique set.
     entry->mAttributes = aMapped;
   }
   RefPtr<nsMappedAttributes> ret = entry->mAttributes;
   return ret.forget();
 }
 
 void
 nsHTMLStyleSheet::DropMappedAttributes(nsMappedAttributes* aMapped)
 {
   NS_ENSURE_TRUE_VOID(aMapped);
-
 #ifdef DEBUG
   uint32_t entryCount = mMappedAttrTable.EntryCount() - 1;
 #endif
 
   mMappedAttrTable.Remove(aMapped);
 
   NS_ASSERTION(entryCount == mMappedAttrTable.EntryCount(), "not removed");
 }
 
+namespace {
+  // Struct containing once-initialized information about
+  // our synthesized rule data
+  struct StaticRuleDataInfo
+  {
+    // the bitmask used.
+    uint64_t mMask;
+    // the number of properties contained
+    size_t mPropCount;
+    // offset of given style struct in array
+    size_t mOffsets[nsStyleStructID_Length];
+    // location of property in given array
+    nsCSSPropertyID* mIndexToPropertyMapping;
+  };
+}
+
+static void
+CalculateIndexArray(StaticRuleDataInfo* aInfo)
+{
+  // this will leak at shutdown, but it's not much and this code is temporary
+  // anyway.
+  aInfo->mIndexToPropertyMapping = new nsCSSPropertyID[aInfo->mPropCount];
+  size_t structOffset;
+  size_t propertyIndex;
+  #define CSS_PROP_LIST_EXCLUDE_LOGICAL
+  #define CSS_PROP(name_, id_, method_, flags_, pref_, parsevariant_,     \
+                   kwtable_, stylestruct_, stylestructoffset_, animtype_) \
+    structOffset = aInfo->mOffsets[eStyleStruct_##stylestruct_]; \
+    propertyIndex = nsCSSProps::PropertyIndexInStruct(eCSSProperty_##id_); \
+    aInfo->mIndexToPropertyMapping[structOffset + propertyIndex] = eCSSProperty_##id_;
+  #include "nsCSSPropList.h"
+  #undef CSS_PROP
+  #undef CSS_PROP_LIST_EXCLUDE_LOGICAL
+}
+
+static StaticRuleDataInfo
+CalculateRuleDataInfo()
+{
+  StaticRuleDataInfo sizes;
+  sizes.mMask = 0;
+  sizes.mPropCount = 0;
+#define STYLE_STRUCT(name, checkdata_cb) \
+  sizes.mMask |= NS_STYLE_INHERIT_BIT(name); \
+  sizes.mOffsets[eStyleStruct_##name] = sizes.mPropCount; \
+  sizes.mPropCount += nsCSSProps::PropertyCountInStruct(eStyleStruct_##name);
+#include "nsStyleStructList.h"
+#undef STYLE_STRUCT
+
+  CalculateIndexArray(&sizes);
+
+  return sizes;
+}
+
+
+
+void
+nsHTMLStyleSheet::CalculateMappedServoDeclarations()
+{
+  // avoid recalculating or reallocating
+  static StaticRuleDataInfo sizes = CalculateRuleDataInfo();
+
+  if (!mMappedAttrsDirty) {
+    return;
+  }
+  mMappedAttrsDirty = false;
+
+  void* dataStorage = alloca(sizes.mPropCount * sizeof(nsCSSValue));
+  for (auto iter = mMappedAttrTable.Iter(); !iter.Done(); iter.Next()) {
+    MappedAttrTableEntry* attr = static_cast<MappedAttrTableEntry*>(iter.Get());
+    if (attr->mAttributes->GetServoStyle()) {
+      // Only handle cases which haven't been filled in already
+      continue;
+    }
+    // Construction cleans up any values we may have set
+    AutoCSSValueArray dataArray(dataStorage, sizes.mPropCount);
+
+    // synthesized ruleData
+    // we pass null for the style context because the code we call into
+    // doesn't deal with the style context. This is temporary.
+    nsRuleData ruleData(sizes.mMask, dataArray.get(),
+                        mDocument->GetShell()->GetPresContext(), nullptr);
+    // Copy the offsets; ruleData won't know where to find properties otherwise
+    mozilla::PodCopy(ruleData.mValueOffsets,
+                     sizes.mOffsets,
+                     nsStyleStructID_Length);
+    attr->mAttributes->LazilyResolveServoDeclaration(&ruleData,
+                                                     sizes.mIndexToPropertyMapping,
+                                                     sizes.mPropCount);
+  }
+}
+
 nsIStyleRule*
 nsHTMLStyleSheet::LangRuleFor(const nsString& aLanguage)
 {
   auto entry =
     static_cast<LangRuleTableEntry*>(mLangRuleTable.Add(&aLanguage, fallible));
   if (!entry) {
     NS_ASSERTION(false, "out of memory");
     return nullptr;
--- a/layout/style/nsHTMLStyleSheet.h
+++ b/layout/style/nsHTMLStyleSheet.h
@@ -57,16 +57,20 @@ public:
   nsresult SetLinkColor(nscolor aColor);
   nsresult SetActiveLinkColor(nscolor aColor);
   nsresult SetVisitedLinkColor(nscolor aColor);
 
   // Mapped Attribute management methods
   already_AddRefed<nsMappedAttributes>
     UniqueMappedAttributes(nsMappedAttributes* aMapped);
   void DropMappedAttributes(nsMappedAttributes* aMapped);
+  // For each mapped presentation attribute in the cache, resolve
+  // the attached ServoDeclarationBlock by running the mapping
+  // and converting the ruledata to Servo specified values.
+  void CalculateMappedServoDeclarations();
 
   nsIStyleRule* LangRuleFor(const nsString& aLanguage);
 
 private: 
   nsHTMLStyleSheet(const nsHTMLStyleSheet& aCopy) = delete;
   nsHTMLStyleSheet& operator=(const nsHTMLStyleSheet& aCopy) = delete;
 
   ~nsHTMLStyleSheet() {}
@@ -170,12 +174,16 @@ private:
   nsIDocument*            mDocument;
   RefPtr<HTMLColorRule> mLinkRule;
   RefPtr<HTMLColorRule> mVisitedRule;
   RefPtr<HTMLColorRule> mActiveRule;
   RefPtr<TableQuirkColorRule> mTableQuirkColorRule;
   RefPtr<TableTHRule>   mTableTHRule;
 
   PLDHashTable            mMappedAttrTable;
+  // Whether or not the mapped attributes table
+  // has been changed since the last call to
+  // CalculateMappedServoDeclarations()
+  bool                    mMappedAttrsDirty;
   PLDHashTable            mLangRuleTable;
 };
 
 #endif /* !defined(nsHTMLStyleSheet_h_) */
--- a/layout/style/nsRuleNode.cpp
+++ b/layout/style/nsRuleNode.cpp
@@ -2349,54 +2349,35 @@ UnsetPropertiesWithoutFlags(const nsStyl
 
   for (size_t i = 0, i_end = nsCSSProps::PropertyCountInStruct(aSID);
        i != i_end; ++i) {
     if ((flagData[i] & aFlags) != aFlags)
       values[i].Reset();
   }
 }
 
-/**
- * We allocate arrays of CSS values with alloca.  (These arrays are a
- * fixed size per style struct, but we don't want to waste the
- * allocation and construction/destruction costs of the big structs when
- * we're handling much smaller ones.)  Since the lifetime of an alloca
- * allocation is the life of the calling function, the caller must call
- * alloca.  However, to ensure that constructors and destructors are
- * balanced, we do the constructor and destructor calling from this RAII
- * class, AutoCSSValueArray.
- */
-struct AutoCSSValueArray {
-  /**
-   * aStorage must be the result of alloca(aCount * sizeof(nsCSSValue))
-   */
-  AutoCSSValueArray(void* aStorage, size_t aCount) {
-    MOZ_ASSERT(size_t(aStorage) % NS_ALIGNMENT_OF(nsCSSValue) == 0,
-               "bad alignment from alloca");
-    mCount = aCount;
-    // Don't use placement new[], since it might store extra data
-    // for the count (on Windows!).
-    mArray = static_cast<nsCSSValue*>(aStorage);
-    for (size_t i = 0; i < mCount; ++i) {
-      new (KnownNotNull, mArray + i) nsCSSValue();
-    }
-  }
-
-  ~AutoCSSValueArray() {
-    for (size_t i = 0; i < mCount; ++i) {
-      mArray[i].~nsCSSValue();
-    }
-  }
-
-  nsCSSValue* get() { return mArray; }
-
-private:
-  nsCSSValue *mArray;
-  size_t mCount;
-};
+AutoCSSValueArray::AutoCSSValueArray(void* aStorage, size_t aCount)
+{
+  MOZ_ASSERT(size_t(aStorage) % NS_ALIGNMENT_OF(nsCSSValue) == 0,
+             "bad alignment from alloca");
+  mCount = aCount;
+  // Don't use placement new[], since it might store extra data
+  // for the count (on Windows!).
+  mArray = static_cast<nsCSSValue*>(aStorage);
+  for (size_t i = 0; i < mCount; ++i) {
+    new (KnownNotNull, mArray + i) nsCSSValue();
+  }
+}
+
+AutoCSSValueArray::~AutoCSSValueArray()
+{
+  for (size_t i = 0; i < mCount; ++i) {
+    mArray[i].~nsCSSValue();
+  }
+}
 
 /* static */ bool
 nsRuleNode::ResolveVariableReferences(const nsStyleStructID aSID,
                                       nsRuleData* aRuleData,
                                       nsStyleContext* aContext)
 {
   MOZ_ASSERT(aSID != eStyleStruct_Variables);
   MOZ_ASSERT(aRuleData->mSIDs & nsCachedStyleData::GetBitForSID(aSID));
--- a/layout/style/nsRuleNode.h
+++ b/layout/style/nsRuleNode.h
@@ -1080,9 +1080,35 @@ private:
 
   // Store style struct on the style context and tell the style context
   // that it doesn't own the data
   static void StoreStyleOnContext(nsStyleContext* aContext,
                                   nsStyleStructID aSID,
                                   void* aStruct);
 };
 
+/**
+ * We allocate arrays of CSS values with alloca.  (These arrays are a
+ * fixed size per style struct, but we don't want to waste the
+ * allocation and construction/destruction costs of the big structs when
+ * we're handling much smaller ones.)  Since the lifetime of an alloca
+ * allocation is the life of the calling function, the caller must call
+ * alloca.  However, to ensure that constructors and destructors are
+ * balanced, we do the constructor and destructor calling from this RAII
+ * class, AutoCSSValueArray.
+ */
+struct AutoCSSValueArray
+{
+  /**
+   * aStorage must be the result of alloca(aCount * sizeof(nsCSSValue))
+   */
+  AutoCSSValueArray(void* aStorage, size_t aCount);
+
+  ~AutoCSSValueArray();
+
+  nsCSSValue* get() { return mArray; }
+
+private:
+  nsCSSValue *mArray;
+  size_t mCount;
+};
+
 #endif