From bedf78633e650b1f42a701e198665d90940a1243 Mon Sep 17 00:00:00 2001 From: Marco Roth Date: Tue, 6 Feb 2024 03:34:21 +0100 Subject: [PATCH 1/3] Support Alpine.js attributes --- gem/Gemfile | 1 + gem/Gemfile.lock | 15 +++++++++++---- gem/lib/phlexing/helpers.rb | 6 +++++- gem/lib/phlexing/parser.rb | 2 +- gem/test/phlexing/converter/attributes_test.rb | 10 ++++++++++ gem/test/phlexing/minifier_test.rb | 12 ++++++++++++ 6 files changed, 40 insertions(+), 6 deletions(-) diff --git a/gem/Gemfile b/gem/Gemfile index 0fe4dc7..32f7a9f 100644 --- a/gem/Gemfile +++ b/gem/Gemfile @@ -8,3 +8,4 @@ gem "maxitest", "~> 4.4" gem "minitest", "~> 5.0" gem "rake", "~> 13.0" gem "rubocop", require: false, github: "joeldrapper/rubocop", branch: "rubocop-user-agent" +gem "html_press", github: "marcoroth/html_press", branch: "non-standard-attributes" diff --git a/gem/Gemfile.lock b/gem/Gemfile.lock index e4621c0..cfc5664 100644 --- a/gem/Gemfile.lock +++ b/gem/Gemfile.lock @@ -14,6 +14,16 @@ GIT ruby-progressbar (~> 1.7) unicode-display_width (>= 1.4.0, < 3.0) +GIT + remote: https://github.com/marcoroth/html_press.git + revision: 24ce95baae9912f8131d050cf253b688eb616cbf + branch: non-standard-attributes + specs: + html_press (0.8.2) + htmlentities + multi_css (>= 0.1.0) + multi_js (>= 0.1.0) + PATH remote: . specs: @@ -115,10 +125,6 @@ GEM execjs (2.8.1) globalid (1.1.0) activesupport (>= 5.0) - html_press (0.8.2) - htmlentities - multi_css (>= 0.1.0) - multi_js (>= 0.1.0) htmlentities (4.3.4) i18n (1.14.1) concurrent-ruby (~> 1.0) @@ -228,6 +234,7 @@ PLATFORMS x86_64-linux DEPENDENCIES + html_press! maxitest (~> 4.4) minitest (~> 5.0) phlexing! diff --git a/gem/lib/phlexing/helpers.rb b/gem/lib/phlexing/helpers.rb index 0bdbc02..8d7d3fc 100644 --- a/gem/lib/phlexing/helpers.rb +++ b/gem/lib/phlexing/helpers.rb @@ -24,7 +24,11 @@ def symbol(string) end def arg(string) - "#{string}: " + if string.include?(".") + %("#{string}": ) + else + "#{string}: " + end end def quote(string) diff --git a/gem/lib/phlexing/parser.rb b/gem/lib/phlexing/parser.rb index 346223d..02c93d9 100644 --- a/gem/lib/phlexing/parser.rb +++ b/gem/lib/phlexing/parser.rb @@ -25,7 +25,7 @@ def self.call(source) elsif source =~ body_tag Nokogiri::HTML::Document.parse(source).css("body").first else - Nokogiri::HTML::DocumentFragment.parse(source) + Nokogiri::HTML5::DocumentFragment.parse(source) end end end diff --git a/gem/test/phlexing/converter/attributes_test.rb b/gem/test/phlexing/converter/attributes_test.rb index 812da84..dc2c3a7 100644 --- a/gem/test/phlexing/converter/attributes_test.rb +++ b/gem/test/phlexing/converter/attributes_test.rb @@ -146,4 +146,14 @@ class Phlexing::Converter::AttributesTest < Minitest::Spec assert_locals "background", "display" end end + + it "should support Alpine.js attributes" do + html = %() + + expected = <<~PHLEX.strip + button("@click.prevent": "something") { "Button" } + PHLEX + + assert_phlex_template expected, html + end end diff --git a/gem/test/phlexing/minifier_test.rb b/gem/test/phlexing/minifier_test.rb index bdd9c57..ccd706b 100644 --- a/gem/test/phlexing/minifier_test.rb +++ b/gem/test/phlexing/minifier_test.rb @@ -99,5 +99,17 @@ class MinifierTest < Minitest::Spec assert_equal expected, Minifier.call(input) end + + it "should not minify alpine.js attributes" do + input = %() + expected = %() + + assert_equal expected, Minifier.call(input) + + input = %() + expected = %() + + assert_equal expected, Minifier.call(input) + end end end From 8550d825a704bc979a199ac8a0de66ce045bbd84 Mon Sep 17 00:00:00 2001 From: Marco Roth Date: Tue, 6 Feb 2024 18:57:07 +0100 Subject: [PATCH 2/3] Also support attributes with `@` --- gem/lib/phlexing/helpers.rb | 8 ++++++-- gem/lib/phlexing/template_generator.rb | 4 ++-- gem/test/phlexing/converter/attributes_test.rb | 12 +++++++++++- gem/test/phlexing/minifier_test.rb | 7 +++++++ gem/test/phlexing/parser_test.rb | 8 ++++---- 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/gem/lib/phlexing/helpers.rb b/gem/lib/phlexing/helpers.rb index 8d7d3fc..8e9240e 100644 --- a/gem/lib/phlexing/helpers.rb +++ b/gem/lib/phlexing/helpers.rb @@ -24,13 +24,17 @@ def symbol(string) end def arg(string) - if string.include?(".") - %("#{string}": ) + if string.include?(".") || string.include?("@") + %(#{double_quotes(string)}: ) else "#{string}: " end end + def double_quotes(string) + %("#{string}") + end + def quote(string) "%(#{string})" end diff --git a/gem/lib/phlexing/template_generator.rb b/gem/lib/phlexing/template_generator.rb index 5acc61d..8c3bc39 100644 --- a/gem/lib/phlexing/template_generator.rb +++ b/gem/lib/phlexing/template_generator.rb @@ -114,7 +114,7 @@ def handle_erb_attribute_output(attribute) end def handle_erb_interpolation_in_tag(attribute) - "**#{parens("#{unwrap_erb(unescape(attribute.value))}: true")}" + "**#{braces("#{unwrap_erb(unescape(attribute.value))}: true")}" end def handle_erb_safe_node(node) @@ -227,7 +227,7 @@ def handle_node(node, level = 0) else handle_element_node(node, level) end - in Nokogiri::HTML4::Document | Nokogiri::HTML4::DocumentFragment | Nokogiri::XML::DTD + in Nokogiri::HTML4::Document | Nokogiri::HTML4::DocumentFragment | Nokogiri::HTML5::DocumentFragment | Nokogiri::XML::DTD handle_document_node(node, level) in Nokogiri::XML::Comment handle_html_comment_node(node) diff --git a/gem/test/phlexing/converter/attributes_test.rb b/gem/test/phlexing/converter/attributes_test.rb index dc2c3a7..b5b54f0 100644 --- a/gem/test/phlexing/converter/attributes_test.rb +++ b/gem/test/phlexing/converter/attributes_test.rb @@ -83,7 +83,7 @@ class Phlexing::Converter::AttributesTest < Minitest::Spec HTML expected = <<~PHLEX.strip - input(type: %(checkbox), **(" selected": true)) + input(type: "checkbox", **{ " selected": true }) PHLEX assert_phlex_template expected, html @@ -156,4 +156,14 @@ class Phlexing::Converter::AttributesTest < Minitest::Spec assert_phlex_template expected, html end + + it "should support attributes with @" do + html = %() + + expected = <<~PHLEX.strip + button("@prevent": "something") { "Button" } + PHLEX + + assert_phlex_template expected, html + end end diff --git a/gem/test/phlexing/minifier_test.rb b/gem/test/phlexing/minifier_test.rb index ccd706b..0c58eb1 100644 --- a/gem/test/phlexing/minifier_test.rb +++ b/gem/test/phlexing/minifier_test.rb @@ -111,5 +111,12 @@ class MinifierTest < Minitest::Spec assert_equal expected, Minifier.call(input) end + + xit "should properly minify attribute interpolation" do + input = %( />) + expected = %( />) + + assert_equal expected, Minifier.call(input) + end end end diff --git a/gem/test/phlexing/parser_test.rb b/gem/test/phlexing/parser_test.rb index 59ff496..6f8fe88 100644 --- a/gem/test/phlexing/parser_test.rb +++ b/gem/test/phlexing/parser_test.rb @@ -30,7 +30,7 @@ def extract_children(node) assert_equal "#document-fragment", extract_children(parser).join(",") assert_dom_equal "", parser.to_xml assert_equal "#document-fragment", parser.name - assert_equal Nokogiri::HTML4::DocumentFragment, parser.class + assert_equal Nokogiri::HTML5::DocumentFragment, parser.class end it "should handle empty string" do @@ -39,7 +39,7 @@ def extract_children(node) assert_equal "#document-fragment", extract_children(parser).join(",") assert_dom_equal "", parser.to_xml assert_equal "#document-fragment", parser.name - assert_equal Nokogiri::HTML4::DocumentFragment, parser.class + assert_equal Nokogiri::HTML5::DocumentFragment, parser.class end it "should handle simple div" do @@ -48,7 +48,7 @@ def extract_children(node) assert_equal "#document-fragment,div", extract_children(parser).join(",") assert_dom_equal %(
), parser.to_html assert_equal "#document-fragment", parser.name - assert_equal Nokogiri::HTML4::DocumentFragment, parser.class + assert_equal Nokogiri::HTML5::DocumentFragment, parser.class end it "should handle ERB" do @@ -57,7 +57,7 @@ def extract_children(node) assert_equal "#document-fragment,div,erb,text", extract_children(parser).join(",") assert_dom_equal %(
some_method
), parser.to_xml assert_equal "#document-fragment", parser.name - assert_equal Nokogiri::HTML4::DocumentFragment, parser.class + assert_equal Nokogiri::HTML5::DocumentFragment, parser.class end it "should handle html" do From ff15c2bec648d627b6c1b3af73893093404ae13a Mon Sep 17 00:00:00 2001 From: Marco Roth Date: Tue, 6 Feb 2024 19:14:28 +0100 Subject: [PATCH 3/3] Also use html_press fork in Rails app --- Gemfile | 1 + Gemfile.lock | 27 +++++++++++++++++---------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/Gemfile b/Gemfile index 3164af0..6d04da7 100644 --- a/Gemfile +++ b/Gemfile @@ -46,6 +46,7 @@ gem "cssbundling-rails" # Simple ERB to Phlex converter gem "phlexing", path: "gem" +gem "html_press", github: "marcoroth/html_press", branch: "non-standard-attributes" # A pure Ruby code highlighter that is compatible with Pygments gem "rouge", "~> 4.2" diff --git a/Gemfile.lock b/Gemfile.lock index 4aab4ca..3941613 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -14,6 +14,16 @@ GIT ruby-progressbar (~> 1.7) unicode-display_width (>= 1.4.0, < 3.0) +GIT + remote: https://github.com/marcoroth/html_press.git + revision: 24ce95baae9912f8131d050cf253b688eb616cbf + branch: non-standard-attributes + specs: + html_press (0.8.2) + htmlentities + multi_css (>= 0.1.0) + multi_js (>= 0.1.0) + PATH remote: gem specs: @@ -109,7 +119,7 @@ GEM bootsnap (1.18.3) msgpack (~> 1.2) builder (3.2.4) - cgi (0.4.0) + cgi (0.4.1) concurrent-ruby (1.2.3) connection_pool (2.4.1) crass (1.0.6) @@ -131,16 +141,12 @@ GEM rainbow (>= 2.1.0) drb (2.2.0) ruby2_keywords - erb (4.0.3) + erb (4.0.4) cgi (>= 0.3.3) erubi (1.12.0) - execjs (2.8.1) + execjs (2.9.1) globalid (1.2.1) activesupport (>= 6.1) - html_press (0.8.2) - htmlentities - multi_css (>= 0.1.0) - multi_js (>= 0.1.0) htmlentities (4.3.4) i18n (1.14.1) concurrent-ruby (~> 1.0) @@ -150,7 +156,7 @@ GEM reline (>= 0.4.2) jsbundling-rails (1.3.0) railties (>= 6.0.0) - json (2.6.3) + json (2.7.1) loofah (2.22.0) crass (~> 1.0.2) nokogiri (>= 1.12.0) @@ -161,7 +167,7 @@ GEM net-smtp marcel (1.0.2) mini_mime (1.1.5) - minitest (5.21.2) + minitest (5.22.0) msgpack (1.7.2) multi_css (0.1.0) css_press @@ -288,7 +294,7 @@ GEM websocket-driver (0.7.6) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) - zeitwerk (2.6.12) + zeitwerk (2.6.13) PLATFORMS arm64-darwin-21 @@ -301,6 +307,7 @@ DEPENDENCIES bootsnap cssbundling-rails debug + html_press! irb (~> 1.11) jsbundling-rails phlexing!