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! 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..8e9240e 100644 --- a/gem/lib/phlexing/helpers.rb +++ b/gem/lib/phlexing/helpers.rb @@ -24,7 +24,15 @@ def symbol(string) end def arg(string) - "#{string}: " + if string.include?(".") || string.include?("@") + %(#{double_quotes(string)}: ) + else + "#{string}: " + end + end + + def double_quotes(string) + %("#{string}") 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/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 812da84..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 @@ -146,4 +146,24 @@ 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 + + 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 bdd9c57..0c58eb1 100644 --- a/gem/test/phlexing/minifier_test.rb +++ b/gem/test/phlexing/minifier_test.rb @@ -99,5 +99,24 @@ 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 + + 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