Skip to main content

High-performance library for inlining CSS into HTML 'style' attributes

Project description

css_inline

build status pypi versions license codecov.io gitter

css-inline inlines CSS into HTML documents, using components from Mozilla's Servo project.

This process is essential for sending HTML emails as you need to use "style" attributes instead of "style" tags.

For instance, the library transforms HTML like this:

<html>
    <head>
        <title>Test</title>
        <style>h1 { color:blue; }</style>
    </head>
    <body>
        <h1>Big Text</h1>
    </body>
</html>

into:

<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <h1 style="color:blue;">Big Text</h1>
    </body>
</html>
  • Uses reliable components from Mozilla's Servo
  • 10-300x faster than alternatives
  • Inlines CSS from style and link tags
  • Removes style and link tags
  • Resolves external stylesheets (including local files)
  • Can process multiple documents in parallel
  • Works on Linux, Windows, and macOS
  • Supports HTML5 & CSS3

Installation

Install with pip:

pip install css_inline

Pre-compiled wheels for most popular platforms are provided. If your platform is not supported, you will need a Rust compiler to build this package from source. The minimum supported Rust version is 1.60.

Usage

import css_inline

HTML = """<html>
<head>
    <title>Test</title>
    <style>h1 { color:blue; }</style>
</head>
<body>
    <h1>Big Text</h1>
</body>
</html>"""

inlined = css_inline.inline(HTML)
# HTML becomes this:
#
# <html>
# <head>
#    <title>Test</title>
#    <style>h1 { color:blue; }</style>
# </head>
# <body>
#     <h1 style="color:blue;">Big Text</h1>
# </body>
# </html>

If you want to inline many HTML documents, you can utilize inline_many that processes the input in parallel.

import css_inline

css_inline.inline_many(["<...>", "<...>"])

inline_many will spawn threads on the Rust level; thus, you can expect it's running faster than css_inline.inline via Python's multiprocessing or threading modules.

Configuration

For configuration options use the CSSInliner class:

import css_inline

inliner = css_inline.CSSInliner(keep_style_tags=True)
inliner.inline("...")
  • keep_style_tags. Specifies whether to keep "style" tags after inlining. Default: False
  • keep_link_tags. Specifies whether to keep "link" tags after inlining. Default: False
  • base_url. The base URL used to resolve relative URLs. If you'd like to load stylesheets from your filesystem, use the file:// scheme. Default: None
  • load_remote_stylesheets. Specifies whether remote stylesheets should be loaded. Default: True
  • extra_css. Extra CSS to be inlined. Default: None
  • preallocate_node_capacity. Advanced. Preallocates capacity for HTML nodes during parsing. This can improve performance when you have an estimate of the number of nodes in your HTML document. Default: 32

You can also skip CSS inlining for an HTML tag by adding the data-css-inline="ignore" attribute to it:

<head>
    <title>Test</title>
    <style>h1 { color:blue; }</style>
</head>
<body>
    <!-- The tag below won't receive additional styles -->
    <h1 data-css-inline="ignore">Big Text</h1>
</body>

The data-css-inline="ignore" attribute also allows you to skip link and style tags:

<head>
    <title>Test</title>
    <!-- Styles below are ignored -->
    <style data-css-inline="ignore">h1 { color:blue; }</style>
</head>
<body>
    <h1>Big Text</h1>
</body>

If you'd like to load stylesheets from your filesystem, use the file:// scheme:

import css_inline

# styles/email is relative to the current directory
inliner = css_inline.CSSInliner(base_url="file://styles/email/")
inliner.inline("...")

Standards support & restrictions

css-inline is built on top of html5ever and cssparser and relies on their behavior for HTML & CSS parsing.

  • Only HTML 5 is supported, not XHTML.
  • Only CSS 3 is supported.
  • Only UTF-8 encoding for string representation. Other document encodings are not yet supported.

If you'd like to work around some XHTML compatibility issues like closing empty tags (<hr> vs. <hr/>), you can use the following snippet that involves lxml:

import css_inline
from lxml import html, etree

document = "..."  # Your HTML document
inlined = css_inline.inline(document)
tree = html.fromstring(inlined)
inlined = etree.tostring(tree).decode(encoding="utf-8")

Performance

Due to the usage of efficient tooling from Mozilla's Servo project (html5ever, rust-cssparser and others) this library has excellent performance characteristics. In comparison with other Python projects, it is usually >10x faster than the nearest alternative.

For inlining CSS in the html document from the Usage section above there is the following breakdown in the benchmarks:

  • css_inline 0.9.0 - 9.72 us
  • premailer 3.10.0 - 207.95 us (x21.39)
  • toronado 0.1.0 - 1.03 ms (x106.86)
  • inlinestyler 0.2.5 - 1.69 ms (x174.64)
  • pynliner 0.8.0 - 2.05 ms (x211.89)

Realistic email 1:

  • css_inline 0.9.0 - 262.33 us
  • premailer 3.10.0 - 2.11 ms (x8.08)
  • toronado 0.1.0 - 25.51 ms (x97.28)
  • inlinestyler 0.2.5 - 4313 ms (x164.43)
  • pynliner 0.8.0 - 72.55 ms (x276.57)

Realistic email 2:

  • css_inline 0.9.0 - 151.83 us
  • premailer 3.10.0 - 4.12 ms (x27.18)
  • toronado 0.1.0 - Error: Pseudo-elements are not supported
  • inlinestyler 0.2.5 - 28.85 ms (x190.03)
  • pynliner 0.8.0 - Error: No match was found

You can take a look at the benchmarks' code at benches/bench.py file. The results above were measured with stable rustc 1.70.0 on Python 3.11.0.

Comparison with other libraries

Besides performance, css-inline differs from other Python libraries for CSS inlining.

  • Generally supports more CSS features than other libraries (for example, toronado and pynliner do not support pseudo-elements);
  • It has fewer configuration options and not as flexible as premailer;
  • Works on fewer platforms than LXML-based libraries (premailer, inlinestyler, toronado, and optionally pynliner);
  • Does not have debug logs yet;
  • Supports only HTML 5.

Python support

css_inline supports CPython 3.7, 3.8, 3.9, 3.10, 3.11 and PyPy 3.7, 3.8, 3.9.

Extra materials

If you want to know how this library was created & how it works internally, you could take a look at these articles:

License

This project is licensed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

css_inline-0.10.0.tar.gz (43.1 kB view hashes)

Uploaded Source

Built Distributions

css_inline-0.10.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

css_inline-0.10.0-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.3 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

css_inline-0.10.0-pp39-pypy39_pp73-macosx_10_7_x86_64.whl (1.3 MB view hashes)

Uploaded PyPy macOS 10.7+ x86-64

css_inline-0.10.0-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

css_inline-0.10.0-pp38-pypy38_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.3 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

css_inline-0.10.0-pp38-pypy38_pp73-macosx_10_7_x86_64.whl (1.3 MB view hashes)

Uploaded PyPy macOS 10.7+ x86-64

css_inline-0.10.0-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

css_inline-0.10.0-pp37-pypy37_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.3 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

css_inline-0.10.0-pp37-pypy37_pp73-macosx_10_7_x86_64.whl (1.3 MB view hashes)

Uploaded PyPy macOS 10.7+ x86-64

css_inline-0.10.0-cp37-abi3-win_amd64.whl (1.3 MB view hashes)

Uploaded CPython 3.7+ Windows x86-64

css_inline-0.10.0-cp37-abi3-win32.whl (1.2 MB view hashes)

Uploaded CPython 3.7+ Windows x86

css_inline-0.10.0-cp37-abi3-musllinux_1_2_x86_64.whl (1.5 MB view hashes)

Uploaded CPython 3.7+ musllinux: musl 1.2+ x86-64

css_inline-0.10.0-cp37-abi3-musllinux_1_2_armv7l.whl (1.4 MB view hashes)

Uploaded CPython 3.7+ musllinux: musl 1.2+ ARMv7l

css_inline-0.10.0-cp37-abi3-musllinux_1_2_aarch64.whl (1.5 MB view hashes)

Uploaded CPython 3.7+ musllinux: musl 1.2+ ARM64

css_inline-0.10.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ x86-64

css_inline-0.10.0-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (1.1 MB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ARMv7l

css_inline-0.10.0-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.3 MB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ARM64

css_inline-0.10.0-cp37-abi3-manylinux_2_12_i686.manylinux2010_i686.whl (1.3 MB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.12+ i686

css_inline-0.10.0-cp37-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (2.5 MB view hashes)

Uploaded CPython 3.7+ macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

css_inline-0.10.0-cp37-abi3-macosx_10_7_x86_64.whl (1.3 MB view hashes)

Uploaded CPython 3.7+ macOS 10.7+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page