dashorst commented on code in PR #1870:
URL: https://github.com/apache/wicket/pull/1870#discussion_r4506152897


##########
wicket-core/src/main/java/org/apache/wicket/markup/html/WebPage.java:
##########
@@ -126,6 +132,86 @@ protected void onRender()
                super.onRender();
        }
 
+       @Override
+       protected boolean renderNext(final MarkupStream markupStream)
+       {
+               MarkupElement element = markupStream.get();
+               if (element instanceof ComponentTag tag && tag.isOpen() &&
+                       tag.getFlag(ComponentTag.RENDERED_BY_PAGE))
+               {
+                       ComponentTag htmlTag = tag.mutable();
+                       onHtmlTag(htmlTag);
+                       htmlTag.writeOutput(getResponse(), 
getApplication().getMarkupSettings()
+                               .getStripWicketTags(), 
getMarkup().getMarkupResourceStream().getWicketNamespace());
+                       return true;
+               }
+               return super.renderNext(markupStream);
+       }
+
+       /**
+        * Called when the <html> tag of this page is rendered, to let 
the page add or change its
+        * attributes. The default sets <code>lang</code> to the language tag 
of {@link #getLocale()},
+        * for example <code>nl-NL</code>, so that screen readers and browsers 
can determine the
+        * language of the page (WCAG 2.2 success criterion 3.1.1), and 
<code>dir="rtl"</code> when
+        * that language is written right to left, see {@link 
Session#isRtlLanguage(Locale)}. It does
+        * not set <code>dir="ltr"</code>, which is the default of the browser.
+        * <p>
+        * It leaves the tag alone when the markup already has a 
<code>lang</code> or
+        * <code>xml:lang</code> attribute, so a page written in one language 
whatever the session
+        * locale can say so in its markup, and when the locale has no 
language, such as
+        * {@link Locale#ROOT}. A <code>dir</code> in the markup is kept as 
well: <code>dir="ltr"</code>
+        * keeps a page left to right in every locale.
+        * <p>
+        * Override this method to add attributes that belong to the document 
as a whole, and call
+        * <code>super</code> to keep the language and direction. An 
application whose stylesheets do
+        * not support right-to-left layout can opt out of the direction in its 
base page:
+        *
+        * <pre>
+        * &#64;Override
+        * protected void onHtmlTag(ComponentTag tag)
+        * {
+        *      super.onHtmlTag(tag);
+        *      tag.remove("dir");
+        * }
+        * </pre>
+        *
+        * Attribute values are escaped when the tag is written.
+        * <p>
+        * With markup inheritance the &lt;html&gt; tag is the one of the base 
page; markup that
+        * chooses between several &lt;html&gt; tags with conditional comments 
has this method called
+        * for each of them. An &lt;html&gt; tag with a <code>wicket:id</code> 
belongs to its component
+        * and is not passed here. The tag is written from its attributes, the 
way a component tag is.
+        * <p>
+        * The &lt;html&gt; tag is only rendered with the whole page. An Ajax 
request does not render it
+        * again, so an attribute that has to change in response to an Ajax 
request, such as a
+        * <code>data-theme</code> switched by an <code>AjaxLink</code>, has to 
be changed in the
+        * browser as well, for instance with
+        * 
<code>target.appendJavaScript("document.documentElement.dataset.theme='dark'")</code>.
+        *
+        * @param tag
+        *            a mutable copy of the &lt;html&gt; tag of the page's 
markup
+        * @since 12.0.0
+        */
+       protected void onHtmlTag(final ComponentTag tag)
+       {
+               if (tag.getAttribute("lang") != null || 
tag.getAttribute("xml:lang") != null)
+               {
+                       return;
+               }
+
+               Locale locale = getLocale();
+               if (locale == null || locale.getLanguage().isEmpty())
+               {
+                       return;
+               }
+
+               tag.put("lang", locale.toLanguageTag());
+               if (tag.getAttribute("dir") == null && 
Session.isRtlLanguage(locale))

Review Comment:
   Thanks for looking at this. I'm not sure yet whether `ltr` should be 
explicit, and I'd like your view.
   
   My hesitation is that most applications are left-to-right only. For them 
`dir="ltr"` would be an extra attribute on every page that changes nothing for 
the browser. The applications that serve both directions probably have to 
handle the direction already for their layout to work at all, and a `dir` they 
write in the markup is kept. Is there a case you have in mind where the 
explicit `ltr` matters? Stylesheets that select on `[dir="ltr"]` come to mind. 
Or would `:dir(ltr)` cover that?
   
   If we do go with it, I think the condition needs a different shape. As 
written, a `dir` from the markup would be overwritten with `ltr`, since the 
condition is false whenever the markup has one. Something like this:
   
   ```java
   if (tag.getAttribute("dir") == null)
   {
        tag.put("dir", Session.isRtlLanguage(locale) ? "rtl" : "ltr");
   }
   ```
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to