aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaximilian Hils <git@maximilianhils.com>2017-08-31 02:14:22 +0200
committerMaximilian Hils <git@maximilianhils.com>2017-09-03 21:02:29 +0200
commite1877577bc058cfd0b9141b250595b38e95a6ecf (patch)
tree57befbce91a25349e2de0f6d2c99f393ed1f770b
parent8030f5003d83fd520c4267c117e1c60d83f6bef5 (diff)
downloadmitmproxy-e1877577bc058cfd0b9141b250595b38e95a6ecf.tar.gz
mitmproxy-e1877577bc058cfd0b9141b250595b38e95a6ecf.tar.bz2
mitmproxy-e1877577bc058cfd0b9141b250595b38e95a6ecf.zip
make split_special_areas more flexible, refs #2537
(cherry picked from commit 31ef7f149e4553eb9403634c0eec6de4d0123386)
-rw-r--r--mitmproxy/contentviews/css.py8
-rw-r--r--mitmproxy/utils/strutils.py22
-rw-r--r--test/mitmproxy/utils/test_strutils.py8
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"(?<!\\)(?:\\\\)*"
+MULTILINE_CONTENT = r"[\s\S]*?"
+SINGLELINE_CONTENT = r".*?"
+MULTILINE_CONTENT_LINE_CONTINUATION = r"(?:.|(?<=\\)\n)*?"
def split_special_areas(
data: str,
- area_delimiter: Iterable[Tuple[str, str]],
+ area_delimiter: Iterable[str],
):
"""
Split a string of code into a [code, special area, code, special area, ..., code] list.
@@ -166,18 +169,13 @@ def split_special_areas(
>>> 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"]