chiark / gitweb /
documentation: trim spaces from the right only if nothing is found.
authorVladimír Vondruš <mosra@centrum.cz>
Thu, 18 Jul 2019 15:24:50 +0000 (17:24 +0200)
committerVladimír Vondruš <mosra@centrum.cz>
Thu, 18 Jul 2019 15:48:56 +0000 (17:48 +0200)
With unconditional trimming it was not possible to narrow down results
for page titles containing spaces (or subpages, which are separated by
double right arrow surrounded by spaces).

documentation/search.js
documentation/test/js-test-data/searchdata.b85
documentation/test/js-test-data/searchdata.bin
documentation/test/populate-js-test-data.py
documentation/test/test-search.js

index 8b8d78286d5aa85fefc611bdd3b396a7177fbef0..67e8e7031293d523317477f6061c21de17e277da 100644 (file)
@@ -232,8 +232,10 @@ var Search = {
     /* Returns the values in UTF-8, but input is in whatever shitty 16bit
        encoding JS has */
     search: function(searchString) {
-        /* Normalize the search string first, convert to UTF-8 */
-        searchString = this.toUtf8(searchString.toLowerCase().trim());
+        /* Normalize the search string first, convert to UTF-8 and trim spaces
+           from the left. From the right they're trimmed only if nothing is
+           found, see below. */
+        searchString = this.toUtf8(searchString.toLowerCase().replace(/^\s+/,''));
 
         /* TODO: maybe i could make use of InputEvent.data and others here */
 
@@ -277,8 +279,18 @@ var Search = {
                 break;
             }
 
-            /* Character not found, exit */
-            if(!found) break;
+            /* Character not found */
+            if(!found) {
+                /* If we found everything except spaces at the end, pretend the
+                   spaces aren't there. On the other hand, we *do* want to
+                   try searching with the spaces first -- it can narrow down
+                   the result list for page names or show subpages (which are
+                   after a lookahead barrier that's a space). */
+                if(!searchString.substr(foundPrefix).trim().length)
+                    searchString = searchString.substr(0, foundPrefix);
+
+                break;
+            }
         }
 
         /* Save the whole found prefix for next time */
index 1de2e51861749ca20e300b3aef944a31cef9bfc3..1a915d15adc2c718c1652bd6410e1338910a9330 100644 (file)
@@ -1 +1 @@
-O+!-w2LOZt004pl003kG000310RR921ONaj009U904M+f4gdgd009&L0BHdL0{{R4AOHX<00ATb04M+fDgXd(00A%n0BHaLHUI!^00BGz06GBy0suk)fI0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gE0A~OJ5&-~i0RagB7y$rb00ABW0CWHWCIJ9r00OE20AVZv0A&FH2LJ#8JOKb@00BS&0A~OJMgag}00B$^0B`^SQUL&B00CG50CfNXUI74e00CqH03ZMXY5@Sd00D3T0Kx$Q1^{*efFJ+?d;tJu00D#n0A~OJiU9y&00sB}0BvXh0Cq9~0CJE40B~Lb0COw=03bsE07+W_06KpF07;bq064b*08PyR01(>%02uZF0003200|EP002#4bZ7u>VQpn|aA9L*O<{CsE@*UZYybcf2s%1#X>KTKZgealX>N2W03&T_ZU6uPIyzQmV{~tF0Ap-nb8}5$bZB2OUolo?V{~tFE@*UZYyton20A)zX>KSfAY*TCb94YBZE0=*0025VQekdqWdLJrVRLg$VRUF;F<&uKVQyz-E@*UZYy<!o20A)zX>KSfAY*TCb94YBZE0>$VP|CkaA9X<E@*UZYz6=TAi}#KQ*~l+VP|Ckb9G{HVP|D7Xmo9C1OQTHV{~C|XKZBz00;m80y;WUWn*+GDFO-s4gnVh8~`A2VP|D-VQpn|aA9L*V{Bn_b7pmJV*mgE
\ No newline at end of file
index dfc16df1941fdb5db84fbdca6c2c75cbb9a6cf42..bf52b592e6216c2a2878aa969e203be31057bd2c 100644 (file)
Binary files a/documentation/test/js-test-data/searchdata.bin and b/documentation/test/js-test-data/searchdata.bin differ
index 9903ee84b83669b1e7edfbe4d56b96e25b5dd3a2..a6410a9488b7ba2fd0da17b23ee3c062bc2d358c 100755 (executable)
@@ -70,7 +70,10 @@ index = map.add("Math::Range::min() const", "classMath_1_1Range.html#min", suffi
 trie.insert("math::range::min()", index, lookahead_barriers=[4, 11])
 trie.insert("range::min()", index, lookahead_barriers=[5])
 trie.insert("min()", index)
-trie.insert("subpage", map.add("Page » Subpage", "subpage.html", flags=ResultFlag.from_type(ResultFlag.NONE, EntryType.PAGE)))
+trie.insert("page", map.add("Page", "page.html", flags=ResultFlag.from_type(ResultFlag.NONE, EntryType.PAGE)))
+index = map.add("Page » Subpage", "subpage.html", flags=ResultFlag.from_type(ResultFlag.NONE, EntryType.PAGE))
+trie.insert("page » subpage", index, lookahead_barriers=[4])
+trie.insert("subpage", index)
 
 trie.insert("rectangle", map.add("Rectangle", "", alias=range_index))
 trie.insert("rect", map.add("Rectangle::Rect()", "", suffix_length=2, alias=range_index))
index ec45d2189d99dde5a068eb40de8f4bbe7dbb1f58..0efdd41d01d51c0ed15493a03b45c837a3ea0ff5 100644 (file)
@@ -79,7 +79,7 @@ const { StringDecoder } = require('string_decoder');
 /* Verify that base85-decoded file is equivalent to the binary */
 {
     let binary = fs.readFileSync(path.join(__dirname, "js-test-data/searchdata.bin"));
-    assert.equal(binary.byteLength, 674);
+    assert.equal(binary.byteLength, 745);
     let b85 = fs.readFileSync(path.join(__dirname, "js-test-data/searchdata.b85"), {encoding: 'utf-8'});
     assert.deepEqual(new DataView(binary.buffer.slice(binary.byteOffset, binary.byteOffset + binary.byteLength)), new DataView(Search.base85decode(b85), 0, binary.byteLength));
 }
@@ -115,7 +115,7 @@ const { StringDecoder } = require('string_decoder');
 {
     let buffer = fs.readFileSync(path.join(__dirname, "js-test-data/searchdata.bin"));
     assert.ok(Search.init(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength)));
-    assert.equal(Search.dataSize, 674);
+    assert.equal(Search.dataSize, 745);
     assert.equal(Search.symbolCount, "7 symbols (0.7 kB)");
     assert.equal(Search.maxResults, 100);
 
@@ -181,9 +181,6 @@ const { StringDecoder } = require('string_decoder');
           suffixLength: 3 }], 'tor'];
     assert.deepEqual(Search.search('vec'), resultsForVec);
 
-    /* Uppercase things and spaces */
-    assert.deepEqual(Search.search(' Vec  '), resultsForVec);
-
     /* Not found */
     assert.deepEqual(Search.search('pizza'), [[], '']);
 
@@ -191,7 +188,7 @@ const { StringDecoder } = require('string_decoder');
     assert.deepEqual(Search.search('su'), [[
         { name: Search.toUtf8('Page » Subpage'),
           url: 'subpage.html',
-          flags: 0,
+          flags: 8, /* has prefix */
           cssClass: 'm-success',
           typeName: 'page',
           suffixLength: 5 }], 'bpage']);
@@ -220,11 +217,63 @@ const { StringDecoder } = require('string_decoder');
           suffixLength: 8 }], '']);
 }
 
+/* Search with spaces */
+{
+    let buffer = fs.readFileSync(path.join(__dirname, "js-test-data/searchdata.bin"));
+    assert.ok(Search.init(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength)));
+    assert.equal(Search.dataSize, 745);
+    assert.equal(Search.symbolCount, "7 symbols (0.7 kB)");
+    assert.equal(Search.maxResults, 100);
+
+    /* No spaces */
+    assert.deepEqual(Search.search('pa'), [[
+        { name: Search.toUtf8('Page'),
+          url: 'page.html',
+          flags: 0,
+          cssClass: 'm-success',
+          typeName: 'page',
+          suffixLength: 2 }], 'ge']);
+
+    /* Spaces from the left are always trimmed */
+    assert.deepEqual(Search.search('  \t pa'), [[
+        { name: Search.toUtf8('Page'),
+          url: 'page.html',
+          flags: 0,
+          cssClass: 'm-success',
+          typeName: 'page',
+          suffixLength: 2 }], 'ge']);
+
+    /* Spaces from the right are trimmed if nothing is found */
+    assert.deepEqual(Search.search('pa \n '), [[
+        { name: Search.toUtf8('Page'),
+          url: 'page.html',
+          flags: 0,
+          cssClass: 'm-success',
+          typeName: 'page',
+          suffixLength: 2 }], 'ge']);
+
+    /* But not if they have results */
+    assert.deepEqual(Search.search('page'), [[
+        { name: Search.toUtf8('Page'),
+          url: 'page.html',
+          flags: 0,
+          cssClass: 'm-success',
+          typeName: 'page',
+          suffixLength: 0 }], '']);
+    assert.deepEqual(Search.search('page '), [[
+        { name: Search.toUtf8('Page » Subpage'),
+          url: 'subpage.html',
+          flags: 8, /* has prefix */
+          cssClass: 'm-success',
+          typeName: 'page',
+          suffixLength: 10 }], Search.toUtf8('» subpage')]);
+}
+
 /* Search, limiting the results to 3 */
 {
     let buffer = fs.readFileSync(path.join(__dirname, "js-test-data/searchdata.bin"));
     assert.ok(Search.init(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength), 3));
-    assert.equal(Search.dataSize, 674);
+    assert.equal(Search.dataSize, 745);
     assert.equal(Search.symbolCount, "7 symbols (0.7 kB)");
     assert.equal(Search.maxResults, 3);
     assert.deepEqual(Search.search('m'), [[
@@ -252,7 +301,7 @@ const { StringDecoder } = require('string_decoder');
 {
     let b85 = fs.readFileSync(path.join(__dirname, "js-test-data/searchdata.b85"), {encoding: 'utf-8'});
     assert.ok(Search.load(b85));
-    assert.equal(Search.dataSize, 676); /* some padding on the end, that's okay */
+    assert.equal(Search.dataSize, 748); /* some padding on the end, that's okay */
     assert.equal(Search.symbolCount, "7 symbols (0.7 kB)");
     assert.equal(Search.maxResults, 100);
     assert.deepEqual(Search.search('min'), [[