Class: Inkcite::Renderer::Style

Inherits:
Object
  • Object
show all
Defined in:
lib/inkcite/renderer/style.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, ctx, styles = {}) ⇒ Style

Returns a new instance of Style.



5
6
7
8
9
10
11
# File 'lib/inkcite/renderer/style.rb', line 5

def initialize name, ctx, styles={}

  @name = name
  @ctx = ctx
  @styles = styles

end

Class Method Details

.needs_prefixing?(key) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/inkcite/renderer/style.rb', line 70

def self.needs_prefixing? key
  PREFIXABLE_KEYS.include?(key.to_sym)
end

Instance Method Details

#[](key) ⇒ Object



13
14
15
# File 'lib/inkcite/renderer/style.rb', line 13

def [] key
  @styles[key]
end

#[]=(key, val) ⇒ Object



17
18
19
# File 'lib/inkcite/renderer/style.rb', line 17

def []= key, val
  @styles[key] = val
end

#to_css(allowed_prefixes = nil) ⇒ Object



21
22
23
# File 'lib/inkcite/renderer/style.rb', line 21

def to_css allowed_prefixes=nil
  "#{@name} { #{to_inline_css(allowed_prefixes)} }"
end

#to_inline_css(allowed_prefixes = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/inkcite/renderer/style.rb', line 25

def to_inline_css allowed_prefixes=nil

  # Inherit the list of allowed prefixes from the context if
  # none were provided.  Otherwise, make sure we're working
  # with an array.
  if allowed_prefixes.nil?
    allowed_prefixes = @ctx.prefixes
  else
    allowed_prefixes = [*allowed_prefixes]
  end

  # This will hold a copy of the key and values including
  # all keys with prefixes.
  _styles = {}

  # A reusable array indicating no prefixing is necessary.
  no_prefixes = ['']

  @styles.each_pair do |key, val|

    # Determine which list of prefixes are needed based on the
    # original key (e.g. :transform) - or use the list of
    # non-modifying prefixes.
    prefixes = if Inkcite::Renderer::Style.needs_prefixing?(key)
      allowed_prefixes
    else
      no_prefixes
    end

    # Iterate through each prefix and create a hybrid key.  Then
    # add the styles to the temporary list.
    prefixes.each do |prefix|
      prefixed_key = "#{prefix}#{key}".to_sym
      _styles[prefixed_key] = val
    end

  end

  Renderer.render_styles(_styles)
end

#to_sObject



66
67
68
# File 'lib/inkcite/renderer/style.rb', line 66

def to_s
  @name.blank? ? to_inline_css : to_css
end