Sha256: 2bcfed8ac3b8629e5008de7d5f2aa3ce78eb39e039ebdfe5051be6c5a1e5cf7d

Contents?: true

Size: 1 KB

Versions: 8

Compression:

Stored size: 1 KB

Contents

require "css_parser"

module Lookbook
  class InspectorPanel::Component < Lookbook::BaseComponent
    attr_reader :panel_styles, :panel_html, :id

    def initialize(id:, name:, system: false, **html_attrs)
      @id = id
      @name = name
      @system = system
      @panel_html = nil
      @panel_styles = nil
      super(**html_attrs)
    end

    def before_render
      if @system == false
        panel_dom = ::Nokogiri::HTML.fragment(content)
        style_tags = panel_dom.css("style")
        if style_tags.any?
          css_parser = ::CssParser::Parser.new
          @panel_styles = ""
          style_tags.each do |style_tag|
            css_parser.load_string! style_tag.text
            css_parser.each_selector do |selector, declarations, specificity|
              @panel_styles += "##{id} #{selector} { #{declarations} }\n"
            end
          end
          @panel_html = content.gsub(/<style(?:\s[^>]*)?>.*<\/style>/, "").html_safe
        end
      end
      @panel_html ||= content
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
lookbook-1.0.8 app/components/lookbook/inspector_panel/component.rb
lookbook-1.0.7 app/components/lookbook/inspector_panel/component.rb
lookbook-1.0.6 app/components/lookbook/inspector_panel/component.rb
lookbook-1.0.5 app/components/lookbook/inspector_panel/component.rb
lookbook-1.0.4 app/components/lookbook/inspector_panel/component.rb
lookbook-1.0.3 app/components/lookbook/inspector_panel/component.rb
lookbook-1.0.2 app/components/lookbook/inspector_panel/component.rb
lookbook-1.0.1 app/components/lookbook/inspector_panel/component.rb