Skip to content

Commit c063039

Browse files
pfeifferfnando
authored andcommitted
Add support for detecting IE8 and IE9 in compatibility mode
1 parent 0b945d4 commit c063039

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

lib/browser.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class Browser
3030

3131
VERSION_REGEX = /(?:Version|MSIE|Opera|Firefox|Chrome|QuickTime|BlackBerry[^\/]+|CoreMedia v)[\/ ]?([a-z0-9.]+)/i
3232

33+
COMPATIBILITY_VIEW_REGEXP = /Trident\/([0-9.]+)/
34+
3335
LANGUAGES = {
3436
"af" => "Afrikaans",
3537
"sq" => "Albanian",
@@ -200,7 +202,13 @@ def version
200202

201203
# Return the full version.
202204
def full_version
203-
_, v = *ua.match(VERSION_REGEX)
205+
if compatibility_view?
206+
_, v = *ua.match(COMPATIBILITY_VIEW_REGEXP)
207+
v.gsub!(/^([0-9])/) { $1.to_i + 4 }
208+
else
209+
_, v = *ua.match(VERSION_REGEX)
210+
end
211+
204212
v || "0.0"
205213
end
206214

@@ -209,6 +217,10 @@ def capable?
209217
webkit? || firefox? || opera? || (ie? && version >= "7")
210218
end
211219

220+
def compatibility_view?
221+
ie? && ua.match(COMPATIBILITY_VIEW_REGEXP)
222+
end
223+
212224
# Detect if browser is WebKit-based.
213225
def webkit?
214226
!!(ua =~ /AppleWebKit/i)

test/browser_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ class BrowserTest < Test::Unit::TestCase
1818
IE6 = "Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)"
1919
IE7 = "Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)"
2020
IE8 = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.2; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
21+
IE8_COMPAT = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/4.0; SLCC1; Media Center PC 5.0; .NET CLR 3.5.21022)"
2122
IE9 = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"
23+
IE9_COMPAT = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/5.0)"
2224
OPERA = "Opera/9.99 (Windows NT 5.1; U; pl) Presto/9.9.9"
2325
FIREFOX = "Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv:1.9.0.2) Gecko/20121223 Ubuntu/9.25 (jaunty) Firefox/3.8"
2426
CHROME = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4"
@@ -149,6 +151,18 @@ def test_detect_ie8
149151
assert_equal "8", @browser.version
150152
end
151153

154+
def test_detect_ie8_in_compatibility_view
155+
@browser.ua = IE8_COMPAT
156+
157+
assert_equal "Internet Explorer", @browser.name
158+
assert @browser.ie?
159+
assert @browser.ie8?
160+
assert @browser.capable?
161+
assert @browser.compatibility_view?
162+
assert_equal "8.0", @browser.full_version
163+
assert_equal "8", @browser.version
164+
end
165+
152166
def test_detect_ie9
153167
@browser.ua = IE9
154168

@@ -160,6 +174,18 @@ def test_detect_ie9
160174
assert_equal "9", @browser.version
161175
end
162176

177+
def test_detect_ie9_in_compatibility_view
178+
@browser.ua = IE9_COMPAT
179+
180+
assert_equal "Internet Explorer", @browser.name
181+
assert @browser.ie?
182+
assert @browser.ie9?
183+
assert @browser.capable?
184+
assert @browser.compatibility_view?
185+
assert_equal "9.0", @browser.full_version
186+
assert_equal "9", @browser.version
187+
end
188+
163189
def test_detect_opera
164190
@browser.ua = OPERA
165191

0 commit comments

Comments
 (0)