mirror of
https://github.com/bitwarden/browser
synced 2025-12-20 10:13:31 +00:00
Enhance trimAndRemoveNonPrintableText to support extended characters (#15919)
Co-authored-by: Jonathan Prusik <jprusik@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
7d23a076de
commit
931f8650cf
@@ -1757,6 +1757,54 @@ describe("CollectAutofillContentService", () => {
|
|||||||
|
|
||||||
expect(parsedText).toEqual("Hello! This is a test string.");
|
expect(parsedText).toEqual("Hello! This is a test string.");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("preserves extended Latin letters like Š and ć", () => {
|
||||||
|
const text = "Šifra ćevapčići korisnika";
|
||||||
|
const result = collectAutofillContentService["trimAndRemoveNonPrintableText"](text);
|
||||||
|
expect(result).toEqual("Šifra ćevapčići korisnika");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("removes zero-width and control characters", () => {
|
||||||
|
const text = "Hello\u200B\u200C\u200D\u2060World\x00\x1F!";
|
||||||
|
const result = collectAutofillContentService["trimAndRemoveNonPrintableText"](text);
|
||||||
|
expect(result).toEqual("Hello World !");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("removes leading and trailing whitespace", () => {
|
||||||
|
const text = " padded text with spaces ";
|
||||||
|
const result = collectAutofillContentService["trimAndRemoveNonPrintableText"](text);
|
||||||
|
expect(result).toEqual("padded text with spaces");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("replaces multiple whitespaces (tabs, newlines, spaces) with one space", () => {
|
||||||
|
const text = "one\t\ntwo \n three\t\tfour";
|
||||||
|
const result = collectAutofillContentService["trimAndRemoveNonPrintableText"](text);
|
||||||
|
expect(result).toEqual("one two three four");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("preserves emoji and symbols", () => {
|
||||||
|
const text = "Text with emoji 🐍🚀 and ©®✓ symbols";
|
||||||
|
const result = collectAutofillContentService["trimAndRemoveNonPrintableText"](text);
|
||||||
|
expect(result).toEqual("Text with emoji 🐍🚀 and ©®✓ symbols");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("handles RTL and LTR marks", () => {
|
||||||
|
const text = "abc\u200F\u202Edеf";
|
||||||
|
const result = collectAutofillContentService["trimAndRemoveNonPrintableText"](text);
|
||||||
|
expect(result).toEqual("abc dеf");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("handles mathematical unicode letters", () => {
|
||||||
|
const text = "Unicode math: 𝒜𝒷𝒸𝒹";
|
||||||
|
const result = collectAutofillContentService["trimAndRemoveNonPrintableText"](text);
|
||||||
|
expect(result).toEqual("Unicode math: 𝒜𝒷𝒸𝒹");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("removes only invisible non-printables, keeps Japanese", () => {
|
||||||
|
const text = "これは\u200Bテストです";
|
||||||
|
const result = collectAutofillContentService["trimAndRemoveNonPrintableText"](text);
|
||||||
|
expect(result).toEqual("これは テストです");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("recursivelyGetTextFromPreviousSiblings", () => {
|
describe("recursivelyGetTextFromPreviousSiblings", () => {
|
||||||
|
|||||||
@@ -713,7 +713,7 @@ export class CollectAutofillContentService implements CollectAutofillContentServ
|
|||||||
*/
|
*/
|
||||||
private trimAndRemoveNonPrintableText(textContent: string): string {
|
private trimAndRemoveNonPrintableText(textContent: string): string {
|
||||||
return (textContent || "")
|
return (textContent || "")
|
||||||
.replace(/[^\x20-\x7E]+|\s+/g, " ") // Strip out non-primitive characters and replace multiple spaces with a single space
|
.replace(/\p{C}+|\s+/gu, " ") // Strip out non-printable characters and replace multiple spaces with a single space
|
||||||
.trim(); // Trim leading and trailing whitespace
|
.trim(); // Trim leading and trailing whitespace
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user