Skip to content

Commit 5ec5793

Browse files
committed
Used checkVisibility.
1 parent b458667 commit 5ec5793

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

djangoproject/static/js/djangoproject.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,19 @@ document.querySelector('.menu-button').addEventListener('click', function () {
6767

6868
// Update search input placeholder text based on the user's operating system
6969
(function () {
70-
const mobile_search_input = document.getElementById('id_mobile-q');
71-
const desktop_search_input = document.getElementById('id_desktop-q');
70+
const inputs = [
71+
document.getElementById('id_desktop-q'),
72+
document.getElementById('id_mobile-q'),
73+
];
7274

73-
if (!mobile_search_input || !desktop_search_input) {
74-
return;
75-
}
75+
const el = inputs.find((el) => el.checkVisibility());
76+
if (!el) return;
7677

7778
const is_mac = navigator.userAgent.indexOf('Mac') !== -1;
7879

79-
const elements = [mobile_search_input, desktop_search_input];
80-
elements.forEach((el) => {
81-
const original_placeholder = el.getAttribute('placeholder');
82-
const new_value = `${original_placeholder} (${is_mac ? '⌘\u200aK' : 'Ctrl+K'})`;
83-
el.setAttribute('placeholder', new_value);
84-
});
80+
const original_placeholder = el.getAttribute('placeholder');
81+
const new_value = `${original_placeholder} (${is_mac ? '⌘\u200aK' : 'Ctrl+K'})`;
82+
el.setAttribute('placeholder', new_value);
8583
})();
8684

8785
// Focus, select, and scroll to search input when key combination is pressed
@@ -98,16 +96,12 @@ window.addEventListener('keydown', function (e) {
9896

9997
e.preventDefault();
10098

101-
function isVisible(el) {
102-
return el && el.offsetParent !== null;
103-
}
104-
10599
const inputs = [
106100
document.getElementById('id_desktop-q'),
107101
document.getElementById('id_mobile-q'),
108102
];
109103

110-
const el = inputs.find(isVisible);
104+
const el = inputs.find((el) => el.checkVisibility());
111105
if (!el) return;
112106

113107
el.select();

djangoproject/tests.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from django.urls import NoReverseMatch, get_resolver
1010
from django.utils.translation import activate, gettext as _
1111
from django_hosts.resolvers import reverse
12-
from playwright.sync_api import sync_playwright
12+
from playwright.sync_api import expect, sync_playwright
1313

1414
from docs.models import DocumentRelease, Release
1515

@@ -241,6 +241,7 @@ def test_search_ctrl_k_hotkey_desktop(self):
241241
desktop_search_bar = page.locator("#id_desktop-q")
242242
self.assertFalse(mobile_search_bar.is_visible())
243243
self.assertTrue(desktop_search_bar.is_visible())
244+
expect(desktop_search_bar).to_have_attribute("placeholder", "Search (Ctrl+K)")
244245
is_focused = page.evaluate("document.activeElement.id === 'id_desktop-q'")
245246
self.assertFalse(is_focused)
246247

@@ -256,6 +257,7 @@ def test_search_ctrl_k_hotkey_mobile(self):
256257
mobile_search_bar = page.locator("#id_mobile-q")
257258
desktop_search_bar = page.locator("#id_desktop-q")
258259
self.assertTrue(mobile_search_bar.is_visible())
260+
expect(mobile_search_bar).to_have_attribute("placeholder", "Search (Ctrl+K)")
259261
self.assertFalse(desktop_search_bar.is_visible())
260262
is_focused = page.evaluate("document.activeElement.id === 'id_mobile-q'")
261263
self.assertFalse(is_focused)
@@ -269,11 +271,7 @@ def test_search_placeholder_mac_mode(self):
269271
page = self.browser.new_page(user_agent="Mozilla/5.0 (Macintosh) AppleWebKit")
270272
page.goto(self.live_server_url)
271273

272-
inputs = page.query_selector_all('[name="q"]')
273-
self.assertEqual(len(inputs), 2)
274-
275-
for el in inputs:
276-
placeholder = el.get_attribute("placeholder")
277-
self.assertIn("⌘\u200aK", placeholder)
274+
desktop_search_bar = page.locator("#id_desktop-q")
275+
expect(desktop_search_bar).to_have_attribute("placeholder", "Search (⌘\u200aK)")
278276

279277
page.close()

0 commit comments

Comments
 (0)