diff options
| author | jvoisin | 2019-12-15 06:44:21 -0800 |
|---|---|---|
| committer | jvoisin | 2019-12-15 06:44:21 -0800 |
| commit | f67cd9d7dcf465bb83597cf9dd64fb8b6bc053db (patch) | |
| tree | 4e3938ee274852e4c8975f41c78ce066aa0a821b | |
| parent | 615997be38f47af83c1d33c9fb56185c3710ca57 (diff) | |
Improve the robustness of the CSS parser
| -rw-r--r-- | libmat2/web.py | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/libmat2/web.py b/libmat2/web.py index b770200..2864d60 100644 --- a/libmat2/web.py +++ b/libmat2/web.py | |||
| @@ -17,7 +17,11 @@ class CSSParser(abstract.AbstractParser): | |||
| 17 | 17 | ||
| 18 | def remove_all(self) -> bool: | 18 | def remove_all(self) -> bool: |
| 19 | with open(self.filename, encoding='utf-8') as f: | 19 | with open(self.filename, encoding='utf-8') as f: |
| 20 | cleaned = re.sub(r'/\*.*?\*/', '', f.read(), 0, self.flags) | 20 | try: |
| 21 | content = f.read() | ||
| 22 | except UnicodeDecodeError: # pragma: no cover | ||
| 23 | raise ValueError | ||
| 24 | cleaned = re.sub(r'/\*.*?\*/', '', content, 0, self.flags) | ||
| 21 | with open(self.output_filename, 'w', encoding='utf-8') as f: | 25 | with open(self.output_filename, 'w', encoding='utf-8') as f: |
| 22 | f.write(cleaned) | 26 | f.write(cleaned) |
| 23 | return True | 27 | return True |
| @@ -25,7 +29,11 @@ class CSSParser(abstract.AbstractParser): | |||
| 25 | def get_meta(self) -> Dict[str, Any]: | 29 | def get_meta(self) -> Dict[str, Any]: |
| 26 | metadata = {} | 30 | metadata = {} |
| 27 | with open(self.filename, encoding='utf-8') as f: | 31 | with open(self.filename, encoding='utf-8') as f: |
| 28 | cssdoc = re.findall(r'/\*(.*?)\*/', f.read(), self.flags) | 32 | try: |
| 33 | content = f.read() | ||
| 34 | except UnicodeDecodeError: # pragma: no cover | ||
| 35 | raise ValueError | ||
| 36 | cssdoc = re.findall(r'/\*(.*?)\*/', content, self.flags) | ||
| 29 | for match in cssdoc: | 37 | for match in cssdoc: |
| 30 | for line in match.splitlines(): | 38 | for line in match.splitlines(): |
| 31 | try: | 39 | try: |
