當前位置: 首頁>>代碼示例>>Python>>正文


Python FilterHTML類代碼示例

本文整理匯總了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 &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,代碼行數:27,代碼來源:run_tests.py

示例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)
開發者ID:OpenLearningNet,項目名稱:FilterHTML,代碼行數:27,代碼來源:run_tests.py

示例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)
開發者ID:OpenLearningNet,項目名稱:FilterHTML,代碼行數:29,代碼來源:run_tests.py

示例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)
開發者ID:Merenon,項目名稱:FilterHTML,代碼行數:52,代碼來源:run_tests.py

示例5: test_escape_data

   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,代碼行數:7,代碼來源:run_tests.py

示例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)
開發者ID:Merenon,項目名稱:FilterHTML,代碼行數:25,代碼來源:run_tests.py

示例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)
開發者ID:Merenon,項目名稱:FilterHTML,代碼行數:26,代碼來源:run_tests.py

示例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 &lt; 4) {
            x = 1 &lt;&lt; 2;
         }
      </pre><br>
      """

      result = FilterHTML.filter_html(input_html, spec)
      self.assertEqual(expected_html, result)
開發者ID:Merenon,項目名稱:FilterHTML,代碼行數:28,代碼來源:run_tests.py

示例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)
開發者ID:Merenon,項目名稱:FilterHTML,代碼行數:30,代碼來源:run_tests.py

示例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)
開發者ID:Merenon,項目名稱:FilterHTML,代碼行數:26,代碼來源:run_tests.py

示例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)
開發者ID:OpenLearningNet,項目名稱:FilterHTML,代碼行數:21,代碼來源:run_tests.py

示例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)
開發者ID:Merenon,項目名稱:FilterHTML,代碼行數:53,代碼來源:run_tests.py

示例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)
開發者ID:Merenon,項目名稱:FilterHTML,代碼行數:23,代碼來源:run_tests.py

示例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)
開發者ID:Merenon,項目名稱:FilterHTML,代碼行數:48,代碼來源:run_tests.py

示例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)
開發者ID:Merenon,項目名稱:FilterHTML,代碼行數:43,代碼來源:run_tests.py


注:本文中的FilterHTML類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。