IgniterCss.Parsers.Parser (igniter_css v0.1.0)

View Source

CSS parsing and manipulation using Python's tinycss2 library.

This module provides functions to work with CSS files by leveraging a Python toolkit built on tinycss2 for parsing, modifying, and analyzing CSS.

Please note that the use of Python in Elixir will remain experimental for now, as we continue to improve it over time and decide whether to adopt it fully.

Summary

Functions

Adds a display: none property to the .hide-scrollbar class. If the class doesn't exist, it creates it.

Adds an @import rule to the CSS if it doesn't already exist.

Adds vendor prefixes to specified CSS properties throughout the stylesheet.

Analyzes a CSS stylesheet and returns various statistics.

Beautifies a CSS stylesheet by adding proper indentation and formatting. We recommend using IgniterCss.Parsers.CSS.Formatter module instead.

Extracts all CSS animations and keyframes.

Extracts all color values from a CSS stylesheet.

Extracts all media queries and their contents.

Gets the CSS properties for a specific selector if it exists, or returns nil.

Merges multiple CSS stylesheets into one, removing duplicates.

Minifies a CSS stylesheet by removing comments, whitespace, and unnecessary characters. We recommend not using this.

Removes duplicate selectors and properties from CSS.

Removes a specific @import rule from the CSS.

Removes a CSS selector and all its properties. Note: If a block is empty after removal, it will be removed as well.

Replaces an entire CSS rule for a specific selector with new declarations.

Checks if a specific CSS selector exists in the stylesheet.

Sorts CSS properties alphabetically within each rule.

Checks if the CSS code is valid by attempting to parse it. Returns :ok if valid, or {:error, reason} if invalid.

Functions

add_hide_scrollbar_property(file_path_or_content, type \\ :content)

Adds a display: none property to the .hide-scrollbar class. If the class doesn't exist, it creates it.

Examples

iex> IgniterCss.Parsers.CSS.Parser.add_hide_scrollbar_property(css_code)
updated css with .hide-scrollbar having display: none

add_import(file_path_or_content, import_url, media_query, type \\ :content)

Adds an @import rule to the CSS if it doesn't already exist.

Parameters

  • file_path_or_content - The CSS code as a string or file path
  • import_url - The URL or path to import (without quotes)
  • media_query - Optional media query to apply to the import (e.g., "screen and (max-width: 768px)")
                or boolean false to indicate no media query
  • type - :content or :path to specify if the first parameter is file content or a path

Examples

iex> IgniterCss.Parsers.CSS.Parser.add_import(css_code, "styles.css", false)
{:ok, :add_import, "css with @import 'styles.css'; added"}

iex> IgniterCss.Parsers.CSS.Parser.add_import(css_code, "mobile.css", "screen and (max-width: 768px)")
{:ok, :add_import, "css with @import 'mobile.css' screen and (max-width: 768px); added"}

add_vendor_prefixes(file_path_or_content, property_name, prefixes, type \\ :content)

Adds vendor prefixes to specified CSS properties throughout the stylesheet.

Parameters

  • css_code - The CSS code as a string
  • property_name - The CSS property to add prefixes to
  • prefixes - List of prefixes to add (e.g., ["-webkit-", "-moz-"])

Examples

iex> prefixes = ["-webkit-", "-moz-", "-ms-"]
iex> IgniterCss.Parsers.CSS.Parser.add_vendor_prefixes(css_code, "user-select", prefixes)
"updated css with vendor prefixes"

analyze_css(file_path_or_content, type \\ :content)

Analyzes a CSS stylesheet and returns various statistics.

Examples

iex> IgniterCss.Parsers.CSS.Parser.analyze_css(css_code)
%{
  "selectors_count" => 15,
  "unique_selectors" => 12,
  "properties_count" => 45,
  "unique_properties" => 20,
  ...
}

beautify(file_path_or_content, type \\ :content)

Beautifies a CSS stylesheet by adding proper indentation and formatting. We recommend using IgniterCss.Parsers.CSS.Formatter module instead.

Examples

iex> IgniterCss.Parsers.CSS.Parser.beautify(css_code)
".header {
    color: #333;
    background: #fff;
}

.footer {
    color: #000;
}"

extract_animations(file_path_or_content, type \\ :content)

Extracts all CSS animations and keyframes.

Examples

iex> IgniterCss.Parsers.CSS.Parser.extract_animations(css_code)
%{
  "fade-in" => %{
    "keyframes" => %{
      "0%" => %{"opacity" => "0"},
      "100%" => %{"opacity" => "1"}
    },
    "used_by" => [".header", ".modal"]
  }
}

extract_colors(file_path_or_content, type \\ :content)

Extracts all color values from a CSS stylesheet.

Examples

iex> IgniterCss.Parsers.CSS.Parser.extract_colors(css_code)
%{
  ".header" => ["color: #333", "background-color: white"],
  ".footer" => ["color: rgba(0, 0, 0, 0.8)"]
}

extract_media_queries(file_path_or_content, type \\ :content)

Extracts all media queries and their contents.

Examples

iex> IgniterCss.Parsers.CSS.Parser.extract_media_queries(css_code)
%{
  "(max-width: 768px)" => [
    %{
      "selector" => ".header",
      "properties" => %{"font-size" => "14px"}
    }
  ]
}

get_selector_properties(file_path_or_content, selector, type \\ :content)

Gets the CSS properties for a specific selector if it exists, or returns nil.

Parameters

  • css_code - The CSS code as a string
  • selector - The CSS selector to check for

Examples

iex> IgniterCss.Parsers.CSS.Parser.get_selector_properties(css_code, ".header")
%{"color" => "blue", "font-size" => "16px"}

iex> IgniterCss.Parsers.CSS.Parser.get_selector_properties(css_code, "#nonexistent")
nil

merge_stylesheets(css_list)

Merges multiple CSS stylesheets into one, removing duplicates.

Examples

iex> IgniterCss.Parsers.CSS.Parser.merge_stylesheets([css_code1, css_code2])
"merged css"

minify(file_path_or_content, type \\ :content)

Minifies a CSS stylesheet by removing comments, whitespace, and unnecessary characters. We recommend not using this.

Examples

iex> IgniterCss.Parsers.CSS.Parser.minify(css_code)
".header{color:#333;background:#fff;}.footer{color:#000;}"

modify_property(file_path_or_content, selector, property_name, new_value, important, type \\ :content)

Modifies a property value for a specific selector.

Parameters

  • css_code - The CSS code as a string
  • selector - The CSS selector to modify
  • property_name - The property name to modify
  • new_value - The new property value
  • important - Whether to mark the property as !important (default: false)

Examples

iex> IgniterCss.Parsers.CSS.Parser.modify_property(css_code, ".header", "color", "blue")
"updated css with .header color: blue"

remove_duplicates(file_path_or_content, type \\ :content)

Removes duplicate selectors and properties from CSS.

Examples

iex> IgniterCss.Parsers.CSS.Parser.remove_duplicates(css_code)
"css without duplicates"

remove_import(file_path_or_content, import_url, type \\ :content)

Removes a specific @import rule from the CSS.

Parameters

  • css_code - The CSS code as a string
  • import_url - The URL or path to remove (matches partial URL)

Examples

iex> IgniterCss.Parsers.CSS.Parser.remove_import(css_code, "styles.css")
"css with @import url('styles.css') removed"

remove_selector(file_path_or_content, selector, type \\ :content)

Removes a CSS selector and all its properties. Note: If a block is empty after removal, it will be removed as well.

Examples

iex> IgniterCss.Parsers.CSS.Parser.remove_selector(css_code, ".unused-class")
"css without .unused-class"

replace_selector_rule(file_path_or_content, selector, new_declarations, type \\ :content)

Replaces an entire CSS rule for a specific selector with new declarations.

Parameters

  • css_code - The CSS code as a string
  • selector - The CSS selector to replace
  • new_declarations - The new CSS declarations as a string (without curly braces)

Examples

iex> IgniterCss.Parsers.CSS.Parser.replace_selector_rule(css_code, ".header", "color: blue; font-size: 20px; padding: 10px;")
"css with .header rule replaced"

selector_exists?(file_path_or_content, selector, type \\ :content)

Checks if a specific CSS selector exists in the stylesheet.

Parameters

  • css_code - The CSS code as a string
  • selector - The CSS selector to check for

Examples

iex> IgniterCss.Parsers.CSS.Parser.selector_exists?(css_code, ".header")
true

iex> IgniterCss.Parsers.CSS.Parser.selector_exists?(css_code, "#nonexistent")
false

sort_properties(file_path_or_content, type \\ :content)

Sorts CSS properties alphabetically within each rule.

Examples

iex> IgniterCss.Parsers.CSS.Parser.sort_properties(css_code)
".header {
    background: #fff;
    color: #333;
    font-size: 16px;
}"

validate_css(file_path_or_content, type \\ :content)

Checks if the CSS code is valid by attempting to parse it. Returns :ok if valid, or {:error, reason} if invalid.

Examples

iex> IgniterCss.Parsers.CSS.Parser.validate_css(css_code)
:ok

iex> IgniterCss.Parsers.CSS.Parser.validate_css("invalid { css")
{:error, "Parse error at line 1, column 10: Missing closing brace"}