Skip to content

Core\Mail\Preview::unusedCss

$preview->unusedCss(): array<string>

unusedCss() returns the list of kept CSS rules that match no element of the mail's HTML. You call unusedCss() on the result of Core\Mail::preview.

A rule matches nothing for one of two reasons. Its selector has a typo, such as .buton for the class button. Or it is for an attribute that a mail program adds later. For example, Outlook adds data-ogsc to the elements in dark mode. Such a rule stays in the <style> element. It still works in that mail program.

The list has only rules that could match an element. An at-rule such as @media, and a rule with a : outside square brackets, such as a:hover, are never in it. keptCss() returns them.

The result has one string for each rule, in the order of the HTML. Each rule in it is also in keptCss().

Use it in a test of a mail template. An empty list means that each rule matched an element.

The examples below show a rule with a typo, a rule for an attribute that Outlook adds, and a test of a template.

Return value

Returns array<string>. One string for each rule, in the order of the HTML. Each is also in keptCss().

Changelog

VersionChange
0.0.6Core\Mail::preview builds a mail body as send would and sends nothing. It returns the HTML with its CSS inlined, the plain text, and the CSS rules that stayed in <style>.

Examples

A rule with a typo

<?nvs
// Shows how `unusedCss()` finds a CSS rule that matches nothing because of a typo.
// The rule is for `.buton`, but the link has the class `button`.
Core\Mail\Preview $mail = Core\Mail::preview(html`<style>
.buton { background: #0a5; color: white }
</style>
<p><a class="button" href="https://example.com/confirm">Confirm your address</a></p>`);
// This prints the `.buton` rule.
foreach ($mail->unusedCss() as string $rule) {
echo 'unused: ', $rule, "\n";
}
Output
unused: .buton { background: #0a5; color: white; }

A rule for a later attribute

<?nvs
// Shows a rule that matches nothing now, but works in a mail program that adds an attribute later.
// Outlook adds the `data-ogsc` attribute when it shows a mail in dark mode.
// No element has it yet, so the rule is in `unusedCss()`. It stays in the `<style>` element.
Core\Mail\Preview $mail = Core\Mail::preview(html`<style>
.logo { color: #111 }
[data-ogsc] .logo { color: #eee }
</style>
<p class="logo">Example Shop</p>`);
foreach ($mail->unusedCss() as string $rule) {
echo 'unused: ', $rule, "\n";
}
foreach ($mail->keptCss() as string $rule) {
echo 'kept: ', $rule, "\n";
}
Output
unused: [data-ogsc] .logo { color: #eee; }
kept: [data-ogsc] .logo { color: #eee; }

Test that every rule is used

<?nvs
// Shows how to test that every CSS rule of a mail template matches an element.
// proof: test
use Core\Html;
use Core\Html\Markup;
use Core\Test;
class Receipt
{
// Builds the body of a receipt. `Html::escape` escapes the total before it is added.
public static function body(string $total): Markup
{
return html`<style>
.total { font-weight: bold }
.note { color: #888 }
</style>
<p class="total">Total: ` + Html::escape($total) + html`</p>
<p class="note">Thank you for your order.</p>`;
}
}
class ReceiptTest
{
#[Test]
public function everyRuleIsUsed(): void
{
// An empty list means that no rule has a typo in its selector.
Test::assertCount(Core\Mail::preview(Receipt::body('59.90 EUR'))->unusedCss(), 0);
}
}
Output
ReceiptTest
✓ everyRuleIsUsed
0 failed, 1 passed, 0 skipped, 0 flaky