Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
SDK
/
exoplayer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
7323b535
authored
Apr 02, 2020
by
ibaker
Committed by
Oliver Woodman
Apr 06, 2020
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Add support for multi-line strings to SpannedToHtmlConverter
PiperOrigin-RevId: 304355717
parent
f3c7c88d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
3 deletions
library/ui/src/main/java/com/google/android/exoplayer2/ui/SpannedToHtmlConverter.java
library/ui/src/test/java/com/google/android/exoplayer2/ui/SpannedToHtmlConverterTest.java
library/ui/src/main/java/com/google/android/exoplayer2/ui/SpannedToHtmlConverter.java
View file @
7323b535
...
...
@@ -33,6 +33,7 @@ import java.util.ArrayList;
import
java.util.Collections
;
import
java.util.Comparator
;
import
java.util.List
;
import
java.util.regex.Pattern
;
/**
* Utility class to convert from <a
...
...
@@ -44,6 +45,9 @@ import java.util.List;
// TODO: Add support for more span types - only a small selection are currently implemented.
/* package */
final
class
SpannedToHtmlConverter
{
// Matches /n and /r/n in ampersand-encoding (returned from Html.escapeHtml).
private
static
final
Pattern
NEWLINE_PATTERN
=
Pattern
.
compile
(
"( )? "
);
private
SpannedToHtmlConverter
()
{}
/**
...
...
@@ -67,7 +71,7 @@ import java.util.List;
return
""
;
}
if
(!(
text
instanceof
Spanned
))
{
return
Html
.
escapeHtml
(
text
);
return
escapeHtml
(
text
);
}
Spanned
spanned
=
(
Spanned
)
text
;
SparseArray
<
Transition
>
spanTransitions
=
findSpanTransitions
(
spanned
);
...
...
@@ -76,7 +80,7 @@ import java.util.List;
int
previousTransition
=
0
;
for
(
int
i
=
0
;
i
<
spanTransitions
.
size
();
i
++)
{
int
index
=
spanTransitions
.
keyAt
(
i
);
html
.
append
(
Html
.
escapeHtml
(
spanned
.
subSequence
(
previousTransition
,
index
)));
html
.
append
(
escapeHtml
(
spanned
.
subSequence
(
previousTransition
,
index
)));
Transition
transition
=
spanTransitions
.
get
(
index
);
Collections
.
sort
(
transition
.
spansRemoved
,
SpanInfo
.
FOR_CLOSING_TAGS
);
...
...
@@ -90,7 +94,7 @@ import java.util.List;
previousTransition
=
index
;
}
html
.
append
(
Html
.
escapeHtml
(
spanned
.
subSequence
(
previousTransition
,
spanned
.
length
())));
html
.
append
(
escapeHtml
(
spanned
.
subSequence
(
previousTransition
,
spanned
.
length
())));
return
html
.
toString
();
}
...
...
@@ -187,6 +191,11 @@ import java.util.List;
return
transition
;
}
private
static
String
escapeHtml
(
CharSequence
text
)
{
String
escaped
=
Html
.
escapeHtml
(
text
);
return
NEWLINE_PATTERN
.
matcher
(
escaped
).
replaceAll
(
"<br>"
);
}
private
static
final
class
SpanInfo
{
/**
* Sort by end index (descending), then by opening tag and then closing tag (both ascending, for
...
...
library/ui/src/test/java/com/google/android/exoplayer2/ui/SpannedToHtmlConverterTest.java
View file @
7323b535
...
...
@@ -133,6 +133,20 @@ public class SpannedToHtmlConverterTest {
}
@Test
public
void
convert_handlesLinebreakInUnspannedString
()
{
String
html
=
SpannedToHtmlConverter
.
convert
(
"String with\nnew line and\r\ncrlf style too"
);
assertThat
(
html
).
isEqualTo
(
"String with<br>new line and<br>crlf style too"
);
}
@Test
public
void
convert_doesntConvertAmpersandLineFeedToBrTag
()
{
String
html
=
SpannedToHtmlConverter
.
convert
(
"String with new line ampersand code"
);
assertThat
(
html
).
isEqualTo
(
"String with&#10;new line ampersand code"
);
}
@Test
public
void
convert_escapesUnrecognisedTagInSpannedString
()
{
SpannableString
spanned
=
new
SpannableString
(
"String with <foo>unrecognised</foo> tags"
);
spanned
.
setSpan
(
...
...
@@ -147,6 +161,13 @@ public class SpannedToHtmlConverterTest {
}
@Test
public
void
convert_handlesLinebreakInSpannedString
()
{
String
html
=
SpannedToHtmlConverter
.
convert
(
"String with\nnew line and\r\ncrlf style too"
);
assertThat
(
html
).
isEqualTo
(
"String with<br>new line and<br>crlf style too"
);
}
@Test
public
void
convert_convertsNonAsciiCharactersToAmpersandCodes
()
{
String
html
=
SpannedToHtmlConverter
.
convert
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment