feeds: clean up other stylesheets before putting ours
parent
ad3ba9de1a
commit
2fe3e0b8ee
|
@ -718,11 +718,29 @@ class Item(Uniq):
|
||||||
class FeedXML(Feed, ParserXML):
|
class FeedXML(Feed, ParserXML):
|
||||||
itemsClass = 'ItemXML'
|
itemsClass = 'ItemXML'
|
||||||
|
|
||||||
|
def root_siblings(self):
|
||||||
|
out = []
|
||||||
|
current = self.root.getprevious()
|
||||||
|
|
||||||
|
while current is not None:
|
||||||
|
out.append(current)
|
||||||
|
current = current.getprevious()
|
||||||
|
|
||||||
|
return out
|
||||||
|
|
||||||
def tostring(self, encoding='unicode', **k):
|
def tostring(self, encoding='unicode', **k):
|
||||||
# override needed due to "getroottree" inclusion
|
# override needed due to "getroottree" inclusion
|
||||||
|
# and to add stylesheet
|
||||||
|
|
||||||
if self.root.getprevious() is None:
|
stylesheets = [x for x in self.root_siblings() if isinstance(x, etree.PIBase) and x.target == 'xml-stylesheet']
|
||||||
self.root.addprevious(etree.PI('xml-stylesheet', 'type="text/xsl" href="/sheet.xsl"'))
|
|
||||||
|
if len(stylesheets):
|
||||||
|
# remove all stylesheets present (be that ours or others')
|
||||||
|
for stylesheet in stylesheets:
|
||||||
|
self.root.append(stylesheet) # needed as we can't delete root siblings https://stackoverflow.com/a/60232366
|
||||||
|
self.root.remove(stylesheet)
|
||||||
|
|
||||||
|
self.root.addprevious(etree.PI('xml-stylesheet', 'type="text/xsl" href="/sheet.xsl"'))
|
||||||
|
|
||||||
return etree.tostring(self.root.getroottree(), encoding=encoding, method='xml', **k)
|
return etree.tostring(self.root.getroottree(), encoding=encoding, method='xml', **k)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue