| OLD | NEW |
| 1 # coding: utf-8 | 1 # coding: utf-8 |
| 2 | 2 |
| 3 # This file is part of the Adblock Plus web scripts, | 3 # This file is part of the Adblock Plus web scripts, |
| 4 # Copyright (C) 2006-2016 Eyeo GmbH | 4 # Copyright (C) 2006-2016 Eyeo GmbH |
| 5 # | 5 # |
| 6 # Adblock Plus is free software: you can redistribute it and/or modify | 6 # Adblock Plus is free software: you can redistribute it and/or modify |
| 7 # it under the terms of the GNU General Public License version 3 as | 7 # it under the terms of the GNU General Public License version 3 as |
| 8 # published by the Free Software Foundation. | 8 # published by the Free Software Foundation. |
| 9 # | 9 # |
| 10 # Adblock Plus is distributed in the hope that it will be useful, | 10 # Adblock Plus is distributed in the hope that it will be useful, |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 | 174 |
| 175 # Insert attributes | 175 # Insert attributes |
| 176 result = escape(result) | 176 result = escape(result) |
| 177 def stringify_attribute((name, value)): | 177 def stringify_attribute((name, value)): |
| 178 return '%s="%s"' % ( | 178 return '%s="%s"' % ( |
| 179 escape(name), | 179 escape(name), |
| 180 escape(self.insert_localized_strings(value, {})) | 180 escape(self.insert_localized_strings(value, {})) |
| 181 ) | 181 ) |
| 182 | 182 |
| 183 for tag in self.whitelist: | 183 for tag in self.whitelist: |
| 184 allowed_contents = "(?:[^<>]|%s)" % "|".join(( |
| 185 "<(?:%s[^<>]*?|/%s)>" % (t, t) |
| 186 for t in map(re.escape, self.whitelist - {tag}) |
| 187 )) |
| 184 saved = saved_attributes.get(tag, []) | 188 saved = saved_attributes.get(tag, []) |
| 185 for attrs in saved: | 189 for attrs in saved: |
| 186 attrs = map(stringify_attribute, attrs) | 190 attrs = map(stringify_attribute, attrs) |
| 187 result = re.sub( | 191 result = re.sub( |
| 188 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag))
, | 192 r"%s(%s*?)%s" % (re_escape("<%s>" % tag), allowed_contents, |
| 193 re_escape("</%s>" % tag)), |
| 189 lambda match: r'<%s%s>%s</%s>' % ( | 194 lambda match: r'<%s%s>%s</%s>' % ( |
| 190 tag, | 195 tag, |
| 191 " " + " ".join(attrs) if attrs else "", | 196 " " + " ".join(attrs) if attrs else "", |
| 192 match.group(1), | 197 match.group(1), |
| 193 tag | 198 tag |
| 194 ), | 199 ), |
| 195 result, 1, flags=re.S | 200 result, 1, flags=re.S |
| 196 ) | 201 ) |
| 197 result = re.sub( | 202 result = re.sub( |
| 198 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)), | 203 r"%s(%s*?)%s" % (re_escape("<%s>" % tag), allowed_contents, |
| 204 re_escape("</%s>" % tag)), |
| 199 r"<%s>\1</%s>" % (tag, tag), | 205 r"<%s>\1</%s>" % (tag, tag), |
| 200 result, flags=re.S | 206 result, flags=re.S |
| 201 ) | 207 ) |
| 202 return result | 208 return result |
| 203 | 209 |
| 204 def insert_localized_strings(self, text, escapes, to_html=lambda s: s): | 210 def insert_localized_strings(self, text, escapes, to_html=lambda s: s): |
| 205 def lookup_string(match): | 211 def lookup_string(match): |
| 206 name, comment, default = match.groups() | 212 name, comment, default = match.groups() |
| 207 if default: | 213 if default: |
| 208 default = to_html(default).strip() | 214 default = to_html(default).strip() |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 stack.pop() | 443 stack.pop() |
| 438 stack[-1]["subitems"].append(item) | 444 stack[-1]["subitems"].append(item) |
| 439 stack.append(item) | 445 stack.append(item) |
| 440 return structured | 446 return structured |
| 441 | 447 |
| 442 converters = { | 448 converters = { |
| 443 "html": RawConverter, | 449 "html": RawConverter, |
| 444 "md": MarkdownConverter, | 450 "md": MarkdownConverter, |
| 445 "tmpl": TemplateConverter, | 451 "tmpl": TemplateConverter, |
| 446 } | 452 } |
| OLD | NEW |