Skip to content

Commit

Permalink
fixes image handling srcset attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangFahl committed Mar 26, 2021
1 parent ae949cc commit 638a5fd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
27 changes: 22 additions & 5 deletions frontend/wikicms.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,37 @@ def proxy(self,path:str)->str:
def filter(self,html):
return self.doFilter(html,self.filterKeys)

def fixNode(self,node,attribute,prefix):
def fixNode(self,node,attribute,prefix,delim=None):
'''
fix the given node
node(BeautifulSoup): the node
attribute(str): the name of the attribute e.g. "href", "src"
prefix(str): the prefix to replace e.g. "/", "/images", "/thumbs"
delim(str): if not None the delimiter for multiple values
'''
siteprefix="/%s%s" % (self.site.name,prefix)
if attribute in node.attrs:
val=node.attrs[attribute]
if val.startswith(prefix):
node.attrs[attribute]=val.replace(prefix,siteprefix,1)
pass
attrval=node.attrs[attribute]
if delim is not None:
vals=attrval.split(delim)
else:
vals=[attrval]
delim=""
newvals=[]
for val in vals:
if val.startswith(prefix):
newvals.append(val.replace(prefix,siteprefix,1))
else:
newvals.append(val)
if delim is not None:
node.attrs[attribute]=delim.join(newvals)

def fixImages(self,soup):
for img in soup.findAll('img'):
self.fixNode(img,"src","/")
self.fixNode(img,"srcset","/",", ")


def fixHtml(self,soup):
'''
Expand Down
6 changes: 4 additions & 2 deletions tests/test_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,15 @@ def testFixHtml(self):
frontend=self.server.enableFrontend('www')
pageTitle,content,error=frontend.getContent("Welcome")
self.assertEqual(pageTitle,"Welcome")
if self.debug:
print(content)
#if self.debug:
print(content)

self.assertFalse('''href="/index.php''' in content)
self.assertTrue('''href="/www/index.php''' in content)
self.assertFalse('''src="/images''' in content)
self.assertTrue('''src="/www/images''' in content)
self.assertFalse('''srcset="/images''' in content)
self.assertTrue('''srcset="/www/images''' in content)
pass


Expand Down

0 comments on commit 638a5fd

Please sign in to comment.