Class: Sevgi::Graphics::Content::CSS

Inherits:
Content
  • Object
show all
Defined in:
lib/sevgi/graphics/auxiliary/content.rb

Overview

CSS content rendered inside a CDATA section. Rules are captured recursively during construction; mutable selectors, property names, and values are stringified once, and embedded CDATA terminators are split safely.

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ void

Creates CSS content.

Parameters:

  • content (Hash)

    CSS rules

Raises:

  • (Sevgi::ArgumentError)

    when content is not a hash, contains a malformed style, cannot be stringified, contains invalid encoding or illegal XML 1.0 characters, contains cycles, or has keys that collide after stringification



198
199
200
201
202
203
# File 'lib/sevgi/graphics/auxiliary/content.rb', line 198

def initialize(content)
  ArgumentError.("CSS content must be a hash") unless content.is_a?(::Hash)
  validate_styles(content)

  super
end

Instance Method Details

#render(renderer, depth) ⇒ void

This method returns an undefined value.

rubocop:disable Metrics/MethodLength Renders CSS content.

Parameters:

  • renderer (Object)

    renderer receiving output

  • depth (Integer)

    current render depth



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/sevgi/graphics/auxiliary/content.rb', line 210

def render(renderer, depth)
  depth += 1

  renderer.append(depth, "<![CDATA[")

  depth += 1
  payload.each do |rule, styles|
    case styles
    when ::Hash
      renderer.append(depth, safe("#{rule} {"))
      renderer.append(depth + 1, *styles.map { |key, value| safe("#{key}: #{value};") })
      renderer.append(depth, "}")
    when ::String, ::Symbol, ::Numeric
      renderer.append(depth, safe("#{rule}: #{styles};"))
    else
      ArgumentError.("Malformed style: #{styles}")
    end
  end

  depth -= 1

  renderer.append(depth, "]]>")
end