From 05e5369a434e8b0e3d9f2bd2db95b37caa66bb3f Mon Sep 17 00:00:00 2001 From: mhenrixon Date: Mon, 2 Dec 2024 16:05:51 +0200 Subject: [PATCH 1/4] chore(deps): bump phlex to 2.0.0.rc --- Gemfile.lock | 4 ++-- lib/phlexy_ui/collapsible_sub_menu.rb | 7 +++++-- lib/phlexy_ui/sub_menu.rb | 9 +++++++-- lib/phlexy_ui/tab.rb | 9 +++++++-- phlexy_ui.gemspec | 2 +- spec/spec_helper.rb | 2 +- spec/support/phlex_helpers.rb | 10 ++++++++++ 7 files changed, 33 insertions(+), 10 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index a2f35bd..6fe2a7a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,7 +2,7 @@ PATH remote: . specs: phlexy_ui (0.1.22) - phlex (>= 1.10) + phlex (>= 2.0.0.rc1) zeitwerk (~> 2.6) GEM @@ -24,7 +24,7 @@ GEM parser (3.3.4.0) ast (~> 2.4.1) racc - phlex (1.11.0) + phlex (2.0.0.rc1) psych (5.1.2) stringio racc (1.8.1) diff --git a/lib/phlexy_ui/collapsible_sub_menu.rb b/lib/phlexy_ui/collapsible_sub_menu.rb index 444379b..3bbdfd9 100644 --- a/lib/phlexy_ui/collapsible_sub_menu.rb +++ b/lib/phlexy_ui/collapsible_sub_menu.rb @@ -3,8 +3,6 @@ module PhlexyUI # @private class CollapsibleSubMenu < Base - include Phlex::DeferredRender - def initialize(*, **) super @items ||= [] @@ -52,6 +50,11 @@ def item(*, **, &) private + def before_template(&) + vanish(&) + super + end + ATTRIBUTES_MAP = { open: true }.freeze diff --git a/lib/phlexy_ui/sub_menu.rb b/lib/phlexy_ui/sub_menu.rb index 476a0ee..fc53eee 100644 --- a/lib/phlexy_ui/sub_menu.rb +++ b/lib/phlexy_ui/sub_menu.rb @@ -3,8 +3,6 @@ module PhlexyUI # @private class SubMenu < Base - include Phlex::DeferredRender - def initialize(*, **) super @items ||= [] @@ -33,5 +31,12 @@ def title(&block) def item(*, **, &) @items << MenuItem.new(*, **, &) end + + private + + def before_template(&) + vanish(&) + super + end end end diff --git a/lib/phlexy_ui/tab.rb b/lib/phlexy_ui/tab.rb index 3249a16..d64e460 100644 --- a/lib/phlexy_ui/tab.rb +++ b/lib/phlexy_ui/tab.rb @@ -3,8 +3,6 @@ module PhlexyUI # @private class Tab < Base - include Phlex::DeferredRender - def initialize(*, id: nil, **) super(*, **) @id = id @@ -39,5 +37,12 @@ def content(*, **options, &) end end end + + private + + def before_template(&) + vanish(&) + super + end end end diff --git a/phlexy_ui.gemspec b/phlexy_ui.gemspec index bcdd9b8..a397e1e 100644 --- a/phlexy_ui.gemspec +++ b/phlexy_ui.gemspec @@ -14,7 +14,7 @@ Gem::Specification.new do |s| s.required_ruby_version = ">= 3.2" - s.add_dependency "phlex", ">= 1.10" + s.add_dependency "phlex", ">= 2.0.0.rc1" s.add_dependency "zeitwerk", "~> 2.6" s.add_development_dependency "standard", "~> 1.39.2" diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index acf6ff2..49c2706 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,7 +2,7 @@ Bundler.setup require "phlexy_ui" -require "phlex/testing/view_helper" +require "phlex/testing" Dir["./spec/support/**/*.rb"].each do |f| require f diff --git a/spec/support/phlex_helpers.rb b/spec/support/phlex_helpers.rb index 98c60e3..7d72ec4 100644 --- a/spec/support/phlex_helpers.rb +++ b/spec/support/phlex_helpers.rb @@ -10,6 +10,16 @@ def phlex_context(&) end end +module Phlex + module Testing + module ViewHelper + include Phlex::Testing::SGML + + alias render render_to_string + end + end +end + RSpec.configure do |config| config.include Phlex::Testing::ViewHelper config.include PhlexHelpers From a9cb7b7c485c65da7e4dc2eecd951326df503667 Mon Sep 17 00:00:00 2001 From: mhenrixon Date: Tue, 3 Dec 2024 09:34:58 +0200 Subject: [PATCH 2/4] fix: prefer yield self over before vanish --- lib/phlexy_ui/collapsible_sub_menu.rb | 7 ++----- lib/phlexy_ui/sub_menu.rb | 9 ++------- lib/phlexy_ui/tab.rb | 9 ++------- 3 files changed, 6 insertions(+), 19 deletions(-) diff --git a/lib/phlexy_ui/collapsible_sub_menu.rb b/lib/phlexy_ui/collapsible_sub_menu.rb index 3bbdfd9..5dce1fa 100644 --- a/lib/phlexy_ui/collapsible_sub_menu.rb +++ b/lib/phlexy_ui/collapsible_sub_menu.rb @@ -9,6 +9,8 @@ def initialize(*, **) end def view_template(&) + yield(self) if block_given? + attributes = generate_attributes(base_modifiers, options, ATTRIBUTES_MAP) generate_classes!( @@ -50,11 +52,6 @@ def item(*, **, &) private - def before_template(&) - vanish(&) - super - end - ATTRIBUTES_MAP = { open: true }.freeze diff --git a/lib/phlexy_ui/sub_menu.rb b/lib/phlexy_ui/sub_menu.rb index fc53eee..8819902 100644 --- a/lib/phlexy_ui/sub_menu.rb +++ b/lib/phlexy_ui/sub_menu.rb @@ -9,6 +9,8 @@ def initialize(*, **) end def view_template(&) + yield(self) if block_given? + if @title div do render @title @@ -31,12 +33,5 @@ def title(&block) def item(*, **, &) @items << MenuItem.new(*, **, &) end - - private - - def before_template(&) - vanish(&) - super - end end end diff --git a/lib/phlexy_ui/tab.rb b/lib/phlexy_ui/tab.rb index d64e460..c976be4 100644 --- a/lib/phlexy_ui/tab.rb +++ b/lib/phlexy_ui/tab.rb @@ -9,6 +9,8 @@ def initialize(*, id: nil, **) end def view_template(&) + yield(self) if block_given? + if @content render TabWithContent.new( *base_modifiers, @@ -37,12 +39,5 @@ def content(*, **options, &) end end end - - private - - def before_template(&) - vanish(&) - super - end end end From 4f206355a7cdcc9fa59ece2988425a4334bc3d6c Mon Sep 17 00:00:00 2001 From: mhenrixon Date: Tue, 3 Dec 2024 09:35:11 +0200 Subject: [PATCH 3/4] fix: update specs to work with 2.0 --- spec/lib/phlexy_ui/button_spec.rb | 65 ++-------------------- spec/lib/phlexy_ui/drawer_spec.rb | 2 +- spec/lib/phlexy_ui/menu_spec.rb | 2 +- spec/lib/phlexy_ui/modal_spec.rb | 6 +- spec/lib/phlexy_ui/radial_progress_spec.rb | 2 +- 5 files changed, 10 insertions(+), 67 deletions(-) diff --git a/spec/lib/phlexy_ui/button_spec.rb b/spec/lib/phlexy_ui/button_spec.rb index d636911..e2d629d 100644 --- a/spec/lib/phlexy_ui/button_spec.rb +++ b/spec/lib/phlexy_ui/button_spec.rb @@ -215,8 +215,8 @@ render described_class.new( :neutral, class: "my-class", - modal: :my_modal_1, - data: {my: :modals} + modal: "my_modal_1", + data: {my: "modals"} ) do "Click me" end @@ -226,8 +226,8 @@ expected_html = html <<~HTML + onclick="my_modal_1.showModal()" + data-my="modals">Click me HTML expect(output).to eq(expected_html) @@ -247,44 +247,6 @@ end end - # TODO: Not needed once Phlex 2.0 is released. - context "when passing malicious code via the block" do - subject(:output) do - render described_class.new(:neutral, modal: :my_modal_1) do - "" - end - end - - it "escapes the code" do - expected_html = html <<~HTML - - HTML - - expect(output).to eq(expected_html) - end - end - - # TODO: Not needed once Phlex 2.0 is released. - context "when passing malicious code via the onclick option" do - subject(:output) do - render described_class.new( - :neutral, - :modal => :my_modal_1, - "onclick" => "" - ) - end - - it "escapes the code" do - expected_html = "" - - expect(output).to eq(expected_html) - end - end - context "when passing malicious code via the modal option" do subject(:output) do render described_class.new(:neutral, modal: %(" onclick="alert('XSS') //;)) @@ -301,24 +263,5 @@ expect(output).to eq(expected_html) end end - - # TODO: Not needed once Phlex 2.0 is released. - context "when passing malicious code via the class option" do - subject(:output) do - render described_class.new( - :neutral, - modal: :my_modal_1, - class: %(" onclick="alert('XSS');) - ) - end - - it "escapes the code" do - expected_html = "" - - expect(output).to eq(expected_html) - end - end end end diff --git a/spec/lib/phlexy_ui/drawer_spec.rb b/spec/lib/phlexy_ui/drawer_spec.rb index ae98c9e..4ce0099 100644 --- a/spec/lib/phlexy_ui/drawer_spec.rb +++ b/spec/lib/phlexy_ui/drawer_spec.rb @@ -184,7 +184,7 @@ let(:component) do Class.new(Phlex::HTML) do def view_template(&) - render PhlexyUI::Drawer.new(:end, id: :my_drawer) do |drawer| + render PhlexyUI::Drawer.new(:end, id: "my_drawer") do |drawer| drawer.toggle(class: "my-toggle", data: {my: "toggles"}) drawer.content(class: "my-content", data: {my: "contents"}) do drawer.button(:primary, class: "my-button", data: {my: "buttons"}) do diff --git a/spec/lib/phlexy_ui/menu_spec.rb b/spec/lib/phlexy_ui/menu_spec.rb index 5d7915f..c6236d6 100644 --- a/spec/lib/phlexy_ui/menu_spec.rb +++ b/spec/lib/phlexy_ui/menu_spec.rb @@ -279,7 +279,7 @@ def view_template(&) end menu.item do |item| - item.submenu :collapsible, :open, :primary, class: "rounded-t-none", data: {my: :collapsible_menus} do |submenu| + item.submenu :collapsible, :open, :primary, class: "rounded-t-none", data: {my: "collapsible_menus"} do |submenu| submenu.title do "Parent 1" end diff --git a/spec/lib/phlexy_ui/modal_spec.rb b/spec/lib/phlexy_ui/modal_spec.rb index 7e3b1be..e8edf3d 100644 --- a/spec/lib/phlexy_ui/modal_spec.rb +++ b/spec/lib/phlexy_ui/modal_spec.rb @@ -11,7 +11,7 @@ def view_template(&) "Open Modal" end - render PhlexyUI::Modal.new(id: :my_modal_1) do |modal| + render PhlexyUI::Modal.new(id: "my_modal_1") do |modal| modal.body do h3 do "Hello!" @@ -64,7 +64,7 @@ def view_template(&) "Open Modal" end - render PhlexyUI::Modal.new(:tap_outside_to_close, id: :my_modal_1) do |modal| + render PhlexyUI::Modal.new(:tap_outside_to_close, id: "my_modal_1") do |modal| modal.body do h3 do "Hello!" @@ -109,7 +109,7 @@ def view_template(&) "Open Modal" end - render PhlexyUI::Modal.new(id: :my_modal_1) do |modal| + render PhlexyUI::Modal.new(id: "my_modal_1") do |modal| modal.body do modal.close_button :sm, :circle, :ghost, class: "absolute right-2 top-2" do "✕" diff --git a/spec/lib/phlexy_ui/radial_progress_spec.rb b/spec/lib/phlexy_ui/radial_progress_spec.rb index 641c435..ccec50c 100644 --- a/spec/lib/phlexy_ui/radial_progress_spec.rb +++ b/spec/lib/phlexy_ui/radial_progress_spec.rb @@ -95,7 +95,7 @@ def view_template(&) style: "background-color: red;", as: :section, data: { - my: :radial_progress + my: "radial_progress" } ) do "50%" From 5e9cabcbf0af2fc64bc3a81b97e17a070008edb9 Mon Sep 17 00:00:00 2001 From: mhenrixon Date: Tue, 3 Dec 2024 09:39:29 +0200 Subject: [PATCH 4/4] fix: lint'em hard --- Gemfile | 2 +- lib/phlexy_ui/collapsible_sub_menu.rb | 2 +- lib/phlexy_ui/tab.rb | 2 +- spec/support/phlex_helpers.rb | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Gemfile b/Gemfile index 0ddf5e3..bb94df8 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ # frozen_string_literal: true source "https://rubygems.org" -gemspec \ No newline at end of file +gemspec diff --git a/lib/phlexy_ui/collapsible_sub_menu.rb b/lib/phlexy_ui/collapsible_sub_menu.rb index 5dce1fa..74277c4 100644 --- a/lib/phlexy_ui/collapsible_sub_menu.rb +++ b/lib/phlexy_ui/collapsible_sub_menu.rb @@ -10,7 +10,7 @@ def initialize(*, **) def view_template(&) yield(self) if block_given? - + attributes = generate_attributes(base_modifiers, options, ATTRIBUTES_MAP) generate_classes!( diff --git a/lib/phlexy_ui/tab.rb b/lib/phlexy_ui/tab.rb index c976be4..0ea7afc 100644 --- a/lib/phlexy_ui/tab.rb +++ b/lib/phlexy_ui/tab.rb @@ -10,7 +10,7 @@ def initialize(*, id: nil, **) def view_template(&) yield(self) if block_given? - + if @content render TabWithContent.new( *base_modifiers, diff --git a/spec/support/phlex_helpers.rb b/spec/support/phlex_helpers.rb index 7d72ec4..85b765b 100644 --- a/spec/support/phlex_helpers.rb +++ b/spec/support/phlex_helpers.rb @@ -15,7 +15,7 @@ module Testing module ViewHelper include Phlex::Testing::SGML - alias render render_to_string + alias_method :render, :render_to_string end end end