DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (b6d82b1a6b02)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
/* -*- 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/. */

/*
 * style sheet and style rule processor representing data from presentational
 * HTML attributes
 */

#include "nsHTMLStyleSheet.h"
#include "nsMappedAttributes.h"
#include "nsGkAtoms.h"
#include "nsPresContext.h"
#include "mozilla/EventStates.h"
#include "mozilla/PresShell.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/DocumentInlines.h"
#include "nsStyleConsts.h"
#include "nsError.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/dom/Element.h"
#include "nsHashKeys.h"
#include "mozilla/OperatorNewExtensions.h"
#include "mozilla/RestyleManager.h"
#include "mozilla/ServoBindings.h"
#include "mozilla/ServoStyleSet.h"

using namespace mozilla;
using namespace mozilla::dom;

// -----------------------------------------------------------

struct MappedAttrTableEntry : public PLDHashEntryHdr {
  nsMappedAttributes* mAttributes;
};

static PLDHashNumber MappedAttrTable_HashKey(const void* key) {
  nsMappedAttributes* attributes =
      static_cast<nsMappedAttributes*>(const_cast<void*>(key));

  return attributes->HashValue();
}

static void MappedAttrTable_ClearEntry(PLDHashTable* table,
                                       PLDHashEntryHdr* hdr) {
  MappedAttrTableEntry* entry = static_cast<MappedAttrTableEntry*>(hdr);

  entry->mAttributes->DropStyleSheetReference();
  memset(entry, 0, sizeof(MappedAttrTableEntry));
}

static bool MappedAttrTable_MatchEntry(const PLDHashEntryHdr* hdr,
                                       const void* key) {
  nsMappedAttributes* attributes =
      static_cast<nsMappedAttributes*>(const_cast<void*>(key));
  const MappedAttrTableEntry* entry =
      static_cast<const MappedAttrTableEntry*>(hdr);

  return attributes->Equals(entry->mAttributes);
}

static const PLDHashTableOps MappedAttrTable_Ops = {
    MappedAttrTable_HashKey, MappedAttrTable_MatchEntry,
    PLDHashTable::MoveEntryStub, MappedAttrTable_ClearEntry, nullptr};

// -----------------------------------------------------------

// -----------------------------------------------------------

nsHTMLStyleSheet::nsHTMLStyleSheet(Document* aDocument)
    : mDocument(aDocument),
      mMappedAttrTable(&MappedAttrTable_Ops, sizeof(MappedAttrTableEntry)),
      mMappedAttrsDirty(false) {
  MOZ_ASSERT(aDocument);
}

void nsHTMLStyleSheet::SetOwningDocument(Document* aDocument) {
  mDocument = aDocument;  // not refcounted
}

void nsHTMLStyleSheet::Reset() {
  mServoUnvisitedLinkDecl = nullptr;
  mServoVisitedLinkDecl = nullptr;
  mServoActiveLinkDecl = nullptr;

  mMappedAttrTable.Clear();
  mMappedAttrsDirty = false;
}

nsresult nsHTMLStyleSheet::ImplLinkColorSetter(
    RefPtr<RawServoDeclarationBlock>& aDecl, nscolor aColor) {
  if (!mDocument || !mDocument->GetPresShell()) {
    return NS_OK;
  }

  MOZ_ASSERT(!ServoStyleSet::IsInServoTraversal());
  aDecl = Servo_DeclarationBlock_CreateEmpty().Consume();
  Servo_DeclarationBlock_SetColorValue(aDecl.get(), eCSSProperty_color, aColor);

  // Now make sure we restyle any links that might need it.  This
  // shouldn't happen often, so just rebuilding everything is ok.
  if (Element* root = mDocument->GetRootElement()) {
    RestyleManager* rm = mDocument->GetPresContext()->RestyleManager();
    rm->PostRestyleEvent(root, RestyleHint::RestyleSubtree(), nsChangeHint(0));
  }
  return NS_OK;
}

nsresult nsHTMLStyleSheet::SetLinkColor(nscolor aColor) {
  return ImplLinkColorSetter(mServoUnvisitedLinkDecl, aColor);
}

nsresult nsHTMLStyleSheet::SetActiveLinkColor(nscolor aColor) {
  return ImplLinkColorSetter(mServoActiveLinkDecl, aColor);
}

nsresult nsHTMLStyleSheet::SetVisitedLinkColor(nscolor aColor) {
  return ImplLinkColorSetter(mServoVisitedLinkDecl, 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");
}

void nsHTMLStyleSheet::CalculateMappedServoDeclarations() {
  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;
    }
    attr->mAttributes->LazilyResolveServoDeclaration(mDocument);
  }
}

size_t nsHTMLStyleSheet::DOMSizeOfIncludingThis(
    MallocSizeOf aMallocSizeOf) const {
  size_t n = aMallocSizeOf(this);

  n += mMappedAttrTable.ShallowSizeOfExcludingThis(aMallocSizeOf);
  for (auto iter = mMappedAttrTable.ConstIter(); !iter.Done(); iter.Next()) {
    auto entry = static_cast<MappedAttrTableEntry*>(iter.Get());
    n += entry->mAttributes->SizeOfIncludingThis(aMallocSizeOf);
  }

  // Measurement of the following members may be added later if DMD finds it is
  // worthwhile:
  // - mURL
  // - mLinkRule
  // - mVisitedRule
  // - mActiveRule
  // - mTableQuirkColorRule
  // - mTableTHRule
  // - mLangRuleTable
  //
  // The following members are not measured:
  // - mDocument, because it's non-owning

  return n;
}