michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0: /* vim: set ts=2 sw=2 et tw=78: */
michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0:
michael@0: /*
michael@0: Implementation description from https://etherpad.mozilla.org/dir-auto
michael@0:
michael@0: Static case
michael@0: ===========
michael@0: When we see a new content node with @dir=auto from the parser, we set the
michael@0: NodeHasDirAuto flag on the node. We won't have enough information to
michael@0: decide the directionality of the node at this point.
michael@0:
michael@0: When we bind a new content node to the document, if its parent has either of
michael@0: the NodeAncestorHasDirAuto or NodeHasDirAuto flags, we set the
michael@0: NodeAncestorHasDirAuto flag on the node.
michael@0:
michael@0: When a new input with @type=text/search/tel/url/email and @dir=auto is added
michael@0: from the parser, we resolve the directionality based on its @value.
michael@0:
michael@0: When a new text node with non-neutral content is appended to a textarea
michael@0: element with NodeHasDirAuto, if the directionality of the textarea element
michael@0: is still unresolved, it is resolved based on the value of the text node.
michael@0: Elements with unresolved directionality behave as LTR.
michael@0:
michael@0: When a new text node with non-neutral content is appended to an element that
michael@0: is not a textarea but has either of the NodeAncestorHasDirAuto or
michael@0: NodeHasDirAuto flags, we walk up the parent chain while the
michael@0: NodeAncestorHasDirAuto flag is present, and when we reach an element with
michael@0: NodeHasDirAuto and no resolved directionality, we resolve the directionality
michael@0: based on the contents of the text node and cease walking the parent chain.
michael@0: Note that we should ignore elements with NodeHasDirAuto with resolved
michael@0: directionality, so that the second text node in this example tree doesn't
michael@0: affect the directionality of the div:
michael@0:
michael@0:
michael@0: foo
michael@0: بار
michael@0:
michael@0:
michael@0: The parent chain walk will be aborted if we hit a script or style element, or
michael@0: if we hit an element with @dir=ltr or @dir=rtl.
michael@0:
michael@0: I will call this algorithm "upward propagation".
michael@0:
michael@0: Each text node should maintain a list of elements which have their
michael@0: directionality determined by the first strong character of that text node.
michael@0: This is useful to make dynamic changes more efficient. One way to implement
michael@0: this is to have a per-document hash table mapping a text node to a set of
michael@0: elements. I'll call this data structure TextNodeDirectionalityMap. The
michael@0: algorithm for appending a new text node above needs to update this data
michael@0: structure.
michael@0:
michael@0: *IMPLEMENTATION NOTE*
michael@0: In practice, the implementation uses two per-node properties:
michael@0:
michael@0: dirAutoSetBy, which is set on a node with auto-directionality, and points to
michael@0: the textnode that contains the strong character which determines the
michael@0: directionality of the node.
michael@0:
michael@0: textNodeDirectionalityMap, which is set on a text node and points to a hash
michael@0: table listing the nodes whose directionality is determined by the text node.
michael@0:
michael@0: Handling dynamic changes
michael@0: ========================
michael@0:
michael@0: We need to handle the following cases:
michael@0:
michael@0: 1. When the value of an input element with @type=text/search/tel/url/email is
michael@0: changed, if it has NodeHasDirAuto, we update the resolved directionality.
michael@0:
michael@0: 2. When the dir attribute is changed from something else (including the case
michael@0: where it doesn't exist) to auto on a textarea or an input element with
michael@0: @type=text/search/tel/url/email, we set the NodeHasDirAuto flag and resolve
michael@0: the directionality based on the value of the element.
michael@0:
michael@0: 3. When the dir attribute is changed from something else (including the case
michael@0: where it doesn't exist) to auto on any element except case 1 above and the bdi
michael@0: element, we run the following algorithm:
michael@0: * We set the NodeHasDirAuto flag.
michael@0: * If the element doesn't have the NodeAncestorHasDirAuto flag, we set the
michael@0: NodeAncestorHasDirAuto flag on all of its child nodes. (Note that if the
michael@0: element does have NodeAncestorHasDirAuto, all of its children should
michael@0: already have this flag too. We can assert this in debug builds.)
michael@0: * To resolve the directionality of the element, we run the algorithm explained
michael@0: in http://www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#the-dir-attribute
michael@0: (I'll call this the "downward propagation algorithm".) by walking the child
michael@0: subtree in tree order. Note that an element with @dir=auto should not affect
michael@0: other elements in its document with @dir=auto. So there is no need to walk up
michael@0: the parent chain in this case. TextNodeDirectionalityMap needs to be updated
michael@0: as appropriate.
michael@0:
michael@0: 3a. When the dir attribute is set to any valid value on an element that didn't
michael@0: have a valid dir attribute before, this means that any descendant of that
michael@0: element will not affect the directionality of any of its ancestors. So we need
michael@0: to check whether any text node descendants of the element are listed in
michael@0: TextNodeDirectionalityMap, and whether the elements whose direction they set
michael@0: are ancestors of the element. If so, we need to rerun the downward propagation
michael@0: algorithm for those ancestors.
michael@0:
michael@0: 4. When the dir attribute is changed from auto to something else (including
michael@0: the case where it gets removed) on a textarea or an input element with
michael@0: @type=text/search/tel/url/email, we unset the NodeHasDirAuto flag and
michael@0: resolve the directionality based on the directionality of the value of the @dir
michael@0: attribute on element itself or its parent element.
michael@0:
michael@0: 5. When the dir attribute is changed from auto to something else (including the
michael@0: case where it gets removed) on any element except case 4 above and the bdi
michael@0: element, we run the following algorithm:
michael@0: * We unset the NodeHasDirAuto flag.
michael@0: * If the element does not have the NodeAncestorHasDirAuto flag, we unset
michael@0: the NodeAncestorHasDirAuto flag on all of its child nodes, except those
michael@0: who are a descendant of another element with NodeHasDirAuto. (Note that if
michael@0: the element has the NodeAncestorHasDirAuto flag, all of its child nodes
michael@0: should still retain the same flag.)
michael@0: * We resolve the directionality of the element based on the value of the @dir
michael@0: attribute on the element itself or its parent element.
michael@0: TextNodeDirectionalityMap needs to be updated as appropriate.
michael@0:
michael@0: 5a. When the dir attribute is removed or set to an invalid value on any
michael@0: element (except a bdi element) with the NodeAncestorHasDirAuto flag which
michael@0: previously had a valid dir attribute, it might have a text node descendant that
michael@0: did not previously affect the directionality of any of its ancestors but should
michael@0: now begin to affect them.
michael@0: We run the following algorithm:
michael@0: * Walk up the parent chain from the element.
michael@0: * For any element that appears in the TextNodeDirectionalityMap, remove the
michael@0: element from the map and rerun the downward propagation algorithm
michael@0: (see section 3).
michael@0: * If we reach an element without either of the NodeHasDirAuto or
michael@0: NodeAncestorHasDirAuto flags, abort the parent chain walk.
michael@0:
michael@0: 6. When an element with @dir=auto is added to the document, we should handle it
michael@0: similar to the case 2/3 above.
michael@0:
michael@0: 7. When an element with NodeHasDirAuto or NodeAncestorHasDirAuto is
michael@0: removed from the document, we should handle it similar to the case 4/5 above,
michael@0: except that we don't need to handle anything in the child subtree. We should
michael@0: also remove all of the occurrences of that node and its descendants from
michael@0: TextNodeDirectionalityMap. (This is the conceptual description of what needs to
michael@0: happen but in the implementation UnbindFromTree is going to be called on all of
michael@0: the descendants so we don't need to descend into the child subtree).
michael@0:
michael@0: 8. When the contents of a text node is changed either from script or by the
michael@0: user, we need to run the following algorithm:
michael@0: * If the change has happened after the first character with strong
michael@0: directionality in the text node, do nothing.
michael@0: * If the text node is a child of a bdi, script or style element, do nothing.
michael@0: * If the text node belongs to a textarea with NodeHasDirAuto, we need to
michael@0: update the directionality of the textarea.
michael@0: * Grab a list of elements affected by this text node from
michael@0: TextNodeDirectionalityMap and re-resolve the directionality of each one of them
michael@0: based on the new contents of the text node.
michael@0: * If the text node does not exist in TextNodeDirectionalityMap, and it has the
michael@0: NodeAncestorHasDirAuto flag set, this could potentially be a text node
michael@0: which is going to start affecting the directionality of its parent @dir=auto
michael@0: elements. In this case, we need to fall back to the (potentially expensive)
michael@0: "upward propagation algorithm". The TextNodeDirectionalityMap data structure
michael@0: needs to be update during this algorithm.
michael@0: * If the new contents of the text node do not have any strong characters, and
michael@0: the old contents used to, and the text node used to exist in
michael@0: TextNodeDirectionalityMap and it has the NodeAncestorHasDirAuto flag set,
michael@0: the elements associated with this text node inside TextNodeDirectionalityMap
michael@0: will now get their directionality from another text node. In this case, for
michael@0: each element in the list retrieved from TextNodeDirectionalityMap, run the
michael@0: downward propagation algorithm (section 3), and remove the text node from
michael@0: TextNodeDirectionalityMap.
michael@0:
michael@0: 9. When a new text node is injected into a document, we need to run the
michael@0: following algorithm:
michael@0: * If the contents of the text node do not have any characters with strong
michael@0: direction, do nothing.
michael@0: * If the text node is a child of a bdi, script or style element, do nothing.
michael@0: * If the text node is appended to a textarea element with NodeHasDirAuto, we
michael@0: need to update the directionality of the textarea.
michael@0: * If the text node has NodeAncestorHasDirAuto, we need to run the "upward
michael@0: propagation algorithm". The TextNodeDirectionalityMap data structure needs to
michael@0: be update during this algorithm.
michael@0:
michael@0: 10. When a text node is removed from a document, we need to run the following
michael@0: algorithm:
michael@0: * If the contents of the text node do not have any characters with strong
michael@0: direction, do nothing.
michael@0: * If the text node is a child of a bdi, script or style element, do nothing.
michael@0: * If the text node is removed from a textarea element with NodeHasDirAuto,
michael@0: set the directionality to "ltr". (This is what the spec currently says, but I'm
michael@0: filing a spec bug to get it fixed -- the directionality should depend on the
michael@0: parent element here.)
michael@0: * If the text node has NodeAncestorHasDirAuto, we need to look at the list
michael@0: of elements being affected by this text node from TextNodeDirectionalityMap,
michael@0: run the "downward propagation algorithm" (section 3) for each one of them,
michael@0: while updating TextNodeDirectionalityMap along the way.
michael@0:
michael@0: 11. If the value of the @dir attribute on a bdi element is changed to an
michael@0: invalid value (or if it's removed), determine the new directionality similar
michael@0: to the case 3 above.
michael@0:
michael@0: == Implemention Notes ==
michael@0: When a new node gets bound to the tree, the BindToTree function gets called.
michael@0: The reverse case is UnbindFromTree.
michael@0: When the contents of a text node change, nsGenericDOMDataNode::SetTextInternal
michael@0: gets called.
michael@0: */
michael@0:
michael@0: #include "mozilla/dom/DirectionalityUtils.h"
michael@0:
michael@0: #include "nsINode.h"
michael@0: #include "nsIContent.h"
michael@0: #include "nsIDocument.h"
michael@0: #include "mozilla/DebugOnly.h"
michael@0: #include "mozilla/dom/Element.h"
michael@0: #include "nsIDOMHTMLDocument.h"
michael@0: #include "nsUnicodeProperties.h"
michael@0: #include "nsTextFragment.h"
michael@0: #include "nsAttrValue.h"
michael@0: #include "nsTextNode.h"
michael@0: #include "nsCheapSets.h"
michael@0:
michael@0: namespace mozilla {
michael@0:
michael@0: using mozilla::dom::Element;
michael@0:
michael@0: /**
michael@0: * Returns true if aElement is one of the elements whose text content should not
michael@0: * affect its own direction, nor the direction of ancestors with dir=auto.
michael@0: *
michael@0: * Note that this does not include , whose content does affect its own
michael@0: * direction when it has dir=auto (which it has by default), so one needs to
michael@0: * test for it separately, e.g. with DoesNotAffectDirectionOfAncestors.
michael@0: * It *does* include textarea, because even if a textarea has dir=auto, it has
michael@0: * unicode-bidi: plaintext and is handled automatically in bidi resolution.
michael@0: */
michael@0: static bool
michael@0: DoesNotParticipateInAutoDirection(const Element* aElement)
michael@0: {
michael@0: nsINodeInfo* nodeInfo = aElement->NodeInfo();
michael@0: return (!aElement->IsHTML() ||
michael@0: nodeInfo->Equals(nsGkAtoms::script) ||
michael@0: nodeInfo->Equals(nsGkAtoms::style) ||
michael@0: nodeInfo->Equals(nsGkAtoms::textarea) ||
michael@0: aElement->IsInAnonymousSubtree());
michael@0: }
michael@0:
michael@0: static inline bool
michael@0: IsBdiWithoutDirAuto(const Element* aElement)
michael@0: {
michael@0: // We are testing for bdi elements without explicit dir="auto", so we can't
michael@0: // use the HasDirAuto() flag, since that will return true for bdi element with
michael@0: // no dir attribute or an invalid dir attribute
michael@0: return (aElement->IsHTML(nsGkAtoms::bdi) &&
michael@0: (!aElement->HasValidDir() || aElement->HasFixedDir()));
michael@0: }
michael@0:
michael@0: /**
michael@0: * Returns true if aElement is one of the element whose text content should not
michael@0: * affect the direction of ancestors with dir=auto (though it may affect its own
michael@0: * direction, e.g. )
michael@0: */
michael@0: static bool
michael@0: DoesNotAffectDirectionOfAncestors(const Element* aElement)
michael@0: {
michael@0: return (DoesNotParticipateInAutoDirection(aElement) ||
michael@0: IsBdiWithoutDirAuto(aElement) ||
michael@0: aElement->HasFixedDir());
michael@0: }
michael@0:
michael@0: /**
michael@0: * Returns the directionality of a Unicode character
michael@0: */
michael@0: static Directionality
michael@0: GetDirectionFromChar(uint32_t ch)
michael@0: {
michael@0: switch(mozilla::unicode::GetBidiCat(ch)) {
michael@0: case eCharType_RightToLeft:
michael@0: case eCharType_RightToLeftArabic:
michael@0: return eDir_RTL;
michael@0:
michael@0: case eCharType_LeftToRight:
michael@0: return eDir_LTR;
michael@0:
michael@0: default:
michael@0: return eDir_NotSet;
michael@0: }
michael@0: }
michael@0:
michael@0: inline static bool NodeAffectsDirAutoAncestor(nsINode* aTextNode)
michael@0: {
michael@0: Element* parent = aTextNode->GetParentElement();
michael@0: return (parent &&
michael@0: !DoesNotParticipateInAutoDirection(parent) &&
michael@0: parent->NodeOrAncestorHasDirAuto());
michael@0: }
michael@0:
michael@0: /**
michael@0: * Various methods for returning the directionality of a string using the
michael@0: * first-strong algorithm defined in http://unicode.org/reports/tr9/#P2
michael@0: *
michael@0: * @param[out] aFirstStrong the offset to the first character in the string with
michael@0: * strong directionality, or UINT32_MAX if there is none (return
michael@0: value is eDir_NotSet).
michael@0: * @return the directionality of the string
michael@0: */
michael@0: static Directionality
michael@0: GetDirectionFromText(const char16_t* aText, const uint32_t aLength,
michael@0: uint32_t* aFirstStrong = nullptr)
michael@0: {
michael@0: const char16_t* start = aText;
michael@0: const char16_t* end = aText + aLength;
michael@0:
michael@0: while (start < end) {
michael@0: uint32_t current = start - aText;
michael@0: uint32_t ch = *start++;
michael@0:
michael@0: if (NS_IS_HIGH_SURROGATE(ch) &&
michael@0: start < end &&
michael@0: NS_IS_LOW_SURROGATE(*start)) {
michael@0: ch = SURROGATE_TO_UCS4(ch, *start++);
michael@0: current++;
michael@0: }
michael@0:
michael@0: // Just ignore lone surrogates
michael@0: if (!IS_SURROGATE(ch)) {
michael@0: Directionality dir = GetDirectionFromChar(ch);
michael@0: if (dir != eDir_NotSet) {
michael@0: if (aFirstStrong) {
michael@0: *aFirstStrong = current;
michael@0: }
michael@0: return dir;
michael@0: }
michael@0: }
michael@0: }
michael@0:
michael@0: if (aFirstStrong) {
michael@0: *aFirstStrong = UINT32_MAX;
michael@0: }
michael@0: return eDir_NotSet;
michael@0: }
michael@0:
michael@0: static Directionality
michael@0: GetDirectionFromText(const char* aText, const uint32_t aLength,
michael@0: uint32_t* aFirstStrong = nullptr)
michael@0: {
michael@0: const char* start = aText;
michael@0: const char* end = aText + aLength;
michael@0:
michael@0: while (start < end) {
michael@0: uint32_t current = start - aText;
michael@0: unsigned char ch = (unsigned char)*start++;
michael@0:
michael@0: Directionality dir = GetDirectionFromChar(ch);
michael@0: if (dir != eDir_NotSet) {
michael@0: if (aFirstStrong) {
michael@0: *aFirstStrong = current;
michael@0: }
michael@0: return dir;
michael@0: }
michael@0: }
michael@0:
michael@0: if (aFirstStrong) {
michael@0: *aFirstStrong = UINT32_MAX;
michael@0: }
michael@0: return eDir_NotSet;
michael@0: }
michael@0:
michael@0: static Directionality
michael@0: GetDirectionFromText(const nsTextFragment* aFrag,
michael@0: uint32_t* aFirstStrong = nullptr)
michael@0: {
michael@0: if (aFrag->Is2b()) {
michael@0: return GetDirectionFromText(aFrag->Get2b(), aFrag->GetLength(),
michael@0: aFirstStrong);
michael@0: }
michael@0:
michael@0: return GetDirectionFromText(aFrag->Get1b(), aFrag->GetLength(),
michael@0: aFirstStrong);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Set the directionality of a node with dir=auto as defined in
michael@0: * http://www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#the-directionality
michael@0: *
michael@0: * @param[in] changedNode If we call this method because the content of a text
michael@0: * node is about to change, pass in the changed node, so that we
michael@0: * know not to return it
michael@0: * @return the text node containing the character that determined the direction
michael@0: */
michael@0: static nsINode*
michael@0: WalkDescendantsSetDirectionFromText(Element* aElement, bool aNotify = true,
michael@0: nsINode* aChangedNode = nullptr)
michael@0: {
michael@0: MOZ_ASSERT(aElement, "Must have an element");
michael@0: MOZ_ASSERT(aElement->HasDirAuto(), "Element must have dir=auto");
michael@0:
michael@0: if (DoesNotParticipateInAutoDirection(aElement)) {
michael@0: return nullptr;
michael@0: }
michael@0:
michael@0: nsIContent* child = aElement->GetFirstChild();
michael@0: while (child) {
michael@0: if (child->IsElement() &&
michael@0: DoesNotAffectDirectionOfAncestors(child->AsElement())) {
michael@0: child = child->GetNextNonChildNode(aElement);
michael@0: continue;
michael@0: }
michael@0:
michael@0: if (child->NodeType() == nsIDOMNode::TEXT_NODE &&
michael@0: child != aChangedNode) {
michael@0: Directionality textNodeDir = GetDirectionFromText(child->GetText());
michael@0: if (textNodeDir != eDir_NotSet) {
michael@0: // We found a descendant text node with strong directional characters.
michael@0: // Set the directionality of aElement to the corresponding value.
michael@0: aElement->SetDirectionality(textNodeDir, aNotify);
michael@0: return child;
michael@0: }
michael@0: }
michael@0: child = child->GetNextNode(aElement);
michael@0: }
michael@0:
michael@0: // We walked all the descendants without finding a text node with strong
michael@0: // directional characters. Set the directionality to LTR
michael@0: aElement->SetDirectionality(eDir_LTR, aNotify);
michael@0: return nullptr;
michael@0: }
michael@0:
michael@0: class nsTextNodeDirectionalityMap
michael@0: {
michael@0: static void
michael@0: nsTextNodeDirectionalityMapDtor(void *aObject, nsIAtom* aPropertyName,
michael@0: void *aPropertyValue, void* aData)
michael@0: {
michael@0: nsINode* textNode = static_cast(aObject);
michael@0: textNode->ClearHasTextNodeDirectionalityMap();
michael@0:
michael@0: nsTextNodeDirectionalityMap* map =
michael@0: reinterpret_cast(aPropertyValue);
michael@0: map->EnsureMapIsClear(textNode);
michael@0: delete map;
michael@0: }
michael@0:
michael@0: public:
michael@0: nsTextNodeDirectionalityMap(nsINode* aTextNode)
michael@0: {
michael@0: MOZ_ASSERT(aTextNode, "Null text node");
michael@0: MOZ_COUNT_CTOR(nsTextNodeDirectionalityMap);
michael@0: aTextNode->SetProperty(nsGkAtoms::textNodeDirectionalityMap, this,
michael@0: nsTextNodeDirectionalityMapDtor);
michael@0: aTextNode->SetHasTextNodeDirectionalityMap();
michael@0: }
michael@0:
michael@0: ~nsTextNodeDirectionalityMap()
michael@0: {
michael@0: MOZ_COUNT_DTOR(nsTextNodeDirectionalityMap);
michael@0: }
michael@0:
michael@0: void AddEntry(nsINode* aTextNode, Element* aElement)
michael@0: {
michael@0: if (!mElements.Contains(aElement)) {
michael@0: mElements.Put(aElement);
michael@0: aElement->SetProperty(nsGkAtoms::dirAutoSetBy, aTextNode);
michael@0: aElement->SetHasDirAutoSet();
michael@0: }
michael@0: }
michael@0:
michael@0: void RemoveEntry(nsINode* aTextNode, Element* aElement)
michael@0: {
michael@0: NS_ASSERTION(mElements.Contains(aElement),
michael@0: "element already removed from map");
michael@0:
michael@0: mElements.Remove(aElement);
michael@0: aElement->ClearHasDirAutoSet();
michael@0: aElement->UnsetProperty(nsGkAtoms::dirAutoSetBy);
michael@0: }
michael@0:
michael@0: private:
michael@0: nsCheapSet > mElements;
michael@0:
michael@0: static nsTextNodeDirectionalityMap* GetDirectionalityMap(nsINode* aTextNode)
michael@0: {
michael@0: MOZ_ASSERT(aTextNode->NodeType() == nsIDOMNode::TEXT_NODE,
michael@0: "Must be a text node");
michael@0: nsTextNodeDirectionalityMap* map = nullptr;
michael@0:
michael@0: if (aTextNode->HasTextNodeDirectionalityMap()) {
michael@0: map = static_cast
michael@0: (aTextNode->GetProperty(nsGkAtoms::textNodeDirectionalityMap));
michael@0: }
michael@0:
michael@0: return map;
michael@0: }
michael@0:
michael@0: static PLDHashOperator SetNodeDirection(nsPtrHashKey* aEntry, void* aDir)
michael@0: {
michael@0: MOZ_ASSERT(aEntry->GetKey()->IsElement(), "Must be an Element");
michael@0: aEntry->GetKey()->SetDirectionality(*reinterpret_cast(aDir),
michael@0: true);
michael@0: return PL_DHASH_NEXT;
michael@0: }
michael@0:
michael@0: static PLDHashOperator ResetNodeDirection(nsPtrHashKey* aEntry, void* aData)
michael@0: {
michael@0: MOZ_ASSERT(aEntry->GetKey()->IsElement(), "Must be an Element");
michael@0: // run the downward propagation algorithm
michael@0: // and remove the text node from the map
michael@0: nsINode* oldTextNode = static_cast(aData);
michael@0: Element* rootNode = aEntry->GetKey();
michael@0: nsINode* newTextNode = nullptr;
michael@0: if (oldTextNode && rootNode->HasDirAuto()) {
michael@0: newTextNode = WalkDescendantsSetDirectionFromText(rootNode, true,
michael@0: oldTextNode);
michael@0: }
michael@0: if (newTextNode) {
michael@0: nsTextNodeDirectionalityMap::AddEntryToMap(newTextNode, rootNode);
michael@0: } else {
michael@0: rootNode->ClearHasDirAutoSet();
michael@0: rootNode->UnsetProperty(nsGkAtoms::dirAutoSetBy);
michael@0: }
michael@0: return PL_DHASH_REMOVE;
michael@0: }
michael@0:
michael@0: static PLDHashOperator ClearEntry(nsPtrHashKey* aEntry, void* aData)
michael@0: {
michael@0: Element* rootNode = aEntry->GetKey();
michael@0: rootNode->ClearHasDirAutoSet();
michael@0: rootNode->UnsetProperty(nsGkAtoms::dirAutoSetBy);
michael@0: return PL_DHASH_REMOVE;
michael@0: }
michael@0:
michael@0: public:
michael@0: void UpdateAutoDirection(Directionality aDir)
michael@0: {
michael@0: mElements.EnumerateEntries(SetNodeDirection, &aDir);
michael@0: }
michael@0:
michael@0: void ClearAutoDirection()
michael@0: {
michael@0: mElements.EnumerateEntries(ResetNodeDirection, nullptr);
michael@0: }
michael@0:
michael@0: void ResetAutoDirection(nsINode* aTextNode)
michael@0: {
michael@0: mElements.EnumerateEntries(ResetNodeDirection, aTextNode);
michael@0: }
michael@0:
michael@0: void EnsureMapIsClear(nsINode* aTextNode)
michael@0: {
michael@0: DebugOnly clearedEntries =
michael@0: mElements.EnumerateEntries(ClearEntry, aTextNode);
michael@0: MOZ_ASSERT(clearedEntries == 0, "Map should be empty already");
michael@0: }
michael@0:
michael@0: static void RemoveElementFromMap(nsINode* aTextNode, Element* aElement)
michael@0: {
michael@0: if (aTextNode->HasTextNodeDirectionalityMap()) {
michael@0: GetDirectionalityMap(aTextNode)->RemoveEntry(aTextNode, aElement);
michael@0: }
michael@0: }
michael@0:
michael@0: static void AddEntryToMap(nsINode* aTextNode, Element* aElement)
michael@0: {
michael@0: nsTextNodeDirectionalityMap* map = GetDirectionalityMap(aTextNode);
michael@0: if (!map) {
michael@0: map = new nsTextNodeDirectionalityMap(aTextNode);
michael@0: }
michael@0:
michael@0: map->AddEntry(aTextNode, aElement);
michael@0: }
michael@0:
michael@0: static void UpdateTextNodeDirection(nsINode* aTextNode, Directionality aDir)
michael@0: {
michael@0: MOZ_ASSERT(aTextNode->HasTextNodeDirectionalityMap(),
michael@0: "Map missing in UpdateTextNodeDirection");
michael@0: GetDirectionalityMap(aTextNode)->UpdateAutoDirection(aDir);
michael@0: }
michael@0:
michael@0: static void ClearTextNodeDirection(nsINode* aTextNode)
michael@0: {
michael@0: MOZ_ASSERT(aTextNode->HasTextNodeDirectionalityMap(),
michael@0: "Map missing in ResetTextNodeDirection");
michael@0: GetDirectionalityMap(aTextNode)->ClearAutoDirection();
michael@0: }
michael@0:
michael@0: static void ResetTextNodeDirection(nsINode* aTextNode)
michael@0: {
michael@0: MOZ_ASSERT(aTextNode->HasTextNodeDirectionalityMap(),
michael@0: "Map missing in ResetTextNodeDirection");
michael@0: GetDirectionalityMap(aTextNode)->ResetAutoDirection(aTextNode);
michael@0: }
michael@0:
michael@0: static void EnsureMapIsClearFor(nsINode* aTextNode)
michael@0: {
michael@0: if (aTextNode->HasTextNodeDirectionalityMap()) {
michael@0: GetDirectionalityMap(aTextNode)->EnsureMapIsClear(aTextNode);
michael@0: }
michael@0: }
michael@0: };
michael@0:
michael@0: Directionality
michael@0: RecomputeDirectionality(Element* aElement, bool aNotify)
michael@0: {
michael@0: MOZ_ASSERT(!aElement->HasDirAuto(),
michael@0: "RecomputeDirectionality called with dir=auto");
michael@0:
michael@0: Directionality dir = eDir_LTR;
michael@0:
michael@0: if (aElement->HasValidDir()) {
michael@0: dir = aElement->GetDirectionality();
michael@0: } else {
michael@0: Element* parent = aElement->GetParentElement();
michael@0: if (parent) {
michael@0: // If the element doesn't have an explicit dir attribute with a valid
michael@0: // value, the directionality is the same as the parent element (but
michael@0: // don't propagate the parent directionality if it isn't set yet).
michael@0: Directionality parentDir = parent->GetDirectionality();
michael@0: if (parentDir != eDir_NotSet) {
michael@0: dir = parentDir;
michael@0: }
michael@0: } else {
michael@0: // If there is no parent element and no dir attribute, the directionality
michael@0: // is LTR.
michael@0: dir = eDir_LTR;
michael@0: }
michael@0:
michael@0: aElement->SetDirectionality(dir, aNotify);
michael@0: }
michael@0: return dir;
michael@0: }
michael@0:
michael@0: void
michael@0: SetDirectionalityOnDescendants(Element* aElement, Directionality aDir,
michael@0: bool aNotify)
michael@0: {
michael@0: for (nsIContent* child = aElement->GetFirstChild(); child; ) {
michael@0: if (!child->IsElement()) {
michael@0: child = child->GetNextNode(aElement);
michael@0: continue;
michael@0: }
michael@0:
michael@0: Element* element = child->AsElement();
michael@0: if (element->HasValidDir() || element->HasDirAuto()) {
michael@0: child = child->GetNextNonChildNode(aElement);
michael@0: continue;
michael@0: }
michael@0: element->SetDirectionality(aDir, aNotify);
michael@0: child = child->GetNextNode(aElement);
michael@0: }
michael@0: }
michael@0:
michael@0: /**
michael@0: * Walk the parent chain of a text node whose dir attribute has been removed and
michael@0: * reset the direction of any of its ancestors which have dir=auto and whose
michael@0: * directionality is determined by a text node descendant.
michael@0: */
michael@0: void
michael@0: WalkAncestorsResetAutoDirection(Element* aElement, bool aNotify)
michael@0: {
michael@0: nsINode* setByNode;
michael@0: Element* parent = aElement->GetParentElement();
michael@0:
michael@0: while (parent && parent->NodeOrAncestorHasDirAuto()) {
michael@0: if (parent->HasDirAutoSet()) {
michael@0: // If the parent has the DirAutoSet flag, its direction is determined by
michael@0: // some text node descendant.
michael@0: // Remove it from the map and reset its direction by the downward
michael@0: // propagation algorithm
michael@0: setByNode =
michael@0: static_cast(parent->GetProperty(nsGkAtoms::dirAutoSetBy));
michael@0: if (setByNode) {
michael@0: nsTextNodeDirectionalityMap::RemoveElementFromMap(setByNode, parent);
michael@0: }
michael@0: }
michael@0: if (parent->HasDirAuto()) {
michael@0: setByNode = WalkDescendantsSetDirectionFromText(parent, aNotify);
michael@0: if (setByNode) {
michael@0: nsTextNodeDirectionalityMap::AddEntryToMap(setByNode, parent);
michael@0: }
michael@0: break;
michael@0: }
michael@0: parent = parent->GetParentElement();
michael@0: }
michael@0: }
michael@0:
michael@0: void
michael@0: WalkDescendantsResetAutoDirection(Element* aElement)
michael@0: {
michael@0: nsIContent* child = aElement->GetFirstChild();
michael@0: while (child) {
michael@0: if (child->HasDirAuto()) {
michael@0: child = child->GetNextNonChildNode(aElement);
michael@0: continue;
michael@0: }
michael@0:
michael@0: if (child->HasTextNodeDirectionalityMap()) {
michael@0: nsTextNodeDirectionalityMap::ResetTextNodeDirection(child);
michael@0: nsTextNodeDirectionalityMap::EnsureMapIsClearFor(child);
michael@0: }
michael@0: child = child->GetNextNode(aElement);
michael@0: }
michael@0: }
michael@0:
michael@0: void
michael@0: WalkDescendantsSetDirAuto(Element* aElement, bool aNotify)
michael@0: {
michael@0: // Only test for DoesNotParticipateInAutoDirection -- in other words, if
michael@0: // aElement is a which is having its dir attribute set to auto (or
michael@0: // removed or set to an invalid value, which are equivalent to dir=auto for
michael@0: // , we *do* want to set AncestorHasDirAuto on its descendants, unlike
michael@0: // in SetDirOnBind where we don't propagate AncestorHasDirAuto to a
michael@0: // being bound to an existing node with dir=auto.
michael@0: if (!DoesNotParticipateInAutoDirection(aElement)) {
michael@0:
michael@0: bool setAncestorDirAutoFlag =
michael@0: #ifdef DEBUG
michael@0: true;
michael@0: #else
michael@0: !aElement->AncestorHasDirAuto();
michael@0: #endif
michael@0:
michael@0: if (setAncestorDirAutoFlag) {
michael@0: nsIContent* child = aElement->GetFirstChild();
michael@0: while (child) {
michael@0: if (child->IsElement() &&
michael@0: DoesNotAffectDirectionOfAncestors(child->AsElement())) {
michael@0: child = child->GetNextNonChildNode(aElement);
michael@0: continue;
michael@0: }
michael@0:
michael@0: MOZ_ASSERT(!aElement->AncestorHasDirAuto() ||
michael@0: child->AncestorHasDirAuto(),
michael@0: "AncestorHasDirAuto set on node but not its children");
michael@0: child->SetAncestorHasDirAuto();
michael@0: child = child->GetNextNode(aElement);
michael@0: }
michael@0: }
michael@0: }
michael@0:
michael@0: nsINode* textNode = WalkDescendantsSetDirectionFromText(aElement, aNotify);
michael@0: if (textNode) {
michael@0: nsTextNodeDirectionalityMap::AddEntryToMap(textNode, aElement);
michael@0: }
michael@0: }
michael@0:
michael@0: void
michael@0: WalkDescendantsClearAncestorDirAuto(Element* aElement)
michael@0: {
michael@0: nsIContent* child = aElement->GetFirstChild();
michael@0: while (child) {
michael@0: if (child->HasDirAuto()) {
michael@0: child = child->GetNextNonChildNode(aElement);
michael@0: continue;
michael@0: }
michael@0:
michael@0: child->ClearAncestorHasDirAuto();
michael@0: child = child->GetNextNode(aElement);
michael@0: }
michael@0: }
michael@0:
michael@0: void SetAncestorDirectionIfAuto(nsINode* aTextNode, Directionality aDir,
michael@0: bool aNotify = true)
michael@0: {
michael@0: MOZ_ASSERT(aTextNode->NodeType() == nsIDOMNode::TEXT_NODE,
michael@0: "Must be a text node");
michael@0:
michael@0: Element* parent = aTextNode->GetParentElement();
michael@0: while (parent && parent->NodeOrAncestorHasDirAuto()) {
michael@0: if (DoesNotParticipateInAutoDirection(parent) || parent->HasFixedDir()) {
michael@0: break;
michael@0: }
michael@0:
michael@0: if (parent->HasDirAuto()) {
michael@0: bool resetDirection = false;
michael@0: nsINode* directionWasSetByTextNode =
michael@0: static_cast(parent->GetProperty(nsGkAtoms::dirAutoSetBy));
michael@0:
michael@0: if (!parent->HasDirAutoSet()) {
michael@0: // Fast path if parent's direction is not yet set by any descendant
michael@0: MOZ_ASSERT(!directionWasSetByTextNode,
michael@0: "dirAutoSetBy property should be null");
michael@0: resetDirection = true;
michael@0: } else {
michael@0: // If parent's direction is already set, we need to know if
michael@0: // aTextNode is before or after the text node that had set it.
michael@0: // We will walk parent's descendants in tree order starting from
michael@0: // aTextNode to optimize for the most common case where text nodes are
michael@0: // being appended to tree.
michael@0: if (!directionWasSetByTextNode) {
michael@0: resetDirection = true;
michael@0: } else if (directionWasSetByTextNode != aTextNode) {
michael@0: nsIContent* child = aTextNode->GetNextNode(parent);
michael@0: while (child) {
michael@0: if (child->IsElement() &&
michael@0: DoesNotAffectDirectionOfAncestors(child->AsElement())) {
michael@0: child = child->GetNextNonChildNode(parent);
michael@0: continue;
michael@0: }
michael@0:
michael@0: if (child == directionWasSetByTextNode) {
michael@0: // we found the node that set the element's direction after our
michael@0: // text node, so we need to reset the direction
michael@0: resetDirection = true;
michael@0: break;
michael@0: }
michael@0:
michael@0: child = child->GetNextNode(parent);
michael@0: }
michael@0: }
michael@0: }
michael@0:
michael@0: if (resetDirection) {
michael@0: if (directionWasSetByTextNode) {
michael@0: nsTextNodeDirectionalityMap::RemoveElementFromMap(
michael@0: directionWasSetByTextNode, parent
michael@0: );
michael@0: }
michael@0: parent->SetDirectionality(aDir, aNotify);
michael@0: nsTextNodeDirectionalityMap::AddEntryToMap(aTextNode, parent);
michael@0: SetDirectionalityOnDescendants(parent, aDir, aNotify);
michael@0: }
michael@0:
michael@0: // Since we found an element with dir=auto, we can stop walking the
michael@0: // parent chain: none of its ancestors will have their direction set by
michael@0: // any of its descendants.
michael@0: return;
michael@0: }
michael@0: parent = parent->GetParentElement();
michael@0: }
michael@0: }
michael@0:
michael@0: bool
michael@0: TextNodeWillChangeDirection(nsIContent* aTextNode, Directionality* aOldDir,
michael@0: uint32_t aOffset)
michael@0: {
michael@0: if (!NodeAffectsDirAutoAncestor(aTextNode)) {
michael@0: nsTextNodeDirectionalityMap::EnsureMapIsClearFor(aTextNode);
michael@0: return false;
michael@0: }
michael@0:
michael@0: uint32_t firstStrong;
michael@0: *aOldDir = GetDirectionFromText(aTextNode->GetText(), &firstStrong);
michael@0: return (aOffset <= firstStrong);
michael@0: }
michael@0:
michael@0: void
michael@0: TextNodeChangedDirection(nsIContent* aTextNode, Directionality aOldDir,
michael@0: bool aNotify)
michael@0: {
michael@0: Directionality newDir = GetDirectionFromText(aTextNode->GetText());
michael@0: if (newDir == eDir_NotSet) {
michael@0: if (aOldDir != eDir_NotSet && aTextNode->HasTextNodeDirectionalityMap()) {
michael@0: // This node used to have a strong directional character but no
michael@0: // longer does. ResetTextNodeDirection() will re-resolve the
michael@0: // directionality of any elements whose directionality was
michael@0: // determined by this node.
michael@0: nsTextNodeDirectionalityMap::ResetTextNodeDirection(aTextNode);
michael@0: }
michael@0: } else {
michael@0: // This node has a strong directional character. If it has a
michael@0: // TextNodeDirectionalityMap property, it already determines the
michael@0: // directionality of some element(s), so call UpdateTextNodeDirection to
michael@0: // reresolve their directionality. Otherwise call
michael@0: // SetAncestorDirectionIfAuto to find ancestor elements which should
michael@0: // have their directionality determined by this node.
michael@0: if (aTextNode->HasTextNodeDirectionalityMap()) {
michael@0: nsTextNodeDirectionalityMap::UpdateTextNodeDirection(aTextNode, newDir);
michael@0: } else {
michael@0: SetAncestorDirectionIfAuto(aTextNode, newDir, aNotify);
michael@0: }
michael@0: }
michael@0: }
michael@0:
michael@0: void
michael@0: SetDirectionFromNewTextNode(nsIContent* aTextNode)
michael@0: {
michael@0: if (!NodeAffectsDirAutoAncestor(aTextNode)) {
michael@0: return;
michael@0: }
michael@0:
michael@0: Element* parent = aTextNode->GetParentElement();
michael@0: if (parent && parent->NodeOrAncestorHasDirAuto()) {
michael@0: aTextNode->SetAncestorHasDirAuto();
michael@0: }
michael@0:
michael@0: Directionality dir = GetDirectionFromText(aTextNode->GetText());
michael@0: if (dir != eDir_NotSet) {
michael@0: SetAncestorDirectionIfAuto(aTextNode, dir);
michael@0: }
michael@0: }
michael@0:
michael@0: void
michael@0: ResetDirectionSetByTextNode(nsTextNode* aTextNode, bool aNullParent)
michael@0: {
michael@0: if (!NodeAffectsDirAutoAncestor(aTextNode)) {
michael@0: nsTextNodeDirectionalityMap::EnsureMapIsClearFor(aTextNode);
michael@0: return;
michael@0: }
michael@0:
michael@0: Directionality dir = GetDirectionFromText(aTextNode->GetText());
michael@0: if (dir != eDir_NotSet && aTextNode->HasTextNodeDirectionalityMap()) {
michael@0: if (aNullParent) {
michael@0: nsTextNodeDirectionalityMap::ClearTextNodeDirection(aTextNode);
michael@0: } else {
michael@0: nsTextNodeDirectionalityMap::ResetTextNodeDirection(aTextNode);
michael@0: }
michael@0: }
michael@0: }
michael@0:
michael@0: void
michael@0: SetDirectionalityFromValue(Element* aElement, const nsAString& value,
michael@0: bool aNotify)
michael@0: {
michael@0: Directionality dir = GetDirectionFromText(PromiseFlatString(value).get(),
michael@0: value.Length());
michael@0: if (dir == eDir_NotSet) {
michael@0: dir = eDir_LTR;
michael@0: }
michael@0:
michael@0: aElement->SetDirectionality(dir, aNotify);
michael@0: }
michael@0:
michael@0: void
michael@0: OnSetDirAttr(Element* aElement, const nsAttrValue* aNewValue,
michael@0: bool hadValidDir, bool hadDirAuto, bool aNotify)
michael@0: {
michael@0: if (aElement->IsHTML(nsGkAtoms::input)) {
michael@0: return;
michael@0: }
michael@0:
michael@0: if (aElement->AncestorHasDirAuto()) {
michael@0: if (!hadValidDir) {
michael@0: // The element is a descendant of an element with dir = auto, is
michael@0: // having its dir attribute set, and previously didn't have a valid dir
michael@0: // attribute.
michael@0: // Check whether any of its text node descendants determine the
michael@0: // direction of any of its ancestors, and redetermine their direction
michael@0: WalkDescendantsResetAutoDirection(aElement);
michael@0: } else if (!aElement->HasValidDir()) {
michael@0: // The element is a descendant of an element with dir = auto and is
michael@0: // having its dir attribute removed or set to an invalid value.
michael@0: // Reset the direction of any of its ancestors whose direction is
michael@0: // determined by a text node descendant
michael@0: WalkAncestorsResetAutoDirection(aElement, aNotify);
michael@0: }
michael@0: } else if (hadDirAuto && !aElement->HasDirAuto()) {
michael@0: // The element isn't a descendant of an element with dir = auto, and is
michael@0: // having its dir attribute set to something other than auto.
michael@0: // Walk the descendant tree and clear the AncestorHasDirAuto flag.
michael@0: //
michael@0: // N.B: For elements other than it would be enough to test that the
michael@0: // current value of dir was "auto" in BeforeSetAttr to know that we
michael@0: // were unsetting dir="auto". For things are more complicated,
michael@0: // since it behaves like dir="auto" whenever the dir attribute is
michael@0: // empty or invalid, so we would have to check whether the old value
michael@0: // was not either "ltr" or "rtl", and the new value was either "ltr"
michael@0: // or "rtl". Element::HasDirAuto() encapsulates all that, so doing it
michael@0: // here is simpler.
michael@0: WalkDescendantsClearAncestorDirAuto(aElement);
michael@0: }
michael@0:
michael@0: if (aElement->HasDirAuto()) {
michael@0: WalkDescendantsSetDirAuto(aElement, aNotify);
michael@0: } else {
michael@0: if (aElement->HasDirAutoSet()) {
michael@0: nsINode* setByNode =
michael@0: static_cast(aElement->GetProperty(nsGkAtoms::dirAutoSetBy));
michael@0: nsTextNodeDirectionalityMap::RemoveElementFromMap(setByNode, aElement);
michael@0: }
michael@0: SetDirectionalityOnDescendants(aElement,
michael@0: RecomputeDirectionality(aElement, aNotify),
michael@0: aNotify);
michael@0: }
michael@0: }
michael@0:
michael@0: void
michael@0: SetDirOnBind(mozilla::dom::Element* aElement, nsIContent* aParent)
michael@0: {
michael@0: // Set the AncestorHasDirAuto flag, unless this element shouldn't affect
michael@0: // ancestors that have dir=auto
michael@0: if (!DoesNotParticipateInAutoDirection(aElement) &&
michael@0: !aElement->IsHTML(nsGkAtoms::bdi) &&
michael@0: aParent && aParent->NodeOrAncestorHasDirAuto()) {
michael@0: aElement->SetAncestorHasDirAuto();
michael@0:
michael@0: nsIContent* child = aElement->GetFirstChild();
michael@0: if (child) {
michael@0: // If we are binding an element to the tree that already has descendants,
michael@0: // and the parent has NodeHasDirAuto or NodeAncestorHasDirAuto, we need
michael@0: // to set NodeAncestorHasDirAuto on all the element's descendants, except
michael@0: // for nodes that don't affect the direction of their ancestors.
michael@0: do {
michael@0: if (child->IsElement() &&
michael@0: DoesNotAffectDirectionOfAncestors(child->AsElement())) {
michael@0: child = child->GetNextNonChildNode(aElement);
michael@0: continue;
michael@0: }
michael@0:
michael@0: child->SetAncestorHasDirAuto();
michael@0: child = child->GetNextNode(aElement);
michael@0: } while (child);
michael@0:
michael@0: // We may also need to reset the direction of an ancestor with dir=auto
michael@0: WalkAncestorsResetAutoDirection(aElement, true);
michael@0: }
michael@0: }
michael@0:
michael@0: if (!aElement->HasDirAuto()) {
michael@0: // if the element doesn't have dir=auto, set its own directionality from
michael@0: // the dir attribute or by inheriting from its ancestors.
michael@0: RecomputeDirectionality(aElement, false);
michael@0: }
michael@0: }
michael@0:
michael@0: void ResetDir(mozilla::dom::Element* aElement)
michael@0: {
michael@0: if (aElement->HasDirAutoSet()) {
michael@0: nsINode* setByNode =
michael@0: static_cast(aElement->GetProperty(nsGkAtoms::dirAutoSetBy));
michael@0: nsTextNodeDirectionalityMap::RemoveElementFromMap(setByNode, aElement);
michael@0: }
michael@0:
michael@0: if (!aElement->HasDirAuto()) {
michael@0: RecomputeDirectionality(aElement, false);
michael@0: }
michael@0: }
michael@0:
michael@0: } // end namespace mozilla
michael@0: