lib/prawn_html/tags/col.rb



# frozen_string_literal: true

module PrawnHtml
  module Tags
    class Col < Tag
      ELEMENTS = [:col].freeze

      def on_context_add(_context)
        table_parent = find_parent_of_type(PrawnHtml::Tags::Table)
        return unless table_parent

        width = parse_width
        table_parent.add_col_width(width) if width
      end

      private

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

      def parse_width
        return attrs[:colwidth].to_f if attrs[:colwidth]

        return nil if attrs[:style].include?('min-width:')

        if attrs[:style] && attrs[:style] =~ /width:\s*(\d+)px/i
          return Regexp.last_match(1).to_f
        end

        nil
      end
    end
  end
end