|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
2 * |
|
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/. */ |
|
6 |
|
7 #ifndef DirectionalityUtils_h___ |
|
8 #define DirectionalityUtils_h___ |
|
9 |
|
10 #include "nscore.h" |
|
11 |
|
12 class nsIContent; |
|
13 class nsIDocument; |
|
14 class nsINode; |
|
15 class nsAString; |
|
16 class nsAttrValue; |
|
17 class nsTextNode; |
|
18 |
|
19 namespace mozilla { |
|
20 namespace dom { |
|
21 class Element; |
|
22 } // namespace dom |
|
23 } // namespace mozilla |
|
24 |
|
25 namespace mozilla { |
|
26 |
|
27 enum Directionality { |
|
28 eDir_NotSet, |
|
29 eDir_RTL, |
|
30 eDir_LTR, |
|
31 eDir_Auto |
|
32 }; |
|
33 |
|
34 /** |
|
35 * Set the directionality of an element according to the algorithm defined at |
|
36 * http://www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#the-directionality, |
|
37 * not including elements with auto direction. |
|
38 * |
|
39 * @return the directionality that the element was set to |
|
40 */ |
|
41 Directionality RecomputeDirectionality(mozilla::dom::Element* aElement, |
|
42 bool aNotify = true); |
|
43 |
|
44 /** |
|
45 * Set the directionality of any descendants of a node that do not themselves |
|
46 * have a dir attribute. |
|
47 * For performance reasons we walk down the descendant tree in the rare case |
|
48 * of setting the dir attribute, rather than walking up the ancestor tree in |
|
49 * the much more common case of getting the element's directionality. |
|
50 */ |
|
51 void SetDirectionalityOnDescendants(mozilla::dom::Element* aElement, |
|
52 Directionality aDir, |
|
53 bool aNotify = true); |
|
54 |
|
55 /** |
|
56 * Walk the descendants of a node in tree order and, for any text node |
|
57 * descendant that determines the directionality of some element and is not a |
|
58 * descendant of another descendant of the original node with dir=auto, |
|
59 * redetermine that element's directionality |
|
60 */ |
|
61 void WalkDescendantsResetAutoDirection(mozilla::dom::Element* aElement); |
|
62 |
|
63 /** |
|
64 * After setting dir=auto on an element, walk its descendants in tree order. |
|
65 * If the node doesn't have the NODE_ANCESTOR_HAS_DIR_AUTO flag, set the |
|
66 * NODE_ANCESTOR_HAS_DIR_AUTO flag on all of its descendants. |
|
67 * Resolve the directionality of the element by the "downward propagation |
|
68 * algorithm" (defined in section 3 in the comments at the beginning of |
|
69 * DirectionalityUtils.cpp) |
|
70 */ |
|
71 void WalkDescendantsSetDirAuto(mozilla::dom::Element* aElement, |
|
72 bool aNotify = true); |
|
73 |
|
74 /** |
|
75 * After unsetting dir=auto on an element, walk its descendants in tree order, |
|
76 * skipping any that have dir=auto themselves, and unset the |
|
77 * NODE_ANCESTOR_HAS_DIR_AUTO flag |
|
78 */ |
|
79 void WalkDescendantsClearAncestorDirAuto(mozilla::dom::Element* aElement); |
|
80 |
|
81 /** |
|
82 * When the contents of a text node are about to change, retrieve the current |
|
83 * directionality of the text |
|
84 * |
|
85 * @return whether the text node affects the directionality of any element |
|
86 */ |
|
87 bool TextNodeWillChangeDirection(nsIContent* aTextNode, Directionality* aOldDir, |
|
88 uint32_t aOffset); |
|
89 |
|
90 /** |
|
91 * After the contents of a text node have changed, change the directionality |
|
92 * of any elements whose directionality is determined by that node |
|
93 */ |
|
94 void TextNodeChangedDirection(nsIContent* aTextNode, Directionality aOldDir, |
|
95 bool aNotify); |
|
96 |
|
97 /** |
|
98 * When a text node is appended to an element, find any ancestors with dir=auto |
|
99 * whose directionality will be determined by the text node |
|
100 */ |
|
101 void SetDirectionFromNewTextNode(nsIContent* aTextNode); |
|
102 |
|
103 /** |
|
104 * When a text node is removed from a document, find any ancestors whose |
|
105 * directionality it determined and redetermine their directionality |
|
106 * |
|
107 * @param aTextNode the text node |
|
108 * @param aNullParent whether the the parent is also being removed |
|
109 * (passed from UnbindFromTree) |
|
110 */ |
|
111 void ResetDirectionSetByTextNode(nsTextNode* aTextNode, bool aNullParent); |
|
112 |
|
113 /** |
|
114 * Set the directionality of an element according to the directionality of the |
|
115 * text in aValue |
|
116 */ |
|
117 void SetDirectionalityFromValue(mozilla::dom::Element* aElement, |
|
118 const nsAString& aValue, |
|
119 bool aNotify); |
|
120 |
|
121 /** |
|
122 * Called when setting the dir attribute on an element, immediately after |
|
123 * AfterSetAttr. This is instead of using BeforeSetAttr or AfterSetAttr, because |
|
124 * in AfterSetAttr we don't know the old value, so we can't identify all cases |
|
125 * where we need to walk up or down the document tree and reset the direction; |
|
126 * and in BeforeSetAttr we can't do the walk because this element hasn't had the |
|
127 * value set yet so the results will be wrong. |
|
128 */ |
|
129 void OnSetDirAttr(mozilla::dom::Element* aElement, |
|
130 const nsAttrValue* aNewValue, |
|
131 bool hadValidDir, |
|
132 bool hadDirAuto, |
|
133 bool aNotify); |
|
134 |
|
135 /** |
|
136 * Called when binding a new element to the tree, to set the |
|
137 * NodeAncestorHasDirAuto flag and set the direction of the element and its |
|
138 * ancestors if necessary |
|
139 */ |
|
140 void SetDirOnBind(mozilla::dom::Element* aElement, nsIContent* aParent); |
|
141 |
|
142 /** |
|
143 * Called when unbinding an element from the tree, to recompute the |
|
144 * directionality of the element if it doesn't have autodirection, and to |
|
145 * clean up any entries in nsTextDirectionalityMap that refer to it. |
|
146 */ |
|
147 void ResetDir(mozilla::dom::Element* aElement); |
|
148 } // end namespace mozilla |
|
149 |
|
150 #endif /* DirectionalityUtils_h___ */ |