dashorst commented on code in PR #1870:
URL: https://github.com/apache/wicket/pull/1870#discussion_r4506589321
##########
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:
Fair enough, it's in: 4f199383d8 writes `dir="ltr"` or `dir="rtl"` whenever
Wicket writes `lang`. A `dir` from the markup is kept, and markup that declares
its own `lang` gets neither.
One nuance from the same MDN page, for the record: an absent `dir` doesn't
mean `auto`. "Omitting `dir` does not enable automatic direction detection … If
no ancestor sets a direction, the default is left-to-right." What convinced me
is that the page also recommends setting `dir` explicitly on the root element,
even for documents in a single script, and that `[dir="ltr"]` selectors need
the attribute to be there.
--
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]