1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef DirectionalityUtils_h___
8 #define DirectionalityUtils_h___
11 #include "nsStringFwd.h"
18 namespace mozilla::dom
{
20 class HTMLSlotElement
;
22 } // namespace mozilla::dom
26 enum class Directionality
: uint8_t { Unset
, Rtl
, Ltr
, Auto
};
29 * Various methods for returning the directionality of a string using the
30 * first-strong algorithm defined in http://unicode.org/reports/tr9/#P2
32 * @param[out] aFirstStrong the offset to the first character in the string with
33 * strong directionality, or UINT32_MAX if there is none (in which
34 * case the return value is Directionality::Unset).
35 * @return the directionality of the string, or Unset if not available.
37 Directionality
GetDirectionFromText(const char16_t
* aText
,
38 const uint32_t aLength
,
39 uint32_t* aFirstStrong
= nullptr);
42 * Set the directionality of an element according to the algorithm defined at
43 * https://html.spec.whatwg.org/#the-directionality, not including elements with
46 * @return the directionality that the element was set to
48 Directionality
RecomputeDirectionality(mozilla::dom::Element
* aElement
,
52 * Conceptually https://html.spec.whatwg.org/#parent-directionality, but a bit
53 * different in how we deal with shadow DOM.
55 * FIXME(bug 1857719): Update directionality to the latest version of the spec.
57 Directionality
GetParentDirectionality(const mozilla::dom::Element
* aElement
);
60 * Set the directionality of any descendants of a node that do not themselves
61 * have a dir attribute.
62 * For performance reasons we walk down the descendant tree in the rare case
63 * of setting the dir attribute, rather than walking up the ancestor tree in
64 * the much more common case of getting the element's directionality.
66 void SetDirectionalityOnDescendants(mozilla::dom::Element
* aElement
,
67 Directionality aDir
, bool aNotify
= true);
70 * Walk the descendants of a node in tree order and, for any text node
71 * descendant that determines the directionality of some element and is not a
72 * descendant of another descendant of the original node with dir=auto,
73 * redetermine that element's directionality
75 void WalkDescendantsResetAutoDirection(mozilla::dom::Element
* aElement
);
78 * In case a slot element was added or removed it may change the directionality
79 * of ancestors or assigned nodes.
81 * aAllAssignedNodesChanged forces the computation of the state for all of the
82 * assigned descendants.
84 void SlotStateChanged(dom::HTMLSlotElement
* aSlot
,
85 bool aAllAssignedNodesChanged
= true);
88 * When only a single node in a slot is reassigned we can save some work
89 * compared to the above.
91 void SlotAssignedNodeChanged(dom::HTMLSlotElement
* aSlot
,
92 nsIContent
& aAssignedNode
);
95 * After setting dir=auto on an element, walk its descendants in tree order.
96 * If the node doesn't have the NODE_ANCESTOR_HAS_DIR_AUTO flag, set the
97 * NODE_ANCESTOR_HAS_DIR_AUTO flag on all of its descendants.
98 * Resolve the directionality of the element by the "downward propagation
99 * algorithm" (defined in section 3 in the comments at the beginning of
100 * DirectionalityUtils.cpp)
102 void WalkDescendantsSetDirAuto(mozilla::dom::Element
* aElement
,
103 bool aNotify
= true);
106 * After unsetting dir=auto on an element, walk its descendants in tree order,
107 * skipping any that have dir=auto themselves, and unset the
108 * NODE_ANCESTOR_HAS_DIR_AUTO flag
110 void WalkDescendantsClearAncestorDirAuto(nsIContent
* aContent
);
113 * When the contents of a text node are about to change, retrieve the current
114 * directionality of the text
116 * @return whether the text node affects the directionality of any element
118 bool TextNodeWillChangeDirection(nsTextNode
* aTextNode
, Directionality
* aOldDir
,
122 * After the contents of a text node have changed, change the directionality
123 * of any elements whose directionality is determined by that node
125 void TextNodeChangedDirection(nsTextNode
* aTextNode
, Directionality aOldDir
,
129 * When a text node is appended to an element, find any ancestors with dir=auto
130 * whose directionality will be determined by the text node
132 void SetDirectionFromNewTextNode(nsTextNode
* aTextNode
);
135 * When a text node is removed from a document, find any ancestors whose
136 * directionality it determined and redetermine their directionality
138 void ResetDirectionSetByTextNode(nsTextNode
*, dom::UnbindContext
&);
141 * Set the directionality of an element according to the directionality of the
144 void SetDirectionalityFromValue(mozilla::dom::Element
* aElement
,
145 const nsAString
& aValue
, bool aNotify
);
148 * Called when setting the dir attribute on an element, immediately after
149 * AfterSetAttr. This is instead of using BeforeSetAttr or AfterSetAttr, because
150 * in AfterSetAttr we don't know the old value, so we can't identify all cases
151 * where we need to walk up or down the document tree and reset the direction;
152 * and in BeforeSetAttr we can't do the walk because this element hasn't had the
153 * value set yet so the results will be wrong.
155 void OnSetDirAttr(mozilla::dom::Element
* aElement
, const nsAttrValue
* aNewValue
,
156 bool hadValidDir
, bool hadDirAuto
, bool aNotify
);
159 * Called when binding a new element to the tree, to set the
160 * NodeAncestorHasDirAuto flag and set the direction of the element and its
161 * ancestors if necessary
163 void SetDirOnBind(mozilla::dom::Element
* aElement
, nsIContent
* aParent
);
166 * Called when unbinding an element from the tree, to recompute the
167 * directionality of the element if it doesn't have autodirection, and to
168 * clean up any entries in nsTextDirectionalityMap that refer to it.
170 void ResetDir(mozilla::dom::Element
* aElement
);
171 } // end namespace mozilla
173 #endif /* DirectionalityUtils_h___ */