solomax commented on code in PR #1870:
URL: https://github.com/apache/wicket/pull/1870#discussion_r4506362734
##########
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>
+ * @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 <html> tag is the one of the base
page; markup that
+ * chooses between several <html> tags with conditional comments
has this method called
+ * for each of them. An <html> 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 <html> 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 <html> 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:
I had some interesting page tuning for RTL but it was long time ago
I've just read MDN to refresh my memories :)
According to
https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/dir
if `dir` is not set it is NOT `ltr` it is `auto`, and browser might perform
some surprising logic :)
I would add these additional 9 bytes just to ensure everything is fixed
--
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]