Robot Framework
markuputils.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 import re
17 
18 from .htmlformatters import LinkFormatter, HtmlFormatter
19 
20 
21 
24 _format_url = LinkFormatter().format_url
25 
28 _format_html = HtmlFormatter().format
29 
32 _generic_escapes = (('&', '&amp;'), ('<', '&lt;'), ('>', '&gt;'))
33 
36 _attribute_escapes = _generic_escapes \
37  + (('"', '&quot;'), ('\n', '&#10;'), ('\r', '&#13;'), ('\t', '&#09;'))
38 
41 _illegal_chars_in_xml = re.compile('[\x00-\x08\x0B\x0C\x0E-\x1F\uFFFE\uFFFF]')
42 
43 
44 def html_escape(text, linkify=True):
45  text = _escape(text)
46  if linkify and '://' in text:
47  text = _format_url(text)
48  return text
49 
50 
51 def xml_escape(text):
52  return _illegal_chars_in_xml.sub('', _escape(text))
53 
54 
55 def html_format(text):
56  return _format_html(_escape(text))
57 
58 
59 def attribute_escape(attr):
60  attr = _escape(attr, _attribute_escapes)
61  return _illegal_chars_in_xml.sub('', attr)
62 
63 
64 def _escape(text, escapes=_generic_escapes):
65  for name, value in escapes:
66  if name in text: # performance optimization
67  text = text.replace(name, value)
68  return text
def _escape(text, escapes=_generic_escapes)
Definition: markuputils.py:64
def html_escape(text, linkify=True)
Definition: markuputils.py:44
def attribute_escape(attr)
Definition: markuputils.py:59