当前位置: 首页>>代码示例>>Python>>正文


Python FilterHTML.filter_html方法代码示例

本文整理汇总了Python中FilterHTML.filter_html方法的典型用法代码示例。如果您正苦于以下问题:Python FilterHTML.filter_html方法的具体用法?Python FilterHTML.filter_html怎么用?Python FilterHTML.filter_html使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在FilterHTML的用法示例。


在下文中一共展示了FilterHTML.filter_html方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_text_attrs

# 需要导入模块: import FilterHTML [as 别名]
# 或者: from FilterHTML import filter_html [as 别名]
   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 &quot; &;'>"
      expected_html = "<img src=\"#\" alt='&quot;hello!&quot; &lt;THIS&gt; &amp; is encoded &quot; &amp;&semi;'>"

      result = FilterHTML.filter_html(input_html, spec)

      self.assertEqual(expected_html, result)
开发者ID:OpenLearningNet,项目名称:FilterHTML,代码行数:29,代码来源:run_tests.py

示例2: test_empty_urls

# 需要导入模块: import FilterHTML [as 别名]
# 或者: from FilterHTML import filter_html [as 别名]
   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)
开发者ID:OpenLearningNet,项目名称:FilterHTML,代码行数:29,代码来源:run_tests.py

示例3: test_boolean_attrs

# 需要导入模块: import FilterHTML [as 别名]
# 或者: from FilterHTML import filter_html [as 别名]
   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)
开发者ID:OpenLearningNet,项目名称:FilterHTML,代码行数:31,代码来源:run_tests.py

示例4: test_tag_removal

# 需要导入模块: import FilterHTML [as 别名]
# 或者: from FilterHTML import filter_html [as 别名]
   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)
开发者ID:Merenon,项目名称:FilterHTML,代码行数:54,代码来源:run_tests.py

示例5: test_escape_data

# 需要导入模块: import FilterHTML [as 别名]
# 或者: from FilterHTML import filter_html [as 别名]
   def test_escape_data(self):
      input_html = "-&gt;"
      expected_html = "-&gt;"

      result = FilterHTML.filter_html(input_html, {})

      self.assertEqual(expected_html, result)
开发者ID:OpenLearningNet,项目名称:FilterHTML,代码行数:9,代码来源:run_tests.py

示例6: test_attribute_value_wildcard

# 需要导入模块: import FilterHTML [as 别名]
# 或者: from FilterHTML import filter_html [as 别名]
   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)
开发者ID:Merenon,项目名称:FilterHTML,代码行数:27,代码来源:run_tests.py

示例7: test_aliases

# 需要导入模块: import FilterHTML [as 别名]
# 或者: from FilterHTML import filter_html [as 别名]
   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)
开发者ID:Merenon,项目名称:FilterHTML,代码行数:28,代码来源:run_tests.py

示例8: test_script_conversion

# 需要导入模块: import FilterHTML [as 别名]
# 或者: from FilterHTML import filter_html [as 别名]
   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 &lt; 4) {
            x = 1 &lt;&lt; 2;
         }
      </pre><br>
      """

      result = FilterHTML.filter_html(input_html, spec)
      self.assertEqual(expected_html, result)
开发者ID:Merenon,项目名称:FilterHTML,代码行数:30,代码来源:run_tests.py

示例9: test_regex_attribute_name_delegates

# 需要导入模块: import FilterHTML [as 别名]
# 或者: from FilterHTML import filter_html [as 别名]
   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)
开发者ID:Merenon,项目名称:FilterHTML,代码行数:32,代码来源:run_tests.py

示例10: test_no_attrs

# 需要导入模块: import FilterHTML [as 别名]
# 或者: from FilterHTML import filter_html [as 别名]
   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)
开发者ID:Merenon,项目名称:FilterHTML,代码行数:28,代码来源:run_tests.py

示例11: test_unquoted_urls

# 需要导入模块: import FilterHTML [as 别名]
# 或者: from FilterHTML import filter_html [as 别名]
   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)
开发者ID:OpenLearningNet,项目名称:FilterHTML,代码行数:23,代码来源:run_tests.py

示例12: test_regex_delegates

# 需要导入模块: import FilterHTML [as 别名]
# 或者: from FilterHTML import filter_html [as 别名]
   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)
开发者ID:Merenon,项目名称:FilterHTML,代码行数:55,代码来源:run_tests.py

示例13: test_invalid_html

# 需要导入模块: import FilterHTML [as 别名]
# 或者: from FilterHTML import filter_html [as 别名]
   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)
开发者ID:Merenon,项目名称:FilterHTML,代码行数:25,代码来源:run_tests.py

示例14: test_styles

# 需要导入模块: import FilterHTML [as 别名]
# 或者: from FilterHTML import filter_html [as 别名]
   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)
开发者ID:Merenon,项目名称:FilterHTML,代码行数:50,代码来源:run_tests.py

示例15: test_text_filtering

# 需要导入模块: import FilterHTML [as 别名]
# 或者: from FilterHTML import filter_html [as 别名]
   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)
开发者ID:Merenon,项目名称:FilterHTML,代码行数:45,代码来源:run_tests.py


注:本文中的FilterHTML.filter_html方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。