@@ -128,13 +128,15 @@ const parseDataURI = (uri) => {
128128 * The payload as text, or `null` when reading it would not round-trip. A
129129 * percent-escaped payload is declined rather than decoded: how much of it the
130130 * author escaped is not recorded anywhere in the URI, so re-escaping would
131- * rewrite bytes nothing asked to change.
131+ * rewrite bytes nothing asked to change. So is one holding a `#`, where the
132+ * payload a browser reads ends and a fragment starts.
132133 * @param {ParsedDataURI } parsed the split URI
133134 * @returns {string | null } the payload as text
134135 */
135136const decodeDataURIPayload = ( parsed ) => {
136137 if ( ! parsed . base64 ) {
137- return parsed . payload . includes ( "%" ) ? null : parsed . payload ;
138+ const { payload } = parsed ;
139+ return payload . includes ( "%" ) || payload . includes ( "#" ) ? null : payload ;
138140 }
139141 // Decoding is lenient rather than throwing, so garbage decodes to something
140142 // that does not round-trip, which is the same answer: leave it alone.
@@ -144,6 +146,50 @@ const decodeDataURIPayload = (parsed) => {
144146 : null ;
145147} ;
146148
149+ /**
150+ * A `data:` URI's payload as text, for a caller that will render it, with the
151+ * language its media type names. `null` when there is nothing to offer: not a
152+ * `data:` URI, a media type naming no language, or a payload that would not
153+ * round-trip.
154+ * @param {string } uri the URI, unquoted and unescaped
155+ * @returns {{ parsed: ParsedDataURI, type: string, payload: string } | null } what it holds, or null
156+ */
157+ const readEmbeddedDataURI = ( uri ) => {
158+ const parsed = parseDataURI ( uri ) ;
159+ if ( parsed === null ) return null ;
160+ const type = languageOfMediaType ( parsed . mediaType ) ;
161+ if ( type === undefined ) return null ;
162+ const payload = decodeDataURIPayload ( parsed ) ;
163+ if ( payload === null || payload === "" ) return null ;
164+ return { parsed, type, payload } ;
165+ } ;
166+
167+ /**
168+ * Escape a payload for a `data:` URI written without `base64`, touching only
169+ * what would change what the URI means. The host's own quoting is the caller's.
170+ * @param {string } text the payload as text
171+ * @returns {string } the escaped payload
172+ */
173+ const encodeDataURIPayload = ( text ) => {
174+ // WHY: `%` starts an escape and `#` a fragment. The URL parser drops every
175+ // tab and newline and strips C0 controls and spaces off the end, so those
176+ // are escaped too, or the payload read back is not the one written.
177+ let trailing = text . length ;
178+ while ( trailing > 0 && text . charCodeAt ( trailing - 1 ) === 0x20 ) trailing -- ;
179+ let out = "" ;
180+ let start = 0 ;
181+ for ( let i = 0 ; i < text . length ; i ++ ) {
182+ const c = text . charCodeAt ( i ) ;
183+ if ( c > 0x20 && c !== 0x7f && c !== 0x25 && c !== 0x23 ) continue ;
184+ if ( c === 0x20 && i < trailing ) continue ;
185+ out += `${ text . slice ( start , i ) } %${ c < 0x10 ? "0" : "" } ${ c
186+ . toString ( 16 )
187+ . toUpperCase ( ) } `;
188+ start = i + 1 ;
189+ }
190+ return start === 0 ? text : out + text . slice ( start ) ;
191+ } ;
192+
147193/**
148194 * Rebuild a `data:` URI around a new payload, in the form it was written in.
149195 * @param {ParsedDataURI } parsed the split URI
@@ -155,10 +201,7 @@ const buildDataURI = (parsed, text) => {
155201 if ( parsed . base64 ) {
156202 return head + Buffer . from ( text , "utf8" ) . toString ( "base64" ) ;
157203 }
158- // Only the two that would change what the URI means: `%` starts an escape and
159- // `#` starts a fragment. Everything else stays as the renderer wrote it, and
160- // the url token's own quoting is the caller's to apply.
161- return head + text . replace ( / % / g, "%25" ) . replace ( / # / g, "%23" ) ;
204+ return head + encodeDataURIPayload ( text ) ;
162205} ;
163206
164207/**
@@ -257,7 +300,9 @@ module.exports = {
257300 decodeDataURI,
258301 decodeDataURIPayload,
259302 embeddedText,
303+ encodeDataURIPayload,
260304 languageOfFilename,
261305 languageOfMediaType,
262- parseDataURI
306+ parseDataURI,
307+ readEmbeddedDataURI
263308} ;
0 commit comments