1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/public/DirectionalityUtils.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,150 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef DirectionalityUtils_h___ 1.11 +#define DirectionalityUtils_h___ 1.12 + 1.13 +#include "nscore.h" 1.14 + 1.15 +class nsIContent; 1.16 +class nsIDocument; 1.17 +class nsINode; 1.18 +class nsAString; 1.19 +class nsAttrValue; 1.20 +class nsTextNode; 1.21 + 1.22 +namespace mozilla { 1.23 +namespace dom { 1.24 +class Element; 1.25 +} // namespace dom 1.26 +} // namespace mozilla 1.27 + 1.28 +namespace mozilla { 1.29 + 1.30 +enum Directionality { 1.31 + eDir_NotSet, 1.32 + eDir_RTL, 1.33 + eDir_LTR, 1.34 + eDir_Auto 1.35 +}; 1.36 + 1.37 +/** 1.38 + * Set the directionality of an element according to the algorithm defined at 1.39 + * http://www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#the-directionality, 1.40 + * not including elements with auto direction. 1.41 + * 1.42 + * @return the directionality that the element was set to 1.43 + */ 1.44 +Directionality RecomputeDirectionality(mozilla::dom::Element* aElement, 1.45 + bool aNotify = true); 1.46 + 1.47 +/** 1.48 + * Set the directionality of any descendants of a node that do not themselves 1.49 + * have a dir attribute. 1.50 + * For performance reasons we walk down the descendant tree in the rare case 1.51 + * of setting the dir attribute, rather than walking up the ancestor tree in 1.52 + * the much more common case of getting the element's directionality. 1.53 + */ 1.54 +void SetDirectionalityOnDescendants(mozilla::dom::Element* aElement, 1.55 + Directionality aDir, 1.56 + bool aNotify = true); 1.57 + 1.58 +/** 1.59 + * Walk the descendants of a node in tree order and, for any text node 1.60 + * descendant that determines the directionality of some element and is not a 1.61 + * descendant of another descendant of the original node with dir=auto, 1.62 + * redetermine that element's directionality 1.63 + */ 1.64 +void WalkDescendantsResetAutoDirection(mozilla::dom::Element* aElement); 1.65 + 1.66 +/** 1.67 + * After setting dir=auto on an element, walk its descendants in tree order. 1.68 + * If the node doesn't have the NODE_ANCESTOR_HAS_DIR_AUTO flag, set the 1.69 + * NODE_ANCESTOR_HAS_DIR_AUTO flag on all of its descendants. 1.70 + * Resolve the directionality of the element by the "downward propagation 1.71 + * algorithm" (defined in section 3 in the comments at the beginning of 1.72 + * DirectionalityUtils.cpp) 1.73 + */ 1.74 +void WalkDescendantsSetDirAuto(mozilla::dom::Element* aElement, 1.75 + bool aNotify = true); 1.76 + 1.77 +/** 1.78 + * After unsetting dir=auto on an element, walk its descendants in tree order, 1.79 + * skipping any that have dir=auto themselves, and unset the 1.80 + * NODE_ANCESTOR_HAS_DIR_AUTO flag 1.81 + */ 1.82 +void WalkDescendantsClearAncestorDirAuto(mozilla::dom::Element* aElement); 1.83 + 1.84 +/** 1.85 + * When the contents of a text node are about to change, retrieve the current 1.86 + * directionality of the text 1.87 + * 1.88 + * @return whether the text node affects the directionality of any element 1.89 + */ 1.90 +bool TextNodeWillChangeDirection(nsIContent* aTextNode, Directionality* aOldDir, 1.91 + uint32_t aOffset); 1.92 + 1.93 +/** 1.94 + * After the contents of a text node have changed, change the directionality 1.95 + * of any elements whose directionality is determined by that node 1.96 + */ 1.97 +void TextNodeChangedDirection(nsIContent* aTextNode, Directionality aOldDir, 1.98 + bool aNotify); 1.99 + 1.100 +/** 1.101 + * When a text node is appended to an element, find any ancestors with dir=auto 1.102 + * whose directionality will be determined by the text node 1.103 + */ 1.104 +void SetDirectionFromNewTextNode(nsIContent* aTextNode); 1.105 + 1.106 +/** 1.107 + * When a text node is removed from a document, find any ancestors whose 1.108 + * directionality it determined and redetermine their directionality 1.109 + * 1.110 + * @param aTextNode the text node 1.111 + * @param aNullParent whether the the parent is also being removed 1.112 + * (passed from UnbindFromTree) 1.113 + */ 1.114 +void ResetDirectionSetByTextNode(nsTextNode* aTextNode, bool aNullParent); 1.115 + 1.116 +/** 1.117 + * Set the directionality of an element according to the directionality of the 1.118 + * text in aValue 1.119 + */ 1.120 +void SetDirectionalityFromValue(mozilla::dom::Element* aElement, 1.121 + const nsAString& aValue, 1.122 + bool aNotify); 1.123 + 1.124 +/** 1.125 + * Called when setting the dir attribute on an element, immediately after 1.126 + * AfterSetAttr. This is instead of using BeforeSetAttr or AfterSetAttr, because 1.127 + * in AfterSetAttr we don't know the old value, so we can't identify all cases 1.128 + * where we need to walk up or down the document tree and reset the direction; 1.129 + * and in BeforeSetAttr we can't do the walk because this element hasn't had the 1.130 + * value set yet so the results will be wrong. 1.131 + */ 1.132 +void OnSetDirAttr(mozilla::dom::Element* aElement, 1.133 + const nsAttrValue* aNewValue, 1.134 + bool hadValidDir, 1.135 + bool hadDirAuto, 1.136 + bool aNotify); 1.137 + 1.138 +/** 1.139 + * Called when binding a new element to the tree, to set the 1.140 + * NodeAncestorHasDirAuto flag and set the direction of the element and its 1.141 + * ancestors if necessary 1.142 + */ 1.143 +void SetDirOnBind(mozilla::dom::Element* aElement, nsIContent* aParent); 1.144 + 1.145 +/** 1.146 + * Called when unbinding an element from the tree, to recompute the 1.147 + * directionality of the element if it doesn't have autodirection, and to 1.148 + * clean up any entries in nsTextDirectionalityMap that refer to it. 1.149 + */ 1.150 +void ResetDir(mozilla::dom::Element* aElement); 1.151 +} // end namespace mozilla 1.152 + 1.153 +#endif /* DirectionalityUtils_h___ */