2 This module implements a VERY limited parser that finds <link> tags in
3 the head of HTML or XHTML documents and parses out their attributes
4 according to the OpenID spec. It is a liberal parser, but it requires
5 these things from the data in order to work:
7 - There must be an open <html> tag
9 - There must be an open <head> tag inside of the <html> tag
11 - Only <link>s that are found inside of the <head> tag are parsed
14 - The parser follows the OpenID specification in resolving the
15 attributes of the link tags. This means that the attributes DO NOT
16 get resolved as they would by an XML or HTML parser. In particular,
17 only certain entities get replaced, and href attributes do not get
18 resolved relative to a base URL.
20 From http://openid.net/specs.bml#linkrel:
22 - The openid.server URL MUST be an absolute URL. OpenID consumers
23 MUST NOT attempt to resolve relative URLs.
25 - The openid.server URL MUST NOT include entities other than &,
26 <, >, and ".
28 The parser ignores SGML comments and <![CDATA[blocks]]>. Both kinds of
29 quoting are allowed for attributes.
31 The parser deals with invalid markup in these ways:
33 - Tag names are not case-sensitive
35 - The <html> tag is accepted even when it is not at the top level
37 - The <head> tag is accepted even when it is not a direct child of
38 the <html> tag, but a <html> tag must be an ancestor of the <head>
41 - <link> tags are accepted even when they are not direct children of
42 the <head> tag, but a <head> tag must be an ancestor of the <link>
45 - If there is no closing tag for an open <html> or <head> tag, the
46 remainder of the document is viewed as being inside of the tag. If
47 there is no closing tag for a <link> tag, the link tag is treated
48 as a short tag. Exceptions to this rule are that <html> closes
49 <html> and <body> or <head> closes <head>
51 - Attributes of the <link> tag are not required to be quoted.
53 - In the case of duplicated attribute names, the attribute coming
54 last in the tag will be the value returned.
56 - Any text that does not parse as an attribute within a link tag will
57 be ignored. (e.g. <link pumpkin rel='openid.server' /> will ignore
60 - If there are more than one <html> or <head> tag, the parser only
61 looks inside of the first one.
63 - The contents of <script> tags are ignored entirely, except unclosed
64 <script> tags. Unclosed <script> tags are ignored.
66 - Any other invalid markup is ignored, including unclosed SGML
67 comments and unclosed <![CDATA[blocks.
70 __all__
= ['parseLinkAttrs']
74 flags
= ( re
.DOTALL
# Match newlines with '.'
76 | re
.VERBOSE
# Allow comments and whitespace in patterns
77 | re
.UNICODE
# Make \b respect Unicode word boundaries
80 # Stuff to remove before we start looking for tags
81 removed_re
= re
.compile(r
'''
91 # make sure script is not an XML namespace
99 # Starts with the tag name at a word boundary, where the tag name is
103 # All of the stuff up to a ">", hopefully attributes.
106 (?: # Match a short tag
115 (?: # One of the specified close tags
126 def tagMatcher(tag_name
, *close_tags
):
128 options
= '|'.join((tag_name
,) + close_tags
)
129 closers
= '(?:%s)' % (options
,)
133 expr
= tag_expr
% locals()
134 return re
.compile(expr
, flags
)
136 # Must contain at least an open html and an open head tag
137 html_find
= tagMatcher('html')
138 head_find
= tagMatcher('head', 'body')
139 link_find
= re
.compile(r
'<link\b(?!:)', flags
)
141 attr_find
= re
.compile(r
'''
142 # Must start with a sequence of word-characters, followed by an equals sign
145 # Then either a quoted or unquoted attribute
148 # Match everything that\'s between matching quote marks
149 (?P
<qopen
>["\'])(?P<q_val>.*?)(?P=qopen)
152 # If the value is not quoted, match up to whitespace
153 (?P<unq_val>(?:[^\s<>/]|/(?!>))+)
161 # Entity replacement:
169 ent_replace = re.compile(r'&(%s);' % '|
'.join(replacements.keys()))
171 "Replace the entities that are specified by OpenID"
172 return replacements.get(mo.group(1), mo.group())
174 def parseLinkAttrs(html):
175 """Find all link tags in a string representing a HTML document and
176 return a list of their attributes.
178 @param html: the text to parse
179 @type html: str or unicode
181 @return: A list of dictionaries of attributes, one for each link tag
182 @rtype: [[(type(html), type(html))]]
184 stripped = removed_re.sub('', html)
185 html_mo = html_find.search(stripped)
186 if html_mo is None or html_mo.start('contents
') == -1:
189 start, end = html_mo.span('contents
')
190 head_mo = head_find.search(stripped, start, end)
191 if head_mo is None or head_mo.start('contents
') == -1:
194 start, end = head_mo.span('contents
')
195 link_mos = link_find.finditer(stripped, head_mo.start(), head_mo.end())
198 for link_mo in link_mos:
199 start = link_mo.start() + 5
201 for attr_mo in attr_find.finditer(stripped, start):
202 if attr_mo.lastgroup == 'end_link
':
205 # Either q_val or unq_val must be present, but not both
206 # unq_val is a True (non-empty) value if it is present
207 attr_name, q_val, unq_val = attr_mo.group(
208 'attr_name
', 'q_val
', 'unq_val
')
209 attr_val = ent_replace.sub(replaceEnt, unq_val or q_val)
211 link_attrs[attr_name] = attr_val
213 matches.append(link_attrs)
217 def relMatches(rel_attr, target_rel):
218 """Does this target_rel appear in the rel_str?"""
220 rels = rel_attr.strip().split()
223 if rel == target_rel:
228 def linkHasRel(link_attrs, target_rel):
229 """Does this link have target_rel as a relationship?"""
231 rel_attr = link_attrs.get('rel
')
232 return rel_attr and relMatches(rel_attr, target_rel)
234 def findLinksRel(link_attrs_list, target_rel):
235 """Filter the list of link attributes on whether it has target_rel
236 as a relationship."""
238 matchesTarget = lambda attrs: linkHasRel(attrs, target_rel)
239 return filter(matchesTarget, link_attrs_list)
241 def findFirstHref(link_attrs_list, target_rel):
242 """Return the value of the href attribute for the first link tag
243 in the list that has target_rel as a relationship."""
245 matches = findLinksRel(link_attrs_list, target_rel)
249 return first.get('href
')