Skip to content

Commit

Permalink
Additional device check for Ipad Pro on Safari (#5502)
Browse files Browse the repository at this point in the history
* Extend isTablet detection to include iPad and touch-enabled Macs

* Extend isTablet detection to include iPad and touch-enabled Macs (fixes #5494)

* split ipad check / add seperate test suite

* export iPad function

* add spacing for linter

* remove only on suite test

* format for temporary variable

* move all ipad checks to appropriate method / update test call

* remove underscore
  • Loading branch information
Drkjr92 committed Apr 1, 2024
1 parent ce68a4e commit a6ec417
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/utils/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,29 @@ module.exports.isMobile = isMobile;
*/
function isTablet (mockUserAgent) {
var userAgent = mockUserAgent || window.navigator.userAgent;
return /ipad|Nexus (7|9)|xoom|sch-i800|playbook|tablet|kindle/i.test(userAgent);

var isTablet = /Nexus (7|9)|xoom|sch-i800|playbook|tablet|kindle/i.test(userAgent);

// Additional check for iPad or MacIntel with touch capabilities and not an MSStream device
return isTablet || isIpad();
}
module.exports.isTablet = isTablet;

/**
* Detect ipad devices.
* @param {string} mockUserAgent - Allow passing a mock user agent for testing.
* @param {string} mockDevicePlatform - Allow passing a mock device platform for testing.
* @param {string} mockDeviceTouchPoints - Allow passing a mock device touch points for testing.
*/
function isIpad (mockUserAgent, mockDevicePlatform, mockDeviceTouchPoints) {
var userAgent = mockUserAgent || window.navigator.userAgent;
var platform = mockDevicePlatform || window.navigator.platform;
var maxTouchPoints = mockDeviceTouchPoints || window.navigator.maxTouchPoints || 0;

return ((platform === 'iPad' || platform === 'MacIntel') && maxTouchPoints > 0 && /Macintosh|Intel|iPad|ipad/i.test(userAgent) && !window.MSStream);
}
module.exports.isIpad = isIpad;

function isIOS () {
return /iPad|iPhone|iPod/.test(window.navigator.platform);
}
Expand Down
23 changes: 23 additions & 0 deletions tests/utils/device.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,29 @@ suite('isTablet', function () {
});
});

suite('isIpad', function () {
test('is true for iPad', function () {
var iPadUserAgent = 'Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1';
var platform = 'iPad';
var maxTouchPoints = 5;
assert.ok(device.isIpad(iPadUserAgent, platform, maxTouchPoints));
});

test('is true for MacIntel with touch capabilities', function () {
var macUserAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.2 Safari/605.1.15';
var platform = 'MacIntel';
var maxTouchPoints = 5;
assert.ok(device.isIpad(macUserAgent, platform, maxTouchPoints));
});

test('is false for MacIntel with no touch capabilities', function () {
var macUserAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.2 Safari/605.1.15';
var platform = 'MacIntel';
var maxTouchPoints = 0;
assert.ok(!device.isIpad(macUserAgent, platform, maxTouchPoints));
});
});

suite('environment', function () {
test('isNodeEnvironment is false for browser tests', function () {
assert.strictEqual(device.isNodeEnvironment, false);
Expand Down

0 comments on commit a6ec417

Please sign in to comment.