Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
8c121ce9
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
8c121ce9
编写于
12月 04, 2019
作者:
A
Aaron Patterson
提交者:
GitHub
12月 04, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #37872 from joelhawksley/content-tag-hash-class-conditional
Add support for conditional values to TagBuilder
上级
77759521
54f418d9
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
93 addition
and
1 deletion
+93
-1
actionview/CHANGELOG.md
actionview/CHANGELOG.md
+4
-0
actionview/lib/action_view/helpers/tag_helper.rb
actionview/lib/action_view/helpers/tag_helper.rb
+27
-1
actionview/test/template/tag_helper_test.rb
actionview/test/template/tag_helper_test.rb
+62
-0
未找到文件。
actionview/CHANGELOG.md
浏览文件 @
8c121ce9
*
Add support for conditional values to TagBuilder.
*Joel Hawksley*
*
`ActionView::Helpers::FormOptionsHelper#select`
should mark option for
`nil`
as selected.
```ruby
...
...
actionview/lib/action_view/helpers/tag_helper.rb
浏览文件 @
8c121ce9
...
...
@@ -92,7 +92,9 @@ def boolean_tag_option(key)
end
def
tag_option
(
key
,
value
,
escape
)
if
value
.
is_a?
(
Array
)
case
value
when
Array
,
Hash
value
=
build_tag_values
(
value
)
value
=
escape
?
safe_join
(
value
,
" "
)
:
value
.
join
(
" "
)
else
value
=
escape
?
ERB
::
Util
.
unwrapped_html_escape
(
value
)
:
value
.
to_s
...
...
@@ -102,6 +104,25 @@ def tag_option(key, value, escape)
end
private
def
build_tag_values
(
*
args
)
tag_values
=
[]
args
.
each
do
|
tag_value
|
case
tag_value
when
String
tag_values
<<
tag_value
if
tag_value
.
present?
when
Hash
tag_value
.
each
do
|
key
,
val
|
tag_values
<<
key
if
val
end
when
Array
tag_values
<<
build_tag_values
(
*
tag_value
).
presence
end
end
tag_values
.
compact
.
flatten
end
def
prefix_tag_option
(
prefix
,
key
,
value
,
escape
)
key
=
"
#{
prefix
}
-
#{
key
.
to_s
.
dasherize
}
"
unless
value
.
is_a?
(
String
)
||
value
.
is_a?
(
Symbol
)
||
value
.
is_a?
(
BigDecimal
)
...
...
@@ -240,6 +261,9 @@ def method_missing(called, *args, **options, &block)
#
# tag("div", data: { name: 'Stephen', city_state: %w(Chicago IL) })
# # => <div data-name="Stephen" data-city-state="["Chicago","IL"]" />
#
# tag("div", class: { highlight: current_user.admin? })
# # => <div class="highlight" />
def
tag
(
name
=
nil
,
options
=
nil
,
open
=
false
,
escape
=
true
)
if
name
.
nil?
tag_builder
...
...
@@ -267,6 +291,8 @@ def tag(name = nil, options = nil, open = false, escape = true)
# # => <div class="strong"><p>Hello world!</p></div>
# content_tag(:div, "Hello world!", class: ["strong", "highlight"])
# # => <div class="strong highlight">Hello world!</div>
# content_tag(:div, "Hello world!", class: ["strong", { highlight: current_user.admin? }])
# # => <div class="strong highlight">Hello world!</div>
# content_tag("select", options, multiple: true)
# # => <select multiple="multiple">...options...</select>
#
...
...
actionview/test/template/tag_helper_test.rb
浏览文件 @
8c121ce9
...
...
@@ -246,6 +246,68 @@ def test_tag_builder_with_unescaped_empty_array_class
assert_equal
'<p class="">limelight</p>'
,
str
end
def
test_content_tag_with_conditional_hash_classes
str
=
content_tag
(
"p"
,
"limelight"
,
class:
{
"song"
:
true
,
"play"
:
false
})
assert_equal
"<p class=
\"
song
\"
>limelight</p>"
,
str
str
=
content_tag
(
"p"
,
"limelight"
,
class:
[{
"song"
:
true
},
{
"play"
:
false
}])
assert_equal
"<p class=
\"
song
\"
>limelight</p>"
,
str
str
=
content_tag
(
"p"
,
"limelight"
,
class:
{
song:
true
,
play:
false
})
assert_equal
"<p class=
\"
song
\"
>limelight</p>"
,
str
str
=
content_tag
(
"p"
,
"limelight"
,
class:
[{
song:
true
},
nil
,
false
])
assert_equal
"<p class=
\"
song
\"
>limelight</p>"
,
str
str
=
content_tag
(
"p"
,
"limelight"
,
class:
[
"song"
,
{
foo:
false
}])
assert_equal
"<p class=
\"
song
\"
>limelight</p>"
,
str
str
=
content_tag
(
"p"
,
"limelight"
,
class:
{
"song"
:
true
,
"play"
:
true
})
assert_equal
"<p class=
\"
song play
\"
>limelight</p>"
,
str
str
=
content_tag
(
"p"
,
"limelight"
,
class:
{
"song"
:
false
,
"play"
:
false
})
assert_equal
'<p class="">limelight</p>'
,
str
end
def
test_tag_builder_with_conditional_hash_classes
str
=
tag
.
p
"limelight"
,
class:
{
"song"
:
true
,
"play"
:
false
}
assert_equal
"<p class=
\"
song
\"
>limelight</p>"
,
str
str
=
tag
.
p
"limelight"
,
class:
[{
"song"
:
true
},
{
"play"
:
false
}]
assert_equal
"<p class=
\"
song
\"
>limelight</p>"
,
str
str
=
tag
.
p
"limelight"
,
class:
{
song:
true
,
play:
false
}
assert_equal
"<p class=
\"
song
\"
>limelight</p>"
,
str
str
=
tag
.
p
"limelight"
,
class:
[{
song:
true
},
nil
,
false
]
assert_equal
"<p class=
\"
song
\"
>limelight</p>"
,
str
str
=
tag
.
p
"limelight"
,
class:
[
"song"
,
{
foo:
false
}]
assert_equal
"<p class=
\"
song
\"
>limelight</p>"
,
str
str
=
tag
.
p
"limelight"
,
class:
{
"song"
:
true
,
"play"
:
true
}
assert_equal
"<p class=
\"
song play
\"
>limelight</p>"
,
str
str
=
tag
.
p
"limelight"
,
class:
{
"song"
:
false
,
"play"
:
false
}
assert_equal
'<p class="">limelight</p>'
,
str
end
def
test_content_tag_with_unescaped_conditional_hash_classes
str
=
content_tag
(
"p"
,
"limelight"
,
{
class:
{
"song"
:
true
,
"play>"
:
true
}
},
false
)
assert_equal
"<p class=
\"
song play>
\"
>limelight</p>"
,
str
str
=
content_tag
(
"p"
,
"limelight"
,
{
class:
[
"song"
,
{
"play>"
:
true
}]
},
false
)
assert_equal
"<p class=
\"
song play>
\"
>limelight</p>"
,
str
end
def
test_tag_builder_with_unescaped_conditional_hash_classes
str
=
tag
.
p
"limelight"
,
class:
{
"song"
:
true
,
"play>"
:
true
},
escape_attributes:
false
assert_equal
"<p class=
\"
song play>
\"
>limelight</p>"
,
str
str
=
tag
.
p
"limelight"
,
class:
[
"song"
,
{
"play>"
:
true
}],
escape_attributes:
false
assert_equal
"<p class=
\"
song play>
\"
>limelight</p>"
,
str
end
def
test_content_tag_with_data_attributes
assert_dom_equal
'<p data-number="1" data-string="hello" data-string-with-quotes="double"quote"party"">limelight</p>'
,
content_tag
(
"p"
,
"limelight"
,
data:
{
number:
1
,
string:
"hello"
,
string_with_quotes:
'double"quote"party"'
})
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录