lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kelv...@apache.org
Subject cvs commit: jakarta-lucene-sandbox/contributions/javascript/queryEscaper testQueryEscaper.html luceneQueryEscaper.js
Date Wed, 18 Feb 2004 06:20:15 GMT
kelvint     2004/02/17 22:20:15

  Added:       contributions/javascript/queryEscaper testQueryEscaper.html
                        luceneQueryEscaper.js
  Log:
  Initial commit.
  
  Revision  Changes    Path
  1.1                  jakarta-lucene-sandbox/contributions/javascript/queryEscaper/testQueryEscaper.html
  
  Index: testQueryEscaper.html
  ===================================================================
  <html>
  
  <head>
  <!-- Change Me -->
  <script language="JavaScript" src="jsUnitCore.js"></script>
  <script language="JavaScript" src="luceneQueryEscaper.js"></script>
  </head>
  <body>
  <script>
  function testGetQueryValue()
  {
    var str = "The quick brown fox jumped over the lazy dog";
    document.test.foobar.value = str;
    assertEquals("Not getting field value correctly", str, getQueryValue(document.test.foobar));
    assertEquals("Not getting string value correctly", str, getQueryValue(str));
  }
  
  function testNormalString()
  {
    var str = "The quick brown fox jumped over the lazy dog";
    assertEquals("Should not be escaping", str, escapeAsterisk(str));
    assertEquals("Should not be escaping", str, escapeQuotes(str));
    assertEquals("Should not be escaping", str, escapeColon(str));
    assertEquals("Should not be escaping", str, escapeQuestionMark(str));
    assertEquals("Should not be escaping", str, escapeExclamationMark(str));
  }
  
  function testEscapeAsterisk()
  {
    var str = "foo*";
    assertEquals("Not escaping " + str, "foo\\*", escapeAsterisk(str));
    
    str = " foo bar *";
    assertEquals("Not escaping " + str, " foo bar \\*", escapeAsterisk(str));
    
    str = "foo* bar*";
    assertEquals("Not escaping " + str, "foo\\* bar\\*", escapeAsterisk(str));  
  }
  
  function testEscapeQuotes()
  {
    var str = "\"foo\"";
    assertEquals("Not escaping " + str, "\\\"foo\\\"", escapeQuotes(str));
  }
  
  function testEscapeColon()
  {
    var str = "foo:bar zoo:zaa";
    assertEquals("Not escaping " + str, "foo\\:bar zoo\\:zaa", escapeColon(str));
  }
  
  function testEscapeQuestionMark()
  {
    var str = "foo? bar?";
    assertEquals("Not escaping " + str, "foo\\? bar\\?", escapeQuestionMark(str));
  }
  
  function testEscapeExclamationMark()
  {
    var str = "foo! bar!";
    assertEquals("Not escaping " + str, "foo\\! bar\\!", escapeExclamationMark(str));
  }
  
  function testEscapeParentheses()
  {
    var str = "foo (bar) (me)";
    assertEquals("Not escaping " + str, "foo \\(bar\\) \\(me\\)", escapeParentheses(str));
  }
  
  function testEscapeSquareBrackets()
  {
    var str = "foo [bar] [me]";
    assertEquals("Not escaping " + str, "foo \\[bar\\] \\[me\\]", escapeSquareBrackets(str));
  }
  
  function testEscapeBraces()
  {
    var str = "foo {bar} {me}";
    assertEquals("Not escaping " + str, "foo \\{bar\\} \\{me\\}", escapeBraces(str));
  }
  
  function testEscapeCaret()
  {
    var str = "f^oo bar^ me";
    assertEquals("Not escaping " + str, "f\\^oo bar\\^ me", escapeCaret(str));
  }
  
  function testEscapeSquiggle()
  {
    var str = "f~oo bar~ me";
    assertEquals("Not escaping " + str, "f\\~oo bar\\~ me", escapeSquiggle(str));
  }
  
  function testEscapeDoubleAmpersands()
  {
    var str = "foo && bar me";
    assertEquals("Not escaping " + str, "foo \\&\\& bar me", escapeDoubleAmpersands(str));
  
    str = "foo && bar& m&e";
    assertEquals("Not escaping " + str, "foo \\&\\& bar& m&e", escapeDoubleAmpersands(str));
  }
  
  function testEscapeDoubleBars()
  {
    var str = "foo || bar me";
    assertEquals("Not escaping " + str, "foo \\|\\| bar me", escapeDoubleBars(str));
  }
  
  function testDoEscapeQuery()
  {
    var str = "The: quick*} {~brown? ^fox! (jumped:[over] || me) \"the && lazy: dog\"";
    assertEquals("Not escaping " + str, "The\\: quick\\*\\} \\{\\~brown\\? \\^fox\\! \\(jumped\\:\\[over\\]
\\|\\| me\\) \\\"the \\&\\& lazy\\: dog\\\"", doEscapeQuery(str));
  }
  
  
  </script>
  <form name="test">
  <input type="text" name="foobar">
  </form>
  </body>
  </html>
  
  
  
  1.1                  jakarta-lucene-sandbox/contributions/javascript/queryEscaper/luceneQueryEscaper.js
  
  Index: luceneQueryEscaper.js
  ===================================================================
  // Special characters are + - && || ! ( ) { } [ ] ^ " ~ * ? : \
  // Special words are (case-sensitive) AND NOT OR
  // We escape the common ones, i.e. ! ? * ( ) " :
  
  // escapes a lucene query.
  // @param Form field that contains the query, or the query string
  function doEscapeQuery(queryArg)
  {
    var query = getQueryValue(queryArg);
    query = escapeAsterisk(query);
    query = escapeQuotes(query);
    query = escapeColon(query);
    query = escapeQuestionMark(query);
    query = escapeExclamationMark(query);
    query = escapeParentheses(query);  
    query = escapeSquareBrackets(query);  
    query = escapeBraces(query);  
    query = escapeCaret(query);  
    query = escapeSquiggle(query);  
    query = escapeDoubleAmpersands(query);  
    query = escapeDoubleBars(query);  
    return query;
  }
  
  function getQueryValue(queryArg)
  {
    var query;
    // check if its a form field
    if(typeof(queryArg.form) != "undefined")
    {
      query = queryArg.value;
    }
    else
    {
      query = queryArg;
    }
    return query;
  }
  
  function escapeAsterisk(query)
  {
    return query.replace(/[\*]/g, "\\*");
  }
  
  function escapeQuotes(query)
  {
    return query.replace(/[\"]/g, "\\\"");
  }
  
  function escapeColon(query)
  {
    return query.replace(/[\:]/g, "\\:");
  }
  
  function escapeQuestionMark(query)
  {
    return query.replace(/[?]/g, "\\?");
  }
  
  function escapeExclamationMark(query)
  {
    return query.replace(/[!]/g, "\\!");
  }
  
  function escapeParentheses(query)
  {
    return query.replace(/[(]/g, "\\(").replace(/[)]/g, "\\)");
  }
  
  function escapeSquareBrackets(query)
  {
    return query.replace(/[\[]/g, "\\[").replace(/[\]]/g, "\\]");
  }
  
  function escapeBraces(query)
  {
    return query.replace(/[{]/g, "\\{").replace(/[}]/g, "\\}");
  }
  
  function escapeCaret(query)
  {
    return query.replace(/[\^]/g, "\\^");
  }
  
  function escapeSquiggle(query)
  {
    return query.replace(/[~]/g, "\\~");
  }
  
  function escapeDoubleAmpersands(query)
  {
    return query.replace(/[&]{2}/g, "\\&\\&");
  }
  
  function escapeDoubleBars(query)
  {
    return query.replace(/[\|]{2}/g, "\\|\\|");
  }
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-dev-help@jakarta.apache.org


Mime
View raw message