Skip to content

Commit

Permalink
Make the directionality account for missing parent element
Browse files Browse the repository at this point in the history
The current algorithm has no defined exit for when an element has no parent element and its dir attribute is in the undefined state.

And also rewrite it to make it easier to change as we want to account for shadow trees in a future version.

This was inspired by work from Brian Kardell and fantasai in this area.

Closes #9452.
  • Loading branch information
annevk committed Jul 24, 2023
1 parent 627176c commit 2f178c5
Showing 1 changed file with 104 additions and 111 deletions.
215 changes: 104 additions & 111 deletions source
Original file line number Diff line number Diff line change
Expand Up @@ -13405,154 +13405,147 @@ Transport Protocol">HTTP&lt;/abbr> today.&lt;/p></code></pre> <!-- DO NOT REWRAP
specifies the element's text directionality. The attribute is an <span>enumerated attribute</span>
with the following keywords and states:</p>

<dl>
<dt>The <dfn attr-value for="html-global/dir"><code data-x="attr-dir-ltr">ltr</code></dfn>
keyword, which maps to the <dfn data-x="attr-dir-ltr-state">ltr</dfn> state</dt>

<dd>
<p>Indicates that the contents of the element are explicitly directionally isolated
left-to-right text.</p>
</dd>

<dt>The <dfn attr-value for="html-global/dir"><code data-x="attr-dir-rtl">rtl</code></dfn>
keyword, which maps to the <dfn data-x="attr-dir-rtl-state">rtl</dfn> state</dt>

<dd>
<p>Indicates that the contents of the element are explicitly directionally isolated
right-to-left text.</p>
</dd>

<dt>The <dfn attr-value for="html-global/dir"><code data-x="attr-dir-auto">auto</code></dfn>
keyword, which maps to the <dfn data-x="attr-dir-auto-state">auto</dfn> state</dt>

<dd>
<p>Indicates that the contents of the element are explicitly directionally isolated text, but
that the direction is to be determined programmatically using the contents of the element (as
described below).</p>
<table>
<tr>
<th>Keyword</th>
<th>State</th>
<th>Brief description</th>
</tr>
<tr>
<td><dfn attr-value for="html-global/dir"><code data-x="attr-dir-ltr">ltr</code></dfn></td>
<td><dfn data-x="attr-dir-ltr-state">ltr</dfn></td>
<td>The contents of the element are explicitly directionally isolated left-to-right text.</td>
</tr>
<tr>
<td><dfn attr-value for="html-global/dir"><code data-x="attr-dir-rtl">rtl</code></dfn></td>
<td><dfn data-x="attr-dir-rtl-state">rtl</dfn></td>
<td>The contents of the element are explicitly directionally isolated right-to-left text.</td>
</tr>
<tr>
<td><dfn attr-value for="html-global/dir"><code data-x="attr-dir-auto">auto</code></dfn></td>
<td><dfn data-x="attr-dir-auto-state">auto</dfn></td>
<td>The contents of the element are explicitly directionally isolated text, but the direction is
to be determined programmatically using the contents of the element (as described below).</td>
</tr>
</table>

<p class="note">The heuristic used by this state is very crude (it just looks at the first
character with a strong directionality, in a manner analogous to the Paragraph Level
determination in the bidirectional algorithm). Authors are urged to only use this value as a
last resort when the direction of the text is truly unknown and no better server-side heuristic
can be applied. <ref spec=BIDI></p>
<div class="note">
<p>The heuristic used by the <span data-x="attr-dir-auto-state">auto</span> state is very crude
(it just looks at the first character with a strong directionality, in a manner analogous to the
Paragraph Level determination in the bidirectional algorithm). Authors are urged to only use this
value as a last resort when the direction of the text is truly unknown and no better server-side
heuristic can be applied. <ref spec=BIDI></p>

<p class="note">For <code>textarea</code> and <code>pre</code> elements, the heuristic is
applied on a per-paragraph level.</p>
</dd>
</dl>
<p>For <code>textarea</code> and <code>pre</code> elements, the heuristic is applied on a
per-paragraph level.</p>
</div>

<p>The attribute has no <i data-x="invalid value default">invalid value default</i> and no <i data-x="missing value default">missing value default</i>.</p>
<p>The <code data-x="attr-dir">dir</code> attribute's <i data-x="invalid value default">invalid
value default</i> and <i data-x="missing value default">missing value default</i> are both the
<dfn data-x="attr-dir-undefined-state">undefined</dfn> state.</p>

<hr>

<p><dfn>The directionality</dfn> of an element (any element, not just an <span data-x="HTML
elements">HTML element</span>) is either '<dfn data-x="concept-ltr">ltr</dfn>' or '<dfn
data-x="concept-rtl">rtl</dfn>', and is determined as per the first appropriate set of steps from
the following list:</p>
data-x="concept-rtl">rtl</dfn>'. To compute <span>the directionality</span> given an element
<var>element</var>, switch on <var>element</var>'s <code data-x="attr-dir">dir</code> attribute
state:</p>

<dl class="switch">
<dt><span data-x="attr-dir-ltr-state">ltr</span></dt>
<dd><p>Return '<span data-x="concept-ltr">ltr</span>'.</p></dd>

<dt><span data-x="attr-dir-rtl-state">rtl</span></dt>
<dd><p>Return '<span data-x="concept-rtl">rtl</span>'.</p></dd>

<dt>If the element's <code data-x="attr-dir">dir</code> attribute is in the <span
data-x="attr-dir-ltr-state">ltr</span> state</dt>

<dt>If the element is a <span>document element</span> and the <code data-x="attr-dir">dir</code>
attribute is not in a defined state (i.e. it is not present or has an invalid value)</dt>

<dt>If the element is an <code>input</code> element whose <code
data-x="attr-input-type">type</code> attribute is in the <span
data-x="attr-input-type-tel">Telephone</span> state, and the <code data-x="attr-dir">dir</code>
attribute is not in a defined state (i.e. it is not present or has an invalid value)</dt>

<dd><p><span>The directionality</span> of the element is '<span
data-x="concept-ltr">ltr</span>'.</p></dd>


<dt>If the element's <code data-x="attr-dir">dir</code> attribute is in the <span
data-x="attr-dir-rtl-state">rtl</span> state</dt>
<dt><span data-x="attr-dir-auto-state">auto</span></dt>
<dd><p>Return <span>the auto directionality</span> with <var>element</var>.</p></dd>

<dd><p><span>The directionality</span> of the element is '<span
data-x="concept-rtl">rtl</span>'.</p></dd>
<dt><span data-x="attr-dir-undefined-state">undefined</span></dt>
<dd>
<ol>
<li><p>If <var>element</var> is a <code>bdi</code> element, then return <span>the auto
directionality</span> with <var>element</var>.</p></li>

<dt>If the element is an <code>input</code> element whose <code
data-x="attr-input-type">type</code> attribute is in the <span
data-x="attr-input-type-text">Text</span>, <span data-x="attr-input-type-search">Search</span>,
<span data-x="attr-input-type-tel">Telephone</span>, <span
data-x="attr-input-type-url">URL</span>, or <span data-x="attr-input-type-email">Email</span>
state, and the <code data-x="attr-dir">dir</code> attribute is in the <span
data-x="attr-dir-auto-state">auto</span> state</dt>
<li><p>If <var>element</var> is an <code>input</code> element whose <code
data-x="attr-input-type">type</code> attribute is in the <span
data-x="attr-input-type-tel">Telephone</span> state, then return '<span
data-x="concept-ltr">ltr</span>'.</p></li>

<dt>If the element is a <code>textarea</code> element and the <code data-x="attr-dir">dir</code>
attribute is in the <span data-x="attr-dir-auto-state">auto</span> state</dt>
<li><p>If <var>element</var> has no parent element, then return '<span
data-x="concept-ltr">ltr</span>'.</p></li>

<dd>
<p>If the element's <span data-x="concept-fe-value">value</span> contains a character of
bidirectional character type AL or R, and there is no character of bidirectional character type
L anywhere before it in the element's <span data-x="concept-fe-value">value</span>, then
<span>the directionality</span> of the element is '<span data-x="concept-rtl">rtl</span>'. <ref spec=BIDI></p>
<li><p>Return <span>the directionality</span> with <var>element</var>'s parent.</p></li>
</ol>
</dd>
</dl>

<p>Otherwise, if the element's <span data-x="concept-fe-value">value</span> is not the empty
string, or if the element is a <span>document element</span>, <span>the directionality</span> of
the element is '<span data-x="concept-ltr">ltr</span>'.</p>
<p>To compute <span>the auto directionality</span> given an element <var>element</var>:</p>

<p>Otherwise, <span>the directionality</span> of the element is the same as the element's parent
element's <span data-x="the directionality">directionality</span>.</p>
</dd>
<ol>
<li>
<p>If <var>element</var> is an <code>input</code> element whose <code
data-x="attr-input-type">type</code> attribute is in the <span
data-x="attr-input-type-text">Text</span>, <span data-x="attr-input-type-search">Search</span>,
<span data-x="attr-input-type-tel">Telephone</span>, <span
data-x="attr-input-type-url">URL</span>, or <span data-x="attr-input-type-email">Email</span>
state, or <var>element</var> is a <code>textarea</code> element, then:</p>

<ol>
<li><p>If <var>element</var>'s <span data-x="concept-fe-value">value</span> contains a
character of bidirectional character type AL or R, and there is no character of bidirectional
character type L anywhere before it in the element's <span
data-x="concept-fe-value">value</span>, then return '<span data-x="concept-rtl">rtl</span>'.
<ref spec=BIDI></p></li>

<li><p>If <var>element</var>'s <span data-x="concept-fe-value">value</span> is not the empty
string or <var>element</var> has no parent element, then return '<span
data-x="concept-ltr">ltr</span>'.</p></li>

<dt>If the element's <code data-x="attr-dir">dir</code> attribute is in the <span
data-x="attr-dir-auto-state">auto</span> state</dt>
<li><p>Return <span>the directionality</span> with <var>element</var>'s parent.</p></li>
</ol>
</li>

<dt>If the element is a <code>bdi</code> element and the <code data-x="attr-dir">dir</code>
attribute is not in a defined state (i.e. it is not present or has an invalid value)</dt>
<li><p>Let <var>codePoint</var> be null.</p></li>

<dd>
<p>Find the first character in <span>tree order</span> that matches the following criteria:</p>
<li>
<p>Set <var>codePoint</var> to the first code point in <span>tree order</span> that matches the
following criteria, if any:</p>

<ul>
<li><p>The code point is from a <code>Text</code> node that is a descendant of
<var>element</var>.</p></li>

<li><p>The character is from a <code>Text</code> node that is a descendant of the element whose
<span data-x="the directionality">directionality</span> is being determined.</p></li>

<li><p>The character is of bidirectional character type L, AL, or R. <ref spec=BIDI></li>
<li><p>The code point is of bidirectional character type L, AL, or R. <ref spec=BIDI></li>

<li>
<p>The character is not in a <code>Text</code> node that has an ancestor element that is a
descendant of the element whose <span data-x="the directionality">directionality</span> is
being determined and that is either:</p>
<p>The code point is not in a <code>Text</code> node that has an ancestor element that is a
descendant of <var>element</var> and that is either:</p>

<ul class="brief">
<li>A <code>bdi</code> element.
<li>A <code>script</code> element.
<li>A <code>style</code> element.
<li>A <code>textarea</code> element.
<li>An element with a <code data-x="attr-dir">dir</code> attribute in a defined state.
<li>A <code>bdi</code> element.</li>
<li>A <code>script</code> element.</li>
<li>A <code>style</code> element.</li>
<li>A <code>textarea</code> element.</li>
<li>An element whose <code data-x="attr-dir">dir</code> attribute is not in the <span
data-x="attr-dir-undefined-state">undefined</span> state.</li>
</ul>
</li>

</ul>
</li>

<p>If such a character is found and it is of bidirectional character type AL or R, <span>the
directionality</span> of the element is '<span data-x="concept-rtl">rtl</span>'.</p>

<p>If such a character is found and it is of bidirectional character type L, <span>the
directionality</span> of the element is '<span data-x="concept-ltr">ltr</span>'.</p>

<p>Otherwise, if the element is a <span>document element</span>, <span>the directionality</span>
of the element is '<span data-x="concept-ltr">ltr</span>'.</p>
<li><p>If <var>codePoint</var> is non-null and it is of bidirectional character type AL or R,
then return '<span data-x="concept-rtl">rtl</span>'.</p></li>

<p>Otherwise, <span>the directionality</span> of the element is the same as the element's parent
element's <span data-x="the directionality">directionality</span>.</p>
</dd>
<li><p>If <var>codePoint</var> is non-null and it is of bidirectional character type L, then
return '<span data-x="concept-ltr">ltr</span>'.</p></li>

<li><p>If <var>element</var> has no parent element, then return '<span
data-x="concept-ltr">ltr</span>'.</p></li>

<dt>If the element has a parent element and the <code data-x="attr-dir">dir</code> attribute is
not in a defined state (i.e. it is not present or has an invalid value)</dt>

<dd><p><span>The directionality</span> of the element is the same as the element's parent
element's <span data-x="the directionality">directionality</span>.</p></dd>
</dl>
<li><p>Return <span>the directionality</span> with <var>element</var>'s parent.</p></li>
</ol>

<p class="note">Since the <code data-x="attr-dir">dir</code> attribute is only defined for
<span>HTML elements</span>, it cannot be present on elements from other namespaces. Thus, elements
Expand Down

0 comments on commit 2f178c5

Please sign in to comment.