Skip to content

Commit ad94bbe

Browse files
authored
Make the directionality account for missing parent element
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.
1 parent 834da11 commit ad94bbe

File tree

1 file changed

+123
-117
lines changed

1 file changed

+123
-117
lines changed

source

Lines changed: 123 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -13443,161 +13443,167 @@ Transport Protocol">HTTP&lt;/abbr> today.&lt;/p></code></pre> <!-- DO NOT REWRAP
1344313443
specifies the element's text directionality. The attribute is an <span>enumerated attribute</span>
1344413444
with the following keywords and states:</p>
1344513445

13446-
<dl>
13447-
<dt>The <dfn attr-value for="html-global/dir"><code data-x="attr-dir-ltr">ltr</code></dfn>
13448-
keyword, which maps to the <dfn data-x="attr-dir-ltr-state">ltr</dfn> state</dt>
13449-
13450-
<dd>
13451-
<p>Indicates that the contents of the element are explicitly directionally isolated
13452-
left-to-right text.</p>
13453-
</dd>
13454-
13455-
<dt>The <dfn attr-value for="html-global/dir"><code data-x="attr-dir-rtl">rtl</code></dfn>
13456-
keyword, which maps to the <dfn data-x="attr-dir-rtl-state">rtl</dfn> state</dt>
13457-
13458-
<dd>
13459-
<p>Indicates that the contents of the element are explicitly directionally isolated
13460-
right-to-left text.</p>
13461-
</dd>
13462-
13463-
<dt>The <dfn attr-value for="html-global/dir"><code data-x="attr-dir-auto">auto</code></dfn>
13464-
keyword, which maps to the <dfn data-x="attr-dir-auto-state">auto</dfn> state</dt>
13465-
13466-
<dd>
13467-
<p>Indicates that the contents of the element are explicitly directionally isolated text, but
13468-
that the direction is to be determined programmatically using the contents of the element (as
13469-
described below).</p>
13446+
<table>
13447+
<thead>
13448+
<tr>
13449+
<th>Keyword</th>
13450+
<th>State</th>
13451+
<th>Brief description</th>
13452+
</tr>
13453+
</thead>
13454+
<tbody>
13455+
<tr>
13456+
<td><dfn attr-value for="html-global/dir"><code data-x="attr-dir-ltr">ltr</code></dfn></td>
13457+
<td><dfn data-x="attr-dir-ltr-state">ltr</dfn></td>
13458+
<td>The contents of the element are explicitly directionally isolated left-to-right text.</td>
13459+
</tr>
13460+
<tr>
13461+
<td><dfn attr-value for="html-global/dir"><code data-x="attr-dir-rtl">rtl</code></dfn></td>
13462+
<td><dfn data-x="attr-dir-rtl-state">rtl</dfn></td>
13463+
<td>The contents of the element are explicitly directionally isolated right-to-left text.</td>
13464+
</tr>
13465+
<tr>
13466+
<td><dfn attr-value for="html-global/dir"><code data-x="attr-dir-auto">auto</code></dfn></td>
13467+
<td><dfn data-x="attr-dir-auto-state">auto</dfn></td>
13468+
<td>The contents of the element are explicitly directionally isolated text, but the direction
13469+
is to be determined programmatically using the contents of the element (as described
13470+
below).</td>
13471+
</tr>
13472+
</tbody>
13473+
</table>
1347013474

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

13477-
<p class="note">For <code>textarea</code> and <code>pre</code> elements, the heuristic is
13478-
applied on a per-paragraph level.</p>
13479-
</dd>
13480-
</dl>
13482+
<p>For <code>textarea</code> and <code>pre</code> elements, the heuristic is applied on a
13483+
per-paragraph level.</p>
13484+
</div>
1348113485

13482-
<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>
13486+
<p>The <code data-x="attr-dir">dir</code> attribute's <i data-x="invalid value default">invalid
13487+
value default</i> and <i data-x="missing value default">missing value default</i> are both the
13488+
<dfn data-x="attr-dir-undefined-state">undefined</dfn> state.</p>
1348313489

1348413490
<hr>
1348513491

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

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

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

13494-
<dt>If the element's <code data-x="attr-dir">dir</code> attribute is in the <span
13495-
data-x="attr-dir-ltr-state">ltr</span> state</dt>
13496-
13497-
<dt>If the element is a <span>document element</span> and the <code data-x="attr-dir">dir</code>
13498-
attribute is not in a defined state (i.e. it is not present or has an invalid value)</dt>
13499-
13500-
<dt>If the element is an <code>input</code> element whose <code
13501-
data-x="attr-input-type">type</code> attribute is in the <span
13502-
data-x="attr-input-type-tel">Telephone</span> state, and the <code data-x="attr-dir">dir</code>
13503-
attribute is not in a defined state (i.e. it is not present or has an invalid value)</dt>
13504-
13505-
<dd><p><span>The directionality</span> of the element is '<span
13506-
data-x="concept-ltr">ltr</span>'.</p></dd>
13507-
13508-
13509-
<dt>If the element's <code data-x="attr-dir">dir</code> attribute is in the <span
13510-
data-x="attr-dir-rtl-state">rtl</span> state</dt>
13511-
13512-
<dd><p><span>The directionality</span> of the element is '<span
13513-
data-x="concept-rtl">rtl</span>'.</p></dd>
13514-
13515-
<dt>If the element is an <code>input</code> element whose <code
13516-
data-x="attr-input-type">type</code> attribute is in the <span
13517-
data-x="attr-input-type-text">Text</span>, <span data-x="attr-input-type-search">Search</span>,
13518-
<span data-x="attr-input-type-tel">Telephone</span>, <span
13519-
data-x="attr-input-type-url">URL</span>, or <span data-x="attr-input-type-email">Email</span>
13520-
state, and the <code data-x="attr-dir">dir</code> attribute is in the <span
13521-
data-x="attr-dir-auto-state">auto</span> state</dt>
13522-
13523-
<dt>If the element is a <code>textarea</code> element and the <code data-x="attr-dir">dir</code>
13524-
attribute is in the <span data-x="attr-dir-auto-state">auto</span> state</dt>
13505+
<dt><span data-x="attr-dir-auto-state">auto</span></dt>
13506+
<dd><p>Return the <span>auto directionality</span> of <var>element</var>.</p></dd>
1352513507

13508+
<dt><span data-x="attr-dir-undefined-state">undefined</span></dt>
1352613509
<dd>
13527-
<p>If the element's <span data-x="concept-fe-value">value</span> contains a character of
13528-
bidirectional character type AL or R, and there is no character of bidirectional character type
13529-
L anywhere before it in the element's <span data-x="concept-fe-value">value</span>, then
13530-
<span>the directionality</span> of the element is '<span data-x="concept-rtl">rtl</span>'.
13531-
<ref>BIDI</ref></p>
13510+
<ol>
13511+
<li><p>If <var>element</var> is a <code>bdi</code> element, then return the <span>auto
13512+
directionality</span> of <var>element</var>.</p></li>
1353213513

13533-
<p>Otherwise, if the element's <span data-x="concept-fe-value">value</span> is not the empty
13534-
string, or if the element is a <span>document element</span>, <span>the directionality</span> of
13535-
the element is '<span data-x="concept-ltr">ltr</span>'.</p>
13514+
<li><p>If <var>element</var> is an <code>input</code> element whose <code
13515+
data-x="attr-input-type">type</code> attribute is in the <span
13516+
data-x="attr-input-type-tel">Telephone</span> state, then return '<span
13517+
data-x="concept-ltr">ltr</span>'.</p></li>
1353613518

13537-
<p>Otherwise, <span>the directionality</span> of the element is the same as the element's parent
13538-
element's <span data-x="the directionality">directionality</span>.</p>
13519+
<li><p>Return the <span>parent directionality</span> of <var>element</var>.</p></li>
13520+
</ol>
1353913521
</dd>
13522+
</dl>
1354013523

13524+
<p class="note">Since the <code data-x="attr-dir">dir</code> attribute is only defined for
13525+
<span>HTML elements</span>, it cannot be present on elements from other namespaces. Thus, elements
13526+
from other namespaces always end up using the <span>parent directionality</span>.</p>
1354113527

13528+
<p>To compute the <dfn>auto directionality</dfn> given an element <var>element</var>:</p>
1354213529

13543-
<dt>If the element's <code data-x="attr-dir">dir</code> attribute is in the <span
13544-
data-x="attr-dir-auto-state">auto</span> state</dt>
13530+
<ol>
13531+
<li>
13532+
<p>If <var>element</var> is an <code>input</code> element whose <code
13533+
data-x="attr-input-type">type</code> attribute is in the <span
13534+
data-x="attr-input-type-text">Text</span>, <span data-x="attr-input-type-search">Search</span>,
13535+
<span data-x="attr-input-type-tel">Telephone</span>, <span
13536+
data-x="attr-input-type-url">URL</span>, or <span data-x="attr-input-type-email">Email</span>
13537+
state, or <var>element</var> is a <code>textarea</code> element, then:</p>
1354513538

13546-
<dt>If the element is a <code>bdi</code> element and the <code data-x="attr-dir">dir</code>
13547-
attribute is not in a defined state (i.e. it is not present or has an invalid value)</dt>
13539+
<ol>
13540+
<li><p>If <var>element</var>'s <span data-x="concept-fe-value">value</span> contains a
13541+
character of bidirectional character type AL or R, and there is no character of bidirectional
13542+
character type L anywhere before it in the element's <span
13543+
data-x="concept-fe-value">value</span>, then return '<span data-x="concept-rtl">rtl</span>'.
13544+
<ref>BIDI</ref></p></li>
1354813545

13549-
<dd>
13550-
<p>Find the first character in <span>tree order</span> that matches the following criteria:</p>
13546+
<li><p>If <var>element</var>'s <span data-x="concept-fe-value">value</span> is not the empty
13547+
string, then return '<span data-x="concept-ltr">ltr</span>'.</p></li>
1355113548

13552-
<ul>
13549+
<li><p>Return the <span>parent directionality</span> of <var>element</var>.</p></li>
13550+
</ol>
13551+
</li>
1355313552

13554-
<li><p>The character is from a <code>Text</code> node that is a descendant of the element whose
13555-
<span data-x="the directionality">directionality</span> is being determined.</p></li>
13553+
<li><p>Let <var>codePoint</var> be null.</p></li>
1355613554

13557-
<li><p>The character is of bidirectional character type L, AL, or R. <ref>BIDI</ref></li>
13555+
<li>
13556+
<p>For each <code>Text</code> node <var>text</var> that is a descendant of <var>element</var>,
13557+
in <span>tree order</span>:</p>
1355813558

13559+
<ol>
1355913560
<li>
13560-
<p>The character is not in a <code>Text</code> node that has an ancestor element that is a
13561-
descendant of the element whose <span data-x="the directionality">directionality</span> is
13562-
being determined and that is either:</p>
13561+
<p>If <var>text</var> has an ancestor element that is a descendant of <var>element</var> and
13562+
is also one of</p>
1356313563

1356413564
<ul class="brief">
13565-
<li>A <code>bdi</code> element.
13566-
<li>A <code>script</code> element.
13567-
<li>A <code>style</code> element.
13568-
<li>A <code>textarea</code> element.
13569-
<li>An element with a <code data-x="attr-dir">dir</code> attribute in a defined state.
13565+
<li>a <code>bdi</code> element</li>
13566+
<li>a <code>script</code> element</li>
13567+
<li>a <code>style</code> element</li>
13568+
<li>a <code>textarea</code> element</li>
13569+
<li>an element whose <code data-x="attr-dir">dir</code> attribute is not in the <span
13570+
data-x="attr-dir-undefined-state">undefined</span> state</li>
1357013571
</ul>
13572+
13573+
<p>then <span>continue</span>.</p>
1357113574
</li>
1357213575

13573-
</ul>
13576+
<li><p>If <var>text</var>'s <span data-x="concept-cd-data">data</span> does not contain a code
13577+
point whose bidirectional character type is L, AL, or R, then <span>continue</span>.
13578+
<ref>BIDI</ref></p></li>
1357413579

13575-
<p>If such a character is found and it is of bidirectional character type AL or R, <span>the
13576-
directionality</span> of the element is '<span data-x="concept-rtl">rtl</span>'.</p>
13580+
<li><p>Set <var>codePoint</var> to the first code point in <var>text</var>'s <span
13581+
data-x="concept-cd-data">data</span> whose bidirectional character type is L, AL, or
13582+
R.</p></li>
1357713583

13578-
<p>If such a character is found and it is of bidirectional character type L, <span>the
13579-
directionality</span> of the element is '<span data-x="concept-ltr">ltr</span>'.</p>
13584+
<li><p><span>Break</span>.</p></li>
13585+
</ol>
13586+
</li>
1358013587

13581-
<p>Otherwise, if the element is a <span>document element</span>, <span>the directionality</span>
13582-
of the element is '<span data-x="concept-ltr">ltr</span>'.</p>
13588+
<li><p>If <var>codePoint</var> is null, then return the <span>parent directionality</span> of
13589+
<var>element</var>.</p></li>
1358313590

13584-
<p>Otherwise, <span>the directionality</span> of the element is the same as the element's parent
13585-
element's <span data-x="the directionality">directionality</span>.</p>
13586-
</dd>
13591+
<li><p>If <var>codePoint</var> is of bidirectional character type AL or R, then return '<span
13592+
data-x="concept-rtl">rtl</span>'.</p></li>
1358713593

13594+
<li><p>If <var>codePoint</var> is of bidirectional character type L, then return '<span
13595+
data-x="concept-ltr">ltr</span>'.</p></li>
13596+
</ol>
1358813597

13589-
<dt>If the element has a parent element and the <code data-x="attr-dir">dir</code> attribute is
13590-
not in a defined state (i.e. it is not present or has an invalid value)</dt>
13598+
<p>To compute the <dfn>parent directionality</dfn> given an element <var>element</var>:</p>
1359113599

13592-
<dd><p><span>The directionality</span> of the element is the same as the element's parent
13593-
element's <span data-x="the directionality">directionality</span>.</p></dd>
13594-
</dl>
13600+
<ol>
13601+
<li><p>If <var>element</var> has no <span>parent element</span>, then return '<span
13602+
data-x="concept-ltr">ltr</span>'.</p></li>
1359513603

13596-
<p class="note">Since the <code data-x="attr-dir">dir</code> attribute is only defined for
13597-
<span>HTML elements</span>, it cannot be present on elements from other namespaces. Thus, elements
13598-
from other namespaces always just inherit their <span data-x="the
13599-
directionality">directionality</span> from their parent element, or, if they don't have one,
13600-
default to '<span data-x="concept-ltr">ltr</span>'.</p>
13604+
<li><p>Return <span data-x="the directionality">directionality</span> of <var>element</var>'s
13605+
<span data-x="parent element">parent</span>.</p></li>
13606+
</ol>
1360113607

1360213608
<div w-nodev>
1360313609

0 commit comments

Comments
 (0)