Skip to content

Improve styling of <span class="stab"> tags in the documentation's main body #101874

Closed
@FedericoStra

Description

@FedericoStra

The Problem

If one tries to put feature labels in the main body of the documentation (what goes inside <div class="docblock">) they get styled differently from the one automatically inserted by rustdoc running with #![feature(doc_auto_cfg)]. In particular the layout is bad and can cause the appearance of scrollbars on the right hand side of each involved paragraph.

Here is an example picture:

screenshot bad

The desirable look would of course be

screenshot good

Code to generate the example documentation above.

Cargo.toml:

[package]
name = "doc-example"
version = "0.1.0"
edition = "2021"

[features]
default = ["some-feature"]
some-feature = []

src/lib.rs:

/*! Cool library.

<!-- REMOVE TO PRODUCE THE BAD VERSION -->
<style>
.docblock .stab {
    display: inline-block;
    margin-bottom: 0;
    padding: 2px;
    line-height: 1.2;
    border-radius: 3px;
}
</style>

Enable the feature <span class="stab portability"><code>some-feature</code></span> to enjoy this crate even more!
Enable the feature <span class="stab portability"><code>some-feature</code></span> to enjoy this crate even more!
Enable the feature <span class="stab portability"><code>some-feature</code></span> to enjoy this crate even more!

Also, stop using [`bar`] as it's <span class="stab deprecated" title="">deprecated</span>.
Also, stop using [`bar`] as it's <span class="stab deprecated" title="">deprecated</span>.
Also, stop using [`bar`] as it's <span class="stab deprecated" title="">deprecated</span>.

Finally, you can use [`quz`] only on <span class="stab portability"><code>Unix or x86-64</code></span>.
Finally, you can use [`quz`] only on <span class="stab portability"><code>Unix or x86-64</code></span>.
Finally, you can use [`quz`] only on <span class="stab portability"><code>Unix or x86-64</code></span>.
*/

#![feature(doc_auto_cfg)]

/// Does fooish things.
#[cfg(feature = "some-feature")]
pub fn foo() {}

/// Does barish things.
#[deprecated(since = "0.1.0", note = "we don't need this anymore")]
pub fn bar() {}

/// Does quzish things.
#[cfg(any(unix, target_arch = "x86_64"))]
pub fn quz() {}

The Cause

The reason is that rustdoc.css has a special rule for .module-item .stab, .import-item .stab, whereas it does not address .stab tags appearing as descendant of .docblock tags.

The Fix

From a crate developer's point of view, the fix for this is simple enough: just inject the following CSS

.docblock .stab {
    display: inline-block;
    margin-bottom: 0;
    padding: 2px;
    line-height: 1.2;
    border-radius: 3px;
}

so that they are rendered the same way as the ones generated by rustdoc's doc_auto_cfg labels.

The Request

Is there any chance that this CSS gets included in the official rustdoc.css style?

The fix would probably consist in adding .docblock .stab to

.module-item .stab,
.import-item .stab {
border-radius: 3px;
display: inline-block;
font-size: 0.875rem;
line-height: 1.2;
margin-bottom: 0;
margin-left: 0.3125em;
padding: 2px;
vertical-align: text-bottom;
}

except that we don't want the left margin, so probably a dedicated rule is more appropriate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-rustdoc-uiArea: Rustdoc UI (generated HTML)C-bugCategory: This is a bug.T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions