eskimo-html 3.0.0.pre.2 → 3.0.0.pre.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/eskimo/html/component.rb +48 -46
- data/lib/eskimo/html/components/autogen.rb +2 -0
- data/lib/eskimo/html/components/html.rb +14 -12
- data/lib/eskimo/html/version.rb +1 -1
- data/spec/component_spec.rb +21 -5
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e6e296637f454ce72ec9339abf7b8f97b548532fb981b2353abe9207f0368754
|
4
|
+
data.tar.gz: d7e7d8a01d127e659f2fed601fd5154145d11ffb91fe9ebbcbd76212e8a4fadc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0ea26664becbbab824135ac9e03b78d3d8d8d9482daf96c4e78a46fc67d6ca95638f7adb7280ec8ffb66fa28dceddc142b41e6ddd410080dda9b0684349474d3
|
7
|
+
data.tar.gz: cb2ad2cbf060bb8d1f577bf6a7a0aebdb00036676773d6bd22c8862b49ed51ca3226d350245bc53c951b32fdde30ea2513d6ef951a8c9f682ffac0f7f053e938
|
@@ -1,50 +1,52 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
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,18 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|
-
|
15
|
-
|
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
|
data/lib/eskimo/html/version.rb
CHANGED
data/spec/component_spec.rb
CHANGED
@@ -3,17 +3,33 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
RSpec.describe Eskimo::HTML::Component do
|
6
|
-
|
7
|
-
|
8
|
-
|
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.
|
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.
|
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.
|
26
|
+
version: 3.0.0.pre.3
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|