2014-06-12 05:11:00 +04: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
|
|
|
|
* 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/. */
|
|
|
|
#ifndef mozilla_CounterStyleManager_h_
|
|
|
|
#define mozilla_CounterStyleManager_h_
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
#include "nsAtom.h"
|
2019-02-02 21:41:44 +03:00
|
|
|
#include "nsGkAtoms.h"
|
2014-06-12 05:11:00 +04:00
|
|
|
#include "nsStringFwd.h"
|
2017-05-13 14:42:23 +03:00
|
|
|
#include "nsDataHashtable.h"
|
2014-06-12 05:11:00 +04:00
|
|
|
#include "nsHashKeys.h"
|
|
|
|
|
|
|
|
#include "nsStyleConsts.h"
|
|
|
|
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
|
|
|
|
class nsPresContext;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2014-06-13 19:34:00 +04:00
|
|
|
class WritingMode;
|
|
|
|
|
2014-06-12 05:11:00 +04:00
|
|
|
typedef int32_t CounterValue;
|
|
|
|
|
|
|
|
class CounterStyleManager;
|
2014-09-25 04:19:00 +04:00
|
|
|
class AnonymousCounterStyle;
|
2014-06-12 05:11:00 +04:00
|
|
|
|
|
|
|
struct NegativeType;
|
|
|
|
struct PadType;
|
|
|
|
|
|
|
|
class CounterStyle {
|
|
|
|
protected:
|
2016-07-09 00:39:53 +03:00
|
|
|
explicit constexpr CounterStyle(int32_t aStyle) : mStyle(aStyle) {}
|
2014-06-12 05:11:00 +04:00
|
|
|
|
|
|
|
private:
|
2015-01-07 02:35:02 +03:00
|
|
|
CounterStyle(const CounterStyle& aOther) = delete;
|
|
|
|
void operator=(const CounterStyle& other) = delete;
|
2014-06-12 05:11:00 +04:00
|
|
|
|
|
|
|
public:
|
2017-08-29 07:44:14 +03:00
|
|
|
constexpr int32_t GetStyle() const { return mStyle; }
|
2014-06-12 05:11:00 +04:00
|
|
|
bool IsNone() const { return mStyle == NS_STYLE_LIST_STYLE_NONE; }
|
|
|
|
bool IsCustomStyle() const { return mStyle == NS_STYLE_LIST_STYLE_CUSTOM; }
|
|
|
|
// A style is dependent if it depends on the counter style manager.
|
|
|
|
// Custom styles are certainly dependent. In addition, some builtin
|
|
|
|
// styles are dependent for fallback.
|
|
|
|
bool IsDependentStyle() const;
|
|
|
|
|
2017-06-20 12:19:52 +03:00
|
|
|
virtual void GetPrefix(nsAString& aResult) = 0;
|
|
|
|
virtual void GetSuffix(nsAString& aResult) = 0;
|
2014-06-12 05:11:00 +04:00
|
|
|
void GetCounterText(CounterValue aOrdinal, WritingMode aWritingMode,
|
2017-06-20 12:19:52 +03:00
|
|
|
nsAString& aResult, bool& aIsRTL);
|
2014-06-12 05:11:00 +04:00
|
|
|
virtual void GetSpokenCounterText(CounterValue aOrdinal,
|
2014-06-13 19:34:00 +04:00
|
|
|
WritingMode aWritingMode,
|
2017-06-20 12:19:52 +03:00
|
|
|
nsAString& aResult, bool& aIsBullet);
|
2014-06-12 05:11:00 +04:00
|
|
|
|
|
|
|
// XXX This method could be removed once ::-moz-list-bullet and
|
|
|
|
// ::-moz-list-number are completely merged into ::marker.
|
|
|
|
virtual bool IsBullet() = 0;
|
|
|
|
|
|
|
|
virtual void GetNegative(NegativeType& aResult) = 0;
|
|
|
|
/**
|
|
|
|
* This method returns whether an ordinal is in the range of this
|
|
|
|
* counter style. Note that, it is possible that an ordinal in range
|
|
|
|
* is rejected by the generating algorithm.
|
|
|
|
*/
|
|
|
|
virtual bool IsOrdinalInRange(CounterValue aOrdinal) = 0;
|
|
|
|
/**
|
|
|
|
* This method returns whether an ordinal is in the default range of
|
|
|
|
* this counter style. This is the effective range when no 'range'
|
|
|
|
* descriptor is specified.
|
|
|
|
*/
|
|
|
|
virtual bool IsOrdinalInAutoRange(CounterValue aOrdinal) = 0;
|
|
|
|
virtual void GetPad(PadType& aResult) = 0;
|
|
|
|
virtual CounterStyle* GetFallback() = 0;
|
|
|
|
virtual uint8_t GetSpeakAs() = 0;
|
|
|
|
virtual bool UseNegativeSign() = 0;
|
|
|
|
|
|
|
|
virtual void CallFallbackStyle(CounterValue aOrdinal,
|
2014-06-13 19:34:00 +04:00
|
|
|
WritingMode aWritingMode, nsAString& aResult,
|
2014-09-25 04:19:00 +04:00
|
|
|
bool& aIsRTL);
|
2014-06-12 05:11:00 +04:00
|
|
|
virtual bool GetInitialCounterText(CounterValue aOrdinal,
|
2014-06-13 19:34:00 +04:00
|
|
|
WritingMode aWritingMode,
|
2017-06-20 12:19:52 +03:00
|
|
|
nsAString& aResult, bool& aIsRTL) = 0;
|
2014-06-12 05:11:00 +04:00
|
|
|
|
2014-09-25 04:19:00 +04:00
|
|
|
virtual AnonymousCounterStyle* AsAnonymous() { return nullptr; }
|
|
|
|
|
2014-06-12 05:11:00 +04:00
|
|
|
protected:
|
2017-08-29 07:44:14 +03:00
|
|
|
const int32_t mStyle;
|
2014-06-12 05:11:00 +04:00
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class AnonymousCounterStyle final : public CounterStyle {
|
2014-09-25 04:19:00 +04:00
|
|
|
public:
|
2017-06-20 12:19:52 +03:00
|
|
|
explicit AnonymousCounterStyle(const nsAString& aContent);
|
2020-01-05 16:10:39 +03:00
|
|
|
AnonymousCounterStyle(StyleSymbolsType, nsTArray<nsString> aSymbols);
|
2014-09-25 04:19:00 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void GetPrefix(nsAString& aResult) override;
|
|
|
|
virtual void GetSuffix(nsAString& aResult) override;
|
|
|
|
virtual bool IsBullet() override;
|
2014-09-25 04:19:00 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void GetNegative(NegativeType& aResult) override;
|
|
|
|
virtual bool IsOrdinalInRange(CounterValue aOrdinal) override;
|
|
|
|
virtual bool IsOrdinalInAutoRange(CounterValue aOrdinal) override;
|
|
|
|
virtual void GetPad(PadType& aResult) override;
|
|
|
|
virtual CounterStyle* GetFallback() override;
|
|
|
|
virtual uint8_t GetSpeakAs() override;
|
|
|
|
virtual bool UseNegativeSign() override;
|
2014-09-25 04:19:00 +04:00
|
|
|
|
|
|
|
virtual bool GetInitialCounterText(CounterValue aOrdinal,
|
|
|
|
WritingMode aWritingMode,
|
2015-03-21 19:28:04 +03:00
|
|
|
nsAString& aResult, bool& aIsRTL) override;
|
2014-09-25 04:19:00 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual AnonymousCounterStyle* AsAnonymous() override { return this; }
|
2014-09-25 04:19:00 +04:00
|
|
|
|
2015-03-27 01:48:10 +03:00
|
|
|
bool IsSingleString() const { return mSingleString; }
|
2019-05-17 02:05:54 +03:00
|
|
|
Span<const nsString> GetSymbols() const { return MakeSpan(mSymbols); }
|
2014-09-25 04:19:00 +04:00
|
|
|
|
2020-01-05 16:10:39 +03:00
|
|
|
uint8_t GetSystem() const;
|
|
|
|
|
2017-05-11 08:05:23 +03:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AnonymousCounterStyle)
|
2014-09-25 04:19:00 +04:00
|
|
|
|
|
|
|
private:
|
2020-03-17 12:38:32 +03:00
|
|
|
~AnonymousCounterStyle() = default;
|
2014-09-25 04:19:00 +04:00
|
|
|
|
2015-03-27 01:48:10 +03:00
|
|
|
bool mSingleString;
|
2020-01-05 16:10:39 +03:00
|
|
|
StyleSymbolsType mSymbolsType;
|
2014-09-25 04:19:00 +04:00
|
|
|
nsTArray<nsString> mSymbols;
|
|
|
|
};
|
|
|
|
|
2017-05-13 14:42:23 +03:00
|
|
|
// A smart pointer to CounterStyle. It either owns a reference to an
|
|
|
|
// anonymous counter style, or weakly refers to a named counter style
|
|
|
|
// managed by counter style manager.
|
|
|
|
class CounterStylePtr {
|
|
|
|
public:
|
|
|
|
CounterStylePtr() : mRaw(0) {}
|
|
|
|
CounterStylePtr(const CounterStylePtr& aOther) : mRaw(aOther.mRaw) {
|
2019-02-02 21:41:44 +03:00
|
|
|
if (!mRaw) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-27 16:33:25 +03:00
|
|
|
switch (GetType()) {
|
|
|
|
case eAnonymousCounterStyle:
|
|
|
|
AsAnonymous()->AddRef();
|
|
|
|
break;
|
2019-02-02 21:41:44 +03:00
|
|
|
case eAtom:
|
2017-05-27 16:33:25 +03:00
|
|
|
AsAtom()->AddRef();
|
|
|
|
break;
|
2019-02-02 21:41:44 +03:00
|
|
|
default:
|
2017-05-27 16:33:25 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown type");
|
|
|
|
break;
|
2017-05-13 14:42:23 +03:00
|
|
|
}
|
|
|
|
}
|
2017-05-22 15:51:20 +03:00
|
|
|
CounterStylePtr(CounterStylePtr&& aOther) : mRaw(aOther.mRaw) {
|
|
|
|
aOther.mRaw = 0;
|
|
|
|
}
|
2017-05-13 14:42:23 +03:00
|
|
|
~CounterStylePtr() { Reset(); }
|
|
|
|
|
|
|
|
CounterStylePtr& operator=(const CounterStylePtr& aOther) {
|
|
|
|
if (this != &aOther) {
|
|
|
|
Reset();
|
|
|
|
new (this) CounterStylePtr(aOther);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2017-05-27 16:33:25 +03:00
|
|
|
CounterStylePtr& operator=(CounterStylePtr&& aOther) {
|
|
|
|
if (this != &aOther) {
|
|
|
|
Reset();
|
|
|
|
mRaw = aOther.mRaw;
|
|
|
|
aOther.mRaw = 0;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2017-05-13 14:42:23 +03:00
|
|
|
CounterStylePtr& operator=(decltype(nullptr)) {
|
|
|
|
Reset();
|
|
|
|
return *this;
|
|
|
|
}
|
2019-02-02 21:41:44 +03:00
|
|
|
CounterStylePtr& operator=(nsStaticAtom* aStaticAtom) {
|
|
|
|
Reset();
|
|
|
|
mRaw = reinterpret_cast<uintptr_t>(aStaticAtom) | eAtom;
|
|
|
|
return *this;
|
|
|
|
}
|
2017-10-03 01:05:19 +03:00
|
|
|
CounterStylePtr& operator=(already_AddRefed<nsAtom> aAtom) {
|
2017-05-27 16:33:25 +03:00
|
|
|
Reset();
|
2019-02-02 21:41:44 +03:00
|
|
|
mRaw = reinterpret_cast<uintptr_t>(aAtom.take()) | eAtom;
|
2017-05-27 16:33:25 +03:00
|
|
|
return *this;
|
|
|
|
}
|
2017-05-13 14:42:23 +03:00
|
|
|
CounterStylePtr& operator=(AnonymousCounterStyle* aCounterStyle) {
|
|
|
|
Reset();
|
|
|
|
if (aCounterStyle) {
|
|
|
|
CounterStyle* raw = do_AddRef(aCounterStyle).take();
|
2017-05-27 16:33:25 +03:00
|
|
|
mRaw = reinterpret_cast<uintptr_t>(raw) | eAnonymousCounterStyle;
|
2017-05-13 14:42:23 +03:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-01-05 16:10:39 +03:00
|
|
|
// TODO(emilio): Make CounterStyle have a single representation, either by
|
|
|
|
// removing CounterStylePtr or by moving this representation to Rust.
|
|
|
|
static CounterStylePtr FromStyle(const StyleCounterStyle& aStyle) {
|
|
|
|
CounterStylePtr ret;
|
|
|
|
if (aStyle.IsName()) {
|
|
|
|
ret = do_AddRef(aStyle.AsName().AsAtom());
|
|
|
|
} else {
|
|
|
|
StyleSymbolsType type = aStyle.AsSymbols()._0;
|
|
|
|
Span<const StyleSymbol> symbols = aStyle.AsSymbols()._1._0.AsSpan();
|
|
|
|
nsTArray<nsString> transcoded(symbols.Length());
|
|
|
|
for (const auto& symbol : symbols) {
|
|
|
|
MOZ_ASSERT(symbol.IsString(), "Should not have <ident> in symbols()");
|
|
|
|
transcoded.AppendElement(
|
|
|
|
NS_ConvertUTF8toUTF16(symbol.AsString().AsString()));
|
|
|
|
}
|
|
|
|
ret = new AnonymousCounterStyle(type, std::move(transcoded));
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-05-13 14:42:23 +03:00
|
|
|
explicit operator bool() const { return !!mRaw; }
|
|
|
|
bool operator!() const { return !mRaw; }
|
|
|
|
bool operator==(const CounterStylePtr& aOther) const {
|
2019-02-02 21:41:44 +03:00
|
|
|
// FIXME(emilio): For atoms this is all right, but for symbols doesn't this
|
|
|
|
// cause us to compare as unequal all the time, even if the specified
|
|
|
|
// symbols didn't change?
|
2017-05-13 14:42:23 +03:00
|
|
|
return mRaw == aOther.mRaw;
|
|
|
|
}
|
|
|
|
bool operator!=(const CounterStylePtr& aOther) const {
|
|
|
|
return mRaw != aOther.mRaw;
|
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* AsAtom() const {
|
2019-02-02 21:41:44 +03:00
|
|
|
MOZ_ASSERT(IsAtom());
|
2017-10-03 01:05:19 +03:00
|
|
|
return reinterpret_cast<nsAtom*>(mRaw & ~eMask);
|
2017-08-29 10:11:13 +03:00
|
|
|
}
|
|
|
|
AnonymousCounterStyle* AsAnonymous() const {
|
|
|
|
MOZ_ASSERT(IsAnonymous());
|
|
|
|
return static_cast<AnonymousCounterStyle*>(
|
|
|
|
reinterpret_cast<CounterStyle*>(mRaw & ~eMask));
|
|
|
|
}
|
|
|
|
|
2019-02-02 21:41:44 +03:00
|
|
|
bool IsAtom() const { return GetType() == eAtom; }
|
|
|
|
bool IsAnonymous() const { return GetType() == eAnonymousCounterStyle; }
|
2017-05-13 14:42:23 +03:00
|
|
|
|
2019-02-02 21:41:44 +03:00
|
|
|
bool IsNone() const { return IsAtom() && AsAtom() == nsGkAtoms::none; }
|
|
|
|
|
|
|
|
private:
|
2017-05-27 16:33:25 +03:00
|
|
|
enum Type : uintptr_t {
|
2019-02-02 21:41:44 +03:00
|
|
|
eAnonymousCounterStyle = 0,
|
|
|
|
eAtom = 1,
|
|
|
|
eMask = 1,
|
2017-05-27 16:33:25 +03:00
|
|
|
};
|
|
|
|
|
2019-02-02 21:41:44 +03:00
|
|
|
static_assert(alignof(CounterStyle) >= 1 << eMask,
|
|
|
|
"We're gonna tag the pointer, so it better fit");
|
|
|
|
static_assert(alignof(nsAtom) >= 1 << eMask,
|
|
|
|
"We're gonna tag the pointer, so it better fit");
|
|
|
|
|
2017-05-27 16:33:25 +03:00
|
|
|
Type GetType() const { return static_cast<Type>(mRaw & eMask); }
|
2017-05-13 14:42:23 +03:00
|
|
|
|
|
|
|
void Reset() {
|
2019-02-02 21:41:44 +03:00
|
|
|
if (!mRaw) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-27 16:33:25 +03:00
|
|
|
switch (GetType()) {
|
|
|
|
case eAnonymousCounterStyle:
|
|
|
|
AsAnonymous()->Release();
|
|
|
|
break;
|
2019-02-02 21:41:44 +03:00
|
|
|
case eAtom:
|
2017-05-27 16:33:25 +03:00
|
|
|
AsAtom()->Release();
|
|
|
|
break;
|
2019-02-02 21:41:44 +03:00
|
|
|
default:
|
2017-05-27 16:33:25 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown type");
|
|
|
|
break;
|
2017-05-13 14:42:23 +03:00
|
|
|
}
|
|
|
|
mRaw = 0;
|
|
|
|
}
|
|
|
|
|
2019-02-02 21:41:44 +03:00
|
|
|
// mRaw contains the pointer, and its last bit is used to store the type of
|
|
|
|
// the pointer.
|
|
|
|
// If the type is eAtom, the pointer owns a reference to an nsAtom
|
|
|
|
// (potentially null).
|
2017-05-27 16:33:25 +03:00
|
|
|
// If the type is eAnonymousCounterStyle, it owns a reference to an
|
2019-02-02 21:41:44 +03:00
|
|
|
// anonymous counter style (never null).
|
2017-05-13 14:42:23 +03:00
|
|
|
uintptr_t mRaw;
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class CounterStyleManager final {
|
2014-06-19 06:28:59 +04:00
|
|
|
private:
|
|
|
|
~CounterStyleManager();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-06-12 05:11:00 +04:00
|
|
|
public:
|
2014-08-08 03:48:38 +04:00
|
|
|
explicit CounterStyleManager(nsPresContext* aPresContext);
|
2014-06-12 05:11:00 +04:00
|
|
|
|
|
|
|
void Disconnect();
|
|
|
|
|
|
|
|
bool IsInitial() const {
|
2017-06-13 09:00:37 +03:00
|
|
|
// only 'none', 'decimal', and 'disc'
|
|
|
|
return mStyles.Count() == 3;
|
2014-06-12 05:11:00 +04:00
|
|
|
}
|
|
|
|
|
2017-06-14 09:39:47 +03:00
|
|
|
// Returns the counter style object for the given name from the style
|
|
|
|
// table if it is already built, and nullptr otherwise.
|
2017-10-03 01:05:19 +03:00
|
|
|
CounterStyle* GetCounterStyle(nsAtom* aName) const {
|
2017-06-14 09:39:47 +03:00
|
|
|
return mStyles.Get(aName);
|
|
|
|
}
|
|
|
|
// Same as GetCounterStyle but try to build the counter style object
|
|
|
|
// rather than returning nullptr if that hasn't been built.
|
2019-02-02 21:41:44 +03:00
|
|
|
CounterStyle* ResolveCounterStyle(nsAtom* aName);
|
|
|
|
CounterStyle* ResolveCounterStyle(const CounterStylePtr& aPtr) {
|
|
|
|
if (aPtr.IsAtom()) {
|
|
|
|
return ResolveCounterStyle(aPtr.AsAtom());
|
|
|
|
}
|
|
|
|
return aPtr.AsAnonymous();
|
|
|
|
}
|
2014-06-12 05:11:00 +04:00
|
|
|
|
|
|
|
static CounterStyle* GetBuiltinStyle(int32_t aStyle);
|
|
|
|
static CounterStyle* GetNoneStyle() {
|
|
|
|
return GetBuiltinStyle(NS_STYLE_LIST_STYLE_NONE);
|
|
|
|
}
|
|
|
|
static CounterStyle* GetDecimalStyle() {
|
|
|
|
return GetBuiltinStyle(NS_STYLE_LIST_STYLE_DECIMAL);
|
|
|
|
}
|
2017-06-13 09:00:37 +03:00
|
|
|
static CounterStyle* GetDiscStyle() {
|
|
|
|
return GetBuiltinStyle(NS_STYLE_LIST_STYLE_DISC);
|
|
|
|
}
|
2014-06-12 05:11:00 +04:00
|
|
|
|
|
|
|
// This method will scan all existing counter styles generated by this
|
|
|
|
// manager, and remove or mark data dirty accordingly. It returns true
|
|
|
|
// if any counter style is changed, false elsewise. This method should
|
|
|
|
// be called when any counter style may be affected.
|
|
|
|
bool NotifyRuleChanged();
|
2017-05-13 14:42:23 +03:00
|
|
|
// NotifyRuleChanged will evict no longer needed counter styles into
|
|
|
|
// mRetiredStyles, and this function destroys all objects listed there.
|
|
|
|
// It should be called only after no one may ever use those objects.
|
|
|
|
void CleanRetiredStyles();
|
2014-06-12 05:11:00 +04:00
|
|
|
|
2014-10-17 00:10:00 +04:00
|
|
|
nsPresContext* PresContext() const { return mPresContext; }
|
|
|
|
|
2014-06-12 05:11:00 +04:00
|
|
|
NS_INLINE_DECL_REFCOUNTING(CounterStyleManager)
|
|
|
|
|
|
|
|
private:
|
2017-05-13 14:42:23 +03:00
|
|
|
void DestroyCounterStyle(CounterStyle* aCounterStyle);
|
|
|
|
|
2014-06-12 05:11:00 +04:00
|
|
|
nsPresContext* mPresContext;
|
2017-10-03 01:05:19 +03:00
|
|
|
nsDataHashtable<nsRefPtrHashKey<nsAtom>, CounterStyle*> mStyles;
|
2017-05-13 14:42:23 +03:00
|
|
|
nsTArray<CounterStyle*> mRetiredStyles;
|
2014-06-12 05:11:00 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif /* !defined(mozilla_CounterStyleManager_h_) */
|