Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F104244238
PHUIInvisibleCharacterView.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Fri, Mar 7, 14:02
Size
2 KB
Mime Type
text/x-php
Expires
Sun, Mar 9, 14:02 (2 d)
Engine
blob
Format
Raw Data
Handle
24676691
Attached To
rPH Phabricator
PHUIInvisibleCharacterView.php
View Options
<?php
/**
* API for replacing whitespace characters and some control characters with
* their printable representations. This is useful for debugging and
* displaying more helpful error messages to users.
*
*/
final
class
PHUIInvisibleCharacterView
extends
AphrontView
{
private
$inputText
;
private
$plainText
=
false
;
// This is a list of the common invisible characters that are
// actually typeable. Other invisible characters will simply
// be displayed as their hex representations.
private
static
$invisibleChars
=
array
(
"
\x
00"
=>
'NULL'
,
"
\t
"
=>
'TAB'
,
"
\n
"
=>
'NEWLINE'
,
"
\x
20"
=>
'SPACE'
,
);
public
function
__construct
(
$input_text
)
{
$this
->
inputText
=
$input_text
;
}
public
function
setPlainText
(
$plain_text
)
{
$this
->
plainText
=
$plain_text
;
return
$this
;
}
public
function
getStringParts
()
{
$input_text
=
$this
->
inputText
;
$text_array
=
phutil_utf8v
(
$input_text
);
for
(
$ii
=
0
;
$ii
<
count
(
$text_array
);
$ii
++)
{
$char
=
$text_array
[
$ii
];
$char_hex
=
bin2hex
(
$char
);
if
(
array_key_exists
(
$char
,
self
::
$invisibleChars
))
{
$text_array
[
$ii
]
=
array
(
'special'
=>
true
,
'value'
=>
'<'
.
self
::
$invisibleChars
[
$char
].
'>'
,
);
}
else
if
(
ord
(
$char
)
<
32
)
{
$text_array
[
$ii
]
=
array
(
'special'
=>
true
,
'value'
=>
'<0x'
.
$char_hex
.
'>'
,
);
}
else
{
$text_array
[
$ii
]
=
array
(
'special'
=>
false
,
'value'
=>
$char
,
);
}
}
return
$text_array
;
}
private
function
renderHtmlArray
()
{
$html_array
=
array
();
$parts
=
$this
->
getStringParts
();
foreach
(
$parts
as
$part
)
{
if
(
$part
[
'special'
])
{
$html_array
[]
=
phutil_tag
(
'span'
,
array
(
'class'
=>
'invisible-special'
),
$part
[
'value'
]);
}
else
{
$html_array
[]
=
$part
[
'value'
];
}
}
return
$html_array
;
}
private
function
renderPlainText
()
{
$parts
=
$this
->
getStringParts
();
$res
=
''
;
foreach
(
$parts
as
$part
)
{
$res
.=
$part
[
'value'
];
}
return
$res
;
}
public
function
render
()
{
require_celerity_resource
(
'phui-invisible-character-view-css'
);
if
(
$this
->
plainText
)
{
return
$this
->
renderPlainText
();
}
else
{
return
$this
->
renderHtmlArray
();
}
}
}
Event Timeline
Log In to Comment