LoginSignup
3
2

More than 5 years have passed since last update.

underscoreコードリーディング(escape)

Posted at

underscoreに詳しくないので、勉強半分でソースコードを読む。

利用するバージョン

underscore.js(v1.8.3)

escapeとは

underscorejs.orgのescape

こんな説明。

_.escape(string)

Escapes a string for insertion into HTML, replacing &, <, >, ", `, and ' characters.

_.escape('Curly, Larry & Moe');
=> "Curly, Larry &amp; Moe"

HTMLに挿入するための文字列をエスケープします。
エスケープするのは&, <, >, ", `, 'です。

underscore.escape

コード的にはこのあたり。

  // Functions for escaping and unescaping strings to/from HTML interpolation.
  var createEscaper = function(map) {
    var escaper = function(match) {
      return map[match];
    };
    // Regexes for identifying a key that needs to be escaped
    var source = '(?:' + _.keys(map).join('|') + ')';
    var testRegexp = RegExp(source);
    var replaceRegexp = RegExp(source, 'g');
    return function(string) {
      string = string == null ? '' : '' + string;
      return testRegexp.test(string) ? string.replace(replaceRegexp, escaper) : string;
    };
  };
  _.escape = createEscaper(escapeMap);

_.escapeはcreateEscaperにescapeMapを渡す関数である。
createEscaperはmapを引数とする関数。内容は以下。
escaperを定義する。引数はmatch。map[match]を返す。
sourceに'(?:' + _.keys(map).join('|') + ')'を代入する。
testRegexpにRegExpのsourceを渡したものを代入する。
replaceRegepにRegExpにsource,'g'を渡したものを代入する。

返り値は以下の処理をもつ関数。
stringに、stringがnullだった場合は空文字を、そうでない場合はstring型にしたstringを代入する。
関数の返り値はstringに対してtestRegexp.testした結果がtrueの場合はstring.replaceにreplaceRegexp,escaperを渡したもの、falseの場合はstringを返す。

escapeMapは以下。

underscore.escapeMap

  // List of HTML entities for escaping.
  var escapeMap = {
    '&': '&amp;',
    '<': '&lt;',
    '>': '&gt;',
    '"': '&quot;',
    "'": '&#x27;',
    '`': '&#x60;'
  };

escapeMapは&,<,>,",',`の各文字と、特殊文字の対応表。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2