Skip to content

Commit

Permalink
Fix whitespace trimming in search page (#1625)
Browse files Browse the repository at this point in the history
* Fix whitespace trimming in search page

* Rename to trimTrailingWhitespacePlusSpace

* Only trimEnd for queryInput
  • Loading branch information
Kuuuube authored Nov 27, 2024
1 parent 3c3f858 commit 22d5f41
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
11 changes: 11 additions & 0 deletions ext/js/data/string-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,14 @@ export function readCodePointsBackward(text, position, count) {
}
return result;
}

/**
* Trims trailing whitespace and adds a space on the end if it needed trimming.
* @param {string} text
* @returns {string}
*/
export function trimTrailingWhitespacePlusSpace(text) {
const prevTextLength = text.length;
const textTrimmed = text.trimEnd();
return prevTextLength === textTrimmed.length ? textTrimmed : textTrimmed + ' ';
}
3 changes: 2 additions & 1 deletion ext/js/display/query-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import {EventDispatcher} from '../core/event-dispatcher.js';
import {log} from '../core/log.js';
import {trimTrailingWhitespacePlusSpace} from '../data/string-util.js';
import {querySelectorNotNull} from '../dom/query-selector.js';
import {convertHiraganaToKatakana, convertKatakanaToHiragana, isStringEntirelyKana} from '../language/ja/japanese.js';
import {TextScanner} from '../language/text-scanner.js';
Expand Down Expand Up @@ -304,7 +305,7 @@ export class QueryParser extends EventDispatcher {
termNode.className = 'query-parser-term';
termNode.dataset.offset = `${offset}`;
for (const {text, reading} of term) {
const trimmedText = text.trim();
const trimmedText = trimTrailingWhitespacePlusSpace(text);
if (reading.length === 0) {
termNode.appendChild(document.createTextNode(trimmedText));
} else {
Expand Down
2 changes: 1 addition & 1 deletion ext/js/display/search-display-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ export class SearchDisplayController {
this._searchBackButton.hidden = !showBackButton;

if (this._queryInput.value !== query) {
this._queryInput.value = query.trim();
this._queryInput.value = query.trimEnd();
this._updateSearchHeight(true);
}
this._setIntroVisible(!valid, animate);
Expand Down

0 comments on commit 22d5f41

Please sign in to comment.