Class: CssCompressor::Compressor
- Inherits:
-
Object
- Object
- CssCompressor::Compressor
- Defined in:
- lib/cssminify2/cssmin_enhanced.rb
Overview
Enhanced CSS Compressor with modular architecture
Constant Summary collapse
- PATTERNS =
Regex patterns used throughout compression
{ comment_start: /\/\*/, comment_end: /\*\//, string_double: /"([^\\"]|\\.|\\)*"/, string_single: /'([^\\']|\\.|\\)*'/, data_url: /url\(\s*(['"]?)data\:/i, rgb_function: /rgb\s*\(\s*([0-9,\s]+)\s*\)(\d+%)?/i, hex_6digit: /(\\=\\s*?[\\\"']?)?#([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])(:?\\}|[^0-9a-f{][^{]*?\\})/i, calc_function: /calc\([^)]*\)/, pseudo_class_chain: /(:[\\w-]+(?:\\([^)]*\\))?(?:\\s+:[\\w-]+(?:\\([^)]*\\))?)+)/, filter_property: /filter\s*:[^;}]+/i, zero_units: /(?i)(^|: ?)((?:[0-9a-z\-\.]+ )*?)?(?:0?\.)?0(?:px|em|%|in|cm|mm|pc|pt|ex|deg|g?rad|m?s|k?hz)/i, multiple_semicolons: /;;+/, whitespace: /\s+/ }.freeze
- COLOR_KEYWORDS =
Color optimization mappings
{ '#ff0000' => 'red', '#f00' => 'red', '#000080' => 'navy', '#008000' => 'green', '#008080' => 'teal', '#800000' => 'maroon', '#800080' => 'purple', '#808000' => 'olive', '#808080' => 'gray', '#c0c0c0' => 'silver', '#ffa500' => 'orange', '#0000ff' => 'blue', '#00ff00' => 'lime', '#ff00ff' => 'fuchsia', '#00ffff' => 'cyan', '#ffff00' => 'yellow', '#000000' => 'black', '#ffffff' => 'white' }.freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#stats ⇒ Object
readonly
Returns the value of attribute stats.
Instance Method Summary collapse
- #compress(css, options = {}) ⇒ Object
-
#initialize(config = Configuration.new) ⇒ Compressor
constructor
A new instance of Compressor.
Constructor Details
#initialize(config = Configuration.new) ⇒ Compressor
Returns a new instance of Compressor.
67 68 69 70 71 |
# File 'lib/cssminify2/cssmin_enhanced.rb', line 67 def initialize(config = Configuration.new) @config = config @preserved_tokens = [] @stats = { original_size: 0, compressed_size: 0, compression_ratio: 0.0 } end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
65 66 67 |
# File 'lib/cssminify2/cssmin_enhanced.rb', line 65 def config @config end |
#stats ⇒ Object (readonly)
Returns the value of attribute stats.
65 66 67 |
# File 'lib/cssminify2/cssmin_enhanced.rb', line 65 def stats @stats end |
Instance Method Details
#compress(css, options = {}) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/cssminify2/cssmin_enhanced.rb', line 73 def compress(css, = {}) @stats[:original_size] = css.length begin # Input normalization css = normalize_input(css) # Core compression pipeline css = extract_data_urls(css) if css.include?('data:') css = process_comments(css) css = preserve_strings(css) css = normalize_whitespace(css) css = preserve_calc_functions(css) css = optimize_selectors(css) css = optimize_properties(css) css = optimize_colors(css) if @config.optimize_colors css = optimize_values(css) css = finalize_compression(css) @stats[:compressed_size] = css.length @stats[:compression_ratio] = (@stats[:original_size] - @stats[:compressed_size]).to_f / @stats[:original_size] * 100 css rescue => e if @config.strict_mode raise CompressError.new("CSS compression failed: #{e.}", e) else # Fallback to basic compression css.gsub(PATTERNS[:whitespace], ' ').strip end end end |