Make WordPress Core

Changeset 37966


Ignore:
Timestamp:
07/05/2016 01:36:59 PM (10 years ago)
Author:
ocean90
Message:

Media: Improve form validation errors handling when editing images.

  • Use the same check for a numeric value used on the crop fields on all the other fields: don't display "NaN", just empty the field.
  • Remove the inline script that runs the initialization of the image editor and call it after the editor UI is fully ready.

Props afercia.
Fixes #36316.

Location:
trunk/src/wp-admin
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/image-edit.php

    r37488 r37966  
    6161                <div class="nowrap">
    6262                <label><span class="screen-reader-text"><?php _e( 'scale width' ); ?></span>
    63                 <input type="text" id="imgedit-scale-width-<?php echo $post_id; ?>" onkeyup="imageEdit.scaleChanged(<?php echo $post_id; ?>, 1)" onblur="imageEdit.scaleChanged(<?php echo $post_id; ?>, 1)" value="<?php echo isset( $meta['width'] ) ? $meta['width'] : 0; ?>" />
     63                <input type="text" id="imgedit-scale-width-<?php echo $post_id; ?>" onkeyup="imageEdit.scaleChanged(<?php echo $post_id; ?>, 1, this)" onblur="imageEdit.scaleChanged(<?php echo $post_id; ?>, 1, this)" value="<?php echo isset( $meta['width'] ) ? $meta['width'] : 0; ?>" />
    6464                </label>
    6565                <span class="imgedit-separator">&times;</span>
    6666                <label><span class="screen-reader-text"><?php _e( 'scale height' ); ?></span>
    67                 <input type="text" id="imgedit-scale-height-<?php echo $post_id; ?>" onkeyup="imageEdit.scaleChanged(<?php echo $post_id; ?>, 0)" onblur="imageEdit.scaleChanged(<?php echo $post_id; ?>, 0)" value="<?php echo isset( $meta['height'] ) ? $meta['height'] : 0; ?>" />
     67                <input type="text" id="imgedit-scale-height-<?php echo $post_id; ?>" onkeyup="imageEdit.scaleChanged(<?php echo $post_id; ?>, 0, this)" onblur="imageEdit.scaleChanged(<?php echo $post_id; ?>, 0, this)" value="<?php echo isset( $meta['height'] ) ? $meta['height'] : 0; ?>" />
    6868                </label>
    6969                <span class="imgedit-scale-warn" id="imgedit-scale-warn-<?php echo $post_id; ?>">!</span>
     
    117117                <div class="nowrap">
    118118                <label><span class="screen-reader-text"><?php _e( 'crop ratio width' ); ?></span>
    119                 <input type="text" id="imgedit-crop-width-<?php echo $post_id; ?>" onkeyup="imageEdit.setRatioSelection(<?php echo $post_id; ?>, 0, this)" />
     119                <input type="text" id="imgedit-crop-width-<?php echo $post_id; ?>" onkeyup="imageEdit.setRatioSelection(<?php echo $post_id; ?>, 0, this)" onblur="imageEdit.setRatioSelection(<?php echo $post_id; ?>, 0, this)" />
    120120                </label>
    121121                <span class="imgedit-separator">:</span>
    122122                <label><span class="screen-reader-text"><?php _e( 'crop ratio height' ); ?></span>
    123                 <input type="text" id="imgedit-crop-height-<?php echo $post_id; ?>" onkeyup="imageEdit.setRatioSelection(<?php echo $post_id; ?>, 1, this)" />
     123                <input type="text" id="imgedit-crop-height-<?php echo $post_id; ?>" onkeyup="imageEdit.setRatioSelection(<?php echo $post_id; ?>, 1, this)" onblur="imageEdit.setRatioSelection(<?php echo $post_id; ?>, 1, this)" />
    124124                </label>
    125125                </div>
     
    130130                <div class="nowrap">
    131131                <label><span class="screen-reader-text"><?php _e( 'selection width' ); ?></span>
    132                 <input type="text" id="imgedit-sel-width-<?php echo $post_id; ?>" onkeyup="imageEdit.setNumSelection(<?php echo $post_id; ?>)" />
     132                <input type="text" id="imgedit-sel-width-<?php echo $post_id; ?>" onkeyup="imageEdit.setNumSelection(<?php echo $post_id; ?>, this)" onblur="imageEdit.setNumSelection(<?php echo $post_id; ?>, this)" />
    133133                </label>
    134134                <span class="imgedit-separator">&times;</span>
    135135                <label><span class="screen-reader-text"><?php _e( 'selection height' ); ?></span>
    136                 <input type="text" id="imgedit-sel-height-<?php echo $post_id; ?>" onkeyup="imageEdit.setNumSelection(<?php echo $post_id; ?>)" />
     136                <input type="text" id="imgedit-sel-height-<?php echo $post_id; ?>" onkeyup="imageEdit.setNumSelection(<?php echo $post_id; ?>, this)" onblur="imageEdit.setNumSelection(<?php echo $post_id; ?>, this)" />
    137137                </label>
    138138                </div>
     
    225225        </div>
    226226        <div class="imgedit-wait" id="imgedit-wait-<?php echo $post_id; ?>"></div>
    227         <script type="text/javascript">jQuery( function() { imageEdit.init(<?php echo $post_id; ?>); });</script>
    228227        <div class="hidden" id="imgedit-leaving-<?php echo $post_id; ?>"><?php _e("There are unsaved changes that will be lost. 'OK' to continue, 'Cancel' to return to the Image Editor."); ?></div>
    229228        </div>
  • trunk/src/wp-admin/js/image-edit.js

    r36223 r37966  
    99
    1010        intval : function(f) {
     11                /*
     12                 * Bitwise OR operator: one of the obscure ways to truncate floating point figures,
     13                 * worth reminding JavaScript doesn't have a distinct "integer" type.
     14                 */
    1115                return f | 0;
    1216        },
     
    8084        },
    8185
    82         scaleChanged : function(postid, x) {
     86        scaleChanged : function( postid, x, el ) {
    8387                var w = $('#imgedit-scale-width-' + postid), h = $('#imgedit-scale-height-' + postid),
    8488                warn = $('#imgedit-scale-warn-' + postid), w1 = '', h1 = '';
     89
     90                if ( false === this.validateNumeric( el ) ) {
     91                        return;
     92                }
    8593
    8694                if ( x ) {
     
    362370                                spin.removeClass( 'is-active' );
    363371                        });
     372                        // Initialise the Image Editor now that everything is ready.
     373                        imageEdit.init( postid );
    364374                });
    365375
     
    587597        },
    588598
    589         setNumSelection : function(postid) {
     599        setNumSelection : function( postid, el ) {
    590600                var sel, elX = $('#imgedit-sel-width-' + postid), elY = $('#imgedit-sel-height-' + postid),
    591601                        x = this.intval( elX.val() ), y = this.intval( elY.val() ),
    592602                        img = $('#image-preview-' + postid), imgh = img.height(), imgw = img.width(),
    593603                        sizer = this.hold.sizer, x1, y1, x2, y2, ias = this.iasapi;
     604
     605                if ( false === this.validateNumeric( el ) ) {
     606                        return;
     607                }
    594608
    595609                if ( x < 1 ) {
     
    651665                        h = $('#image-preview-' + postid).height();
    652666
    653                 if ( !this.intval( $(el).val() ) ) {
    654                         $(el).val('');
     667                if ( false === this.validateNumeric( el ) ) {
    655668                        return;
    656669                }
     
    677690                        }
    678691                }
     692        },
     693
     694        validateNumeric: function( el ) {
     695                if ( ! this.intval( $( el ).val() ) ) {
     696                        $( el ).val( '' );
     697                        return false;
     698                }
    679699        }
    680700};
Note: See TracChangeset for help on using the changeset viewer.