@@ -14,6 +14,18 @@ function commandExecutedByCmd(args: string[]): string {
1414 return remainder . slice ( 1 , - 1 ) ;
1515}
1616
17+ /**
18+ * Target `start` receives after cmd quote parsing: one outer quote pair is
19+ * removed, and each `""` inside it is one literal `"`. `%` is not rewritten.
20+ */
21+ function targetReceivedByStart ( executed : string ) : string {
22+ const prefix = 'start "" ' ;
23+ if ( ! executed . startsWith ( prefix ) ) throw new Error ( "not a start command" ) ;
24+ const quoted = executed . slice ( prefix . length ) ;
25+ if ( ! quoted . startsWith ( '"' ) || ! quoted . endsWith ( '"' ) ) throw new Error ( "target not quoted" ) ;
26+ return quoted . slice ( 1 , - 1 ) . replaceAll ( '""' , '"' ) ;
27+ }
28+
1729/** Split on `&` that is not inside double quotes, the way `cmd.exe` does. */
1830function splitUnquotedAmpersand ( command : string ) : string [ ] {
1931 const parts : string [ ] = [ ] ;
@@ -45,13 +57,15 @@ test("win32 打开带 & 的登录 URL 时,needapikey 留在同一条 start 命
4557 const executed = commandExecutedByCmd ( invocation . args ) ;
4658 expect ( executed ) . toBe ( `start "" "${ LOGIN_URL } "` ) ;
4759 expect ( splitUnquotedAmpersand ( executed ) ) . toEqual ( [ executed ] ) ;
48- expect ( executed ) . toContain ( "&needapikey=true" ) ;
60+ expect ( targetReceivedByStart ( executed ) ) . toBe ( LOGIN_URL ) ;
4961} ) ;
5062
51- test ( "win32 转义 cmd 会展开的 % 和引号" , ( ) => {
52- const invocation = buildOpenInvocation ( 'https://example.com/a%20b?"q"' , "win32" ) ;
53- const executed = commandExecutedByCmd ( invocation . args ) ;
54- expect ( executed ) . toBe ( 'start "" "https://example.com/a%%20b?""q"""' ) ;
63+ test ( "win32 传给 start 的目标保持原样,% 不会被写成 %%" , ( ) => {
64+ const target = 'https://example.com/a%20b?"q"&x=1' ;
65+ const executed = commandExecutedByCmd ( buildOpenInvocation ( target , "win32" ) . args ) ;
66+ expect ( executed ) . not . toContain ( "%%" ) ;
67+ expect ( targetReceivedByStart ( executed ) ) . toBe ( target ) ;
68+ expect ( splitUnquotedAmpersand ( executed ) ) . toEqual ( [ executed ] ) ;
5569} ) ;
5670
5771test ( "darwin / linux 直接把目标交给系统打开命令" , ( ) => {
0 commit comments