Skip to content

Commit c37f9f9

Browse files
committed
Restored search hotkey for desktop.
1 parent e81dd71 commit c37f9f9

File tree

6 files changed

+95
-7
lines changed

6 files changed

+95
-7
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ jobs:
3939
run: |
4040
python -m pip install --upgrade pip setuptools
4141
python -m pip install -r requirements/tests.txt
42+
- name: Install playwright browsers
43+
run: |
44+
python -m playwright install --with-deps
4245
- name: Set up databases
4346
run: |
4447
PGPASSWORD="postgres" createuser -U postgres -d djangoproject --superuser -h localhost

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ COPY ./requirements ./requirements
3131
RUN apt-get update \
3232
&& apt-get install --assume-yes --no-install-recommends ${BUILD_DEPENDENCIES} \
3333
&& python3 -m pip install --no-cache-dir -r ${REQ_FILE} \
34+
&& if [ "${REQ_FILE}" = "requirements/tests.txt" ]; then \
35+
echo "Installing Playwright browsers..."; \
36+
playwright install --with-deps; \
37+
fi \
3438
&& apt-get purge --assume-yes --auto-remove ${BUILD_DEPENDENCIES} \
3539
&& apt-get distclean
3640

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ APP_LIST ?= accounts aggregator blog contact dashboard djangoproject docs founda
44
SCSS = djangoproject/scss
55
STATIC = djangoproject/static
66

7-
ci: compilemessages test
7+
ci: compilemessages collectstatics test
88
@python -m coverage report
99

1010
compilemessages:

djangoproject/static/js/djangoproject.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +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 el = document.getElementById('id_q');
70+
const elements = document.querySelectorAll('[id="id_q"]');
7171

72-
if (!el) {
72+
if (!elements.length) {
7373
return;
7474
}
7575

76-
const original_placeholder = el.getAttribute('placeholder');
7776
const is_mac = navigator.userAgent.indexOf('Mac') !== -1;
78-
const new_value = `${original_placeholder} (${is_mac ? '⌘\u200aK' : 'Ctrl+K'})`;
7977

80-
el.setAttribute('placeholder', new_value);
78+
elements.forEach((el) => {
79+
const original_placeholder = el.getAttribute('placeholder');
80+
const new_value = `${original_placeholder} (${is_mac ? '⌘\u200aK' : 'Ctrl+K'})`;
81+
el.setAttribute('placeholder', new_value);
82+
});
8183
})();
8284

8385
// Focus, select, and scroll to search input when key combination is pressed
@@ -94,7 +96,13 @@ window.addEventListener('keydown', function (e) {
9496

9597
e.preventDefault();
9698

97-
const el = document.querySelector('#id_q');
99+
function querySelectorVisible(selector) {
100+
return [...document.querySelectorAll(selector)].find(
101+
(el) => el.offsetParent !== null,
102+
);
103+
}
104+
105+
const el = querySelectorVisible('#id_q');
98106

99107
el.select();
100108
el.focus();

djangoproject/tests.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
import os
12
from http import HTTPStatus
23
from io import StringIO
34

45
from django.conf import settings
6+
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
57
from django.core.management import call_command
68
from django.test import TestCase
79
from django.urls import NoReverseMatch, get_resolver
810
from django.utils.translation import activate, gettext as _
911
from django_hosts.resolvers import reverse
12+
from playwright.sync_api import sync_playwright
1013

1114
from docs.models import DocumentRelease, Release
1215

@@ -211,3 +214,72 @@ def test_single_h1_per_page(self):
211214
response = self.client.get(url)
212215
self.assertEqual(response.status_code, 200)
213216
self.assertContains(response, "<h1", count=1)
217+
218+
219+
class EndToEndTests(ReleaseMixin, StaticLiveServerTestCase):
220+
@classmethod
221+
def setUpClass(cls):
222+
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
223+
super().setUpClass()
224+
cls.playwright = sync_playwright().start()
225+
cls.browser = cls.playwright.chromium.launch()
226+
227+
@classmethod
228+
def tearDownClass(cls):
229+
super().tearDownClass()
230+
cls.browser.close()
231+
cls.playwright.stop()
232+
233+
def setUp(self):
234+
self.setUpTestData()
235+
236+
def test_search_ctrl_k_hotkey_desktop(self):
237+
page = self.browser.new_page()
238+
page.goto(self.live_server_url)
239+
240+
mobile_search_bar, desktop_search_bar = page.query_selector_all("#id_q")
241+
self.assertFalse(mobile_search_bar.is_visible())
242+
self.assertTrue(desktop_search_bar.is_visible())
243+
is_focused = page.evaluate(
244+
"el => document.activeElement === el", desktop_search_bar
245+
)
246+
self.assertFalse(is_focused)
247+
248+
page.keyboard.press("Control+KeyK")
249+
is_focused = page.evaluate(
250+
"el => document.activeElement === el", desktop_search_bar
251+
)
252+
self.assertTrue(is_focused)
253+
page.close()
254+
255+
def test_search_ctrl_k_hotkey_mobile(self):
256+
page = self.browser.new_page(viewport={"width": 375, "height": 812})
257+
page.goto(self.live_server_url)
258+
259+
mobile_search_bar, desktop_search_bar = page.query_selector_all("#id_q")
260+
self.assertTrue(mobile_search_bar.is_visible())
261+
self.assertFalse(desktop_search_bar.is_visible())
262+
is_focused = page.evaluate(
263+
"el => document.activeElement === el", mobile_search_bar
264+
)
265+
self.assertFalse(is_focused)
266+
267+
page.keyboard.press("Control+KeyK")
268+
is_focused = page.evaluate(
269+
"el => document.activeElement === el", mobile_search_bar
270+
)
271+
self.assertTrue(is_focused)
272+
page.close()
273+
274+
def test_search_placeholder_mac_mode(self):
275+
page = self.browser.new_page(user_agent="Mozilla/5.0 (Macintosh) AppleWebKit")
276+
page.goto(self.live_server_url)
277+
278+
inputs = page.query_selector_all("#id_q")
279+
self.assertEqual(len(inputs), 2)
280+
281+
for el in inputs:
282+
placeholder = el.get_attribute("placeholder")
283+
self.assertIn("⌘\u200aK", placeholder)
284+
285+
page.close()

requirements/tests.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
-r dev.txt
22
coverage==7.11.3
3+
playwright==1.56.0
34
requests-mock==1.12.1
45
tblib>=3.0.0

0 commit comments

Comments
 (0)