Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion DeviceDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -866,9 +866,16 @@ protected function parseBot(): void
*/
protected function parseClient(): void
{
$parsers = $this->getClientParsers();
$parsers = $this->getClientParsers();
$osName = $this->getOsAttribute('name');
$osVersion = $this->getOsAttribute('version');

foreach ($parsers as $parser) {
if ($parser instanceof Browser) {
$parser->setOsName($osName);
$parser->setOsVersion($osVersion);
}

$parser->setYamlParser($this->getYamlParser());
$parser->setCache($this->getCache());
$parser->setUserAgent($this->getUserAgent());
Expand Down
38 changes: 38 additions & 0 deletions Parser/Client/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ class Browser extends AbstractClientParser
*/
protected $parserName = 'browser';

/**
* @var string
*/
protected $osName = '';

/**
* @var string
*/
protected $osVersion = '';

/**
* Known browsers mapped to their internal short codes
*
Expand Down Expand Up @@ -876,6 +886,26 @@ public function setUserAgent(string $ua): void
$this->browserHints->setUserAgent($ua);
}

/**
* Sets the os name
*
* @param string $osName os name
*/
public function setOsName(string $osName = ''): void
{
$this->osName = $osName;
}

/**
* Sets the os version
*
* @param string $osVersion os version
*/
public function setOsVersion(string $osVersion = ''): void
{
$this->osVersion = $osVersion;
}

/**
* Sets the Cache class
*
Expand Down Expand Up @@ -1119,6 +1149,14 @@ public function parse(): ?array
$family = 'Firefox';
}

/**
* Mobile Safari version is always the iOS / iPadOS version
* See https://developer.apple.com/documentation/safari-release-notes
*/
if (\in_array($this->osName, ['iOS', 'iPadOS']) && '' !== $this->osVersion && 'Mobile Safari' === $name) {
$version = $this->osVersion;
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just this condition will be enough for us?

        // Mobile Safari version is always the iOS / iPadOS version
        // See https://developer.apple.com/documentation/safari-release-notes
        if ('Mobile Safari' === $name && '' === $version) {
            \preg_match(
                '~iP(?:hone|ad) ?OS ([0-9]{1,2})[_.]([0-9]{1,2})(?:[_.]([0-9]{1,2}))?~i',
                $this->userAgent,
                $osMatch
            );
            if ($osMatch) {
                $version = \implode('.', \array_filter([$osMatch[1], $osMatch[2], $osMatch[3] ?? null]));
            }
        }
        

Then our browser parser will not depend on OS parser.
This will also work correctly when using BrowserParser alone.

Copy link
Collaborator Author

@liviuconcioiu liviuconcioiu Mar 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just this condition will be enough for us?

Then our browser parser will not depend on OS parser. This will also work correctly when using BrowserParser alone.

It will not work because there are user agents like:

MobileSafari/9537.53 CFNetwork/672.1.13 Darwin/13.1.0

Another way I tried to do this is was to change the browser version with os version in DeviceDetector.php, with only a few lines, but then I encountered the failed tests, which use only the BrowserParser.

I'm happy with any other suggestions, if we can do this, and tests pass.

return [
'type' => 'browser',
'name' => $name,
Expand Down
8 changes: 8 additions & 0 deletions Tests/Parser/Client/BrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use DeviceDetector\Parser\Client\Browser;
use DeviceDetector\Parser\Client\Browser\Engine;
use DeviceDetector\Parser\Client\Hints\BrowserHints;
use DeviceDetector\Parser\OperatingSystem;
use PHPUnit\Framework\TestCase;
use Spyc;

Expand All @@ -32,6 +33,13 @@ public function testParse(string $useragent, array $client, ?array $headers = nu
$browserParser->setVersionTruncation(Browser::VERSION_TRUNCATION_NONE);
$browserParser->setUserAgent($useragent);

$osParser = new OperatingSystem();
$osParser->setUserAgent($useragent);
$os = $osParser->parse();

$browserParser->setOsName($os['name'] ?? '');
$browserParser->setOsVersion($os['version'] ?? '');

if (null !== $headers) {
$browserParser->setClientHints(ClientHints::factory($headers));
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/Parser/Client/fixtures/browser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,7 @@
client:
type: browser
name: Mobile Safari
version: ""
version: 5.0.1
engine: WebKit
engine_version: 533.17.9
family: Safari
Expand All @@ -1066,7 +1066,7 @@
client:
type: browser
name: Mobile Safari
version: "9537.53"
version: "7.1"
engine: WebKit
engine_version: ""
family: Safari
Expand Down Expand Up @@ -9707,7 +9707,7 @@
client:
type: browser
name: Mobile Safari
version: "11.0"
version: "17.2"
engine: WebKit
engine_version: 604.1.38
family: Safari
Expand Down
2 changes: 1 addition & 1 deletion Tests/fixtures/phablet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8433,7 +8433,7 @@
client:
type: browser
name: Mobile Safari
version: ""
version: 15.4.1
engine: WebKit
engine_version: 605.1.15
device:
Expand Down
4 changes: 2 additions & 2 deletions Tests/fixtures/portable_media_player.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
client:
type: browser
name: Mobile Safari
version: ""
version: 4.2.1
engine: WebKit
engine_version: 533.17.9
device:
Expand All @@ -26,7 +26,7 @@
client:
type: browser
name: Mobile Safari
version: ""
version: 4.3.0
engine: WebKit
engine_version: 533.17.9
device:
Expand Down
12 changes: 6 additions & 6 deletions Tests/fixtures/smartphone-2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@
client:
type: browser
name: Mobile Safari
version: "4.0"
version: 3.1.2
engine: WebKit
engine_version: "528.18"
device:
Expand All @@ -242,7 +242,7 @@
client:
type: browser
name: Mobile Safari
version: ""
version: "4.1"
engine: WebKit
engine_version: "532.9"
device:
Expand All @@ -260,7 +260,7 @@
client:
type: browser
name: Mobile Safari
version: ""
version: 4.2.1
engine: WebKit
engine_version: 533.17.9
device:
Expand All @@ -278,7 +278,7 @@
client:
type: browser
name: Mobile Safari
version: ""
version: 4.3.3
engine: WebKit
engine_version: 533.17.9
device:
Expand All @@ -296,7 +296,7 @@
client:
type: browser
name: Mobile Safari
version: ""
version: 5.0.1
engine: WebKit
engine_version: 533.17.9
device:
Expand Down Expand Up @@ -368,7 +368,7 @@
client:
type: browser
name: Mobile Safari
version: "7.0"
version: 7.0.4
engine: WebKit
engine_version: 537.51.1
device:
Expand Down
2 changes: 1 addition & 1 deletion Tests/fixtures/smartphone-3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9812,7 +9812,7 @@
client:
type: browser
name: Mobile Safari
version: ""
version: "11.3"
engine: WebKit
engine_version: ""
device:
Expand Down
2 changes: 1 addition & 1 deletion Tests/fixtures/tablet-3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9521,7 +9521,7 @@
client:
type: browser
name: Mobile Safari
version: ""
version: "14.1"
engine: WebKit
engine_version: ""
device:
Expand Down
6 changes: 3 additions & 3 deletions Tests/fixtures/tablet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6614,7 +6614,7 @@
client:
type: browser
name: Mobile Safari
version: ""
version: 4.3.2
engine: WebKit
engine_version: 533.17.9
device:
Expand All @@ -6632,7 +6632,7 @@
client:
type: browser
name: Mobile Safari
version: ""
version: 5.1.1
engine: WebKit
engine_version: ""
device:
Expand Down Expand Up @@ -6722,7 +6722,7 @@
client:
type: browser
name: Mobile Safari
version: "7.0"
version: "7.0.4"
engine: WebKit
engine_version: 537.51.1
device:
Expand Down
Loading