Revision: 5646
Author:   [email protected]
Date:     Wed Dec 18 19:52:11 2013 UTC
Log:      Add <canvas>.toDataURL().
https://codereview.appspot.com/43470045

[email protected]

http://code.google.com/p/google-caja/source/detail?r=5646

Modified:
 /trunk/src/com/google/caja/plugin/domado.js
 /trunk/tests/com/google/caja/plugin/test-domado-canvas-guest.html
 /trunk/tests/com/google/caja/plugin/test-scan-guest.js

=======================================
--- /trunk/src/com/google/caja/plugin/domado.js Tue Dec 10 23:52:12 2013 UTC
+++ /trunk/src/com/google/caja/plugin/domado.js Wed Dec 18 19:52:11 2013 UTC
@@ -5276,6 +5276,25 @@
                   // user agent, return null and abort these steps."
                   return null;
               }
+            }),
+ toDataURL: Props.ampMethod(function(privates, opt_type, opt_arg) {
+              if (opt_type !== undefined) {
+                opt_type = String(opt_type).toLowerCase();
+              }
+              // Whitelist of types to be cautious, and because we need
+              // to sanitize the varargs
+              switch (opt_type) {
+                case 'image/png':
+                  return privates.feral.toDataURL('image/png');
+                case 'image/jpeg':
+                  return privates.feral.toDataURL('image/jpeg', +opt_arg);
+                default:
+ console.warn('Domado: Discarding unrecognized MIME type ' +
+                      opt_type + ' for canvas.toDataURL.');
+                  /* fall through */
+                case undefined:
+                  return privates.feral.toDataURL();
+              }
             })
           }; }
         });
=======================================
--- /trunk/tests/com/google/caja/plugin/test-domado-canvas-guest.html Wed Aug 14 04:56:12 2013 UTC +++ /trunk/tests/com/google/caja/plugin/test-domado-canvas-guest.html Wed Dec 18 19:52:11 2013 UTC
@@ -37,7 +37,17 @@
     assertEquals("set canvas width", 20, canvas.width);
     assertEquals("set canvas height", 10, canvas.height);

-    // Getting the context
+    pass('testCanvasElement');
+  });
+</script>
+
+<p id="testCanvasGetContext" class="testcontainer">
+  Canvas getContext
+  <canvas id="testCanvasGetContext-canvas">fallback</canvas>
+</p>
+<script type="text/javascript">
+  jsunitRegister('testCanvasGetContext', function testCanvasGetContext() {
+    var canvas = document.getElementById('testCanvasGetContext-canvas');
     expectFailure(
         function() {
           canvas.getContext();
@@ -53,12 +63,41 @@
     var context = canvas.getContext("2d");
     assertNotNull("context", context);
     assertNotUndefined("context", context);
-    assertEquals("context equals itself", context,
- document.getElementById('testCanvasElement-canvas').getContext("2d")); + assertEquals("context equals itself", context, canvas.getContext("2d"));
+    pass();
+  });
+</script>

-    // TODO: toDataURL
+<p id="testCanvasToDataURL" class="testcontainer">
+  Canvas toDataURL
+  <!-- Zoomed in for visual inspection -->
+  <canvas id="testCanvasToDataURL-canvas"
+          width="3" height="3"
+          style="width: 30px; height: 30px;">fallback</canvas>
+</p>
+<script type="text/javascript">
+  jsunitRegister('testCanvasToDataURL', function testCanvasToDataURL() {
+    var canvas = document.getElementById('testCanvasToDataURL-canvas');
+    var context = canvas.getContext('2d');
+    context.fillRect(1, 1, 1, 1);

-    pass('testCanvasElement');
+    var url = canvas.toDataURL();
+    assertTrue(url, /data:image\/png(?:;base64)?,/.test(url));
+
+ // TODO(kpreid): Once we support loading data: URIs, try writing the image
+    // back to a canvas. For now, displaying it as best we can:
+    document.getElementById('testCanvasToDataURL').appendChild(
+        document.createTextNode(url));
+
+    // Try optional arguments.
+    // Note: Assuming JPEG support which is not mandatory.
+    url = canvas.toDataURL('image/jpeg', 0.1);
+    assertTrue(url, /data:image\/jpeg(?:;base64)?,/.test(url));
+
+    // TODO(kpreid): Feature-test and test toDataURLHD and toBlob[HD].
+    // HD versions give full-resolution data
+
+    pass();
   });
 </script>

=======================================
--- /trunk/tests/com/google/caja/plugin/test-scan-guest.js Tue Dec 10 23:52:12 2013 UTC +++ /trunk/tests/com/google/caja/plugin/test-scan-guest.js Wed Dec 18 19:52:11 2013 UTC
@@ -996,6 +996,10 @@
     argsByProp('getPropertyValue', genMethod(genCSSPropertyName));
argsByProp('getContext', genMethod(G.value(undefined, null, 'bogus', '2d',
         'webgl', 'experimental-webgl')));
+    argsByProp('toDataURL', G.any(
+        genMethod(),
+        genMethod(genMediaType),
+        genMethod(genMediaType, genSmallInteger)));
     argsByProp('querySelector', genMethod(genCSSSelector));
     argsByProp('querySelectorAll', freshResult(genMethod(genCSSSelector)));
     argsByProp('getElementById', genMethod(G.value(

--

--- You received this message because you are subscribed to the Google Groups "Google Caja Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to