Selaa lähdekoodia

Add `computedstyle` option for calculating the width (#5559)

This allows for more accurate resolution of the width when compared
to the `resolve` method. This is more relevant for jQuery 1.x, where
the `resolve` method cannot find the width of a hidden select box,
but it also applies to newer versions of jQuery where the `width()`
method provided by jQuery doesn't fully match `getComputedStyle()`.

Fixes #3278
Fixes #5502
Closes #5259
Kevin Brown 5 vuotta sitten
vanhempi
commit
b5f136ff72
2 muutettua tiedostoa jossa 24 lisäystä ja 0 poistoa
  1. 6 0
      src/js/select2/core.js
  2. 18 0
      tests/options/width-tests.js

+ 6 - 0
src/js/select2/core.js

@@ -162,6 +162,12 @@ define([
       return null;
     }
 
+    if (method == 'computedstyle') {
+      var computedStyle = window.getComputedStyle($element[0]);
+
+      return computedStyle.width;
+    }
+
     return method;
   };
 

+ 18 - 0
tests/options/width-tests.js

@@ -64,3 +64,21 @@ test('resolve falls back to element if there is no style', function (assert) {
 
   assert.equal(width, '500px');
 });
+
+test('computedstyle gets the style if parent is invisible', function (assert) {
+  var $style = $(
+    '<style type="text/css">.css-set-width { width: 500px; }</style>'
+  );
+  var $test = $(
+    '<div style="display:none;">' +
+      '<select class="css-set-width"></select>' +
+    '</div>'
+  );
+
+  $('#qunit-fixture').append($style);
+  $('#qunit-fixture').append($test);
+
+  var width = select._resolveWidth($test.find('select'), 'computedstyle');
+
+  assert.equal(width, '500px');
+});