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