11/*!
22 * PayPalJSButtons
33 * JavaScript integration for PayPal's payment buttons
4- * @version 1.0.1 - 2013-05-07
4+ * @version 1.0.1 - 2013-06-05
55 * @author Jeff Harrell <https://github.com/jeffharrell/>
66 */
77/*!
88 * MiniCart
99 *
10- * Improve your PayPal integration by creating an overlay which appears as a user adds products to their cart.
11- *
12- * @version 2.5.0 - 2012-12-02, 1:04:43 PM
1310 * @author Jeff Harrell <https://github.com/jeffharrell/>
14- * @url http://www.minicartjs.com/
15- * @license <eBay Open Source License Agreement <https://github.com/jeffharrell/MiniCart/blob/master/LICENSE>>
11+ * @license MIT <https://github.com/jeffharrell/MiniCart/blob/master/LICENSE>
1612 */
1713if ( typeof PAYPAL === 'undefined' || ! PAYPAL ) {
1814 var PAYPAL = { } ;
@@ -62,11 +58,11 @@ PAYPAL.apps = PAYPAL.apps || {};
6258 * Strings used for display text
6359 */
6460 strings : {
65- button : '' ,
66- subtotal : '' ,
67- discount : '' ,
68- shipping : '' ,
69- processing : ''
61+ button : 'Checkout ' ,
62+ subtotal : 'Subtotal: ' ,
63+ discount : 'Discount: ' ,
64+ shipping : 'does not include shipping & tax ' ,
65+ processing : 'Processing... '
7066 } ,
7167
7268 /**
@@ -222,7 +218,7 @@ PAYPAL.apps = PAYPAL.apps || {};
222218 css . push ( '#' + name + ' ul { position:relative; overflow-x:hidden; overflow-y:auto; height:130px; margin:0 0 7px; padding:0; list-style-type:none; border-top:1px solid #ccc; border-bottom:1px solid #ccc; } ' ) ;
223219 css . push ( '#' + name + ' li { position:relative; margin:-1px 0 0; padding:6px 5px 6px 0; border-top:1px solid #f2f2f2; } ' ) ;
224220 css . push ( '#' + name + ' li a { display: block; width: 155px; color:#333; text-decoration:none; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; } ' ) ;
225- css . push ( '#' + name + ' li a span { color:#999; font-size:10px; } ' ) ;
221+ css . push ( '#' + name + ' li a span { display:block; color:#999; font-size:10px; } ' ) ;
226222 css . push ( '#' + name + ' li .quantity { position:absolute; top:.5em; right:78px; width:22px; padding:1px; border:1px solid #83a8cc; text-align:right; } ' ) ;
227223 css . push ( '#' + name + ' li .price { position:absolute; top:.5em; right:4px; } ' ) ;
228224 css . push ( '#' + name + ' li .remove { position:absolute; top:9px; right:60px; width:14px; height:14px; background:url(' + config . assetURL + 'images/minicart_sprite.png) no-repeat -134px -4px; border:0; cursor:pointer; } ' ) ;
@@ -289,24 +285,24 @@ PAYPAL.apps = PAYPAL.apps || {};
289285
290286 UI . button = document . createElement ( 'input' ) ;
291287 UI . button . type = 'submit' ;
292- UI . button . value = config . strings . button || 'Checkout' ;
288+ UI . button . value = config . strings . button ;
293289 UI . summary . appendChild ( UI . button ) ;
294290
295291 UI . subtotal = document . createElement ( 'span' ) ;
296- UI . subtotal . innerHTML = config . strings . subtotal || 'Subtotal: ' ;
292+ $ . util . setText ( UI . subtotal , config . strings . subtotal ) ;
297293
298294 UI . subtotalAmount = document . createElement ( 'span' ) ;
299- UI . subtotalAmount . innerHTML = '0.00' ;
295+ $ . util . setText ( UI . subtotalAmount , '0.00' ) ;
300296
301297 UI . subtotal . appendChild ( UI . subtotalAmount ) ;
302298 UI . summary . appendChild ( UI . subtotal ) ;
303299
304300 UI . shipping = document . createElement ( 'span' ) ;
305301 UI . shipping . className = 'shipping' ;
306- UI . shipping . innerHTML = config . strings . shipping || 'does not include shipping & tax' ;
302+ $ . util . setText ( UI . shipping , config . strings . shipping ) ;
307303 UI . summary . appendChild ( UI . shipping ) ;
308304
309- // Workaround: IE 6 and IE 7/8 in quirks mode do not support position:fixed in CSS
305+ // Workaround: IE 6 and IE 7/8 in quirks mode do not support position: fixed in CSS
310306 if ( window . attachEvent && ! window . opera ) {
311307 version = navigator . userAgent . match ( / M S I E \s ( [ ^ ; ] * ) / ) ;
312308
@@ -378,6 +374,14 @@ PAYPAL.apps = PAYPAL.apps || {};
378374 }
379375 } ) ;
380376
377+ // Redraw the cart if the back/forward cache doesn't re-render
378+ $ . event . add ( window , 'pageshow' , function ( e ) {
379+ if ( e . persisted ) {
380+ _redrawCartItems ( true ) ;
381+ minicart . hide ( ) ;
382+ }
383+ } ) ;
384+
381385 // Update other windows when HTML5 localStorage is updated
382386 if ( window . attachEvent && ! window . opera ) {
383387 $ . event . add ( document , 'storage' , function ( e ) {
@@ -535,12 +539,13 @@ PAYPAL.apps = PAYPAL.apps || {};
535539
536540
537541 /**
538- * Resets the card and renders the products
542+ * Resets the cart and renders the products
539543 */
540544 var _redrawCartItems = function ( silent ) {
541545 minicart . products = [ ] ;
542- minicart . UI . itemList . innerHTML = '' ;
543- minicart . UI . subtotalAmount . innerHTML = '' ;
546+ $ . util . setText ( minicart . UI . itemList , '' ) ;
547+ $ . util . setText ( minicart . UI . subtotalAmount , '' ) ;
548+ minicart . UI . button . value = config . strings . button ;
544549
545550 _parseStorage ( ) ;
546551 minicart . updateSubtotal ( silent ) ;
@@ -705,7 +710,7 @@ PAYPAL.apps = PAYPAL.apps || {};
705710 }
706711 }
707712
708- minicart . UI . button . value = config . strings . processing || 'Processing...' ;
713+ minicart . UI . button . value = config . strings . processing ;
709714 } ;
710715
711716
@@ -929,7 +934,7 @@ PAYPAL.apps = PAYPAL.apps || {};
929934 }
930935
931936 // Update the UI
932- subtotalEl . innerHTML = $ . util . formatCurrency ( subtotal , currency_code ) ;
937+ $ . util . setText ( subtotalEl , $ . util . formatCurrency ( subtotal , currency_code ) ) ;
933938
934939 // Yellow fade on update
935940 if ( ! silent ) {
@@ -1064,8 +1069,8 @@ PAYPAL.apps = PAYPAL.apps || {};
10641069 minicart . products = [ ] ;
10651070
10661071 if ( isShowing ) {
1067- ui . itemList . innerHTML = '' ;
1068- ui . subtotalAmount . innerHTML = '' ;
1072+ $ . util . setText ( ui . itemList , '' ) ;
1073+ $ . util . setText ( ui . subtotalAmount , '' ) ;
10691074 minicart . hide ( null , true ) ;
10701075 }
10711076
@@ -1127,32 +1132,31 @@ PAYPAL.apps = PAYPAL.apps || {};
11271132 name = this . product . item_name ;
11281133 }
11291134
1130- this . nameNode . innerHTML = name ;
1131- this . nameNode . title = name ;
1135+ $ . util . setText ( this . nameNode , name ) ;
11321136 this . nameNode . href = this . product . href ;
11331137 this . nameNode . appendChild ( this . metaNode ) ;
11341138
11351139 // Meta info
11361140 if ( this . product . item_number ) {
1137- this . metaNode . innerHTML = '<br />#' + this . product . item_number ;
1141+ $ . util . setText ( this . metaNode , this . product . item_number , null , '<br>' ) ;
11381142 }
11391143
11401144 // Options
11411145 options = this . getOptions ( ) ;
11421146
11431147 for ( key in options ) {
1144- this . metaNode . innerHTML += '<br />' + key + ': ' + options [ key ] ;
1148+ $ . util . setText ( this . metaNode , key + ': ' + options [ key ] , this . metaNode . innerHTML , '<br>' ) ;
11451149 }
11461150
11471151 // Discount
11481152 discount = this . getDiscount ( ) ;
11491153
1150- if ( discount ) {
1154+ if ( discount >= 0 ) {
11511155 this . discountInput . type = 'hidden' ;
11521156 this . discountInput . name = 'discount_amount_' + position ;
11531157 this . discountInput . value = discount ;
11541158
1155- this . metaNode . appendChild ( this . discountNode ) ;
1159+ this . metaNode . appendChild ( this . discountInput ) ;
11561160 }
11571161
11581162 // Price
@@ -1208,7 +1212,7 @@ PAYPAL.apps = PAYPAL.apps || {};
12081212 quantity ;
12091213
12101214 // Discounts: Amount-based
1211- if ( this . product . discount_amount ) {
1215+ if ( this . product . discount_amount >= 0 ) {
12121216 // Discount amount for the first item
12131217 discount = parseFloat ( this . product . discount_amount ) ;
12141218
@@ -1217,12 +1221,12 @@ PAYPAL.apps = PAYPAL.apps || {};
12171221 quantity = this . getQuantity ( ) ;
12181222
12191223 if ( quantity > 1 ) {
1220- discount += Math . max ( quantity - 1 , discountNum ) * parseFloat ( this . product . discount_amount2 ) ;
1224+ discount += Math . min ( quantity - 1 , discountNum ) * parseFloat ( this . product . discount_amount2 ) ;
12211225 }
12221226 }
12231227
12241228 // Discounts: Percentage-based
1225- } else if ( this . product . discount_rate ) {
1229+ } else if ( this . product . discount_rate >= 0 ) {
12261230 // Discount amount on the first item
12271231 discount = this . product . amount * parseFloat ( this . product . discount_rate ) / 100 ;
12281232
@@ -1231,7 +1235,7 @@ PAYPAL.apps = PAYPAL.apps || {};
12311235 quantity = this . getQuantity ( ) ;
12321236
12331237 if ( quantity > 1 ) {
1234- discount += Math . max ( quantity - 1 , discountNum ) * this . product . amount * parseFloat ( this . product . discount_amount2 ) / 100 ;
1238+ discount += Math . min ( quantity - 1 , discountNum ) * this . product . amount * parseFloat ( this . product . discount_amount2 ) / 100 ;
12351239 }
12361240 }
12371241 }
@@ -1284,9 +1288,7 @@ PAYPAL.apps = PAYPAL.apps || {};
12841288 this . metaNode . appendChild ( this . discountNode ) ;
12851289 }
12861290
1287- this . discountNode . innerHTML = '<br />' ;
1288- this . discountNode . innerHTML += config . strings . discount || 'Discount: ' ;
1289- this . discountNode . innerHTML += $ . util . formatCurrency ( discount , this . settings . currency_code ) ;
1291+ $ . util . setText ( this . discountNode , this . discountNode . innerHTML + config . strings . discount + $ . util . formatCurrency ( discount , this . settings . currency_code ) ) ;
12901292 }
12911293 }
12921294
@@ -1312,7 +1314,7 @@ PAYPAL.apps = PAYPAL.apps || {};
13121314 setPrice : function ( value ) {
13131315 value = parseFloat ( value , 10 ) ;
13141316
1315- this . priceNode . innerHTML = $ . util . formatCurrency ( value . toFixed ( 2 ) , this . settings . currency_code ) ;
1317+ $ . util . setText ( this . priceNode , $ . util . formatCurrency ( value . toFixed ( 2 ) , this . settings . currency_code ) ) ;
13161318 } ,
13171319
13181320
@@ -1640,7 +1642,7 @@ PAYPAL.apps = PAYPAL.apps || {};
16401642 if ( tag === 'select' ) {
16411643 return input . options [ input . selectedIndex ] . value ;
16421644 } else if ( tag === 'textarea' ) {
1643- return input . innerHTML ;
1645+ return input . innerText ;
16441646 } else {
16451647 if ( input . type === 'radio' ) {
16461648 return ( input . checked ) ? input . value : null ;
@@ -1653,6 +1655,23 @@ PAYPAL.apps = PAYPAL.apps || {};
16531655 } ,
16541656
16551657
1658+ /**
1659+ * Convenient method to set the innerText of an element
1660+ *
1661+ * @param node {HTMLElement} The element
1662+ * @param contetn {String} The content to escape
1663+ * @param after {String} The content before
1664+ * @param after {String} The content afterwards
1665+ */
1666+ setText : function ( node , content , before , after ) {
1667+ var str = before || '' ;
1668+ str += content = content . replace ( '<' , '<' ) . replace ( '>' , '>' ) ;
1669+ str += ( after || '' ) ;
1670+
1671+ node . innerHTML = str ;
1672+ } ,
1673+
1674+
16561675 /**
16571676 * Formats a float into a currency
16581677 *
0 commit comments