Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

posthtml-safe-class-names

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

posthtml-safe-class-names

Replace escaped characters in HTML class names and CSS selectors.

  • 2.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
9.1K
increased by4.04%
Maintainers
1
Weekly downloads
 
Created
Source

Safe Class Names

Replace escaped characters in class names and CSS selectors

Version License Build Downloads

Introduction

This plugin replaces escaped characters in class names from your <style> tags and inside class="" attributes with safe characters, that do not need escaping.

By default, it replaces:

  • \: and \/ with -
  • \% with pc
  • \. with _

See the full list of replacements.

But... why?

So you can use those cool Tailwind CSS selectors in HTML emails. 😎

Escaped characters in CSS selectors or HTML class names are not supported by all email clients (currently Gmail being the biggest one), so you can use this plugin to replace them with safe alternatives.

Install

$ npm i posthtml posthtml-safe-class-names

Usage

Consider example.html:

<!DOCTYPE html>
<html>
<head>
  <style>
    .w-3\/5 {
      width: 60%;
    }
    
    /* JIT arbitrary values are also supported */
    .bg-\[\#1da1f1\] {
      background-color: #1da1f1;
    }

    @media (max-width: 600px) {
      .sm\:w-1\/2 {
        width: 50%;
      }
    }
  </style>
</head>
<body>
  <div class="w-3/5 sm:w-1/2 bg-[#1da1f1]">Lorem ipsum</div>
</body>
</html>
const fs = require('fs')
const posthtml = require('posthtml')
const safeClassNames = require('posthtml-safe-class-names')

const source = fs.readFileSync('./example.html')

posthtml([
    safeClassNames()
  ])
  .process(source)
  .then(result => fs.writeFileSync('./after.html', result.html))

Result:

<!DOCTYPE html>
<html>
<head>
  <style>
    .sm-w-3-5 {
      width: 60%;
    }

    .bg-_1da1f1 {
      background-color: #1da1f1;
    }

    @media (max-width: 600px) {
      .sm-w-1-2 {
        width: 50%;
      }
    }
  </style>
</head>
<body>
  <div class="w-3-5 sm-w-1-2 bg-_1da1f1">Lorem ipsum</div>
</body>
</html>

Options

replacements

The plugin accepts an options object where you can define character replacement mappings:

{
  ':': '-',
  '\/': '-',
  '%': 'pc',
  '.': '_',
  // ...
}

See the full list of replacements.

Besides adding new mappings, you can of course override the default ones.

Using the same example.html, let's choose to replace \: in our class names with __ instead of -:

posthtml([
    safeClassNames({
      replacements: {
        ':': '__',
      }
    })
  ])
  .process(source)
  .then(result => fs.writeFileSync('./after.html', result.html))

Result:

<!DOCTYPE html>
<html>
<head>
  <style>
    .sm__w-3-5 {
      width: 60%;
    }

    .bg-_1da1f1 {
      background-color: #1da1f1;
    }

    @media (max-width: 600px) {
      .sm__w-1-2 {
        width: 50%;
      }
    }
  </style>
</head>
<body>
  <div class="w-3-5 sm__w-1-2 bg-_1da1f1">Lorem ipsum</div>
</body>
</html>

Keywords

FAQs

Package last updated on 15 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc