Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
2c2b0bea
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,发现更多精彩内容 >>
提交
2c2b0bea
编写于
5月 26, 2012
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add `:escape` option for `truncate`
This options can be used to not escape the result by default.
上级
eedc5135
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
29 addition
and
2 deletion
+29
-2
actionpack/CHANGELOG.md
actionpack/CHANGELOG.md
+5
-0
actionpack/lib/action_view/helpers/text_helper.rb
actionpack/lib/action_view/helpers/text_helper.rb
+5
-2
actionpack/test/template/text_helper_test.rb
actionpack/test/template/text_helper_test.rb
+19
-0
未找到文件。
actionpack/CHANGELOG.md
浏览文件 @
2c2b0bea
## Rails 4.0.0 (unreleased) ##
## Rails 4.0.0 (unreleased) ##
*
`truncate`
now always returns an escaped HTMl-safe string. The option
`:escape`
can be used as
false to not escape the result.
*Li Ellis Gallardo + Rafael Mendonça França*
*
`truncate`
now accepts a block to show extra content when the text is truncated.
*Li Ellis Gallardo*
*
`truncate`
now accepts a block to show extra content when the text is truncated.
*Li Ellis Gallardo*
*
Add
`week_field`
,
`week_field_tag`
,
`month_field`
,
`month_field_tag`
,
`datetime_local_field`
,
*
Add
`week_field`
,
`week_field_tag`
,
`month_field`
,
`month_field_tag`
,
`datetime_local_field`
,
...
...
actionpack/lib/action_view/helpers/text_helper.rb
浏览文件 @
2c2b0bea
...
@@ -64,7 +64,9 @@ def safe_concat(string)
...
@@ -64,7 +64,9 @@ def safe_concat(string)
#
#
# Pass a block if you want to show extra content when the text is truncated.
# Pass a block if you want to show extra content when the text is truncated.
#
#
# The result is marked as HTML-safe, but the it is escaped first.
# The result is marked as HTML-safe, but it is escaped by default, unless <tt>:escape</tt> is
# +false+. Care should be taken if +text+ contains HTML tags or entities, because truncation
# may produce invalid HTML (such as unbalanced or incomplete tags).
#
#
# truncate("Once upon a time in a world far far away")
# truncate("Once upon a time in a world far far away")
# # => "Once upon a time in a world..."
# # => "Once upon a time in a world..."
...
@@ -87,7 +89,8 @@ def truncate(text, options = {}, &block)
...
@@ -87,7 +89,8 @@ def truncate(text, options = {}, &block)
if
text
if
text
length
=
options
.
fetch
(
:length
,
30
)
length
=
options
.
fetch
(
:length
,
30
)
content
=
ERB
::
Util
.
html_escape
(
text
.
truncate
(
length
,
options
))
content
=
text
.
truncate
(
length
,
options
)
content
=
options
[
:escape
]
==
false
?
content
.
html_safe
:
ERB
::
Util
.
html_escape
(
content
)
content
<<
capture
(
&
block
)
if
block_given?
&&
text
.
length
>
length
content
<<
capture
(
&
block
)
if
block_given?
&&
text
.
length
>
length
content
content
end
end
...
...
actionpack/test/template/text_helper_test.rb
浏览文件 @
2c2b0bea
...
@@ -119,6 +119,15 @@ def test_truncate_should_escape_the_input
...
@@ -119,6 +119,15 @@ def test_truncate_should_escape_the_input
assert_equal
"Hello <sc..."
,
truncate
(
"Hello <script>code!</script>World!!"
,
:length
=>
12
)
assert_equal
"Hello <sc..."
,
truncate
(
"Hello <script>code!</script>World!!"
,
:length
=>
12
)
end
end
def
test_truncate_should_not_escape_the_input_with_escape_false
assert_equal
"Hello <sc..."
,
truncate
(
"Hello <script>code!</script>World!!"
,
:length
=>
12
,
:escape
=>
false
)
end
def
test_truncate_with_escape_false_should_be_html_safe
truncated
=
truncate
(
"Hello <script>code!</script>World!!"
,
:length
=>
12
,
:escape
=>
false
)
assert
truncated
.
html_safe?
end
def
test_truncate_with_block_should_be_html_safe
def
test_truncate_with_block_should_be_html_safe
truncated
=
truncate
(
"Here's a long test and I need a continue to read link"
,
:length
=>
27
)
{
link_to
'Continue'
,
'#'
}
truncated
=
truncate
(
"Here's a long test and I need a continue to read link"
,
:length
=>
27
)
{
link_to
'Continue'
,
'#'
}
assert
truncated
.
html_safe?
assert
truncated
.
html_safe?
...
@@ -129,6 +138,16 @@ def test_truncate_with_block_should_escape_the_input
...
@@ -129,6 +138,16 @@ def test_truncate_with_block_should_escape_the_input
truncate
(
"<script>code!</script>Here's a long test and I need a continue to read link"
,
:length
=>
27
)
{
link_to
'Continue'
,
'#'
}
truncate
(
"<script>code!</script>Here's a long test and I need a continue to read link"
,
:length
=>
27
)
{
link_to
'Continue'
,
'#'
}
end
end
def
test_truncate_with_block_should_not_escape_the_input_with_escape_false
assert_equal
"<script>code!</script>He...<a href=
\"
#
\"
>Continue</a>"
,
truncate
(
"<script>code!</script>Here's a long test and I need a continue to read link"
,
:length
=>
27
,
:escape
=>
false
)
{
link_to
'Continue'
,
'#'
}
end
def
test_truncate_with_block_with_escape_false_should_be_html_safe
truncated
=
truncate
(
"<script>code!</script>Here's a long test and I need a continue to read link"
,
:length
=>
27
,
:escape
=>
false
)
{
link_to
'Continue'
,
'#'
}
assert
truncated
.
html_safe?
end
def
test_truncate_with_block_should_escape_the_block
def
test_truncate_with_block_should_escape_the_block
assert_equal
"Here's a long test and I...<script>alert('foo');</script>"
,
assert_equal
"Here's a long test and I...<script>alert('foo');</script>"
,
truncate
(
"Here's a long test and I need a continue to read link"
,
:length
=>
27
)
{
"<script>alert('foo');</script>"
}
truncate
(
"Here's a long test and I need a continue to read link"
,
:length
=>
27
)
{
"<script>alert('foo');</script>"
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录