次の入力があります。
Hi! How are you? <script>//NOT EVIL!</script>
Wassup? :P
LOOOL!!! :D :D :D
次に、絵文字ライブラリを実行すると、次のようになります。
Hi! How are you? <script>//NOT EVIL!</script>
Wassup? <img class="smiley" alt="" title="tongue, :P" src="ui/emoticons/15.gif">
LOOOL!!! <img class="smiley" alt="" title="big grin, :D" src="ui/emoticons/5.gif"> <img class="smiley" alt="" title="big grin, :P" src="ui/emoticons/5.gif"> <img class="smiley" alt="" title="big grin, :P" src="ui/emoticons/5.gif">
XSSを防ぐためにHTMLエンティティをエスケープする機能があります。したがって、最初の行の生の入力で実行すると、次のようになります。
Hi! How are you? <script>//NOT EVIL!</script>
ここで、すべての入力をエスケープする必要がありますが、同時に、絵文字を初期状態で保持する必要があります。だから<:-P
顔文字があるとそのままで にならない<:-P
。
私は感情化されたテキストで正規表現分割を実行することを考えていました。次に、各部分を個別に処理し、文字列を連結しますが、正規表現を簡単にバイパスできるかどうかはわかりません。形式は常に次のようになります。
[<img class="smiley" alt="]
[empty string]
[" title="]
[one of the values from a big list]
[, ]
[another value from the list (may be matching original emoticon)]
[" src="ui/emoticons/]
[integer from Y to X]
[.gif">]
リストを使用すると、20-30-40 個の絵文字を含む可能性のあるテキストに対してその正規表現を実行する必要があるため、時間がかかる場合があります。さらに、処理するテキスト メッセージが 5-10-15 件ある場合もあります。これに対するエレガントな解決策は何でしょうか? これには、サードパーティのライブラリまたは jQuery を使用する準備ができています。PHPの前処理も可能です。