Kaynağa Gözat

Fixes ZF-11634 - Recaptcha form decorator was incompatible with IE (no addEventListener support). Added addEvent proxy.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24391 44c647ce-9c0f-0410-b52a-842ac1e357ba
padraic 14 yıl önce
ebeveyn
işleme
077807ed50

+ 17 - 4
library/Zend/Form/Decorator/Captcha/ReCaptcha.php

@@ -79,6 +79,8 @@ class Zend_Form_Decorator_Captcha_ReCaptcha extends Zend_Form_Decorator_Abstract
         // Create a window.onload event so that we can bind to the form.
         // Once bound, add an onsubmit event that will replace the hidden field 
         // values with those produced by ReCaptcha
+        // zendBindEvent mediates between Mozilla's addEventListener and
+        // IE's sole support for addEvent.
         $js =<<<EOJ
 <script type="text/javascript" language="JavaScript">
 function windowOnLoad(fn) {
@@ -90,11 +92,22 @@ function windowOnLoad(fn) {
         fn();
     };
 }
+function zendBindEvent(el, eventName, eventHandler) {
+    if (el.addEventListener){
+        el.addEventListener(eventName, eventHandler, false); 
+    } else if (el.attachEvent){
+        el.attachEvent('on'+eventName, eventHandler);
+    }
+}
 windowOnLoad(function(){
-    document.getElementById("$challengeId").form.addEventListener("submit", function(e) {
-        document.getElementById("$challengeId").value = document.getElementById("recaptcha_challenge_field").value;
-        document.getElementById("$responseId").value = document.getElementById("recaptcha_response_field").value;
-    }, false);
+    zendBindEvent(
+        document.getElementById("$challengeId").form,
+        'submit',
+        function(e) {
+            document.getElementById("$challengeId").value = document.getElementById("recaptcha_challenge_field").value;
+            document.getElementById("$responseId").value = document.getElementById("recaptcha_response_field").value;
+        }    
+    );
 });
 </script>
 EOJ;

+ 5 - 2
tests/Zend/Form/Decorator/ReCaptchaTest.php

@@ -95,8 +95,11 @@ class Zend_Form_Decorator_ReCaptchaTest extends PHPUnit_Framework_TestCase
         $this->assertContains('old = window.onload', $html);
         $this->assertContains('if (old)', $html);
 
+        //Test that we create IE/Mozilla zendBindEvent mediator
+        $this->assertContains('function zendBindEvent', $html);
+
         // Test that we add an event listener for the form submit event
-        $this->assertContains('document.getElementById("captcha-challenge").form.addEventListener("submit", function(e)', $html);
+        $this->assertContains('document.getElementById("captcha-challenge").form,', $html);
 
         // Test that we reset the hidden fields with the global recaptcha values
         $this->assertContains('document.getElementById("captcha-challenge").value = document.getElementById("recaptcha_challenge_field").value', $html);
@@ -117,7 +120,7 @@ class Zend_Form_Decorator_ReCaptchaTest extends PHPUnit_Framework_TestCase
     {
         $this->element->setBelongsTo('contact');
         $html = $this->decorator->render('');
-        $this->assertContains('document.getElementById("contact-captcha-challenge").form.addEventListener("submit", function(e)', $html);
+        $this->assertContains('document.getElementById("contact-captcha-challenge").form,', $html);
         $this->assertContains('document.getElementById("contact-captcha-challenge").value = document.getElementById("recaptcha_challenge_field").value', $html);
         $this->assertContains('document.getElementById("contact-captcha-response").value = document.getElementById("recaptcha_response_field").value', $html);
     }