Skip to content

JDK-8308659: Use CSS scroll-margin instead of flexbox layout in API documentation #15969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from

Conversation

hns
Copy link
Member

@hns hns commented Sep 28, 2023

A few years ago we switched to Flexbox Layout to implement the sticky header in the API docs generated by javadoc. This solved the problem of anchor link targets not being positioned correctly, but it also introduced a few new ones:

  • It required a workaround to get browser history to work (JDK-8249133, JDK-8250779, 8286832)
  • It changed certain aspects of scrolling behavior in the browser (JDK-8301080)
  • It changed the way some CSS properties are interpreted (JDK-8315800)

The reason for most of these problems is that the layout paradigm used by Flexbox is very different from traditional layout of HTML pages. The scroll-margin-* CSS properties introduced by the CSS Scroll Snap Module provide a simpler and less intrusive way to implement link target offsets in combination with sticky elements implemented using position: sticky. However, although it is implemented in all supported browsers, it comes with its own challanges and quirks, which are explained below.

The first problem to overcome was that position: sticky is more fragile on mobile browsers than Flexbox. If some part of the page content overflows the allotted horizontal space, the whole page can be zoomed out to view the whole content. This causes the header to become unsticky and the link target offsets to become erroneous. It was therefore necessary to make sure page content never overflows its allotted horizontal space, either by adding line break opportunities, or by making all or part of the page horizontally scrollable when its content overflows. Line break opportunities are difficult to add especially in preformatted code, so I opted for making the parts of the page most likely containing long lines of code scrollable.

The next problem was that enabling horizontal scrolling on an element or one of its containing elements breaks the scroll-margin-top property in Chrome due to a browser quirk (both desktop and mobile versions). This means that the scrolling must occur in a child element rather than the sections or other elements serving as link targets.

When enabling horizontal scrolling on the contents of sections containing user-provided content, another problem is that it disables Margin Collapse which regulates margins between adjacent and contained boxes. Since these sections can contain a mix of HTML containers, it is almost impossible to preserve the margins between elements while making them scrollable.

The solution to these problems was to add a new <div class="horizontal-scroll"> to the sections containing user-provided content. Adding a single scrollable element per section allowed the margins to be controlled in a relatively safe way, although some CSS is introduced to limit the margin of certain trailing block elements in certain sections.

The scroll-margin-top property is set to the normal height of the sticky header in stylesheet.css, with a line added to search.js to set the property dynamically to the actual header height to accommodate for the pre-release notification banner. In a previous version of this branch, the pre-release banner was excluded from the sticky header, but it was made sticky again in the final version to make the output more comparable.

The generated documentation should render the same on supported browsers pixel by pixel. Before/after snapshots of the JDK docs are available here:

Before: https://cr.openjdk.org/~hannesw/8308659/api.00/
After: https://cr.openjdk.org/~hannesw/8308659/api.01/


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8308659: Use CSS scroll-margin instead of flexbox layout in API documentation (Enhancement - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/15969/head:pull/15969
$ git checkout pull/15969

Update a local copy of the PR:
$ git checkout pull/15969
$ git pull https://git.openjdk.org/jdk.git pull/15969/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 15969

View PR using the GUI difftool:
$ git pr show -t 15969

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/15969.diff

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Sep 28, 2023

👋 Welcome back hannesw! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Sep 28, 2023

@hns The following label will be automatically applied to this pull request:

  • javadoc

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the javadoc javadoc-dev@openjdk.org label Sep 28, 2023
buildConstructorComments(constructorContent);
buildTagInfo(constructorContent);

Content scrollBox = HtmlTree.DIV(HtmlStyle.horizontalScroll);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stylistically, it's a shame to change the name of the variable in this and similar situations when all you are changing is its initial value. In the lines that follow, its only important that it is some kind of container, not specifically a scrollBox. This is the variable name equivalent of whether to write

Map m = new HashMap();

or

HashMap m = new HashMap();

Comment on lines 230 to 253
// Workaround for scroll position not being included in browser history (8249133)
document.addEventListener("DOMContentLoaded", function(e) {
var contentDiv = document.querySelector("div.flex-content");
window.addEventListener("popstate", function(e) {
if (e.state !== null) {
contentDiv.scrollTop = e.state;
}
});
window.addEventListener("hashchange", function(e) {
history.replaceState(contentDiv.scrollTop, document.title);
});
var timeoutId;
contentDiv.addEventListener("scroll", function(e) {
if (timeoutId) {
clearTimeout(timeoutId);
}
timeoutId = setTimeout(function() {
history.replaceState(contentDiv.scrollTop, document.title);
}, 100);
});
if (!location.hash) {
history.replaceState(contentDiv.scrollTop, document.title);
}
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always nice to see workarounds removed!

@hns hns marked this pull request as ready for review October 5, 2023 14:38
@openjdk openjdk bot added the rfr Pull request is ready for review label Oct 5, 2023
@mlbridge
Copy link

mlbridge bot commented Oct 5, 2023

Webrevs

/**
* The class of a {@code div} element that allows its horizontal overflow to be scrolled.
*/
horizontalScroll,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quibble: scroll? or scrollable?

@@ -105,13 +105,13 @@ protected void buildMethodDoc(Content detailsList) {
for (Element method : methods) {
currentMethod = (ExecutableElement)method;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a separate RFE, it would be nice if getVisibleMembers took an optional Class<T> argument that is used to cast the result of the method to List<T extends Element> instead of just Element. This would enable to eliminate downstream casts, like here on line 106, to get the subtype.

See a similar technique in Utils for accessing sublists of a list of DocTree nodes,

Comment on lines +108 to +114
Content div = HtmlTree.DIV(HtmlStyle.horizontalScroll);
buildSignature(div);
buildDeprecationInfo(div);
buildPreviewInfo(div);
buildMethodComments(div);
buildTagInfo(div);
methodContent.add(div);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Purely as a matter of style, I like the style of building "bottom up", so first build all those items into a ContentBuilder, then make a div with HtmlTree.DIV(HtmlStyle.horizontalScroll, contentBuilder) then add the div into the methodContent, as on line 114.

@openjdk
Copy link

openjdk bot commented Oct 5, 2023

@hns This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8308659: Use CSS scroll-margin instead of flexbox layout in API documentation

Reviewed-by: jjg

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 186 new commits pushed to the master branch:

  • 668d4b0: 8318154: Improve stability of WheelModifier.java test
  • a36eaf0: 8317112: Add screenshot for Frame/DefaultSizeTest.java
  • a27fc7e: 8317994: Serial: Use SerialHeap in generation
  • 37eb986: 8154846: SwingNode does not resize when content size constraints are changed
  • 37aed6f: 8315362: NMT: summary diff reports threads count incorrectly
  • 1e930db: 8316585: [REDO] runtime/InvocationTests spend a lot of time on dependency verification
  • 0275efa: 8316304: (fs) Add support for BasicFileAttributes.creationTime() for Linux
  • 77d40ce: 8318085: ProblemList jdk/jfr/api/consumer/recordingstream/TestOnEvent.java on linux-aarch64
  • 4ea1b99: 8317262: LockStack::contains(oop) fails "assert(t->is_Java_thread()) failed: incorrect cast to JavaThread"
  • 01ea1ef: 8305971: NPE in JavacProcessingEnvironment for missing enum constructor body
  • ... and 176 more: https://git.openjdk.org/jdk/compare/cfcbfc6cae7d8fc276c5a54917e97adea7cf5621...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Oct 5, 2023
 - Move scroll-margin update from search.js to script.js
 - Use DOMContentLoaded event to update scroll-margin
 - Avoid use of :has() CSS pseudo-class which is unsupported on Firefox
@hns
Copy link
Member Author

hns commented Oct 16, 2023

I have updated this PR with a few additional fixes:

  • The callback to update the scroll-margin to accomodate for the draft header has moved from search.js to script.js which is always available (search.js is dependent on the index).
  • The callback is implemented in pure JavaScript instead of relying on jQuery, and uses the DOMContentLoaded event instead of the window load event, which avoids flickering on Chrome and Safari (still a little bit of flickering in Firefox under some conditions).
  • I removed the change to make tab buttons scrollable instead of adding line breaks as the :has() CSS pseudo-class is not supported on Firefox. This change was unrelated to the issue at hand.

Generated documentation can be viewed here: https://cr.openjdk.org/~hannesw/8308659/api.03/

@hns
Copy link
Member Author

hns commented Oct 16, 2023

/integrate

@openjdk
Copy link

openjdk bot commented Oct 16, 2023

Going to push as commit eb7d972.
Since your change was applied there have been 188 commits pushed to the master branch:

  • 7028fb9: 8317975: [JVMCI] assert(pointee != nullptr) failed: invariant
  • 36993ae: 8316918: Optimize conversions duplicated across phi nodes
  • 668d4b0: 8318154: Improve stability of WheelModifier.java test
  • a36eaf0: 8317112: Add screenshot for Frame/DefaultSizeTest.java
  • a27fc7e: 8317994: Serial: Use SerialHeap in generation
  • 37eb986: 8154846: SwingNode does not resize when content size constraints are changed
  • 37aed6f: 8315362: NMT: summary diff reports threads count incorrectly
  • 1e930db: 8316585: [REDO] runtime/InvocationTests spend a lot of time on dependency verification
  • 0275efa: 8316304: (fs) Add support for BasicFileAttributes.creationTime() for Linux
  • 77d40ce: 8318085: ProblemList jdk/jfr/api/consumer/recordingstream/TestOnEvent.java on linux-aarch64
  • ... and 178 more: https://git.openjdk.org/jdk/compare/cfcbfc6cae7d8fc276c5a54917e97adea7cf5621...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Oct 16, 2023
@openjdk openjdk bot closed this Oct 16, 2023
@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Oct 16, 2023
@openjdk openjdk bot removed the rfr Pull request is ready for review label Oct 16, 2023
@openjdk
Copy link

openjdk bot commented Oct 16, 2023

@hns Pushed as commit eb7d972.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integrated Pull request has been integrated javadoc javadoc-dev@openjdk.org
Development

Successfully merging this pull request may close these issues.

2 participants