mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-07-28 14:44:41 +02:00
[XML formats] Ensure elements are connected to DOM before further manipulation (#2806)
We are setting xmlns attributes at the root element but PHP would still attach redundant ones to the DOM elements created with `createElementNS`. That was because PHP reconciles namespace attributes when appending elements to DOM but since we previously only attached the elements after all children were attached, the reconciliation algorithm was not able to see the root element’s attributes. To fix this, let’s attach each element to its parent immediately after it is created.
This commit is contained in:
parent
37f211a37e
commit
1af6cbeb1e
2 changed files with 41 additions and 43 deletions
|
@ -28,17 +28,17 @@ class AtomFormat extends FormatAbstract{
|
|||
$document = new DomDocument('1.0', $this->getCharset());
|
||||
$document->formatOutput = true;
|
||||
$feed = $document->createElementNS(self::ATOM_NS, 'feed');
|
||||
$feed->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:media', self::MRSS_NS);
|
||||
$document->appendChild($feed);
|
||||
$feed->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:media', self::MRSS_NS);
|
||||
|
||||
$title = $document->createElement('title');
|
||||
$feed->appendChild($title);
|
||||
$title->setAttribute('type', 'text');
|
||||
$title->appendChild($document->createTextNode($extraInfos['name']));
|
||||
$feed->appendChild($title);
|
||||
|
||||
$id = $document->createElement('id');
|
||||
$id->appendChild($document->createTextNode($feedUrl));
|
||||
$feed->appendChild($id);
|
||||
$id->appendChild($document->createTextNode($feedUrl));
|
||||
|
||||
$uriparts = parse_url($uri);
|
||||
if(!empty($extraInfos['icon'])) {
|
||||
|
@ -47,38 +47,38 @@ class AtomFormat extends FormatAbstract{
|
|||
$iconUrl = $uriparts['scheme'] . '://' . $uriparts['host'] . '/favicon.ico';
|
||||
}
|
||||
$icon = $document->createElement('icon');
|
||||
$icon->appendChild($document->createTextNode($iconUrl));
|
||||
$feed->appendChild($icon);
|
||||
$icon->appendChild($document->createTextNode($iconUrl));
|
||||
|
||||
$logo = $document->createElement('logo');
|
||||
$logo->appendChild($document->createTextNode($iconUrl));
|
||||
$feed->appendChild($logo);
|
||||
$logo->appendChild($document->createTextNode($iconUrl));
|
||||
|
||||
$feedTimestamp = gmdate(DATE_ATOM, $this->lastModified);
|
||||
$updated = $document->createElement('updated');
|
||||
$updated->appendChild($document->createTextNode($feedTimestamp));
|
||||
$feed->appendChild($updated);
|
||||
$updated->appendChild($document->createTextNode($feedTimestamp));
|
||||
|
||||
// since we can't guarantee that all items have an author,
|
||||
// a global feed author is mandatory
|
||||
$feedAuthor = 'RSS-Bridge';
|
||||
$author = $document->createElement('author');
|
||||
$authorName = $document->createElement('name');
|
||||
$authorName->appendChild($document->createTextNode($feedAuthor));
|
||||
$author->appendChild($authorName);
|
||||
$feed->appendChild($author);
|
||||
$authorName = $document->createElement('name');
|
||||
$author->appendChild($authorName);
|
||||
$authorName->appendChild($document->createTextNode($feedAuthor));
|
||||
|
||||
$linkAlternate = $document->createElement('link');
|
||||
$feed->appendChild($linkAlternate);
|
||||
$linkAlternate->setAttribute('rel', 'alternate');
|
||||
$linkAlternate->setAttribute('type', 'text/html');
|
||||
$linkAlternate->setAttribute('href', $uri);
|
||||
$feed->appendChild($linkAlternate);
|
||||
|
||||
$linkSelf = $document->createElement('link');
|
||||
$feed->appendChild($linkSelf);
|
||||
$linkSelf->setAttribute('rel', 'self');
|
||||
$linkSelf->setAttribute('type', 'application/atom+xml');
|
||||
$linkSelf->setAttribute('href', $feedUrl);
|
||||
$feed->appendChild($linkSelf);
|
||||
|
||||
foreach($this->getItems() as $item) {
|
||||
$entryTimestamp = $item->getTimestamp();
|
||||
|
@ -111,39 +111,40 @@ class AtomFormat extends FormatAbstract{
|
|||
$entryContent = ' ';
|
||||
|
||||
$entry = $document->createElement('entry');
|
||||
$feed->appendChild($entry);
|
||||
|
||||
$title = $document->createElement('title');
|
||||
$entry->appendChild($title);
|
||||
$title->setAttribute('type', 'html');
|
||||
$title->appendChild($document->createTextNode($entryTitle));
|
||||
$entry->appendChild($title);
|
||||
|
||||
$entryTimestamp = gmdate(DATE_ATOM, $entryTimestamp);
|
||||
$published = $document->createElement('published');
|
||||
$published->appendChild($document->createTextNode($entryTimestamp));
|
||||
$entry->appendChild($published);
|
||||
$published->appendChild($document->createTextNode($entryTimestamp));
|
||||
|
||||
$updated = $document->createElement('updated');
|
||||
$updated->appendChild($document->createTextNode($entryTimestamp));
|
||||
$entry->appendChild($updated);
|
||||
$updated->appendChild($document->createTextNode($entryTimestamp));
|
||||
|
||||
$id = $document->createElement('id');
|
||||
$id->appendChild($document->createTextNode($entryID));
|
||||
$entry->appendChild($id);
|
||||
$id->appendChild($document->createTextNode($entryID));
|
||||
|
||||
if (!empty($entryUri)) {
|
||||
$entryLinkAlternate = $document->createElement('link');
|
||||
$entry->appendChild($entryLinkAlternate);
|
||||
$entryLinkAlternate->setAttribute('rel', 'alternate');
|
||||
$entryLinkAlternate->setAttribute('type', 'text/html');
|
||||
$entryLinkAlternate->setAttribute('href', $entryUri);
|
||||
$entry->appendChild($entryLinkAlternate);
|
||||
}
|
||||
|
||||
if (!empty($item->getAuthor())) {
|
||||
$author = $document->createElement('author');
|
||||
$authorName = $document->createElement('name');
|
||||
$authorName->appendChild($document->createTextNode($item->getAuthor()));
|
||||
$author->appendChild($authorName);
|
||||
$entry->appendChild($author);
|
||||
$authorName = $document->createElement('name');
|
||||
$author->appendChild($authorName);
|
||||
$authorName->appendChild($document->createTextNode($item->getAuthor()));
|
||||
}
|
||||
|
||||
$content = $document->createElement('content');
|
||||
|
@ -153,25 +154,23 @@ class AtomFormat extends FormatAbstract{
|
|||
|
||||
foreach($item->getEnclosures() as $enclosure) {
|
||||
$entryEnclosure = $document->createElement('link');
|
||||
$entry->appendChild($entryEnclosure);
|
||||
$entryEnclosure->setAttribute('rel', 'enclosure');
|
||||
$entryEnclosure->setAttribute('type', getMimeType($enclosure));
|
||||
$entryEnclosure->setAttribute('href', $enclosure);
|
||||
$entry->appendChild($entryEnclosure);
|
||||
}
|
||||
|
||||
foreach($item->getCategories() as $category) {
|
||||
$entryCategory = $document->createElement('category');
|
||||
$entryCategory->setAttribute('term', $category);
|
||||
$entry->appendChild($entryCategory);
|
||||
$entryCategory->setAttribute('term', $category);
|
||||
}
|
||||
|
||||
if (!empty($item->thumbnail)) {
|
||||
$thumbnail = $document->createElementNS(self::MRSS_NS, 'media:thumbnail');
|
||||
$thumbnail->setAttribute('url', $item->thumbnail);
|
||||
$thumbnail = $document->createElementNS(self::MRSS_NS, 'thumbnail');
|
||||
$entry->appendChild($thumbnail);
|
||||
$thumbnail->setAttribute('url', $item->thumbnail);
|
||||
}
|
||||
|
||||
$feed->appendChild($entry);
|
||||
}
|
||||
|
||||
$toReturn = $document->saveXML();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue