Use [disabled] attribute only on tags that support it

Add Capybara selector for disabled links.
This commit is contained in:
2026-07-06 18:28:34 +02:00
parent d4be84875e
commit 774b1eb19b
5 changed files with 31 additions and 26 deletions

View File

@@ -57,6 +57,11 @@
} }
/* NOTE: move to higher priority layer instead of using !important?; add CSS /* NOTE: move to higher priority layer instead of using !important?; add CSS
* @layer requirements in README */ * @layer requirements in README */
/* Disable elements with:
* `[aria-disabled=true]` - non-form elements (e.g. links),
* `[disabled]` - form controls, that may be enabled conditionally,
* `[readonly]` - form controls, that stay disabled for form lifetime. */
[aria-disabled=true],
[disabled] { [disabled] {
border-color: var(--color-border-gray) !important; border-color: var(--color-border-gray) !important;
color: var(--color-border-gray) !important; color: var(--color-border-gray) !important;

View File

@@ -50,22 +50,31 @@ function formValidate(event) {
window.formValidate = formValidate window.formValidate = formValidate
/* Turbo stream actions */ // Turbo stream actions.
const FORM_CONTROLS = ["BUTTON", "FIELDSET", "INPUT", "OPTGROUP", "OPTION", "SELECT",
"TEXTAREA"]
Turbo.StreamElement.prototype.disableElement = function(element) { Turbo.StreamElement.prototype.disableElement = function(element) {
if (FORM_CONTROLS.includes(element.tagName)) {
element.setAttribute("disabled", "disabled") element.setAttribute("disabled", "disabled")
} else {
element.setAttribute("aria-disabled", "true") element.setAttribute("aria-disabled", "true")
// NOTE: tabindex should not be disabled?
element.setAttribute("tabindex", "-1") element.setAttribute("tabindex", "-1")
} }
}
Turbo.StreamActions.disable = function() { Turbo.StreamActions.disable = function() {
this.targetElements.forEach((e) => { this.disableElement(e) }) this.targetElements.forEach((e) => { this.disableElement(e) })
} }
Turbo.StreamElement.prototype.enableElement = function(element) { Turbo.StreamElement.prototype.enableElement = function(element) {
if (FORM_CONTROLS.includes(element.tagName)) {
element.removeAttribute("disabled") element.removeAttribute("disabled")
} else {
element.removeAttribute("aria-disabled") element.removeAttribute("aria-disabled")
// Assume 'tabindex' is not used explicitly, so removing it is safe // Assume 'tabindex' is not used explicitly, so removing it is safe.
element.removeAttribute("tabindex") element.removeAttribute("tabindex")
} }
}
Turbo.StreamActions.enable = function() { Turbo.StreamActions.enable = function() {
this.targetElements.forEach((e) => { this.enableElement(e) }) this.targetElements.forEach((e) => { this.enableElement(e) })
} }

View File

@@ -67,21 +67,11 @@ class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
ActiveRecord::Base.establish_connection(TEST_CONFIGS[name.to_sym]) ActiveRecord::Base.establish_connection(TEST_CONFIGS[name.to_sym])
end end
#def assert_stale(element) Capybara.modify_selector(:link) do
# assert_raises(Selenium::WebDriver::Error::StaleElementReferenceError) { element.tag_name } expression_filter(:disabled) do |xpath, value|
#end builder(xpath).add_attribute_conditions('aria-disabled': value)
end
# HTML does not allow [disabled] attribute on <a> tag, so it's not possible to end
# easily find them using e.g. :link selector
#Capybara.add_selector(:disabled_link) do
# label "<a> tag with [disabled] attribute"
#end
#test "click disabled link" do
# Link should be unclickable
# assert_raises(Selenium::WebDriver::Error::ElementClickInterceptedError) do
# # Use custom selector for disabled links
# find('a[disabled]').click
# end
#end
end end

View File

@@ -41,9 +41,10 @@ class UnitsTest < ApplicationSystemTestCase
sign_in sign_in
link_labels.slice!(:new_unit, :new_subunit) link_labels.slice!(:new_unit, :new_subunit)
type, label = link_labels.to_a.sample type, label = link_labels.to_a.sample
new_link = all(:link, exact_text: label).sample all(:link, exact_text: label).sample.then do |link|
new_link.click link.click
assert_equal 'disabled', new_link[:disabled] link.assert_matches_selector :link, disabled: true
end
values = nil values = nil
within 'tbody > tr:has(input[type=text], textarea)' do within 'tbody > tr:has(input[type=text], textarea)' do
@@ -74,7 +75,7 @@ class UnitsTest < ApplicationSystemTestCase
assert_no_selector :fillable_field assert_no_selector :fillable_field
assert_selector 'tr', count: @user.units.count assert_selector 'tr', count: @user.units.count
end end
assert_no_selector :element, :a, 'disabled': 'disabled', assert_no_selector :link, disabled: true,
exact_text: Regexp.union(link_labels.values) exact_text: Regexp.union(link_labels.values)
assert_equal values, Unit.last.attributes.slice(*values.keys) assert_equal values, Unit.last.attributes.slice(*values.keys)
end end
@@ -126,7 +127,7 @@ class UnitsTest < ApplicationSystemTestCase
if type == :edit if type == :edit
assert_no_selector :link, exact_text: link[:text] assert_no_selector :link, exact_text: link[:text]
else else
assert_equal 'disabled', link[:disabled] link.assert_matches_selector :link, disabled: true
end end
within 'tbody > tr:has(input[type=text])' do within 'tbody > tr:has(input[type=text])' do
@@ -159,7 +160,7 @@ class UnitsTest < ApplicationSystemTestCase
refute subunit_link&.visible? refute subunit_link&.visible?
links[:new_subunit].delete(subunit_link) links[:new_subunit].delete(subunit_link)
else else
assert link[:disabled] link.assert_matches_selector :link, disabled: true
end end
type, link = random_link[] type, link = random_link[]

View File

@@ -8,7 +8,7 @@ class UsersTest < ApplicationSystemTestCase
test 'sign in' do test 'sign in' do
visit root_url visit root_url
assert find_link(href: new_user_session_path)[:disabled] assert_selector :link, text: t(:sign_in), disabled: true
sign_in sign_in
assert_no_current_path new_user_session_path assert_no_current_path new_user_session_path
@@ -27,7 +27,7 @@ class UsersTest < ApplicationSystemTestCase
assert_current_path new_user_session_path assert_current_path new_user_session_path
assert_text t('devise.failure.invalid', authentication_keys: label.downcase_first) assert_text t('devise.failure.invalid', authentication_keys: label.downcase_first)
assert find_link(href: new_user_session_path)[:disabled] assert_selector :link, text: t(:sign_in), disabled: true
assert has_field?(label, with: email) assert has_field?(label, with: email)
end end
@@ -89,7 +89,7 @@ class UsersTest < ApplicationSystemTestCase
test 'register' do test 'register' do
visit root_url visit root_url
click_on t(:register) click_on t(:register)
assert find_link(href: new_user_registration_path)[:disabled] assert_selector :link, text: t(:register), disabled: true
fill_in User.human_attribute_name(:email), with: random_email fill_in User.human_attribute_name(:email), with: random_email
password = random_password password = random_password