本文整理汇总了Python中FilterHTML类的典型用法代码示例。如果您正苦于以下问题:Python FilterHTML类的具体用法?Python FilterHTML怎么用?Python FilterHTML使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FilterHTML类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_text_attrs
def test_text_attrs(self):
spec = {
'img': {
'src': 'url',
'alt': 'alphanumeric|empty'
}
}
input_html = "<img src=\"\" alt=\"\">"
expected_html = "<img src=\"#\" alt=\"\">"
result = FilterHTML.filter_html(input_html, spec)
self.assertEqual(expected_html, result)
spec = {
'img': {
'src': 'url',
'alt': 'text',
}
}
input_html = "<img src=\"\" alt='\"hello!\" <THIS> & is encoded " &;'>"
expected_html = "<img src=\"#\" alt='"hello!" <THIS> & is encoded " &;'>"
result = FilterHTML.filter_html(input_html, spec)
self.assertEqual(expected_html, result)
示例2: test_empty_urls
def test_empty_urls(self):
spec = {
'a': {
'href': 'url|empty'
}
}
input_html = "<a href=\" \"></a>"
expected_html = "<a href=\"\"></a>"
result = FilterHTML.filter_html(input_html, spec)
self.assertEqual(expected_html, result)
input_html = "<a href=\"\"></a>"
expected_html = "<a href=\"\"></a>"
result = FilterHTML.filter_html(input_html, spec)
self.assertEqual(expected_html, result)
input_html = "<a href=\"javascript://invalid\"></a>"
expected_html = "<a href=\"\"></a>"
result = FilterHTML.filter_html(input_html, spec)
self.assertEqual(expected_html, result)
示例3: test_boolean_attrs
def test_boolean_attrs(self):
spec = {
'input': {
'type': 'alpha',
'checked': 'boolean'
}
}
input_html = "<input type=\"checkbox\" checked>"
expected_html = "<input type=\"checkbox\" checked>"
result = FilterHTML.filter_html(input_html, spec)
self.assertEqual(expected_html, result)
input_html = "<input type=checkbox checked>"
expected_html = "<input type=\"checkbox\" checked>"
result = FilterHTML.filter_html(input_html, spec)
self.assertEqual(expected_html, result)
input_html = "<input type= checked>"
expected_html = "<input checked>"
result = FilterHTML.filter_html(input_html, spec)
self.assertEqual(expected_html, result)
示例4: test_tag_removal
def test_tag_removal(self):
spec = {
'span': {},
'br': {},
}
input_html = """
<span>This is a test</span>
<script>
if (x < 4) {
x = 1 << 2;
// this should all be gone
}
</script><br>
"""
expected_html = """
<span>This is a test</span>
<br>
"""
result = FilterHTML.filter_html(input_html, spec)
self.assertEqual(expected_html, result)
input_html = """
<span>This is a test</span>
<script>
if (x < 4) {
x = 1 << 2;
// this should all be gone
}
</script><br>
<style>
.foo < a {
color: red;
}
</style><div>
Here's a whole lot of stuff to remove
</div><br><!-- comment -->
"""
expected_html = """
<span>This is a test</span>
<br>
<br>
"""
result = FilterHTML.filter_html(input_html, spec, remove=['script', 'style', 'div'])
self.assertEqual(expected_html, result)
示例5: test_escape_data
def test_escape_data(self):
input_html = "->"
expected_html = "->"
result = FilterHTML.filter_html(input_html, {})
self.assertEqual(expected_html, result)
示例6: test_attribute_value_wildcard
def test_attribute_value_wildcard(self):
spec = {
'span': {'id': '*'},
'div': {}
}
input_html = """
<span id="just-an-id">This span tag is allowed, id-attribute has wildcard.</span>
<span id="true">This span tag is allowed, id-attribute has wildcard.</span>
<span id="fooBar1234">This span tag is allowed, id-attribute has wildcard.</span>
<span width="100px">This span tag is allowed, but its width-attribute stripped</span>
<div id="remove-me">This div tag is allowed, but its attribtues stripped</div>
"""
expected_html = """
<span id="just-an-id">This span tag is allowed, id-attribute has wildcard.</span>
<span id="true">This span tag is allowed, id-attribute has wildcard.</span>
<span id="fooBar1234">This span tag is allowed, id-attribute has wildcard.</span>
<span>This span tag is allowed, but its width-attribute stripped</span>
<div>This div tag is allowed, but its attribtues stripped</div>
"""
result = FilterHTML.filter_html(input_html, spec)
self.assertEqual(expected_html, result)
示例7: test_aliases
def test_aliases(self):
spec = {
'p': {
'class': [
'centered'
]
},
'strong': {},
'br': {},
'b': 'strong',
'center': 'p class="centered"'
}
input_html = """
<b>This is a test</b><br>
<center>centered text</center>
"""
expected_html = """
<strong>This is a test</strong><br>
<p class="centered">centered text</p>
"""
result = FilterHTML.filter_html(input_html, spec)
self.assertEqual(expected_html, result)
示例8: test_script_conversion
def test_script_conversion(self):
spec = {
'span': {},
'br': {},
'pre': {},
'script': 'pre'
}
input_html = """
<span>This is a test</span>
<script>
if (x < 4) {
x = 1 << 2;
}
</script><br>
"""
expected_html = """
<span>This is a test</span>
<pre>
if (x < 4) {
x = 1 << 2;
}
</pre><br>
"""
result = FilterHTML.filter_html(input_html, spec)
self.assertEqual(expected_html, result)
示例9: test_regex_attribute_name_delegates
def test_regex_attribute_name_delegates(self):
spec = {
'span': {
re.compile(r'^data-[\w-]+$'): ['true', 'false'],
'id': ['test']
},
}
input_html = """
<span data-attr-one="true">Span content</span>
<span data-attr-two="true" data-attribute-three="false">Span content</span>
<span id="remove-me" data-test="true">Span content</span>
<span id="test" data-test="true">Span content</span>
<span width="100px">Span content</span>
<div data-foobar="false">Tag and Attribute allowed</div>
"""
expected_html = """
<span data-attr-one="true">Span content</span>
<span data-attr-two="true" data-attribute-three="false">Span content</span>
<span data-test="true">Span content</span>
<span id="test" data-test="true">Span content</span>
<span>Span content</span>
Tag and Attribute allowed
"""
result = FilterHTML.filter_html(input_html, spec)
self.assertEqual(expected_html, result)
示例10: test_no_attrs
def test_no_attrs(self):
spec = {
'b': {},
'em': {},
'br': {},
'hr': {},
'span': {}
}
input_html = """
<b>This is a test</b><br>
<span class="invalid-class">This span tag should be allowed, but its attributes stripped</span>
<div>This div tag is not allowed, and the tag will be removed</div>
<span id="invalid-id">This span tag is allowed, but its attributes stripped</span>
"""
expected_html = """
<b>This is a test</b><br>
<span>This span tag should be allowed, but its attributes stripped</span>
This div tag is not allowed, and the tag will be removed
<span>This span tag is allowed, but its attributes stripped</span>
"""
result = FilterHTML.filter_html(input_html, spec)
self.assertEqual(expected_html, result)
示例11: test_unquoted_urls
def test_unquoted_urls(self):
spec = {
'a': {
'href': 'url',
'checked': 'boolean'
}
}
input_html = "<a href=http://www.example.com></a>"
expected_html = "<a href=\"http://www.example.com\"></a>"
result = FilterHTML.filter_html(input_html, spec)
self.assertEqual(expected_html, result)
input_html = "<a href= checked></a>"
expected_html = "<a href=\"#\" checked></a>"
result = FilterHTML.filter_html(input_html, spec)
self.assertEqual(expected_html, result)
示例12: test_regex_delegates
def test_regex_delegates(self):
def filter_color(color):
if color in ['red', 'green', 'blue']:
return color
else:
return 'red'
spec = {
'span': {
'style': {
'color': filter_color,
}
},
'div': {
'style': {
'width': re.compile(r'^\d+px$'),
'height': re.compile(r'^\d+px$')
}
},
'b': {},
'br': {},
}
input_html = """
<b>This is a test</b><br>
<span style="color:red;">red</span><br>
<span style="color:green;">green</span><br>
<span style="color:blue;">blue</span><br>
<span style="color:rgb(255, 255, 255);">white</span><br>
<span style="color:rgb(0, 255, 255);">cyan</span><br>
<span style="color:hsla(40, 20%, 10%, 0.1);">hsla</span><br>
<span style="color:invalid;">invalid</span><br>
<div style="width:32px;height:24px;">div</div>
<div style="width:32px;height:invalid;">invalid div</div>
"""
expected_html = """
<b>This is a test</b><br>
<span style="color:red;">red</span><br>
<span style="color:green;">green</span><br>
<span style="color:blue;">blue</span><br>
<span style="color:red;">white</span><br>
<span style="color:red;">cyan</span><br>
<span style="color:red;">hsla</span><br>
<span style="color:red;">invalid</span><br>
<div style="width:32px;height:24px;">div</div>
<div style="width:32px;">invalid div</div>
"""
result = FilterHTML.filter_html(input_html, spec)
self.assertEqual(expected_html, result)
示例13: test_invalid_html
def test_invalid_html(self):
spec = {
'b': {},
'br': {},
'span': {
'id': 'alphanumeric'
},
}
with self.assertRaises(FilterHTML.TagMismatchError):
result = FilterHTML.filter_html("<b>test<br>", spec)
with self.assertRaises(FilterHTML.TagMismatchError):
result = FilterHTML.filter_html("<br>test</b>", spec)
with self.assertRaises(FilterHTML.TagMismatchError):
result = FilterHTML.filter_html("<span><br>test</b>", spec)
with self.assertRaises(FilterHTML.HTMLSyntaxError):
result = FilterHTML.filter_html("<br>test<span id=\"foo", spec)
result = FilterHTML.filter_html(u'<span id="\u2713">check</span>', spec)
self.assertEqual('<span>check</span>', result)
示例14: test_styles
def test_styles(self):
spec = {
'span': {
'style': {
'color': 'color',
}
},
'div': {
'style': {
'width': 'measurement',
'height': 'measurement'
}
},
'b': {},
'br': {},
}
input_html = """
<b>This is a test</b><br>
<span style="color:red;">red</span><br>
<span style="color:#fff;">#fff</span><br>
<span style="color:#f0f0ef;">#f0f0ef</span><br>
<span style="color:rgb(255, 255, 255);">white</span><br>
<span style="color:rgba(0, 255, 255, 0.5);">cyan</span><br>
<span style="color:hsla(40, 20%, 10%, 0.1);">hsla</span><br>
<span style="color:hsl(40, 20%, 10%);">hsl</span><br>
<span style="color:invalid;">invalid</span><br>
<div style="width:32px;height:24px;">div</div>
<div style="width:32px;height:invalid;">invalid div</div>
"""
expected_html = """
<b>This is a test</b><br>
<span style="color:red;">red</span><br>
<span style="color:#fff;">#fff</span><br>
<span style="color:#f0f0ef;">#f0f0ef</span><br>
<span style="color:rgb(255, 255, 255);">white</span><br>
<span style="color:rgba(0, 255, 255, 0.5);">cyan</span><br>
<span style="color:hsla(40, 20%, 10%, 0.1);">hsla</span><br>
<span style="color:hsl(40, 20%, 10%);">hsl</span><br>
<span>invalid</span><br>
<div style="width:32px;height:24px;">div</div>
<div style="width:32px;">invalid div</div>
"""
result = FilterHTML.filter_html(input_html, spec)
self.assertEqual(expected_html, result)
示例15: test_text_filtering
def test_text_filtering(self):
spec = {
'a': {
'href': 'url'
},
'br': {}
}
# very basic url autolinking
URLIZE_RE = '(%s)' % '|'.join([
r'<(?:f|ht)tps?://[^>]*>',
r'\b(?:f|ht)tps?://[^)<>\s]+[^.,)<>\s]',
])
def urlize(text, stack):
is_inside_a_tag = False
for tag in stack:
tag_name, attributes = tag
if tag_name == 'a':
is_inside_a_tag = True
break
if is_inside_a_tag:
# already linked
return text
else:
return re.sub(URLIZE_RE, r'<a href="\1" class="invalid">\1</a>', text)
input_html = """
<a href="http://www.example.com">example</a><br>
<a href="http://www.example.com">http://www.example.com</a><br>
http://www.example.com<br>
"""
expected_html = """
<a href="http://www.example.com">example</a><br>
<a href="http://www.example.com">http://www.example.com</a><br>
<a href="http://www.example.com">http://www.example.com</a><br>
"""
result = FilterHTML.filter_html(input_html, spec, text_filter=urlize)
self.assertEqual(expected_html, result)