eskimo-html 3.0.0.pre.2 → 3.0.0.pre.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '09dc7cd04be64cacab00e2faa22c4e494c0de733cfa5901be9e7d9ff5d4e2d83'
4
- data.tar.gz: 17e778f0a32fc66a25a01f02af689dbd30ad0a11d7cecbb71e88646f7e3431da
3
+ metadata.gz: e6e296637f454ce72ec9339abf7b8f97b548532fb981b2353abe9207f0368754
4
+ data.tar.gz: d7e7d8a01d127e659f2fed601fd5154145d11ffb91fe9ebbcbd76212e8a4fadc
5
5
  SHA512:
6
- metadata.gz: 0707a2398290443e95ae539e75432df8d0ee5d07eea81e10bf9dca9ed9641334bb03cd1f4ded68280b43d02d176fc37c53f3dd70220a35b08432a76563340c4d
7
- data.tar.gz: fe80ed672329e21f2fbb20813515c3af310ee3446e85842e29776d46ad6a8f56940bc8bb13602a0c95906b1f6dbf959d7e964576a3dafce4f7a4108e41b7c577
6
+ metadata.gz: 0ea26664becbbab824135ac9e03b78d3d8d8d9482daf96c4e78a46fc67d6ca95638f7adb7280ec8ffb66fa28dceddc142b41e6ddd410080dda9b0684349474d3
7
+ data.tar.gz: cb2ad2cbf060bb8d1f577bf6a7a0aebdb00036676773d6bd22c8862b49ed51ca3226d350245bc53c951b32fdde30ea2513d6ef951a8c9f682ffac0f7f053e938
@@ -1,50 +1,52 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Base HTMLElement component which takes a {tag_name}, a set of {attributes},
4
- # and optionally a set of {children} and turns them into a beautiful -- believe
5
- # it or not -- HTML tag:
6
- #
7
- # <x-foo <!-- attributes -->>
8
- # <!-- children -->
9
- # </x-foo>
10
- #
11
- # See {ASCII::Component} for how this works under the hood since it's similar.
12
- class Eskimo::HTML::Component
13
- # Mapping of Ruby attribute name to HTML attribute name; some words like
14
- # "class" are reserved and are problematic when passed as attributes so this
15
- # hash supports a method to rewrite those.
16
- ATTRIBUTE_REWRITES = {
17
- 'className' => 'class'
18
- }
19
-
20
- attr_reader :attributes
21
- attr_reader :children
22
- attr_reader :tag_name
23
-
24
- def initialize(attributes = {}, &children)
25
- @attributes = attributes
26
- @children = children
27
- end
28
-
29
- def render(render:, **)
30
- tag_with_attributes = "#{tag_name} #{serialize_attributes}"
31
-
32
- "<#{tag_with_attributes.strip}>#{render[@children]}</#{tag_name}>"
33
- end
34
-
35
- private
36
-
37
- def serialize_attributes
38
- attributes.map do |(k, v)|
39
- k = ATTRIBUTE_REWRITES.fetch(k.to_s, k.to_s)
40
-
41
- case v
42
- when true
43
- "#{k}=\"#{k}\""
44
- when false
45
- else
46
- "#{k}=\"#{v}\""
47
- end
48
- end.join(' ')
3
+ module Eskimo::HTML
4
+ # Base HTMLElement component which takes a {tag_name}, a set of {attributes},
5
+ # and optionally a set of {children} and turns them into a beautiful -- believe
6
+ # it or not -- HTML tag:
7
+ #
8
+ # <x-foo <!-- attributes -->>
9
+ # <!-- children -->
10
+ # </x-foo>
11
+ #
12
+ # See {ASCII::Component} for how this works under the hood since it's similar.
13
+ class Component
14
+ # Mapping of Ruby attribute name to HTML attribute name; some words like
15
+ # "class" are reserved and are problematic when passed as attributes so this
16
+ # hash supports a method to rewrite those.
17
+ ATTRIBUTE_REWRITES = {
18
+ 'className' => 'class'
19
+ }
20
+
21
+ attr_reader :attributes
22
+ attr_reader :children
23
+ attr_reader :tag_name
24
+
25
+ def initialize(attributes = {}, &children)
26
+ @attributes = attributes
27
+ @children = children
28
+ end
29
+
30
+ def render(render:, **)
31
+ tag_with_attributes = "#{tag_name} #{serialize_attributes}"
32
+
33
+ "<#{tag_with_attributes.strip}>#{render[@children]}</#{tag_name}>"
34
+ end
35
+
36
+ private
37
+
38
+ def serialize_attributes
39
+ attributes.map do |(k, v)|
40
+ k = ATTRIBUTE_REWRITES.fetch(k.to_s, k.to_s)
41
+
42
+ case v
43
+ when true
44
+ "#{k}=\"#{k}\""
45
+ when false
46
+ else
47
+ "#{k}=\"#{v}\""
48
+ end
49
+ end.join(' ')
50
+ end
49
51
  end
50
52
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'eskimo/html/autogen_tags'
4
+
3
5
  Eskimo::HTML::AUTOGEN_TAGS.each do |tag|
4
6
  eval <<~RUBY
5
7
  class Eskimo::HTML::#{tag.capitalize} < Eskimo::HTML::Component
@@ -1,18 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Eskimo::HTML::Html < Eskimo::HTML::Component
4
- def initialize(doctype: 'html', **)
5
- @doctype = doctype
6
- @tag_name = 'html'
7
- super
8
- @attributes.delete(:doctype)
9
- end
10
-
11
- def render(**)
12
- if @doctype.to_s.empty?
3
+ module Eskimo::HTML
4
+ class Html < Component
5
+ def initialize(doctype: 'html', **)
6
+ @doctype = doctype
7
+ @tag_name = 'html'
13
8
  super
14
- else
15
- "<!DOCTYPE #{@doctype}>\n" + super
9
+ @attributes.delete(:doctype)
10
+ end
11
+
12
+ def render(**)
13
+ if @doctype.to_s.empty?
14
+ super
15
+ else
16
+ "<!DOCTYPE #{@doctype}>\n" + super
17
+ end
16
18
  end
17
19
  end
18
20
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Eskimo
4
4
  module HTML
5
- VERSION = '3.0.0.pre.2'
5
+ VERSION = '3.0.0.pre.3'
6
6
  end
7
7
  end
@@ -3,17 +3,33 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  RSpec.describe Eskimo::HTML::Component do
6
- it 'accepts a block of children' do
7
- class CustomComponent < Eskimo::HTML::Component
8
- def tag_name
9
- 'foo'
10
- end
6
+ class CustomComponent < Eskimo::HTML::Component
7
+ def tag_name
8
+ 'foo'
11
9
  end
10
+ end
12
11
 
12
+ it 'accepts a block of children' do
13
13
  expect(
14
14
  Eskimo::Core::Renderer.new.apply {
15
15
  CustomComponent.new { 'hello world!' }
16
16
  }
17
17
  ).to eq("<foo>hello world!</foo>")
18
18
  end
19
+
20
+ it 'sets attribute="attribute" if its value is "true"' do
21
+ expect(
22
+ Eskimo::Core::Renderer.new.apply {
23
+ CustomComponent.new(disabled: true) { 'hello world!' }
24
+ }
25
+ ).to eq("<foo disabled=\"disabled\">hello world!</foo>")
26
+ end
27
+
28
+ it 'omits attribute="attribute" if its value is "false"' do
29
+ expect(
30
+ Eskimo::Core::Renderer.new.apply {
31
+ CustomComponent.new(disabled: false) { 'hello world!' }
32
+ }
33
+ ).to eq("<foo>hello world!</foo>")
34
+ end
19
35
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eskimo-html
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0.pre.2
4
+ version: 3.0.0.pre.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ahmad Amireh
@@ -14,16 +14,16 @@ dependencies:
14
14
  name: eskimo-core
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 3.0.0.pre.2
19
+ version: 3.0.0.pre.3
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 3.0.0.pre.2
26
+ version: 3.0.0.pre.3
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement