lib/prawn_html/tags/th.rb



# frozen_string_literal: true

module PrawnHtml
  module Tags
    class Th < Tag
      ELEMENTS = [:th].freeze

      def on_context_add(_context)
        @text_content = +''
      end

      def on_text_node(content)
        @text_content ||= +''
        @text_content << content
      end

      def on_context_remove(_context)
        row_parent = find_parent_of_type(PrawnHtml::Tags::Tr)
        return unless row_parent

        cell_data = { content: "<b>#{@text_content.strip}</b>" }

        cell_data[:colspan] = attrs[:colspan].to_i if attrs[:colspan]
        cell_data[:rowspan] = attrs[:rowspan].to_i if attrs[:rowspan]

        if attrs[:width]
          cell_data[:width] = attrs[:width].to_i
        end

        row_parent.add_cell(cell_data)
        @text_content = +''
      end

      private

      def find_parent_of_type(klass)
        p = parent
        p = p.parent while p && !p.is_a?(klass)
        p
      end
    end
  end
end