Class: PrawnHtml::Context

Inherits:
Array
  • Object
show all
Defined in:
lib/prawn_html/context.rb

Constant Summary collapse

DEFAULT_STYLES =
{
  size: 16 * PX
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*_args) ⇒ Context

Init the Context



13
14
15
16
17
18
# File 'lib/prawn_html/context.rb', line 13

def initialize(*_args)
  super
  @last_text_node = false
  @merged_styles = nil
  @previous_tag = nil
end

Instance Attribute Details

#last_text_nodeObject

Returns the value of attribute last_text_node.



10
11
12
# File 'lib/prawn_html/context.rb', line 10

def last_text_node
  @last_text_node
end

#previous_tagObject (readonly)

Returns the value of attribute previous_tag.



9
10
11
# File 'lib/prawn_html/context.rb', line 9

def previous_tag
  @previous_tag
end

Instance Method Details

#add(element) ⇒ Context

Add an element to the context

Set the parent for the previous element in the chain. Run ‘on_context_add` callback method on the added element.

Parameters:

  • element (Tag)

    the element to add

Returns:

  • (Context)

    the context updated



28
29
30
31
32
33
34
# File 'lib/prawn_html/context.rb', line 28

def add(element)
  element.parent = last
  push(element)
  element.on_context_add(self) if element.respond_to?(:on_context_add)
  @merged_styles = nil
  self
end

#before_contentString

Evaluate before content

Returns:

  • (String)

    before content string



39
40
41
# File 'lib/prawn_html/context.rb', line 39

def before_content
  (last.respond_to?(:before_content) && last.before_content) || ''
end

#block_stylesHash

Merges the context block styles

Returns:

  • (Hash)

    the hash of merged styles



46
47
48
49
50
51
52
# File 'lib/prawn_html/context.rb', line 46

def block_styles
  each_with_object({}) do |element, res|
    element.block_styles.each do |key, value|
      Attributes.merge_attr!(res, key, value)
    end
  end
end

#inspectObject

:nocov:



66
67
68
# File 'lib/prawn_html/context.rb', line 66

def inspect
  map(&:class).map(&:to_s).join(', ')
end

#merged_stylesHash

Merge the context styles for text nodes

Returns:

  • (Hash)

    the hash of merged styles



57
58
59
60
61
62
63
# File 'lib/prawn_html/context.rb', line 57

def merged_styles
  @merged_styles ||=
    each_with_object(DEFAULT_STYLES.dup) do |element, res|
      evaluate_element_styles(element, res)
      element.update_styles(res)
    end
end

#remove_lastObject

Remove the last element from the context



72
73
74
75
76
77
78
# File 'lib/prawn_html/context.rb', line 72

def remove_last
  last.on_context_remove(self) if last.respond_to?(:on_context_remove)
  @merged_styles = nil
  @last_text_node = false
  @previous_tag = last
  pop
end

#white_space_pre?boolean

White space is equal to ‘pre’?

Returns:

  • (boolean)

    white space property of the last element is equal to ‘pre’



83
84
85
# File 'lib/prawn_html/context.rb', line 83

def white_space_pre?
  last && last.styles[:white_space] == :pre
end