Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
774e77c2
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,发现更多精彩内容 >>
未验证
提交
774e77c2
编写于
12月 10, 2019
作者:
E
Eileen M. Uchitelle
提交者:
GitHub
12月 10, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #37918 from joelhawksley/class_names_helper
Introduce class_names helper
上级
e36097a9
f24734a7
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
45 addition
and
20 deletion
+45
-20
actionview/lib/action_view/helpers/tag_helper.rb
actionview/lib/action_view/helpers/tag_helper.rb
+34
-20
actionview/test/template/tag_helper_test.rb
actionview/test/template/tag_helper_test.rb
+11
-0
未找到文件。
actionview/lib/action_view/helpers/tag_helper.rb
浏览文件 @
774e77c2
...
@@ -94,7 +94,7 @@ def boolean_tag_option(key)
...
@@ -94,7 +94,7 @@ def boolean_tag_option(key)
def
tag_option
(
key
,
value
,
escape
)
def
tag_option
(
key
,
value
,
escape
)
case
value
case
value
when
Array
,
Hash
when
Array
,
Hash
value
=
build_tag_values
(
value
)
if
key
.
to_s
==
"class"
value
=
TagHelper
.
build_tag_values
(
value
)
if
key
.
to_s
==
"class"
value
=
escape
?
safe_join
(
value
,
" "
)
:
value
.
join
(
" "
)
value
=
escape
?
safe_join
(
value
,
" "
)
:
value
.
join
(
" "
)
else
else
value
=
escape
?
ERB
::
Util
.
unwrapped_html_escape
(
value
)
:
value
.
to_s
value
=
escape
?
ERB
::
Util
.
unwrapped_html_escape
(
value
)
:
value
.
to_s
...
@@ -104,25 +104,6 @@ def tag_option(key, value, escape)
...
@@ -104,25 +104,6 @@ def tag_option(key, value, escape)
end
end
private
private
def
build_tag_values
(
*
args
)
tag_values
=
[]
args
.
each
do
|
tag_value
|
case
tag_value
when
Hash
tag_value
.
each
do
|
key
,
val
|
tag_values
<<
key
if
val
end
when
Array
tag_values
<<
build_tag_values
(
*
tag_value
).
presence
else
tag_values
<<
tag_value
.
to_s
if
tag_value
.
present?
end
end
tag_values
.
compact
.
flatten
end
def
prefix_tag_option
(
prefix
,
key
,
value
,
escape
)
def
prefix_tag_option
(
prefix
,
key
,
value
,
escape
)
key
=
"
#{
prefix
}
-
#{
key
.
to_s
.
dasherize
}
"
key
=
"
#{
prefix
}
-
#{
key
.
to_s
.
dasherize
}
"
unless
value
.
is_a?
(
String
)
||
value
.
is_a?
(
Symbol
)
||
value
.
is_a?
(
BigDecimal
)
unless
value
.
is_a?
(
String
)
||
value
.
is_a?
(
Symbol
)
||
value
.
is_a?
(
BigDecimal
)
...
@@ -309,6 +290,19 @@ def content_tag(name, content_or_options_with_block = nil, options = nil, escape
...
@@ -309,6 +290,19 @@ def content_tag(name, content_or_options_with_block = nil, options = nil, escape
end
end
end
end
# Returns a string of class names built from +args+.
#
# ==== Examples
# class_names("foo", "bar")
# # => "foo bar"
# class_names({ foo: true, bar: false })
# # => "foo"
# class_names(nil, false, 123, "", "foo", { bar: true })
# # => "foo bar"
def
class_names
(
*
args
)
safe_join
(
build_tag_values
(
*
args
),
" "
)
end
# Returns a CDATA section with the given +content+. CDATA sections
# Returns a CDATA section with the given +content+. CDATA sections
# are used to escape blocks of text containing characters which would
# are used to escape blocks of text containing characters which would
# otherwise be recognized as markup. CDATA sections begin with the string
# otherwise be recognized as markup. CDATA sections begin with the string
...
@@ -339,6 +333,26 @@ def escape_once(html)
...
@@ -339,6 +333,26 @@ def escape_once(html)
end
end
private
private
def
build_tag_values
(
*
args
)
tag_values
=
[]
args
.
each
do
|
tag_value
|
case
tag_value
when
Hash
tag_value
.
each
do
|
key
,
val
|
tag_values
<<
key
if
val
end
when
Array
tag_values
<<
build_tag_values
(
*
tag_value
).
presence
else
tag_values
<<
tag_value
.
to_s
if
tag_value
.
present?
end
end
tag_values
.
compact
.
flatten
end
module_function
:build_tag_values
def
tag_builder
def
tag_builder
@tag_builder
||=
TagBuilder
.
new
(
self
)
@tag_builder
||=
TagBuilder
.
new
(
self
)
end
end
...
...
actionview/test/template/tag_helper_test.rb
浏览文件 @
774e77c2
...
@@ -338,6 +338,17 @@ def test_tag_builder_with_unescaped_conditional_hash_classes
...
@@ -338,6 +338,17 @@ def test_tag_builder_with_unescaped_conditional_hash_classes
assert_equal
"<p class=
\"
song play>
\"
>limelight</p>"
,
str
assert_equal
"<p class=
\"
song play>
\"
>limelight</p>"
,
str
end
end
def
test_class_names
assert_equal
"song play"
,
class_names
([
"song"
,
{
"play"
:
true
}])
assert_equal
"song"
,
class_names
({
"song"
:
true
,
"play"
:
false
})
assert_equal
"song"
,
class_names
([{
"song"
:
true
},
{
"play"
:
false
}])
assert_equal
"song"
,
class_names
({
song:
true
,
play:
false
})
assert_equal
"song"
,
class_names
([{
song:
true
},
nil
,
false
])
assert_equal
"song"
,
class_names
([
"song"
,
{
foo:
false
}])
assert_equal
"song play"
,
class_names
({
"song"
:
true
,
"play"
:
true
})
assert_equal
""
,
class_names
({
"song"
:
false
,
"play"
:
false
})
end
def
test_content_tag_with_data_attributes
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>'
,
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"'
})
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录