-
Notifications
You must be signed in to change notification settings - Fork 590
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| } |
| 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) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -109,7 +109,7 @@ var DocMediaInsert = common.Shortcut{ | |
| } | ||
| mediaType := runtime.Str("type") | ||
| caption := runtime.Str("caption") | ||
| selection := strings.TrimSpace(runtime.Str("selection-with-ellipsis")) | ||
| selection, _ := common.NormalizeSelectionWithEllipsis(strings.TrimSpace(runtime.Str("selection-with-ellipsis"))) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Silent normalization here (and also at line 254): the |
||
| hasSelection := selection != "" | ||
| fileViewType := fileViewMap[runtime.Str("file-view")] | ||
|
|
||
|
|
@@ -251,7 +251,7 @@ var DocMediaInsert = common.Shortcut{ | |
| } | ||
| fmt.Fprintf(runtime.IO().ErrOut, "Root block ready: %s (%d children)\n", parentBlockID, insertIndex) | ||
|
|
||
| selection := strings.TrimSpace(runtime.Str("selection-with-ellipsis")) | ||
| selection, _ := common.NormalizeSelectionWithEllipsis(strings.TrimSpace(runtime.Str("selection-with-ellipsis"))) | ||
| if selection != "" { | ||
| before := runtime.Bool("before") | ||
| // Redact the selection when logging — it is copied verbatim from | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.