Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mastodon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Pierre Boudes
mastodon
Commits
684a970b
Unverified
Commit
684a970b
authored
2 years ago
by
Christian Schmidt
Committed by
GitHub
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Unescape HTML entities (#24019)
parent
9dfe2dbd
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
app/lib/plain_text_formatter.rb
+5
-1
5 additions, 1 deletion
app/lib/plain_text_formatter.rb
spec/lib/plain_text_formatter_spec.rb
+56
-5
56 additions, 5 deletions
spec/lib/plain_text_formatter_spec.rb
with
61 additions
and
6 deletions
app/lib/plain_text_formatter.rb
+
5
−
1
View file @
684a970b
...
...
@@ -18,7 +18,7 @@ class PlainTextFormatter
if
local?
text
else
strip_tags
(
insert_newlines
).
chomp
html_entities
.
decode
(
strip_tags
(
insert_newlines
)
)
.
chomp
end
end
...
...
@@ -27,4 +27,8 @@ class PlainTextFormatter
def
insert_newlines
text
.
gsub
(
NEWLINE_TAGS_RE
)
{
|
match
|
"
#{
match
}
\n
"
}
end
def
html_entities
HTMLEntities
.
new
end
end
This diff is collapsed.
Click to expand it.
spec/lib/plain_text_formatter_spec.rb
+
56
−
5
View file @
684a970b
...
...
@@ -6,7 +6,7 @@ RSpec.describe PlainTextFormatter do
describe
'#to_s'
do
subject
{
described_class
.
new
(
status
.
text
,
status
.
local?
).
to_s
}
context
'
given a post with local status
'
do
context
'
when status is local
'
do
let
(
:status
)
{
Fabricate
(
:status
,
text:
'<p>a text by a nerd who uses an HTML tag in text</p>'
,
uri:
nil
)
}
it
'returns the raw text'
do
...
...
@@ -14,12 +14,63 @@ RSpec.describe PlainTextFormatter do
end
end
context
'
given a post with remote status
'
do
context
'
when status is remote
'
do
let
(
:remote_account
)
{
Fabricate
(
:account
,
domain:
'remote.test'
,
username:
'bob'
,
url:
'https://remote.test/'
)
}
let
(
:status
)
{
Fabricate
(
:status
,
account:
remote_account
,
text:
'<p>Hello</p><script>alert("Hello")</script>'
)
}
it
'returns tag-stripped text'
do
expect
(
subject
).
to
eq
'Hello'
context
'when text contains inline HTML tags'
do
let
(
:status
)
{
Fabricate
(
:status
,
account:
remote_account
,
text:
'<b>Lorem</b> <em>ipsum</em>'
)
}
it
'strips the tags'
do
expect
(
subject
).
to
eq
'Lorem ipsum'
end
end
context
'when text contains <p> tags'
do
let
(
:status
)
{
Fabricate
(
:status
,
account:
remote_account
,
text:
'<p>Lorem</p><p>ipsum</p>'
)
}
it
'inserts a newline'
do
expect
(
subject
).
to
eq
"Lorem
\n
ipsum"
end
end
context
'when text contains a single <br> tag'
do
let
(
:status
)
{
Fabricate
(
:status
,
account:
remote_account
,
text:
'Lorem<br>ipsum'
)
}
it
'inserts a newline'
do
expect
(
subject
).
to
eq
"Lorem
\n
ipsum"
end
end
context
'when text contains consecutive <br> tag'
do
let
(
:status
)
{
Fabricate
(
:status
,
account:
remote_account
,
text:
'Lorem<br><br><br>ipsum'
)
}
it
'inserts a single newline'
do
expect
(
subject
).
to
eq
"Lorem
\n
ipsum"
end
end
context
'when text contains HTML entity'
do
let
(
:status
)
{
Fabricate
(
:status
,
account:
remote_account
,
text:
'Lorem & ipsum ❤'
)
}
it
'unescapes the entity'
do
expect
(
subject
).
to
eq
'Lorem & ipsum ❤'
end
end
context
'when text contains <script> tag'
do
let
(
:status
)
{
Fabricate
(
:status
,
account:
remote_account
,
text:
'Lorem <script> alert("Booh!") </script>ipsum'
)
}
it
'strips the tag and its contents'
do
expect
(
subject
).
to
eq
'Lorem ipsum'
end
end
context
'when text contains an HTML comment tags'
do
let
(
:status
)
{
Fabricate
(
:status
,
account:
remote_account
,
text:
'Lorem <!-- Booh! -->ipsum'
)
}
it
'strips the comment'
do
expect
(
subject
).
to
eq
'Lorem ipsum'
end
end
end
end
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment