IgniterCss.Parsers.Parser (igniter_css v0.1.0)
View SourceCSS 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.
Modifies a property value for a specific selector.
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
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
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 pathimport_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"}
Adds vendor prefixes to specified CSS properties throughout the stylesheet.
Parameters
css_code
- The CSS code as a stringproperty_name
- The CSS property to add prefixes toprefixes
- 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"
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,
...
}
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;
}"
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"]
}
}
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)"]
}
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"}
}
]
}
Gets the CSS properties for a specific selector if it exists, or returns nil.
Parameters
css_code
- The CSS code as a stringselector
- 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
Merges multiple CSS stylesheets into one, removing duplicates.
Examples
iex> IgniterCss.Parsers.CSS.Parser.merge_stylesheets([css_code1, css_code2])
"merged css"
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;}"
Modifies a property value for a specific selector.
Parameters
css_code
- The CSS code as a stringselector
- The CSS selector to modifyproperty_name
- The property name to modifynew_value
- The new property valueimportant
- 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"
Removes duplicate selectors and properties from CSS.
Examples
iex> IgniterCss.Parsers.CSS.Parser.remove_duplicates(css_code)
"css without duplicates"
Removes a specific @import rule from the CSS.
Parameters
css_code
- The CSS code as a stringimport_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"
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"
Replaces an entire CSS rule for a specific selector with new declarations.
Parameters
css_code
- The CSS code as a stringselector
- The CSS selector to replacenew_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"
Checks if a specific CSS selector exists in the stylesheet.
Parameters
css_code
- The CSS code as a stringselector
- 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
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;
}"
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"}