Skip to content

Commit 8edeb96

Browse files
apply suggestions
1 parent d22aa30 commit 8edeb96

File tree

3 files changed

+190
-4
lines changed

3 files changed

+190
-4
lines changed

docs/rules/relative-font-units.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The `font-size` property in CSS defines the size of the text. It can be set usin
1010
1. Length units (e.g., `px`, `em`, `rem`, `pt`).
1111
1. Percentages (`%`, relative to the parent element's font size).
1212

13-
Generally, relative unit such as `rem` or `em` are preferred over absolute ones like `px`, `pt` because of following reasons:
13+
Generally, relative units such as `rem` or `em` are preferred over absolute ones like `px`, `pt` because of the following reasons:
1414

1515
- **Responsive Design** - Relative units adapt better to various screen widths and pixel densities (e.g., mobile vs. desktop).
1616
- **Accessibility** - Relative units allow text to scale when users adjust browser settings or zoom levels.
@@ -21,11 +21,11 @@ Generally, relative unit such as `rem` or `em` are preferred over absolute ones
2121

2222
This rule enforces the use of relative units for font size.
2323

24-
### Options
24+
## Options
2525

26-
This rule accepts an option which is an object with following property:
26+
This rule accepts an option which is an object with the following property:
2727

28-
- `allow` (default: `["rem"]`) - Specify an array of relative units that are allowed to be used. You can use following units:
28+
- `allow` (default: `["rem"]`) - Specify an array of relative units that are allowed to be used. You can use the following units:
2929

3030
- **%**: Represents the "percentage" of the parent element’s font size, allowing the text to scale relative to its container.
3131
- **cap**: Represents the "cap height" (nominal height of capital letters) of the element's font.
@@ -44,6 +44,8 @@ This rule accepts an option which is an object with following property:
4444
Example of **incorrect** code for default `{ allow: ["rem"] }` option:
4545

4646
```css
47+
/* eslint css/relative-font-units: ["error", { allow: ["rem"] }] */
48+
4749
a {
4850
font-size: 10px;
4951
}
@@ -60,6 +62,8 @@ c {
6062
Example of **correct** code for default `{ allow: ["rem"] }` option:
6163

6264
```css
65+
/* eslint css/relative-font-units: ["error", { allow: ["rem"] }] */
66+
6367
a {
6468
font-size: 2rem;
6569
}
@@ -79,6 +83,8 @@ Font size can also be specified in `font` property:
7983
Example of **correct** code for `{ allow: ["em", "%"] }` option:
8084

8185
```css
86+
/* eslint css/relative-font-units: ["error", { allow: ["em", "%"] }] */
87+
8288
a {
8389
font-size: 2em;
8490
}

src/rules/relative-font-units.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,15 @@ const fontSizeIdentifiers = new Set([
4242
"large",
4343
"x-large",
4444
"xx-large",
45+
"xxx-large",
4546
"smaller",
4647
"larger",
48+
"math",
49+
"inherit",
50+
"initial",
51+
"revert",
52+
"revert-layer",
53+
"unset",
4754
]);
4855

4956
//-----------------------------------------------------------------------------

tests/rules/relative-font-units.test.js

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,18 @@ ruleTester.run("relative-font-units", rule, {
194194
},
195195
],
196196
},
197+
{
198+
code: dedent`
199+
a {
200+
font: italic small-caps bold condensed 18rem/1.6px "Georgia", serif;
201+
}
202+
`,
203+
options: [
204+
{
205+
allow: ["rem", "em"],
206+
},
207+
],
208+
},
197209
],
198210
invalid: [
199211
{
@@ -544,6 +556,22 @@ ruleTester.run("relative-font-units", rule, {
544556
},
545557
],
546558
},
559+
{
560+
code: "a { font-size: xxx-large; }",
561+
errors: [
562+
{
563+
messageId: "allowedFontUnits",
564+
},
565+
],
566+
},
567+
{
568+
code: "a { font: xxx-large Arial, sans-serif; }",
569+
errors: [
570+
{
571+
messageId: "allowedFontUnits",
572+
},
573+
],
574+
},
547575
{
548576
code: "a { font-size: smaller; }",
549577
errors: [
@@ -576,6 +604,102 @@ ruleTester.run("relative-font-units", rule, {
576604
},
577605
],
578606
},
607+
{
608+
code: "a { font-size: math; }",
609+
errors: [
610+
{
611+
messageId: "allowedFontUnits",
612+
},
613+
],
614+
},
615+
{
616+
code: "a { font: math Arial, sans-serif; }",
617+
errors: [
618+
{
619+
messageId: "allowedFontUnits",
620+
},
621+
],
622+
},
623+
{
624+
code: "a { font-size: inherit; }",
625+
errors: [
626+
{
627+
messageId: "allowedFontUnits",
628+
},
629+
],
630+
},
631+
{
632+
code: "a { font: inherit Arial, sans-serif; }",
633+
errors: [
634+
{
635+
messageId: "allowedFontUnits",
636+
},
637+
],
638+
},
639+
{
640+
code: "a { font-size: initial; }",
641+
errors: [
642+
{
643+
messageId: "allowedFontUnits",
644+
},
645+
],
646+
},
647+
{
648+
code: "a { font: initial Arial, sans-serif; }",
649+
errors: [
650+
{
651+
messageId: "allowedFontUnits",
652+
},
653+
],
654+
},
655+
{
656+
code: "a { font-size: revert; }",
657+
errors: [
658+
{
659+
messageId: "allowedFontUnits",
660+
},
661+
],
662+
},
663+
{
664+
code: "a { font: revert Arial, sans-serif; }",
665+
errors: [
666+
{
667+
messageId: "allowedFontUnits",
668+
},
669+
],
670+
},
671+
{
672+
code: "a { font-size: revert-layer; }",
673+
errors: [
674+
{
675+
messageId: "allowedFontUnits",
676+
},
677+
],
678+
},
679+
{
680+
code: "a { font: revert-layer Arial, sans-serif; }",
681+
errors: [
682+
{
683+
messageId: "allowedFontUnits",
684+
},
685+
],
686+
},
687+
{
688+
code: "a { font-size: unset; }",
689+
errors: [
690+
{
691+
messageId: "allowedFontUnits",
692+
},
693+
],
694+
},
695+
{
696+
code: "a { font: unset Arial, sans-serif; }",
697+
errors: [
698+
{
699+
messageId: "allowedFontUnits",
700+
},
701+
],
702+
},
579703
{
580704
code: dedent`
581705
a {
@@ -719,5 +843,54 @@ ruleTester.run("relative-font-units", rule, {
719843
},
720844
],
721845
},
846+
{
847+
code: dedent`
848+
a {
849+
font: italic bold 1.2em/2 "Helvetica", sans-serif;
850+
}
851+
`,
852+
errors: [
853+
{
854+
messageId: "allowedFontUnits",
855+
},
856+
],
857+
},
858+
{
859+
code: dedent`
860+
a {
861+
font: ultra-condensed small-caps 1.2em "Fira Sans", sans-serif;
862+
}
863+
`,
864+
errors: [
865+
{
866+
messageId: "allowedFontUnits",
867+
},
868+
],
869+
},
870+
{
871+
code: dedent`
872+
a {
873+
font: italic small-caps 700 condensed 16px/1.5 "Helvetica Neue", sans-serif;
874+
}
875+
`,
876+
errors: [
877+
{
878+
messageId: "allowedFontUnits",
879+
},
880+
],
881+
},
882+
{
883+
code: dedent`
884+
a {
885+
font: caption;
886+
font-size: 20px;
887+
}
888+
`,
889+
errors: [
890+
{
891+
messageId: "allowedFontUnits",
892+
},
893+
],
894+
},
722895
],
723896
});

0 commit comments

Comments
 (0)