-
Notifications
You must be signed in to change notification settings - Fork 554
feat(doc,drive): normalize curly quotes and CRLF in --selection-with-ellipsis #621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
herbertliu
wants to merge
2
commits into
main
Choose a base branch
from
feat/doc-selection-normalization
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package common | ||
|
|
||
| import "strings" | ||
|
|
||
| // NormalizeSelectionWithEllipsis returns a canonical form of a user-typed | ||
| // --selection-with-ellipsis value suitable for server-side matching, along | ||
| // with a flag indicating whether any rewrite happened. | ||
| // | ||
| // The Lark docx store keeps punctuation in a canonical shape — straight ASCII | ||
| // quotes, LF line endings — while user-provided selection strings often come | ||
| // from pasted prose that has been auto-corrected to curly quotes, CRLF, or | ||
| // other typographic variants. Matching is strict byte-level, so a curly/ | ||
| // straight mismatch on a single character is enough to defeat the whole | ||
| // selection. | ||
| // | ||
| // The normalization set is deliberately conservative: only transformations | ||
| // that are virtually always safe (typographic quotes and CR line endings) | ||
| // are applied. Full/half-width Latin punctuation or CJK punctuation is left | ||
| // alone, since those can legitimately appear verbatim in the document body. | ||
| func NormalizeSelectionWithEllipsis(s string) (string, bool) { | ||
| if s == "" { | ||
| return s, false | ||
| } | ||
| out := s | ||
| // Curly single quotes → ASCII apostrophe. | ||
| out = strings.ReplaceAll(out, "\u2018", "'") | ||
| out = strings.ReplaceAll(out, "\u2019", "'") | ||
| // Curly double quotes → ASCII double quote. | ||
| out = strings.ReplaceAll(out, "\u201C", "\"") | ||
| out = strings.ReplaceAll(out, "\u201D", "\"") | ||
| // CRLF / standalone CR → LF. Lark stores LF internally; sending CRLF in | ||
| // a selection would require the document to contain literal CR bytes, | ||
| // which it never does. | ||
| out = strings.ReplaceAll(out, "\r\n", "\n") | ||
| out = strings.ReplaceAll(out, "\r", "\n") | ||
| return out, out != s | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package common | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestNormalizeSelectionWithEllipsis(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| input string | ||
| want string | ||
| wantChanged bool | ||
| }{ | ||
| { | ||
| name: "empty passes through", | ||
| input: "", | ||
| want: "", | ||
| wantChanged: false, | ||
| }, | ||
| { | ||
| name: "cjk-only selection is untouched", | ||
| input: "欢迎大家多给反馈", | ||
| want: "欢迎大家多给反馈", | ||
| wantChanged: false, | ||
| }, | ||
| { | ||
| name: "ascii-only selection is untouched", | ||
| input: "hello world", | ||
| want: "hello world", | ||
| wantChanged: false, | ||
| }, | ||
| { | ||
| name: "curly single quotes normalized", | ||
| input: "\u2018That\u2019s All\u2019", | ||
| want: "'That's All'", | ||
| wantChanged: true, | ||
| }, | ||
| { | ||
| name: "curly double quotes normalized", | ||
| input: "he said \u201Chello\u201D", | ||
| want: "he said \"hello\"", | ||
| wantChanged: true, | ||
| }, | ||
| { | ||
| name: "mixed curly + straight normalized", | ||
| input: "start\u2019s...end", | ||
| want: "start's...end", | ||
| wantChanged: true, | ||
| }, | ||
| { | ||
| name: "crlf collapsed to lf", | ||
| input: "line1\r\nline2", | ||
| want: "line1\nline2", | ||
| wantChanged: true, | ||
| }, | ||
| { | ||
| name: "standalone cr collapsed to lf", | ||
| input: "line1\rline2", | ||
| want: "line1\nline2", | ||
| wantChanged: true, | ||
| }, | ||
| { | ||
| name: "already lf is untouched", | ||
| input: "line1\nline2", | ||
| want: "line1\nline2", | ||
| wantChanged: false, | ||
| }, | ||
| { | ||
| name: "chinese punctuation deliberately untouched", | ||
| input: "你好,世界", | ||
| want: "你好,世界", | ||
| wantChanged: false, | ||
| }, | ||
| { | ||
| name: "fullwidth latin deliberately untouched", | ||
| input: "ABC", | ||
| want: "ABC", | ||
| wantChanged: false, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| got, changed := NormalizeSelectionWithEllipsis(tt.input) | ||
| if got != tt.want { | ||
| t.Errorf("NormalizeSelectionWithEllipsis(%q) = %q, want %q", tt.input, got, tt.want) | ||
| } | ||
| if changed != tt.wantChanged { | ||
| t.Errorf("NormalizeSelectionWithEllipsis(%q) changed=%v, want %v", tt.input, changed, tt.wantChanged) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.