Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Alpine.js-like attributes #288

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
27 changes: 17 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -301,6 +307,7 @@ DEPENDENCIES
bootsnap
cssbundling-rails
debug
html_press!
irb (~> 1.11)
jsbundling-rails
phlexing!
Expand Down
1 change: 1 addition & 0 deletions gem/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
15 changes: 11 additions & 4 deletions gem/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -228,6 +234,7 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
html_press!
maxitest (~> 4.4)
minitest (~> 5.0)
phlexing!
Expand Down
10 changes: 9 additions & 1 deletion gem/lib/phlexing/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion gem/lib/phlexing/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions gem/lib/phlexing/template_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
22 changes: 21 additions & 1 deletion gem/test/phlexing/converter/attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -146,4 +146,24 @@ class Phlexing::Converter::AttributesTest < Minitest::Spec
assert_locals "background", "display"
end
end

it "should support Alpine.js attributes" do
html = %(<button @click.prevent="something">Button</button>)

expected = <<~PHLEX.strip
button("@click.prevent": "something") { "Button" }
PHLEX

assert_phlex_template expected, html
end

it "should support attributes with @" do
html = %(<button @prevent="something">Button</button>)

expected = <<~PHLEX.strip
button("@prevent": "something") { "Button" }
PHLEX

assert_phlex_template expected, html
end
end
19 changes: 19 additions & 0 deletions gem/test/phlexing/minifier_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,24 @@ class MinifierTest < Minitest::Spec

assert_equal expected, Minifier.call(input)
end

it "should not minify alpine.js attributes" do
input = %(<button @click.prevent="something">Button</button>)
expected = %(<button @click.prevent="something">Button</button>)

assert_equal expected, Minifier.call(input)

input = %(<button @click.prevent='something'>Button</button>)
expected = %(<button @click.prevent='something'>Button</button>)

assert_equal expected, Minifier.call(input)
end

xit "should properly minify attribute interpolation" do
input = %(<input type="checkbox" <%= "selected" %> />)
expected = %(<input type="checkbox" <%= "selected" %> />)

assert_equal expected, Minifier.call(input)
end
end
end
8 changes: 4 additions & 4 deletions gem/test/phlexing/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -48,7 +48,7 @@ def extract_children(node)
assert_equal "#document-fragment,div", extract_children(parser).join(",")
assert_dom_equal %(<div></div>), 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
Expand All @@ -57,7 +57,7 @@ def extract_children(node)
assert_equal "#document-fragment,div,erb,text", extract_children(parser).join(",")
assert_dom_equal %(<div> <erb loud=""> some_method </erb> </div>), 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
Expand Down
Loading