Needlworks / Textcube

Textcube : Brand yourself! / Personalized web publishing platform with multi-user support
http://www.textcube.org
Other
207 stars 55 forks source link

stripHTML() strips necessary whitespaces at mobile #1780

Closed Snack-X closed 9 years ago

Snack-X commented 9 years ago

모바일 환경 상에서 페이지 상의 <pre> 태그의 내용이 한 줄로 붙어나오는 문제가 있습니다. library/view/view.phpgetEntryContentView() 함수에서 호출하는 stripHTML() 에서 모든 연속된 whitespace character를 한개의 space로 치환하면서 생기는 문제로 보입니다.

아래는 테스트 코드이며, 실행 결과는 https://ideone.com/RZu0HS 를 참조하셔도 됩니다.

<?php

// from library/function/html.php

function stripHTML($text, $allowTags = array()) {
    $text = preg_replace('/<(script|style)[^>]*>.*?<\/\1>/si', '', $text);
    if(count($allowTags) == 0)
        $text = preg_replace('/<[\w\/!]+[^>]*>/', '', $text);
    else {
        preg_match_all('/<\/?([\w!]+)[^>]*?>/s', $text, $matches);
        for($i=0; $i<count($matches[0]); $i++) {
            if (!in_array(strtolower($matches[1][$i]), $allowTags))
                $text = str_replace($matches[0][$i], '', $text);
        }
    }
    $text = preg_replace('/&nbsp;?|\xc2\xa0\x20/', ' ', $text);
    $text = trim(preg_replace('/\s+/', ' ', $text));
    if(!empty($text))
        $text = str_replace(array('&#39;', '&apos;', '&quot;'), array('\'', '\'', '"'), $text);
    return $text;
}

// sample content

$input = <<<CODE
<pre>#include <stdio.h>

int main() {
  printf("Hello, world!");
  return 0;
}
</pre>
CODE;

echo "Without stripHTML:\n";
echo $input;
echo "\n\n";

// from library/view/view.php getEntryContentView()

$output = stripHTML($input, array('a', 'abbr', 'acronym', 'address','b', 'blockquote', 'br', 'caption', 'cite', 'code', 'dd', 'del', 'dfn', 'div', 'dl', 'dt', 'em', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'iframe', 'img', 'ins', 'kbd', 'li', 'ol', 'p', 'pre', 'q', 's', 'samp', 'span', 'strike', 'strong', 'sub', 'sup', 'table', 'tbody', 'td', 'tfoot', 'th', 'thead', 'tr', 'u', 'ul', 'var'));

echo "With stripHTML:\n";
echo $output;