Skip to content

Commit

Permalink
Check img|source[@srcset] as part of check -l
Browse files Browse the repository at this point in the history
Create phantom img[@src] for each *[@srcset] item, allowing lxml
to understand each item as a link.

Resolves issue #1989.
  • Loading branch information
da2x committed Sep 1, 2015
1 parent 9ba5c01 commit a970c13
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Expand Up @@ -27,6 +27,7 @@ Features
Bugfixes
--------

* Check ``img|source[@srcset]`` as part of ``check -l`` (Issue #1989)
* Clean up translations for third party components
* ``pagekind["main_index"]`` set on the main indexes to differentiate
them from all the other indexes.
Expand Down
2 changes: 1 addition & 1 deletion nikola/nikola.py
Expand Up @@ -1104,7 +1104,7 @@ def rewrite_links(self, doc, src, lang):
doc.rewrite_links(lambda dst: self.url_replacer(src, dst, lang), resolve_base_href=False)

# lxml ignores srcset in img and source elements, so do that by hand
objs = list(doc.findall('*//img')) + list(doc.findall('*//source'))
objs = list(doc.findall('(*//img|*//source)'))
for obj in objs:
if 'srcset' in obj.attrib:
urls = [u.strip() for u in obj.attrib['srcset'].split(',')]
Expand Down
10 changes: 9 additions & 1 deletion nikola/plugins/command/check.py
Expand Up @@ -204,7 +204,15 @@ def analyze(self, fname, find_sources=False, check_remote=False):
return False

d = lxml.html.fromstring(open(filename, 'rb').read())
for l in d.iterlinks():
extra_objs = lxml.html.fromstring('html')

# Turn elements with a srcset attribute into individual img elements with src attributes
for obj in list(d.xpath('(*//img|*//source)')):
if 'srcset' in obj.attrib:
for srcset_item in obj.attrib['srcset'].split(','):
extra_objs.append(lxml.etree.Element('img', src=srcset_item.strip().split(' ')[-0]))

This comment has been minimized.

Copy link
@ralsina

ralsina Sep 1, 2015

Member

-0?


for l in list(d.iterlinks()) + list(extra_objs.iterlinks()):
target = l[2]
if target == "#":
continue
Expand Down

0 comments on commit a970c13

Please sign in to comment.