rux 1.1.0 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +3 -1
- data/lib/rux/ast/tag_node.rb +4 -0
- data/lib/rux/default_tag_builder.rb +1 -1
- data/lib/rux/default_visitor.rb +2 -2
- data/lib/rux/utils.rb +2 -2
- data/lib/rux/version.rb +1 -1
- data/spec/parser_spec.rb +18 -0
- data/spec/render_spec.rb +20 -0
- data/spec/spec_helper.rb +10 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 74fe5ea5efe72399854ca97874db7ae0f9aa59abcc9201fc31725991881148b5
|
4
|
+
data.tar.gz: 5a07947618af42f689f84784ed6a4ce095ef4693cd6e6187008cd4a5cf774b4c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe10394671a5d56275289a84013f85ae13bf84c6690febf2906ed63d54005c13aee9417db3285855a75e3fc85484b51b84daa596223cd16844ecd4fbe008d728
|
7
|
+
data.tar.gz: 368880ab3405473e61114d846ec3aa54ced6490a070abd358857fcba21b373b8dcfad5eeb8a4b1100b6429a97c24e642b2c4e60bb33384baa3b9da2194d251ab
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
# 1.1.2
|
2
|
+
* Don't slugify HTML attributes in the tag builder either.
|
3
|
+
|
4
|
+
# 1.1.1
|
5
|
+
* Don't slugify HTML attributes.
|
6
|
+
- Previously rux would emit `<div data-foo="bar">` as `<div data_foo="bar">` because it treated HTML attributes as if they were being passed as Ruby arguments, which don't allow dashes. If these arguments are passed to a component initializer, then they must be slugified, but HTML attributes shouldn't be affected.
|
7
|
+
|
1
8
|
# 1.1.0
|
2
9
|
* Remove newlines between elements. (@aalin, #3)
|
3
10
|
|
data/README.md
CHANGED
@@ -170,7 +170,7 @@ end
|
|
170
170
|
|
171
171
|
In other words, positional arguments are not allowed. This is because there's no such thing as a positional HTML attribute - all HTML attributes are key/value pairs. So, in order to match up with HTML, rux components are written with keyword arguments.
|
172
172
|
|
173
|
-
Note also that the rux parser will replace dashes with underscores in
|
173
|
+
Note also that the rux parser will replace dashes with underscores in component tag attributes to adhere to both HTML and Ruby syntax conventions, since HTML attributes use dashes while Ruby keyword arguments use underscores. For example, here's how to write a rux tag for `MyComponent` above:
|
174
174
|
|
175
175
|
```ruby
|
176
176
|
<MyComponent first-name="Homer" last-name="Simpson" />
|
@@ -178,6 +178,8 @@ Note also that the rux parser will replace dashes with underscores in rux tag at
|
|
178
178
|
|
179
179
|
Notice that the rux attribute "first-name" is passed to `MyComponent#initialize` as "first_name".
|
180
180
|
|
181
|
+
Attributes on regular tags, i.e. non-component tags like `<div>` and `<span>`, are not modified. In other words, `<div data-foo="foo">` does _not_ become `<div data_foo="foo">` because that would be very annoying.
|
182
|
+
|
181
183
|
## How it Works
|
182
184
|
|
183
185
|
Translating rux code (Ruby + HTML tags) into Ruby code happens in three phases: lexing, parsing, and emitting. The lexer phase is implemented as a wrapper around the lexer from the [Parser gem](https://github.com/whitequark/parser) that looks for specific patterns in the token stream. When it finds an opening HTML tag, it hands off lexing to the rux lexer. When the tag ends, the lexer continues emitting Ruby tokens, and so on.
|
data/lib/rux/ast/tag_node.rb
CHANGED
@@ -13,7 +13,7 @@ module Rux
|
|
13
13
|
''.tap do |result|
|
14
14
|
attributes.each_pair.with_index do |(k, v), idx|
|
15
15
|
result << ' ' unless idx == 0
|
16
|
-
result << "#{k
|
16
|
+
result << "#{k}=\"#{CGI.escape_html(v.to_s)}\""
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
data/lib/rux/default_visitor.rb
CHANGED
@@ -22,10 +22,10 @@ module Rux
|
|
22
22
|
|
23
23
|
at = node.attrs.each_with_object([]) do |(k, v), ret|
|
24
24
|
next if k == 'as'
|
25
|
-
ret << Utils.attr_to_hash_elem(k, visit(v))
|
25
|
+
ret << Utils.attr_to_hash_elem(k, visit(v), slugify: node.component?)
|
26
26
|
end
|
27
27
|
|
28
|
-
if node.
|
28
|
+
if node.component?
|
29
29
|
result << "render(#{node.name}.new"
|
30
30
|
|
31
31
|
unless node.attrs.empty?
|
data/lib/rux/utils.rb
CHANGED
data/lib/rux/version.rb
CHANGED
data/spec/parser_spec.rb
CHANGED
@@ -270,4 +270,22 @@ describe Rux::Parser do
|
|
270
270
|
}
|
271
271
|
RUBY
|
272
272
|
end
|
273
|
+
|
274
|
+
it 'slugifies ruby arguments' do
|
275
|
+
code = <<~RUX
|
276
|
+
<Hello data-foo="bar" />
|
277
|
+
RUX
|
278
|
+
expect(compile(code)).to eq(<<~RUBY.strip)
|
279
|
+
render(Hello.new(data_foo: "bar"))
|
280
|
+
RUBY
|
281
|
+
end
|
282
|
+
|
283
|
+
it 'does not slugify HTML attributes' do
|
284
|
+
code = <<~RUX
|
285
|
+
<div data-foo="bar" />
|
286
|
+
RUX
|
287
|
+
expect(compile(code)).to eq(<<~RUBY.strip)
|
288
|
+
Rux.tag("div", { :"data-foo" => "bar" })
|
289
|
+
RUBY
|
290
|
+
end
|
273
291
|
end
|
data/spec/render_spec.rb
CHANGED
@@ -63,4 +63,24 @@ describe Rux do
|
|
63
63
|
"<div><p>Hello World</p><p>Hello World</p><p>Hello World</p><p>Hello World</p></div>"
|
64
64
|
)
|
65
65
|
end
|
66
|
+
|
67
|
+
it 'slugifies ruby arguments' do
|
68
|
+
result = render(<<~RUBY)
|
69
|
+
<DataComponent data-foo="foo" />
|
70
|
+
RUBY
|
71
|
+
|
72
|
+
expect(result).to eq(
|
73
|
+
"<div data-foo=\"foo\"></div>"
|
74
|
+
)
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'does not slugify HTML attributes' do
|
78
|
+
result = render(<<~RUBY)
|
79
|
+
<div data-foo="bar"></div>
|
80
|
+
RUBY
|
81
|
+
|
82
|
+
expect(result).to eq(
|
83
|
+
"<div data-foo=\"bar\"></div>"
|
84
|
+
)
|
85
|
+
end
|
66
86
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -33,5 +33,15 @@ class ArgsComponent < ViewComponent::Base
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
+
class DataComponent < ViewComponent::Base
|
37
|
+
def initialize(data_foo:)
|
38
|
+
@data_foo = data_foo
|
39
|
+
end
|
40
|
+
|
41
|
+
def call
|
42
|
+
"<div data-foo=\"#{@data_foo}\"></div>"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
36
46
|
RSpec.configure do |config|
|
37
47
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rux
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cameron Dutro
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-05-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: parser
|
@@ -97,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
97
|
- !ruby/object:Gem::Version
|
98
98
|
version: '0'
|
99
99
|
requirements: []
|
100
|
-
rubygems_version: 3.
|
100
|
+
rubygems_version: 3.4.5
|
101
101
|
signing_key:
|
102
102
|
specification_version: 4
|
103
103
|
summary: A jsx-inspired way to write view components.
|