Skip to content

Commit

Permalink
fix: Don't truncate speech containing , (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed May 24, 2021
1 parent 2354c96 commit 2c39190
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 21 deletions.
8 changes: 8 additions & 0 deletions .changeset/quick-goats-sniff.md
@@ -0,0 +1,8 @@
---
"screen-reader-testing-library": patch
---

Speech containing a comma is no longer truncated

For example, `Choose time, selected time is ...` previously resulted in `["Choose tim"]`.
The previous speech parser only used a regular expression which cannot handle context-sensitive languages.
22 changes: 19 additions & 3 deletions src/__tests__/speechLogParser.js
Expand Up @@ -200,8 +200,7 @@ it("ignores commands", () => {
"Search…",
],
Array [
"Too much output to announc",
"",
"Too much output to announce, navigate to rows manually to readteardown JestAssertionError: expect(received).toEqual(expected) // deep equality - Expected - 2 + Received + 2 Array [ Array [ + \\"angChangeCommand (\\\\",
],
Array [
"nvda.ini (Working Tree) - nvda-snapshot-testing - Visual Studio Code",
Expand All @@ -217,7 +216,7 @@ it("ignores commands", () => {
"complementary landmark",
],
Array [
"Terminal ",
"Terminal 2, PowerShell Integrated Console",
"edit",
"blank",
],
Expand All @@ -234,3 +233,20 @@ it("ignores commands", () => {
]
`);
});

it("is context sensitive", () => {
expect(
extractSpeechLines(
"Speaking [LangChangeCommand ('en'), 'clickable', 'main landmark', 'button', 'Choose time, selected time is 12:00 AM']"
)
).toMatchInlineSnapshot(`
Array [
Array [
"clickable",
"main landmark",
"button",
"Choose time, selected time is 12:00 AM",
],
]
`);
});
53 changes: 35 additions & 18 deletions src/logParser.js
Expand Up @@ -10,27 +10,44 @@ function extractSpeechLines(nvdaLog) {
})
.map((line) => {
// In: "Speaking ['speech1', 'speech2', 'speech 3']"
// Out: "['speech1', 'speech2', 'speech 3']"
// Out: "'speech1', 'speech2', 'speech 3'"
const listText = line.trim().replace(/^Speaking \[([^\]]+)\]/, "$1");

// In: "['speech1', 'speech2', 'speech 3']"
// Out: the corresponding array structure in JS with added quotes
return listText
.split(",")
.map((rawSpeech) => {
const speech = rawSpeech.trim();
if (speech.startsWith("'")) {
return speech.slice(1, -1);
// light-weight parser for
// In: "'speech1', 'speech2, other', 'speech 3'"
// Put: ['speech1', 'speech2, other', 'speech 3']
/**
* @type {string[]}
*/
const spoken = [];
/**
* @type {'type' | 'command' | 'speech'}
*/
let currentlyParsing = "type";
let speech = "";
for (const token of listText) {
if (currentlyParsing === "type") {
if (token === "'") {
currentlyParsing = "speech";
} else if (!/(\s|,)/.test(token)) {
currentlyParsing = "command";
}
// ignore commands for now
return null;
})
.filter(
/**
* @returns {speech is string}
*/
(speech) => speech !== null
);
} else if (currentlyParsing === "command") {
if (token === ",") {
currentlyParsing = "type";
}
} else if (currentlyParsing === "speech") {
if (token === "'") {
spoken.push(speech);
speech = "";
currentlyParsing = "type";
} else {
speech += token;
}
}
}

return spoken;
});
}

Expand Down

0 comments on commit 2c39190

Please sign in to comment.