2017-10-27 20:33:53 +03:00
|
|
|
/* -*- 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
|
2012-05-21 15:12:37 +04:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2017-08-17 00:10:56 +03:00
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2004-04-13 01:56:09 +04:00
|
|
|
|
2006-03-25 08:47:31 +03:00
|
|
|
/*
|
|
|
|
* style sheet and style rule processor representing data from presentational
|
|
|
|
* HTML attributes
|
|
|
|
*/
|
|
|
|
|
2004-04-13 01:56:09 +04:00
|
|
|
#include "nsHTMLStyleSheet.h"
|
2004-01-26 22:22:05 +03:00
|
|
|
#include "nsMappedAttributes.h"
|
2006-12-26 20:47:52 +03:00
|
|
|
#include "nsGkAtoms.h"
|
2004-08-01 03:15:21 +04:00
|
|
|
#include "nsPresContext.h"
|
2014-04-03 08:18:36 +04:00
|
|
|
#include "mozilla/EventStates.h"
|
2019-03-29 18:12:47 +03:00
|
|
|
#include "mozilla/PresShell.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2019-03-29 18:11:04 +03:00
|
|
|
#include "mozilla/dom/DocumentInlines.h"
|
1998-09-10 23:32:14 +04:00
|
|
|
#include "nsStyleConsts.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2010-05-05 22:18:05 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2013-05-30 12:00:20 +04:00
|
|
|
#include "nsHashKeys.h"
|
2016-08-17 22:28:45 +03:00
|
|
|
#include "mozilla/OperatorNewExtensions.h"
|
2017-02-13 06:21:33 +03:00
|
|
|
#include "mozilla/RestyleManager.h"
|
2018-06-05 14:39:42 +03:00
|
|
|
#include "mozilla/ServoBindings.h"
|
2017-06-01 21:37:02 +03:00
|
|
|
#include "mozilla/ServoStyleSet.h"
|
2010-04-30 17:12:06 +04:00
|
|
|
|
2013-05-30 12:00:20 +04:00
|
|
|
using namespace mozilla;
|
2010-04-30 17:12:06 +04:00
|
|
|
using namespace mozilla::dom;
|
2001-06-01 02:19:43 +04:00
|
|
|
|
1998-08-27 04:50:52 +04:00
|
|
|
// -----------------------------------------------------------
|
1998-07-25 05:20:48 +04:00
|
|
|
|
2002-08-17 16:17:03 +04:00
|
|
|
struct MappedAttrTableEntry : public PLDHashEntryHdr {
|
2019-05-01 11:47:10 +03:00
|
|
|
nsMappedAttributes* mAttributes;
|
1998-09-02 06:10:44 +04:00
|
|
|
};
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
static PLDHashNumber MappedAttrTable_HashKey(const void* key) {
|
|
|
|
nsMappedAttributes* attributes =
|
|
|
|
static_cast<nsMappedAttributes*>(const_cast<void*>(key));
|
1998-09-02 06:10:44 +04:00
|
|
|
|
2004-01-26 22:22:05 +03:00
|
|
|
return attributes->HashValue();
|
1998-09-02 06:10:44 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
static void MappedAttrTable_ClearEntry(PLDHashTable* table,
|
|
|
|
PLDHashEntryHdr* hdr) {
|
|
|
|
MappedAttrTableEntry* entry = static_cast<MappedAttrTableEntry*>(hdr);
|
1998-09-02 06:10:44 +04:00
|
|
|
|
2002-08-17 16:17:03 +04:00
|
|
|
entry->mAttributes->DropStyleSheetReference();
|
|
|
|
memset(entry, 0, sizeof(MappedAttrTableEntry));
|
1998-09-02 06:10:44 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
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);
|
2002-08-17 16:17:03 +04:00
|
|
|
|
2004-01-26 22:22:05 +03:00
|
|
|
return attributes->Equals(entry->mAttributes);
|
2002-08-17 16:17:03 +04:00
|
|
|
}
|
|
|
|
|
2013-11-19 06:51:48 +04:00
|
|
|
static const PLDHashTableOps MappedAttrTable_Ops = {
|
2002-08-17 16:17:03 +04:00
|
|
|
MappedAttrTable_HashKey, MappedAttrTable_MatchEntry,
|
2015-09-15 00:23:47 +03:00
|
|
|
PLDHashTable::MoveEntryStub, MappedAttrTable_ClearEntry, nullptr};
|
2013-05-30 12:00:20 +04:00
|
|
|
|
|
|
|
// -----------------------------------------------------------
|
|
|
|
|
1998-12-29 06:38:16 +03:00
|
|
|
// -----------------------------------------------------------
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsHTMLStyleSheet::nsHTMLStyleSheet(Document* aDocument)
|
2013-06-14 09:34:37 +04:00
|
|
|
: mDocument(aDocument),
|
2015-05-19 06:43:18 +03:00
|
|
|
mMappedAttrTable(&MappedAttrTable_Ops, sizeof(MappedAttrTableEntry)),
|
2017-01-20 02:56:53 +03:00
|
|
|
mMappedAttrsDirty(false) {
|
2012-08-04 11:44:01 +04:00
|
|
|
MOZ_ASSERT(aDocument);
|
2001-08-04 06:43:05 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void nsHTMLStyleSheet::SetOwningDocument(Document* aDocument) {
|
1998-12-03 23:20:35 +03:00
|
|
|
mDocument = aDocument; // not refcounted
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
2013-06-14 09:34:37 +04:00
|
|
|
void nsHTMLStyleSheet::Reset() {
|
2017-06-01 21:37:02 +03:00
|
|
|
mServoUnvisitedLinkDecl = nullptr;
|
|
|
|
mServoVisitedLinkDecl = nullptr;
|
|
|
|
mServoActiveLinkDecl = nullptr;
|
|
|
|
|
2015-05-19 06:43:18 +03:00
|
|
|
mMappedAttrTable.Clear();
|
2017-01-20 02:56:53 +03:00
|
|
|
mMappedAttrsDirty = false;
|
1999-02-03 22:38:16 +03:00
|
|
|
}
|
|
|
|
|
2017-06-01 21:37:02 +03:00
|
|
|
nsresult nsHTMLStyleSheet::ImplLinkColorSetter(
|
2019-05-01 11:47:10 +03:00
|
|
|
RefPtr<RawServoDeclarationBlock>& aDecl, nscolor aColor) {
|
2019-03-29 18:12:47 +03:00
|
|
|
if (!mDocument || !mDocument->GetPresShell()) {
|
2010-06-25 17:59:57 +04:00
|
|
|
return NS_OK;
|
1998-07-25 05:20:48 +04:00
|
|
|
}
|
2003-02-22 22:15:46 +03:00
|
|
|
|
2018-03-23 19:01:34 +03:00
|
|
|
MOZ_ASSERT(!ServoStyleSet::IsInServoTraversal());
|
|
|
|
aDecl = Servo_DeclarationBlock_CreateEmpty().Consume();
|
|
|
|
Servo_DeclarationBlock_SetColorValue(aDecl.get(), eCSSProperty_color, aColor);
|
2003-02-22 22:15:46 +03:00
|
|
|
|
2010-06-18 20:23:05 +04:00
|
|
|
// Now make sure we restyle any links that might need it. This
|
|
|
|
// shouldn't happen often, so just rebuilding everything is ok.
|
2019-05-01 11:47:10 +03:00
|
|
|
if (Element* root = mDocument->GetRootElement()) {
|
|
|
|
RestyleManager* rm = mDocument->GetPresContext()->RestyleManager();
|
2019-03-14 14:47:50 +03:00
|
|
|
rm->PostRestyleEvent(root, RestyleHint::RestyleSubtree(), nsChangeHint(0));
|
2010-06-18 20:23:05 +04:00
|
|
|
}
|
1998-07-25 05:20:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-18 20:23:05 +04:00
|
|
|
nsresult nsHTMLStyleSheet::SetLinkColor(nscolor aColor) {
|
2018-02-01 07:04:04 +03:00
|
|
|
return ImplLinkColorSetter(mServoUnvisitedLinkDecl, aColor);
|
2010-06-18 20:23:05 +04:00
|
|
|
}
|
2003-02-22 22:15:46 +03:00
|
|
|
|
2010-06-18 20:23:05 +04:00
|
|
|
nsresult nsHTMLStyleSheet::SetActiveLinkColor(nscolor aColor) {
|
2018-02-01 07:04:04 +03:00
|
|
|
return ImplLinkColorSetter(mServoActiveLinkDecl, aColor);
|
1998-07-25 05:20:48 +04:00
|
|
|
}
|
|
|
|
|
2004-04-13 01:56:09 +04:00
|
|
|
nsresult nsHTMLStyleSheet::SetVisitedLinkColor(nscolor aColor) {
|
2018-02-01 07:04:04 +03:00
|
|
|
return ImplLinkColorSetter(mServoVisitedLinkDecl, aColor);
|
1998-07-25 05:20:48 +04:00
|
|
|
}
|
|
|
|
|
2004-04-13 01:56:09 +04:00
|
|
|
already_AddRefed<nsMappedAttributes> nsHTMLStyleSheet::UniqueMappedAttributes(
|
2019-05-01 11:47:10 +03:00
|
|
|
nsMappedAttributes* aMapped) {
|
2017-01-20 02:56:53 +03:00
|
|
|
mMappedAttrsDirty = true;
|
2019-05-01 11:47:10 +03:00
|
|
|
auto entry = static_cast<MappedAttrTableEntry*>(
|
2015-09-15 00:23:12 +03:00
|
|
|
mMappedAttrTable.Add(aMapped, fallible));
|
2012-07-30 18:20:58 +04:00
|
|
|
if (!entry) return nullptr;
|
2002-08-17 16:17:03 +04:00
|
|
|
if (!entry->mAttributes) {
|
|
|
|
// We added a new entry to the hashtable, so we have a new unique set.
|
|
|
|
entry->mAttributes = aMapped;
|
1999-07-07 05:30:43 +04:00
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsMappedAttributes> ret = entry->mAttributes;
|
2013-04-22 15:15:59 +04:00
|
|
|
return ret.forget();
|
1999-07-07 05:30:43 +04:00
|
|
|
}
|
1998-09-02 06:10:44 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void nsHTMLStyleSheet::DropMappedAttributes(nsMappedAttributes* aMapped) {
|
2012-12-24 08:45:57 +04:00
|
|
|
NS_ENSURE_TRUE_VOID(aMapped);
|
2002-08-17 16:17:03 +04:00
|
|
|
#ifdef DEBUG
|
2014-08-26 03:56:33 +04:00
|
|
|
uint32_t entryCount = mMappedAttrTable.EntryCount() - 1;
|
2002-08-17 16:17:03 +04:00
|
|
|
#endif
|
|
|
|
|
2015-09-15 00:23:24 +03:00
|
|
|
mMappedAttrTable.Remove(aMapped);
|
2002-08-17 16:17:03 +04:00
|
|
|
|
2014-08-26 03:56:33 +04:00
|
|
|
NS_ASSERTION(entryCount == mMappedAttrTable.EntryCount(), "not removed");
|
1998-09-02 06:10:44 +04:00
|
|
|
}
|
|
|
|
|
2018-01-05 15:47:04 +03:00
|
|
|
void nsHTMLStyleSheet::CalculateMappedServoDeclarations() {
|
2017-01-20 02:56:53 +03:00
|
|
|
for (auto iter = mMappedAttrTable.Iter(); !iter.Done(); iter.Next()) {
|
2019-05-01 11:47:10 +03:00
|
|
|
MappedAttrTableEntry* attr = static_cast<MappedAttrTableEntry*>(iter.Get());
|
2017-01-20 02:56:53 +03:00
|
|
|
if (attr->mAttributes->GetServoStyle()) {
|
|
|
|
// Only handle cases which haven't been filled in already
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-05 15:47:04 +03:00
|
|
|
attr->mAttributes->LazilyResolveServoDeclaration(mDocument);
|
2017-01-20 02:56:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-23 16:03:39 +04:00
|
|
|
size_t nsHTMLStyleSheet::DOMSizeOfIncludingThis(
|
|
|
|
MallocSizeOf aMallocSizeOf) const {
|
2012-02-02 01:58:01 +04:00
|
|
|
size_t n = aMallocSizeOf(this);
|
2011-07-19 20:40:18 +04:00
|
|
|
|
2015-07-30 08:28:20 +03:00
|
|
|
n += mMappedAttrTable.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
for (auto iter = mMappedAttrTable.ConstIter(); !iter.Done(); iter.Next()) {
|
2019-05-01 11:47:10 +03:00
|
|
|
auto entry = static_cast<MappedAttrTableEntry*>(iter.Get());
|
2015-07-30 08:28:20 +03:00
|
|
|
n += entry->mAttributes->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
2011-07-19 20:40:18 +04:00
|
|
|
|
2012-02-02 01:58:01 +04:00
|
|
|
// Measurement of the following members may be added later if DMD finds it is
|
|
|
|
// worthwhile:
|
|
|
|
// - mURL
|
|
|
|
// - mLinkRule
|
|
|
|
// - mVisitedRule
|
|
|
|
// - mActiveRule
|
|
|
|
// - mTableQuirkColorRule
|
|
|
|
// - mTableTHRule
|
2013-05-30 12:00:20 +04:00
|
|
|
// - mLangRuleTable
|
2012-02-02 01:58:01 +04:00
|
|
|
//
|
|
|
|
// The following members are not measured:
|
|
|
|
// - mDocument, because it's non-owning
|
|
|
|
|
|
|
|
return n;
|
2011-07-19 20:40:18 +04:00
|
|
|
}
|