r111769 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r111768‎ | r111769 | r111770 >
Date:17:55, 17 February 2012
Author:jdlrobson
Status:ok
Tags:
Comment:
add tests for beta_opensearch.js

these tests mostly cover highlighting of keywords in
search results
one of the tests exposes a problem where the character ?
appears in the title. Fixed with adjustment to escapeJsString
using more complete code snippet from http://simonwillison.net/2010/Jul/4/escaping/
Modified paths:
  • /trunk/extensions/MobileFrontend/MobileFrontend.body.php (modified) (history)
  • /trunk/extensions/MobileFrontend/javascripts/beta_opensearch.js (modified) (history)
  • /trunk/extensions/MobileFrontend/tests/js/test_beta_opensearch.js (added) (history)

Diff [purge]

Index: trunk/extensions/MobileFrontend/tests/js/test_beta_opensearch.js
@@ -0,0 +1,69 @@
 2+var MFEOS = MobileFrontend.opensearch;
 3+var MFET = window.MobileFrontendTests;
 4+
 5+module("beta_opensearch.js - test highlight", {
 6+ setup: function() {
 7+ MFET.createFixtures();
 8+ MFEOS.init();
 9+ },
 10+ teardown: function() {
 11+ $("#betaos").remove();
 12+ MFET.cleanFixtures();
 13+ }
 14+});
 15+
 16+test("no results", function() {
 17+ MFEOS.writeResults([]);
 18+ strictEqual($("#results").text(), "No Results");
 19+});
 20+
 21+test("writeResults with highlighted text (case differs)", function() {
 22+ var results = [
 23+ { label: "Hello world", value: "/HelloWorld" },
 24+ { label: "Hello kitty", value: "/HelloKitty" }
 25+ ];
 26+ $("#search").val("el");
 27+ MFET.triggerEvent($("#search")[0], "keyup");
 28+ MFEOS.writeResults(results);
 29+ var pageLink = $("#results .suggestions-result a.search-result-item")[0];
 30+ var pageLink2 = $("#results .suggestions-result a.search-result-item")[1];
 31+ strictEqual($(pageLink).text(), "Hello world", "check the label is correct");
 32+ strictEqual($(pageLink).html(), "H<strong>el</strong>lo world", "check the highlight is correct");
 33+ strictEqual($(pageLink2).html(), "H<strong>el</strong>lo kitty", "check the highlight is correct");
 34+});
 35+
 36+test("writeResults with highlighted text (case differs)", function() {
 37+ var results = [
 38+ { label: "Hello world", value: "/HelloWorld" },
 39+ { label: "Hello kitty", value: "/HelloKitty" }
 40+ ];
 41+ $("#search").val("hel");
 42+ MFET.triggerEvent($("#search")[0], "keyup");
 43+ MFEOS.writeResults(results);
 44+ var pageLink = $("#results .suggestions-result a.search-result-item")[0];
 45+ strictEqual($(pageLink).html(), "<strong>Hel</strong>lo world", "check the highlight is correct");
 46+});
 47+
 48+test("writeResults with highlighted text (special character &amp;)", function() {
 49+ var results = [
 50+ { label: "Belle & Sebastian", value: "/B1" },
 51+ { label: "Belle & the Beast", value: "/B2" }
 52+ ];
 53+ $("#search").val("Belle & S");
 54+ MFET.triggerEvent($("#search")[0], "keyup");
 55+ MFEOS.writeResults(results);
 56+ var pageLink = $("#results .suggestions-result a.search-result-item")[0];
 57+ strictEqual($(pageLink).html(), "<strong>Belle &amp; S</strong>ebastian", "check the highlight is correct");
 58+});
 59+
 60+test("writeResults with highlighted text (special character ?)", function() {
 61+ var results = [
 62+ { label: "Title with ? in it", value: "/B1" }
 63+ ];
 64+ $("#search").val("with ?");
 65+ MFET.triggerEvent($("#search")[0], "keyup");
 66+ MFEOS.writeResults(results);
 67+ var pageLink = $("#results .suggestions-result a.search-result-item")[0];
 68+ strictEqual($(pageLink).html(), "Title <strong>with ?</strong> in it", "check the highlight is correct");
 69+});
 70+
Index: trunk/extensions/MobileFrontend/MobileFrontend.body.php
@@ -1302,6 +1302,13 @@
13031303 'localBasePath' => dirname( __FILE__ ),
13041304 'remoteExtPath' => 'MobileFrontend',
13051305 );
 1306+ $testModules['qunit']['ext.mobilefrontend.tests.beta'] = array(
 1307+ 'scripts' => array( 'tests/js/fixtures.js', 'javascripts/beta_application.js',
 1308+ 'javascripts/beta_opensearch.js', 'tests/js/test_beta_opensearch.js' ),
 1309+ 'dependencies' => array( ),
 1310+ 'localBasePath' => dirname( __FILE__ ),
 1311+ 'remoteExtPath' => 'MobileFrontend',
 1312+ );
13061313 return true;
13071314 }
13081315
Index: trunk/extensions/MobileFrontend/javascripts/beta_opensearch.js
@@ -40,6 +40,10 @@
4141 search.onfocus = function() {
4242 var pE, pT, pTT, rrd, rrdD,
4343 removeResultsEl;
 44+ sb = document.getElementById( 'searchbox' );
 45+ sq = document.getElementById( 'sq' );
 46+ content = document.getElementById( 'content' );
 47+ footer = document.getElementById( 'footer' );
4448 resetViewPort();
4549
4650 if ( zeroRatedBanner ) {
@@ -305,7 +309,7 @@
306310 }
307311
308312 function escapeJsString( str ) {
309 - return String( str ).replace( /\\/g, '\\\\' ).replace( /'/g, "\\'" ).replace( /\n/g, '\\n' );
 313+ return str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
310314 }
311315
312316 function writeResults( sections ) {

Status & tagging log