Fix DataUri scheme handler in image-loader
This commit is contained in:
		
							parent
							
								
									f78f153f9c
								
							
						
					
					
						commit
						fed3d1fe33
					
				| @ -3,7 +3,6 @@ package ru.noties.markwon.il; | ||||
| import android.net.Uri; | ||||
| import android.support.annotation.NonNull; | ||||
| import android.support.annotation.Nullable; | ||||
| import android.text.TextUtils; | ||||
| 
 | ||||
| import java.io.ByteArrayInputStream; | ||||
| import java.util.Collection; | ||||
| @ -19,7 +18,7 @@ public class DataUriSchemeHandler extends SchemeHandler { | ||||
|         return new DataUriSchemeHandler(DataUriParser.create(), DataUriDecoder.create()); | ||||
|     } | ||||
| 
 | ||||
|     private static final String START = "data://"; | ||||
|     private static final String START = "data:"; | ||||
| 
 | ||||
|     private final DataUriParser uriParser; | ||||
|     private final DataUriDecoder uriDecoder; | ||||
| @ -38,7 +37,12 @@ public class DataUriSchemeHandler extends SchemeHandler { | ||||
|             return null; | ||||
|         } | ||||
| 
 | ||||
|         final String part = raw.substring(START.length()); | ||||
|         String part = raw.substring(START.length()); | ||||
|          | ||||
|         // this part is added to support `data://` with which this functionality was released | ||||
|         if (part.startsWith("//")) { | ||||
|             part = part.substring(2); | ||||
|         } | ||||
| 
 | ||||
|         final DataUri dataUri = uriParser.parse(part); | ||||
|         if (dataUri == null) { | ||||
|  | ||||
| @ -71,6 +71,33 @@ public class DataUriSchemeHandlerTest { | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void correct_real() { | ||||
| 
 | ||||
|         final class Item { | ||||
| 
 | ||||
|             final String contentType; | ||||
|             final String data; | ||||
| 
 | ||||
|             Item(String contentType, String data) { | ||||
|                 this.contentType = contentType; | ||||
|                 this.data = data; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         final Map<String, Item> expected = new HashMap<String, Item>() {{ | ||||
|             put("data:text/plain;,123", new Item("text/plain", "123")); | ||||
|             put("data:image/svg+xml;base64,MTIz", new Item("image/svg+xml", "123")); | ||||
|         }}; | ||||
| 
 | ||||
|         for (Map.Entry<String, Item> entry : expected.entrySet()) { | ||||
|             final ImageItem item = handler.handle(entry.getKey(), Uri.parse(entry.getKey())); | ||||
|             assertNotNull(entry.getKey(), item); | ||||
|             assertEquals(entry.getKey(), entry.getValue().contentType, item.contentType()); | ||||
|             assertEquals(entry.getKey(), entry.getValue().data, readStream(item.inputStream())); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @NonNull | ||||
|     private static String readStream(@NonNull InputStream stream) { | ||||
|         try { | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Dimitry Ivanov
						Dimitry Ivanov