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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



220
221
222
223
224
225
# File 'lib/sevgi/graphics/auxiliary/content.rb', line 220

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) ⇒ Object

Renders CSS content.

Parameters:

  • renderer (#append)

    rendering output collaborator

  • depth (Integer)

    current render depth

Returns:

  • (Object)

    ignored by the rendering engine



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/sevgi/graphics/auxiliary/content.rb', line 231

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