Skip to content

Commit

Permalink
Added replacement and flagging for selects and .form-control/.form-se…
Browse files Browse the repository at this point in the history
…lect

The replacement logic can handle selects that are defined on a single line,
while the flagging is for selects where the node name and the class name
are on different lines.
  • Loading branch information
orangejenny committed Sep 18, 2024
1 parent 3b93a01 commit 0338ba6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
get_spec,
make_direct_css_renames,
make_numbered_css_renames,
make_select_form_control_renames,
make_template_tag_renames,
make_data_attribute_renames,
make_javascript_dependency_renames,
Expand All @@ -17,6 +18,7 @@
flag_crispy_forms_in_template,
flag_file_inputs,
flag_inline_styles,
flag_selects_without_form_control,
add_todo_comments_for_flags,
update_gruntfile,
)
Expand Down Expand Up @@ -577,6 +579,8 @@ def make_template_line_changes(old_line, spec, is_fresh_migration):

new_line, attribute_renames = make_data_attribute_renames(new_line, spec)
renames.extend(attribute_renames)
new_line, select_renames = make_select_form_control_renames(new_line, spec)
renames.extend(select_renames)
new_line, template_dependency_renames = make_template_dependency_renames(new_line, spec)
renames.extend(template_dependency_renames)
new_line, template_tag_renames = make_template_tag_renames(new_line, spec)
Expand All @@ -590,6 +594,7 @@ def get_flags_in_template_line(template_line, spec):
flags.extend(flag_crispy_forms_in_template(template_line))
flags.extend(flag_file_inputs(template_line))
flags.extend(flag_inline_styles(template_line))
flags.extend(flag_selects_without_form_control(template_line))
return flags

@staticmethod
Expand Down
18 changes: 18 additions & 0 deletions corehq/apps/hqwebapp/tests/utils/test_bootstrap_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
get_spec,
make_direct_css_renames,
make_numbered_css_renames,
make_select_form_control_renames,
make_template_tag_renames,
make_data_attribute_renames,
make_javascript_dependency_renames,
Expand All @@ -13,6 +14,7 @@
replace_path_references,
check_bootstrap3_references_in_template,
flag_crispy_forms_in_template,
flag_selects_without_form_control,
check_bootstrap3_references_in_javascript,
flag_file_inputs,
flag_inline_styles,
Expand Down Expand Up @@ -50,6 +52,15 @@ def test_make_numbered_css_renames_bootstrap5():
eq(renames, ['renamed col-xs-<num> to col-sm-<num>'])


def test_make_select_form_control_renames_bootstrap5():
line = """ <select data-bind:"visible: showMe" class="form-control">\n"""
final_line, renames = make_select_form_control_renames(
line, get_spec('bootstrap_3_to_5')
)
eq(final_line, """ <select data-bind:"visible: showMe" class="form-select">\n""")
eq(renames, ['renamed form-control to form-select'])


def test_make_template_tag_renames_bootstrap5():
line = """ {% requirejs_main "data_dictionary/js/data_dictionary" %}\n"""
final_line, renames = make_template_tag_renames(
Expand Down Expand Up @@ -266,6 +277,13 @@ def test_flag_extended_changed_javascript_plugins_bootstrap5():
'components/popovers/\n']])


def test_flag_selects_without_form_control_bootstrap5():
line = """ <select\n"""
flags = flag_selects_without_form_control(line)
eq(flags[0][0], "css-select-form-control")
eq(flags[0][1].startswith("Please replace form-control with form-select."), True)


def test_file_contains_reference_to_path():
filedata = """
{# Our Libraries #}
Expand Down
23 changes: 23 additions & 0 deletions corehq/apps/hqwebapp/utils/bootstrap/changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ def make_numbered_css_renames(line, spec):
)


def make_select_form_control_renames(line, spec):
if "<select" in line:
if re.search(_get_direct_css_regex("form-control"), line):
return _do_rename(
line,
{"form-control": "form-select"},
_get_direct_css_regex,
lambda x: r"\1form-select\3"
)
return line, []


def make_template_tag_renames(line, spec):
return _do_rename(
line,
Expand Down Expand Up @@ -266,6 +278,17 @@ def flag_crispy_forms_in_template(line):
return flags


def flag_selects_without_form_control(line):
flags = []
if "<select" in line:
if "form-select" not in line and "form-control" not in line:
flags.append([
"css-select-form-control",
_get_change_guide("css-select-form-control")
])
return flags


def file_contains_reference_to_path(filedata, path_reference):
regex = _get_path_reference_regex(path_reference)
return re.search(regex, filedata) is not None
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Please replace form-control with form-select.

In Bootstrap 3, selects used the `form-control` class.
In Bootstrap5, selects use `form-select` instead.

Please remove `form-control` from selects and add `form-select`.

0 comments on commit 0338ba6

Please sign in to comment.