From e1877577bc058cfd0b9141b250595b38e95a6ecf Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Thu, 31 Aug 2017 02:14:22 +0200 Subject: make split_special_areas more flexible, refs #2537 (cherry picked from commit 31ef7f149e4553eb9403634c0eec6de4d0123386) --- mitmproxy/contentviews/css.py | 8 ++++---- mitmproxy/utils/strutils.py | 22 ++++++++++------------ test/mitmproxy/utils/test_strutils.py | 8 ++++---- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/mitmproxy/contentviews/css.py b/mitmproxy/contentviews/css.py index 8fa09ed3..30a583da 100644 --- a/mitmproxy/contentviews/css.py +++ b/mitmproxy/contentviews/css.py @@ -14,10 +14,10 @@ A custom CSS prettifier. Compared to other prettifiers, its main features are: """ CSS_SPECIAL_AREAS = ( - ("'", strutils.NO_ESCAPE + "'"), - ('"', strutils.NO_ESCAPE + '"'), - (r"/\*", r"\*/"), - ("//", "$") + "'" + strutils.SINGLELINE_CONTENT + strutils.NO_ESCAPE + "'", + '"' + strutils.SINGLELINE_CONTENT + strutils.NO_ESCAPE + '"', + r"/\*" + strutils.MULTILINE_CONTENT + "\*/", + "//" + strutils.SINGLELINE_CONTENT + "$" ) CSS_SPECIAL_CHARS = "{};:" diff --git a/mitmproxy/utils/strutils.py b/mitmproxy/utils/strutils.py index 37bed7de..71d1c54c 100644 --- a/mitmproxy/utils/strutils.py +++ b/mitmproxy/utils/strutils.py @@ -1,7 +1,7 @@ import io import re import codecs -from typing import AnyStr, Optional, cast, Iterable, Tuple +from typing import AnyStr, Optional, cast, Iterable def always_bytes(str_or_bytes: Optional[AnyStr], *encode_args) -> Optional[bytes]: @@ -153,11 +153,14 @@ def _restore_from_private_code_plane(matchobj): NO_ESCAPE = r"(?>> split_special_areas( >>> "test /* don't modify me */ foo", - >>> [(r"/\*", r"\*/")]) # (left delimiter regex, right delimiter regex) + >>> [r"/\*[\s\S]*?\*/"]) # (regex matching comments) ["test ", "/* don't modify me */", " foo"] "".join(split_special_areas(x, ...)) == x always holds true. """ - patterns = "|".join( - r"{lchar}[\s\S]*?{rchar}".format( - lchar=a, - rchar=b, - ) for (a, b) in area_delimiter) return re.split( - "({})".format(patterns), + "({})".format("|".join(area_delimiter)), data, flags=re.MULTILINE ) @@ -185,7 +183,7 @@ def split_special_areas( def escape_special_areas( data: str, - area_delimiter: Iterable[Tuple[str, str]], + area_delimiter: Iterable[str], control_characters, ): """ @@ -200,11 +198,11 @@ def escape_special_areas( >>> print(x) if (true) { console.log('{}'); } - >>> x = escape_special_areas(x, "{", [("'", "'")]) + >>> x = escape_special_areas(x, "{", ["'" + SINGLELINE_CONTENT + "'"]) >>> print(x) if (true) { console.log('�}'); } >>> x = re.sub(r"\s*{\s*", " {\n ", x) - >>> x = unescape_special_areas(x, "{", [("'", "'")]) + >>> x = unescape_special_areas(x) >>> print(x) if (true) { console.log('{}'); } diff --git a/test/mitmproxy/utils/test_strutils.py b/test/mitmproxy/utils/test_strutils.py index 7ec72e4e..dfe2c620 100644 --- a/test/mitmproxy/utils/test_strutils.py +++ b/test/mitmproxy/utils/test_strutils.py @@ -99,8 +99,8 @@ def test_hexdump(): ESCAPE_QUOTES = [ - ("'", strutils.NO_ESCAPE + "'"), - ('"', strutils.NO_ESCAPE + '"') + "'" + strutils.SINGLELINE_CONTENT + strutils.NO_ESCAPE + "'", + '"' + strutils.SINGLELINE_CONTENT + strutils.NO_ESCAPE + '"' ] @@ -113,11 +113,11 @@ def test_split_special_areas(): ) == ["foo ", "'b\\'a\"r'", " baz"] assert strutils.split_special_areas( "foo\n/*bar\nbaz*/\nqux", - [(r'/\*', r'\*/')] + [r'/\*[\s\S]+?\*/'] ) == ["foo\n", "/*bar\nbaz*/", "\nqux"] assert strutils.split_special_areas( "foo\n//bar\nbaz", - [(r'//', r'$')] + [r'//.+$'] ) == ["foo\n", "//bar", "\nbaz"] -- cgit v1.2.3