Files
mozilla45esr/layout/base/nsCounterManager.h
roytam1 ac0651448a import changes from tenfourfox: https://github.com/classilla/tenfourfox/compare/966d8cab820858bcda2fa8e945ed14b66d9e83f9...e7767cdaa56ee060b14702ed64ae594958fcb1dd
- issue #385: unconditionally disable JS source compression (6a91f9666)
- #359: security.pki.sha1_enforcement_level -> 1 (966d8cab8)
- for #375: M1349940 M1355595 M1334635 M1353708 M1354782 M1354781 M1349742 M1345315 M1356655 M1356663 M1357638 M1358809 M1307134 M1331342 M1220466 (964aa7e7c)
- #378: ensure remaining users of faulty LZ4 module never execute (922a32738)
- #387: pulled nsGenConList.* with relevant deps up to mozilla-central (b1831fc75)
- prerequisite for #375: M1347759 (8e58b5c8f)
- #375: M1348278 (8d665811d)
- #368: pref on Brotli (7da09aa4e)
- #375: M1330912 (sync patch only) (bea84e507)
- #375: M1342719 M1342720 (f4015e0c1)
- Bug 1071646 - Light refactoring of lexical binding helpers in Parser. (r=jorendorff) (167ad8564)
- Bug 1071646 - Make functions block-scoped in JS and implement Annex B semantics for compatibility. (r=jorendorff) (25844de1b)
- Bug 1071646 - Support labelled function declarations in sloppy mode per Annex B.3.2. (r=jorendorff) (37bf861a9)
- Bug 1071646 - Introduce JSOP_BINDVAR to support Annex B.3.3.3. (r=jorendorff) (884bd2ab5)
- Bug 1071646 - Cast ParseNode to Definition using as<T>. (r=jorendorff) (feba46e3f)
- Bug 1071646 - Support JSOP_BINDVAR in Baseline. (r=jandem) (92b6a3b48)
- #372: merge changeset 299512 (c28302281)
- Bug 1071646 - Support JSOP_BINDVAR in Ion. (r=jandem) (d05f77998)
- Bug 1231758 - Fix bogus assertion in BCE for Annex B function assignment. (r=jorendorff) (a11dcf0cc)
- Bug 1234717 - Fix upvar TDZ for block-scoped functions. (r=jorendorff) (2ec542b5a)
- Bug 1071646 - Forgot to commit tests on backout and relanding cycle. (r=me) (069a26bf9)
- #372: merge changeset 303861 (745f7e7a2)
- Bug 1235590 - Allow redeclaring block-scoped functions and warn about deprecation for now. (r=jorendorff) (8a51cc1f9)
- Bug 1243793 - Fix handling of labels when emitting hoisted function definitions. (r=jorendorff) (c0ffd70f9)
- Bug 1236875 - Fix BytecodeEmitter::atBodyLevel() for modules r=efaust (5b2c22f62)
- Bug 1304641 - TraceLogger: Also throw compartment mismatch error when running in the jits, r=bbouvier (717134569)
- Bug 1198833 - Variable redeclaration should be a syntax error r=shu (ac6710b0f)
- #393, Bug 1135377 - Part 1: Implement RegExp unicode flag. r=till, f=anba (d00063089)
- #393, Bug 1135377 - Part 2: Parse RegExp unicode character in non-CharacterClass. r=till, f=anba (c85a176bd)
- #393, Bug 1135377 - Part 3: Parse RegExp unicode character in CharacterClass. r=till, f=anba (68f44ec41)
- #393, Bug 1135377 - Part 4: Support everything Atom in RegExp with unicode flag. r=till, f=anba (4e924a688)
- #393, Bug 1135377 - Part 5: Support CharacterClassEscape in RegExp with unicode flag. r=till, f=anba (45a4712b0)
- #393, Bug 1135377 - Part 6: Support ignoreCase for BMP in RegExp with unicode flag. r=till, f=anba (cf744e9d3)
- #393, Bug 1135377 - Part 7: Support ignoreCase for non-BMP in RegExp with unicode flag. r=till, f=anba (f31a9f9e8)
- #393, Bug 1135377 - Part 8: Disallow extended pattern in RegExp with unicode flag. r=till, f=anba (c05db4075)
- #393, Bug 1135377 - Part 9: Use RegExp unicode flag in String.prototype.{match,replace,split}. r=till, f=anba (8636a96b7)
- #393, Bug 1135377 - Part 10: Decrement index when it points trail surrogate that has corresponding lead surrogate. r=till, f=anba (522d06ab3)
- #393, Bug 1135377 - Part 11: Support back reference with unicode flag. r=till, f=anba (122e41a28)
- #393, Bug 1279467 - Fix null handling in RegExp character class with unicode flag. r=till (741f170e6)
- #393: bustage fix (3e39760c8)
- #393, Bug 1281739 - Do not match K, S, k, s, KELVIN SIGN, LATIN SMALL LETTER LONG S with \W in unicode ignoreCase RegExp. r=till (3c5d4bab8)
- #375: M1233101 (a6a0b895f)
- #375, #391: branch hint malloc and recursion checks, clean up MAsm type barrier spooge (a008a1978)
- closes #380: fix toSource() tests (f9de6783b)
- #394: fix and enable ES7 exponentiation operator (6dc5f54dc)
- #396: M1342009, plus refactor to PLDHashTable a la M1352888 (06fcc091f)
- #375: M1347634 1352235 (aa6b13c59)
- #375: M1351303 (f7badd24e)
- #375: M1287277 M1259677; temporarily disable M1351303 due to crashes (75d95f15b)
- #375: M1363423 M1273828 M1364661 (e7767cdaa)
2018-05-31 14:43:16 +08:00

283 lines
9.9 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
// vim:cindent:ai:sw=4:ts=4:et:
/* 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/. */
/* implementation of CSS counters (for numbering things) */
#ifndef nsCounterManager_h_
#define nsCounterManager_h_
#include "mozilla/Attributes.h"
#include "nsGenConList.h"
#include "nsAutoPtr.h"
#include "nsClassHashtable.h"
#include "mozilla/Likely.h"
#include "CounterStyleManager.h"
class nsCounterList;
struct nsCounterUseNode;
struct nsCounterChangeNode;
struct nsCounterNode : public nsGenConNode {
enum Type {
RESET, // a "counter number" pair in 'counter-reset'
INCREMENT, // a "counter number" pair in 'counter-increment'
USE // counter() or counters() in 'content'
};
Type mType;
// Counter value after this node
int32_t mValueAfter;
// mScopeStart points to the node (usually a RESET, but not in the
// case of an implied 'counter-reset') that created the scope for
// this element (for a RESET, its outer scope, i.e., the one it is
// inside rather than the one it creates).
// May be null for all types, but only when mScopePrev is also null.
// Being null for a non-RESET means that it is an implied
// 'counter-reset'. Being null for a RESET means it has no outer
// scope.
nsCounterNode *mScopeStart;
// mScopePrev points to the previous node that is in the same scope,
// or for a RESET, the previous node in the scope outside of the
// reset.
// May be null for all types, but only when mScopeStart is also
// null. Following the mScopePrev links will eventually lead to
// mScopeStart. Being null for a non-RESET means that it is an
// implied 'counter-reset'. Being null for a RESET means it has no
// outer scope.
nsCounterNode *mScopePrev;
inline nsCounterUseNode* UseNode();
inline nsCounterChangeNode* ChangeNode();
// For RESET and INCREMENT nodes, aPseudoFrame need not be a
// pseudo-element, and aContentIndex represents the index within the
// 'counter-reset' or 'counter-increment' property instead of within
// the 'content' property but offset to ensure that (reset,
// increment, use) sort in that order. (This slight weirdness
// allows sharing a lot of code with 'quotes'.)
nsCounterNode(int32_t aContentIndex, Type aType)
: nsGenConNode(aContentIndex)
, mType(aType)
, mValueAfter(0)
, mScopeStart(nullptr)
, mScopePrev(nullptr)
{
}
// to avoid virtual function calls in the common case
inline void Calc(nsCounterList* aList);
};
struct nsCounterUseNode : public nsCounterNode {
// The same structure passed through the style system: an array
// containing the values in the counter() or counters() in the order
// given in the CSS spec.
RefPtr<nsCSSValue::Array> mCounterFunction;
nsPresContext* mPresContext;
RefPtr<mozilla::CounterStyle> mCounterStyle;
// false for counter(), true for counters()
bool mAllCounters;
// args go directly to member variables here and of nsGenConNode
nsCounterUseNode(nsPresContext* aPresContext,
nsCSSValue::Array* aCounterFunction,
uint32_t aContentIndex, bool aAllCounters)
: nsCounterNode(aContentIndex, USE)
, mCounterFunction(aCounterFunction)
, mPresContext(aPresContext)
, mCounterStyle(nullptr)
, mAllCounters(aAllCounters)
{
NS_ASSERTION(aContentIndex <= INT32_MAX, "out of range");
}
virtual bool InitTextFrame(nsGenConList* aList,
nsIFrame* aPseudoFrame, nsIFrame* aTextFrame) override;
mozilla::CounterStyle* GetCounterStyle();
void SetCounterStyleDirty()
{
mCounterStyle = nullptr;
}
// assign the correct |mValueAfter| value to a node that has been inserted
// Should be called immediately after calling |Insert|.
void Calc(nsCounterList* aList);
// The text that should be displayed for this counter.
void GetText(nsString& aResult);
};
struct nsCounterChangeNode : public nsCounterNode {
int32_t mChangeValue; // the numeric value of the increment or reset
// |aPseudoFrame| is not necessarily a pseudo-element's frame, but
// since it is for every other subclass of nsGenConNode, we follow
// the naming convention here.
// |aPropIndex| is the index of the value within the list in the
// 'counter-increment' or 'counter-reset' property.
nsCounterChangeNode(nsIFrame* aPseudoFrame,
nsCounterNode::Type aChangeType,
int32_t aChangeValue,
int32_t aPropIndex)
: nsCounterNode(// Fake a content index for resets and increments
// that comes before all the real content, with
// the resets first, in order, and then the increments.
aPropIndex + (aChangeType == RESET
? (INT32_MIN)
: (INT32_MIN / 2)),
aChangeType)
, mChangeValue(aChangeValue)
{
NS_ASSERTION(aPropIndex >= 0, "out of range");
NS_ASSERTION(aChangeType == INCREMENT || aChangeType == RESET,
"bad type");
mPseudoFrame = aPseudoFrame;
CheckFrameAssertions();
}
// assign the correct |mValueAfter| value to a node that has been inserted
// Should be called immediately after calling |Insert|.
void Calc(nsCounterList* aList);
};
inline nsCounterUseNode* nsCounterNode::UseNode()
{
NS_ASSERTION(mType == USE, "wrong type");
return static_cast<nsCounterUseNode*>(this);
}
inline nsCounterChangeNode* nsCounterNode::ChangeNode()
{
NS_ASSERTION(mType == INCREMENT || mType == RESET, "wrong type");
return static_cast<nsCounterChangeNode*>(this);
}
inline void nsCounterNode::Calc(nsCounterList* aList)
{
if (mType == USE)
UseNode()->Calc(aList);
else
ChangeNode()->Calc(aList);
}
class nsCounterList : public nsGenConList {
public:
nsCounterList() : nsGenConList(),
mDirty(false)
{}
void Insert(nsCounterNode* aNode) {
nsGenConList::Insert(aNode);
// Don't SetScope if we're dirty -- we'll reset all the scopes anyway,
// and we can't usefully compute scopes right now.
if (MOZ_LIKELY(!IsDirty())) {
SetScope(aNode);
}
}
nsCounterNode* First() {
return static_cast<nsCounterNode*>(mList.getFirst());
}
static nsCounterNode* Next(nsCounterNode* aNode) {
return static_cast<nsCounterNode*>(nsGenConList::Next(aNode));
}
static nsCounterNode* Prev(nsCounterNode* aNode) {
return static_cast<nsCounterNode*>(nsGenConList::Prev(aNode));
}
static int32_t ValueBefore(nsCounterNode* aNode) {
return aNode->mScopePrev ? aNode->mScopePrev->mValueAfter : 0;
}
// Correctly set |aNode->mScopeStart| and |aNode->mScopePrev|
void SetScope(nsCounterNode *aNode);
// Recalculate |mScopeStart|, |mScopePrev|, and |mValueAfter| for
// all nodes and update text in text content nodes.
void RecalcAll();
bool IsDirty() { return mDirty; }
void SetDirty() { mDirty = true; }
private:
bool mDirty;
};
/**
* The counter manager maintains an |nsCounterList| for each named
* counter to keep track of all scopes with that name.
*/
class nsCounterManager {
public:
nsCounterManager();
// Returns true if dirty
bool AddCounterResetsAndIncrements(nsIFrame *aFrame);
// Gets the appropriate counter list, creating it if necessary.
// Guaranteed to return non-null. (Uses an infallible hashtable API.)
nsCounterList* CounterListFor(const nsSubstring& aCounterName);
// Clean up data in any dirty counter lists.
void RecalcAll();
// Set all counter styles dirty
void SetAllCounterStylesDirty();
// Destroy nodes for the frame in any lists, and return whether any
// nodes were destroyed.
bool DestroyNodesFor(nsIFrame *aFrame);
// Clear all data.
void Clear() { mNames.Clear(); }
#ifdef DEBUG
void Dump();
#endif
static int32_t IncrementCounter(int32_t aOldValue, int32_t aIncrement)
{
// Addition of unsigned values is defined to be arithmetic
// modulo 2^bits (C++ 2011, 3.9.1 [basic.fundamental], clause 4);
// addition of signed values is undefined (and clang does
// something very strange if we use it here). Likewise integral
// conversion from signed to unsigned is also defined as modulo
// 2^bits (C++ 2011, 4.7 [conv.integral], clause 2); conversion
// from unsigned to signed is however undefined (ibid., clause 3),
// but to do what we want we must nonetheless depend on that
// small piece of undefined behavior.
int32_t newValue = int32_t(uint32_t(aOldValue) + uint32_t(aIncrement));
// The CSS Working Group resolved that a counter-increment that
// exceeds internal limits should not increment at all.
// http://lists.w3.org/Archives/Public/www-style/2013Feb/0392.html
// (This means, for example, that if aIncrement is 5, the
// counter will get stuck at the largest multiple of 5 less than
// the maximum 32-bit integer.)
if ((aIncrement > 0) != (newValue > aOldValue)) {
newValue = aOldValue;
}
return newValue;
}
private:
// for |AddCounterResetsAndIncrements| only
bool AddResetOrIncrement(nsIFrame *aFrame, int32_t aIndex,
const nsStyleCounterData *aCounterData,
nsCounterNode::Type aType);
nsClassHashtable<nsStringHashKey, nsCounterList> mNames;
};
#endif /* nsCounterManager_h_ */