Skip to content

Commit

Permalink
feat: add line breaks in class attr accordingly (close #75)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Nov 5, 2024
1 parent d25b42b commit 8d38b5d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
21 changes: 20 additions & 1 deletion markup_fmt/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,26 @@ impl<'s> DocGen<'s> for NativeAttribute<'s> {
docs.push(Doc::text("="));
docs.push(quote.clone());
if self.name.eq_ignore_ascii_case("class") {
docs.push(Doc::text(value.split_ascii_whitespace().join(" ")));
let value = value.trim();
let maybe_line_break = if value.contains('\n') {
Doc::hard_line()
} else {
Doc::nil()
};
docs.push(
maybe_line_break
.clone()
.concat(itertools::intersperse(
value
.trim()
.lines()
.filter(|line| !line.is_empty())
.map(|line| Doc::text(line.split_ascii_whitespace().join(" "))),
Doc::hard_line(),
))
.nest_with_ctx(ctx),
);
docs.push(maybe_line_break);
} else if self.name.eq_ignore_ascii_case("style") {
docs.push(Doc::text(ctx.format_style_attr(&value, value_start)));
} else {
Expand Down
9 changes: 8 additions & 1 deletion markup_fmt/tests/fmt/html/attributes/attributes.snap
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,11 @@ and HTML5 Apps. It also documents Mozilla products, like Firefox OS."
<X a="1"> </X>
<X a="1" b="2"> </X>
<X a="1" b="2" c="3"> </X>
<p class="foo bar baz"></p>
<p
class="
foo
bar
baz
"
>
</p>
8 changes: 7 additions & 1 deletion markup_fmt/tests/fmt/html/attributes/class-bem1.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
source: markup_fmt/tests/fmt.rs
---
<div
class="ProviderMeasuresContainer__heading-row d-flex flex-column flex-lg-row justify-content-start justify-content-lg-between align-items-start align-items-lg-center"
class="
ProviderMeasuresContainer__heading-row
d-flex
flex-column flex-lg-row
justify-content-start justify-content-lg-between
align-items-start align-items-lg-center
"
>
Foo
</div>
Expand Down
42 changes: 38 additions & 4 deletions markup_fmt/tests/fmt/html/attributes/class-names.snap
Original file line number Diff line number Diff line change
@@ -1,26 +1,60 @@
---
source: markup_fmt/tests/fmt.rs
---
<img class="foo bar">
<img
class="
foo
bar
"
>

<img class="">
<img class>

<img
class="looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong a-long-long-long-long-long-class-name another-long-long-long-class-name foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar"
class="
looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
a-long-long-long-long-long-class-name
another-long-long-long-class-name
foo bar
foo bar
foo bar
foo bar
foo bar
foo bar
foo bar
foo bar
foo bar
foo bar
foo bar
foo bar
foo bar
"
>

<img class="{{ ...classes }}">
<img class="foo bar {{ otherClass }}">

<!-- escaped -->
<!-- from: https://developer.mozilla.org/en-US/docs/Web/API/CSS/escape#Basic_results -->
<img class="\.foo\#bar \(\)\[\]\{\} --a \30 \ufffd">
<img
class="
\.foo\#bar
\(\)\[\]\{\}
--a
\30
\ufffd
"
>

<!-- from yahoo website -->
<div
id="header-wrapper"
class="Bgc(#fff) Bdbc(t) Bdbs(s) Bdbw(1px) D(tb) Pos(f) Tbl(f) W(100%) Z(4) has-scrolled_Bdc($c-fuji-grey-d) Scrolling_Bdc($c-fuji-grey-d) has-scrolled_Bxsh($headerShadow) Scrolling_Bxsh($headerShadow)"
class="
Bgc(#fff) Bdbc(t) Bdbs(s) Bdbw(1px) D(tb) Pos(f) Tbl(f) W(100%) Z(4)
has-scrolled_Bdc($c-fuji-grey-d) Scrolling_Bdc($c-fuji-grey-d) has-scrolled_Bxsh($headerShadow)
Scrolling_Bxsh($headerShadow)
"
>
<div
class="Bgc(#fff) M(a) Maw(1301px) Miw(1000px) Pb(12px) Pt(22px) Pos(r) TranslateZ(0) Z(6)"
Expand Down

0 comments on commit 8d38b5d

Please sign in to comment.