Skip to content

Commit

Permalink
Merge pull request #4 from tolgee/stepangranat/fix-unicode-chars-inde…
Browse files Browse the repository at this point in the history
…xing

fix: double char unicode characters breaking placeholders
  • Loading branch information
stepan662 authored Nov 18, 2024
2 parents 8355fff + 9b70ba9 commit 6eb7603
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { EditorMulti } from "./editor/EditorMulti";

function App() {
const [value, setValue] = useState(
`I have {value, number, ::short} items in basket. This is <a href="test">italic</i>.`
`🐭-I have {value, number, ::short} items in basket. This is <a href="test">italic</i>.`
);
const [placeholders, setPlaceholders] = useState(true);

Expand Down
8 changes: 4 additions & 4 deletions src/parser/lezer/tokenizer/InputStreamLike.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
export const InputStreamLike = (input: string) => {
const codePoints = [...input].map((ch) => ch.charCodeAt(0));

let position = 0;

const self = {
get next() {
return codePoints[position] ?? -1;
const char = input[position];
return char ? input.charCodeAt(position) : -1;
},
get pos() {
return position;
},
peek(val: number) {
return codePoints[position + val] ?? -1;
const char = input[position + val];
return char ? input.charCodeAt(position + val) : -1;
},
advance(val = 1) {
position += val;
Expand Down
15 changes: 15 additions & 0 deletions src/parser/placeholders/getPlaceholders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,19 @@ describe("get placeholders", () => {
expect(placeholders![3].normalizedValue).toEqual("</a>");
expect(placeholders![4].normalizedValue).toEqual("{test}");
});

it("correctly indexes characters when unicode chars present - tags", () => {
const placeholders = getPlaceholders("🐭-<link>blog</link>");
const first = placeholders![0];
expect(first.normalizedValue).toEqual("<link>");
expect(first.position.start).toEqual(3);
expect(placeholders![1].normalizedValue).toEqual("</link>");
});

it("correctly indexes characters when unicode chars present - placeholders", () => {
const placeholders = getPlaceholders("🐭-{test}-");
const first = placeholders![0];
expect(first.normalizedValue).toEqual("{test}");
expect(first.position.start).toEqual(3);
});
});

0 comments on commit 6eb7603

Please sign in to comment.