Path: blob/master/src/view/phui/PHUIInvisibleCharacterView.php
12249 views
<?php12/**3* API for replacing whitespace characters and some control characters with4* their printable representations. This is useful for debugging and5* displaying more helpful error messages to users.6*7*/8final class PHUIInvisibleCharacterView extends AphrontView {910private $inputText;11private $plainText = false;1213// This is a list of the common invisible characters that are14// actually typeable. Other invisible characters will simply15// be displayed as their hex representations.16private static $invisibleChars = array(17"\x00" => 'NULL',18"\t" => 'TAB',19"\n" => 'NEWLINE',20"\x20" => 'SPACE',21);2223public function __construct($input_text) {24$this->inputText = $input_text;25}2627public function setPlainText($plain_text) {28$this->plainText = $plain_text;29return $this;30}3132public function getStringParts() {33$input_text = $this->inputText;34$text_array = phutil_utf8v($input_text);3536for ($ii = 0; $ii < count($text_array); $ii++) {37$char = $text_array[$ii];38$char_hex = bin2hex($char);39if (array_key_exists($char, self::$invisibleChars)) {40$text_array[$ii] = array(41'special' => true,42'value' => '<'.self::$invisibleChars[$char].'>',43);44} else if (ord($char) < 32) {45$text_array[$ii] = array(46'special' => true,47'value' => '<0x'.$char_hex.'>',48);49} else {50$text_array[$ii] = array(51'special' => false,52'value' => $char,53);54}55}56return $text_array;57}5859private function renderHtmlArray() {60$html_array = array();61$parts = $this->getStringParts();62foreach ($parts as $part) {63if ($part['special']) {64$html_array[] = phutil_tag(65'span',66array('class' => 'invisible-special'),67$part['value']);68} else {69$html_array[] = $part['value'];70}71}72return $html_array;73}7475private function renderPlainText() {76$parts = $this->getStringParts();77$res = '';78foreach ($parts as $part) {79$res .= $part['value'];80}81return $res;82}8384public function render() {85require_celerity_resource('phui-invisible-character-view-css');86if ($this->plainText) {87return $this->renderPlainText();88} else {89return $this->renderHtmlArray();90}91}9293}949596