From 5911041777f584e8c42cd98fc2979ca88e931f40 Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Fri, 20 Jan 2023 13:43:47 -0500 Subject: [PATCH] #11516: Tweak fix to ensure proper highlighting --- netbox/netbox/views/misc.py | 9 ++++++++- netbox/utilities/utils.py | 14 ++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/netbox/netbox/views/misc.py b/netbox/netbox/views/misc.py index adf539bbe..3c8c93f84 100644 --- a/netbox/netbox/views/misc.py +++ b/netbox/netbox/views/misc.py @@ -1,3 +1,4 @@ +import re from collections import namedtuple from django.conf import settings @@ -160,7 +161,13 @@ class SearchView(View): lookup=lookup ) - if form.cleaned_data['lookup'] != LookupTypes.EXACT: + # If performing a regex search, pass the highlight value as a compiled pattern + if form.cleaned_data['lookup'] == LookupTypes.REGEX: + try: + highlight = re.compile(f"({form.cleaned_data['q']})", flags=re.IGNORECASE) + except re.error: + pass + elif form.cleaned_data['lookup'] != LookupTypes.EXACT: highlight = form.cleaned_data['q'] table = SearchTable(results, highlight=highlight) diff --git a/netbox/utilities/utils.py b/netbox/utilities/utils.py index 15275f858..b6f626eb4 100644 --- a/netbox/utilities/utils.py +++ b/netbox/utilities/utils.py @@ -514,11 +514,21 @@ def clean_html(html, schemes): def highlight_string(value, highlight, trim_pre=None, trim_post=None, trim_placeholder='...'): """ Highlight a string within a string and optionally trim the pre/post portions of the original string. + + Args: + value: The body of text being searched against + highlight: The string of compiled regex pattern to highlight in `value` + trim_pre: Maximum length of pre-highlight text to include + trim_post: Maximum length of post-highlight text to include + trim_placeholder: String value to swap in for trimmed pre/post text """ # Split value on highlight string try: - pre, match, post = re.split(fr'({re.escape(highlight)})', value, maxsplit=1, flags=re.IGNORECASE) - except ValueError: + if type(highlight) is re.Pattern: + pre, match, post = highlight.split(value, maxsplit=1) + else: + pre, match, post = re.split(fr'({highlight})', value, maxsplit=1, flags=re.IGNORECASE) + except ValueError as e: # Match not found return escape(value)