@@ -6,35 +6,111 @@ const DEFAULT_TAG_HANDLERS: Readonly<Record<string, string>> = Object.assign(
66 }
77)
88
9- function tagPercentEncode ( source : string ) {
10- return encodeURI ( source ) . replace ( / ! / g, '%21' )
9+ // YAML 1.2.2, [40] ns-tag-char and [39] ns-uri-char, apart from alphanumerics.
10+ const TAG_SUFFIX_CHARS = "-#;/?:@&=+$_.~*'()"
11+ const TAG_URI_CHARS = TAG_SUFFIX_CHARS + '!,[]'
12+ const HEX = '0123456789ABCDEF'
13+
14+ function percentByte ( byte : number ) {
15+ return '%' + HEX [ byte >> 4 ] + HEX [ byte & 0x0F ]
16+ }
17+
18+ function tagPercentEncode ( source : string , isSuffix : boolean ) {
19+ const allowed = isSuffix ? TAG_SUFFIX_CHARS : TAG_URI_CHARS
20+ let result = ''
21+
22+ for ( let index = 0 ; index < source . length ; index ++ ) {
23+ let ch = source . charCodeAt ( index )
24+
25+ if ( ( ch >= 0x30 && ch <= 0x39 ) ||
26+ ( ch >= 0x41 && ch <= 0x5A ) ||
27+ ( ch >= 0x61 && ch <= 0x7A ) ||
28+ allowed . includes ( source [ index ] ) ) {
29+ result += source [ index ]
30+ continue
31+ }
32+
33+ if ( ch >= 0xD800 && ch <= 0xDBFF ) {
34+ ch = 0x10000 + ( ( ch & 0x3FF ) << 10 ) + ( source . charCodeAt ( ++ index ) & 0x3FF )
35+ }
36+
37+ if ( ch < 0x80 ) {
38+ result += percentByte ( ch )
39+ } else if ( ch < 0x800 ) {
40+ result += percentByte ( 0xC0 | ( ch >> 6 ) ) + percentByte ( 0x80 | ( ch & 0x3F ) )
41+ } else if ( ch < 0x10000 ) {
42+ result += percentByte ( 0xE0 | ( ch >> 12 ) ) +
43+ percentByte ( 0x80 | ( ( ch >> 6 ) & 0x3F ) ) + percentByte ( 0x80 | ( ch & 0x3F ) )
44+ } else {
45+ result += percentByte ( 0xF0 | ( ch >> 18 ) ) +
46+ percentByte ( 0x80 | ( ( ch >> 12 ) & 0x3F ) ) +
47+ percentByte ( 0x80 | ( ( ch >> 6 ) & 0x3F ) ) + percentByte ( 0x80 | ( ch & 0x3F ) )
48+ }
49+ }
50+
51+ return result
52+ }
53+
54+ function tagPercentDecode ( source : string ) {
55+ let result = ''
56+ let index = 0
57+
58+ // Convert escaped UTF-8 bytes without validating malformed encodings.
59+ while ( index < source . length ) {
60+ if ( source . charCodeAt ( index ) !== 0x25 /* % */ ) {
61+ result += source [ index ++ ]
62+ continue
63+ }
64+
65+ let ch = parseInt ( source . slice ( index + 1 , index + 3 ) , 16 )
66+ index += 3
67+
68+ if ( ch >= 0xC0 ) {
69+ let remaining = ch < 0xE0 ? 1 : ch < 0xF0 ? 2 : 3
70+ ch &= ( 1 << ( 6 - remaining ) ) - 1
71+
72+ while ( remaining -- > 0 ) {
73+ ch = ( ch << 6 ) | ( parseInt ( source . slice ( index + 1 , index + 3 ) , 16 ) & 0x3F )
74+ index += 3
75+ }
76+ }
77+
78+ if ( ch < 0x10000 ) {
79+ result += String . fromCharCode ( ch )
80+ } else {
81+ ch -= 0x10000
82+ result += String . fromCharCode ( 0xD800 | ( ch >> 10 ) , 0xDC00 | ( ch & 0x3FF ) )
83+ }
84+ }
85+
86+ return result
1187}
1288
1389function tagNameFull ( rawTag : string , tagHandlers ?: Readonly < Record < string , string > > ) {
1490 if ( rawTag . startsWith ( '!<' ) && rawTag . endsWith ( '>' ) ) {
15- return decodeURIComponent ( rawTag . slice ( 2 , - 1 ) )
91+ return tagPercentDecode ( rawTag . slice ( 2 , - 1 ) )
1692 }
1793
1894 const handleEnd = rawTag . indexOf ( '!' , 1 )
1995 const handle = handleEnd === - 1 ? '!' : rawTag . slice ( 0 , handleEnd + 1 )
2096 const prefix = tagHandlers ?. [ handle ] ?? DEFAULT_TAG_HANDLERS [ handle ] ?? handle
2197
22- return decodeURIComponent ( prefix ) + decodeURIComponent ( rawTag . slice ( handle . length ) )
98+ return tagPercentDecode ( prefix ) + tagPercentDecode ( rawTag . slice ( handle . length ) )
2399}
24100
25101function tagNameShort ( fullTag : string ) {
26102 let tag = fullTag
27103
28104 if ( tag . charCodeAt ( 0 ) === 0x21 ) {
29105 tag = tag . slice ( 1 )
30- return `!${ tagPercentEncode ( tag ) } `
106+ return `!${ tagPercentEncode ( tag , true ) } `
31107 }
32108
33109 if ( tag . slice ( 0 , 18 ) === 'tag:yaml.org,2002:' ) {
34- return `!!${ tagPercentEncode ( tag . slice ( 18 ) ) } `
110+ return `!!${ tagPercentEncode ( tag . slice ( 18 ) , true ) } `
35111 }
36112
37- return `!<${ tagPercentEncode ( tag ) } >`
113+ return `!<${ tagPercentEncode ( tag , false ) } >`
38114}
39115
40116export {
0 commit comments