Class: PrawnHtml::Tag

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/prawn_html/tag.rb

Constant Summary collapse

CALLBACKS =
{
  'Background' => Callbacks::Background,
  'StrikeThrough' => Callbacks::StrikeThrough
}.freeze
TAG_CLASSES =
%w[
  A B Blockquote Body Br Code Del Div H Hr I Img Li Mark Ol P Pre Small Span Sub Sup U Ul
  Table Tr Td Th Tbody Colgroup Col
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, attributes: {}, options: {}) ⇒ Tag

Init the Tag

Parameters:

  • tag name

  • (defaults to: {})

    hash of element attributes

  • (defaults to: {})

    options (container width/height/etc.)



27
28
29
30
31
# File 'lib/prawn_html/tag.rb', line 27

def initialize(tag, attributes: {}, options: {})
  @tag = tag
  @options = options
  @attrs = Attributes.new(attributes)
end

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



20
21
22
# File 'lib/prawn_html/tag.rb', line 20

def attrs
  @attrs
end

#parentObject

Returns the value of attribute parent.



19
20
21
# File 'lib/prawn_html/tag.rb', line 19

def parent
  @parent
end

#tagObject (readonly)

Returns the value of attribute tag.



20
21
22
# File 'lib/prawn_html/tag.rb', line 20

def tag
  @tag
end

Class Method Details

.class_for(tag_name) ⇒ Tag

Evaluate the Tag class from a tag name

Returns:

  • the class for the tag if available or nil



79
80
81
82
83
84
85
86
# File 'lib/prawn_html/tag.rb', line 79

def class_for(tag_name)
  @tag_classes ||= TAG_CLASSES.each_with_object({}) do |tag_class, res|
    klass = const_get("PrawnHtml::Tags::#{tag_class}")
    k = [klass] * klass::ELEMENTS.size
    res.merge!(klass::ELEMENTS.zip(k).to_h)
  end
  @tag_classes[tag_name]
end

Instance Method Details

#block?Boolean

Is a block tag?

Returns:

  • true if the type of the tag is block, false otherwise



36
37
38
# File 'lib/prawn_html/tag.rb', line 36

def block?
  false
end

#block_stylesHash

Styles to apply to the block

Returns:

  • hash of styles to apply



43
44
45
46
47
# File 'lib/prawn_html/tag.rb', line 43

def block_styles
  block_styles = styles.slice(*Attributes::STYLES_APPLY[:block])
  block_styles[:mode] = attrs.data['mode'].to_sym if attrs.data.include?('mode')
  block_styles
end

#process_styles(element_styles: nil) ⇒ Object

Process tag styles

Parameters:

  • (defaults to: nil)

    extra styles to apply to the element



52
53
54
55
56
57
# File 'lib/prawn_html/tag.rb', line 52

def process_styles(element_styles: nil)
  attrs.merge_text_styles!(tag_styles, options: options) if respond_to?(:tag_styles)
  attrs.merge_text_styles!(element_styles, options: options) if element_styles
  attrs.merge_text_styles!(attrs.style, options: options)
  attrs.merge_text_styles!(extra_styles, options: options) if respond_to?(:extra_styles)
end

#tag_close_stylesHash

Styles to apply on tag closing

Returns:

  • hash of styles to apply



62
63
64
# File 'lib/prawn_html/tag.rb', line 62

def tag_close_styles
  styles.slice(*Attributes::STYLES_APPLY[:tag_close])
end

#tag_open_stylesHash

Styles to apply on tag opening

Returns:

  • hash of styles to apply



69
70
71
# File 'lib/prawn_html/tag.rb', line 69

def tag_open_styles
  styles.slice(*Attributes::STYLES_APPLY[:tag_open])
end