Skip to content

Commit 6effff7

Browse files
committed
Bugfix for addChild values with "special chars".
1 parent c4a8c7f commit 6effff7

5 files changed

Lines changed: 33 additions & 33 deletions

File tree

src/AbstractElement.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public function addContributor(string $name, ?string $email = null, ?string $uri
141141

142142
public function addChildrenTo(SimpleXMLElement $parent): void
143143
{
144-
$parent->addChild('id', $this->id);
144+
$parent->addChild('id', htmlspecialchars($this->id));
145145

146146
self::addChildWithTypeToElement($parent, 'title', $this->title, $this->titleType);
147147

@@ -170,7 +170,7 @@ public function addChildrenTo(SimpleXMLElement $parent): void
170170
foreach ($elements as $element) {
171171
$child = $parent->addChild($type);
172172
foreach ($element as $name => $attribute) {
173-
$child->addChild($name, $attribute);
173+
$child->addChild($name, htmlspecialchars($attribute));
174174
}
175175
}
176176
}
@@ -205,7 +205,7 @@ protected static function addChildWithTypeToElement(SimpleXMLElement $parent, st
205205
$element = $parent->addChild($name);
206206
self::addCData($data, $element);
207207
} else {
208-
$element = $parent->addChild($name, $data);
208+
$element = $parent->addChild($name, htmlspecialchars($data));
209209
}
210210
} else {
211211
$element = $parent->addChild($name);

src/Feed.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,19 @@ public function addChildrenTo(SimpleXMLElement $parent): void
135135
parent::addChildrenTo($parent);
136136

137137
if (null !== $this->subtitle) {
138-
$parent->addChild('subtitle', $this->subtitle);
138+
$parent->addChild('subtitle', htmlspecialchars($this->subtitle));
139139
}
140140

141141
if (null !== $this->logo) {
142-
$parent->addChild('logo', $this->logo);
142+
$parent->addChild('logo', htmlspecialchars($this->logo));
143143
}
144144

145145
if (null !== $this->icon) {
146-
$parent->addChild('icon', $this->icon);
146+
$parent->addChild('icon', htmlspecialchars($this->icon));
147147
}
148148

149149
if (null !== $this->generator) {
150-
$generator = $parent->addChild('generator', $this->generator);
150+
$generator = $parent->addChild('generator', htmlspecialchars($this->generator));
151151
if (null !== $this->generatorVersion) {
152152
$generator->addAttribute('version', $this->generatorVersion);
153153
}
@@ -157,7 +157,7 @@ public function addChildrenTo(SimpleXMLElement $parent): void
157157
}
158158

159159
foreach ($this->customElements as $customElement) {
160-
$element = $parent->addChild($customElement['name'], $customElement['value'], $customElement['uri']);
160+
$element = $parent->addChild($customElement['name'], htmlspecialchars($customElement['value']), $customElement['uri']);
161161
foreach ($customElement['attributes'] as $name => $value) {
162162
$element->addAttribute($name, $value);
163163
}
@@ -180,7 +180,7 @@ public function getSimpleXML(): SimpleXMLElement
180180

181181
$attributesString = '';
182182
foreach ($attributes as $name => $attribute) {
183-
$attributesString .= " {$name}=\"{$attribute}\"";
183+
$attributesString .= ' '.$name.'="'.htmlspecialchars($attribute).'"';
184184
}
185185

186186
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"'.$attributesString.' />');

tests/FeedTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,18 @@ public function testFeedCreation1(): void
2626
{
2727
$feed = new Feed();
2828
$feed->setTitle('title');
29-
$feed->addAuthor('author', 'author@test.com', 'http://test.com/author');
30-
$feed->addAuthor('author', 'author@test.com', 'http://test.com/author');
29+
$feed->addAuthor('author', 'author@test.com', 'http://test.com/author?a=b&c=d');
30+
$feed->addAuthor('author', 'author@test.com', 'http://test.com/author?a=b&c=d');
3131
$feed->setRights('©2019');
32-
$feed->addLink('http://test.com/link', 'via', 'text/html');
32+
$feed->addLink('http://test.com/link?a=b&c=d', 'via', 'text/html');
3333
$feed->addCategory('term', 'http://scheme.com', 'label');
3434
$feed->setId('tag:test');
3535
$feed->setLanguage('en');
36-
$feed->setIconUri('http://test.com/icon');
37-
$feed->setLogoUri('http://test.com/logo');
38-
$feed->setSubtitle('subtitle');
39-
$feed->setGenerator('generator', 'http://test.com/generator', 'version');
40-
$feed->addContributor('contributor', 'contributor@test.com', 'http://test.com/contributor');
36+
$feed->setIconUri('http://test.com/icon?a=b&c=d');
37+
$feed->setLogoUri('http://test.com/logo?a=b&c=d');
38+
$feed->setSubtitle('subtitle & co');
39+
$feed->setGenerator('generator', 'http://test.com/generator?a=b&c=d', 'version');
40+
$feed->addContributor('contributor', 'contributor@test.com', 'http://test.com/contributor?a=b&c=d');
4141
$feed->setUpdatedDateTime(new DateTime('2019-05-04T20:00:40Z'));
4242
$feed->addCustomElement('sy', 'http://purl.org/rss/1.0/modules/syndication', 'updatePeriod', 'hourly');
4343
$feed->addCustomElement('sy', 'http://purl.org/rss/1.0/modules/syndication', 'updateFrequency', 10);
@@ -136,19 +136,19 @@ public function testFeedCreation4(): void
136136
{
137137
$sourceFeed = new Feed();
138138
$sourceFeed->setTitle('source title');
139-
$sourceFeed->setId('tag:source');
139+
$sourceFeed->setId('https://test.com/source?a=b&c=d');
140140
$sourceFeed->setUpdatedDateTime(new DateTime('2019-03-04T20:00:40Z'));
141141

142142
$feed = new Feed();
143143
$feed->setTitle('title');
144-
$feed->setId('tag:test');
144+
$feed->setId('https://test.com/feed?a=b&c=d');
145145
$feed->setUpdatedDateTime(new DateTime('2019-05-04T20:00:40Z'));
146146

147147
$entry = new Entry();
148148
$entry->setTitle('entry title', 'html');
149-
$entry->setId('tag:entry-test');
149+
$entry->setId('https://test.com/entry?a=b&c=d');
150150
$entry->setContent(null);
151-
$entry->addLink('http://alternate.com', 'alternate');
151+
$entry->addLink('http://alternate.com?a=b&c=d', 'alternate');
152152
$entry->setUpdatedDateTime(new DateTime('2019-05-04T21:00:40Z'));
153153
$entry->setSource($sourceFeed);
154154

tests/feed_1.xml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@
55
<updated>2019-05-04T20:00:40+00:00</updated>
66
<rights>©2019</rights>
77
<category term="term" scheme="http://scheme.com" label="label"/>
8-
<link href="http://test.com/link" rel="via" type="text/html"/>
8+
<link href="http://test.com/link?a=b&amp;c=d" rel="via" type="text/html"/>
99
<author>
1010
<name>author</name>
1111
<email>author@test.com</email>
12-
<uri>http://test.com/author</uri>
12+
<uri>http://test.com/author?a=b&amp;c=d</uri>
1313
</author>
1414
<author>
1515
<name>author</name>
1616
<email>author@test.com</email>
17-
<uri>http://test.com/author</uri>
17+
<uri>http://test.com/author?a=b&amp;c=d</uri>
1818
</author>
1919
<contributor>
2020
<name>contributor</name>
2121
<email>contributor@test.com</email>
22-
<uri>http://test.com/contributor</uri>
22+
<uri>http://test.com/contributor?a=b&amp;c=d</uri>
2323
</contributor>
24-
<subtitle>subtitle</subtitle>
25-
<logo>http://test.com/logo</logo>
26-
<icon>http://test.com/icon</icon>
27-
<generator version="version" uri="http://test.com/generator">generator</generator>
24+
<subtitle>subtitle &amp; co</subtitle>
25+
<logo>http://test.com/logo?a=b&amp;c=d</logo>
26+
<icon>http://test.com/icon?a=b&amp;c=d</icon>
27+
<generator version="version" uri="http://test.com/generator?a=b&amp;c=d">generator</generator>
2828
<sy:updatePeriod>hourly</sy:updatePeriod>
2929
<sy:updateFrequency>10</sy:updateFrequency>
3030
<entry>

tests/feed_4.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<feed xmlns="http://www.w3.org/2005/Atom">
3-
<id>tag:test</id>
3+
<id>https://test.com/feed?a=b&amp;c=d</id>
44
<title>title</title>
55
<updated>2019-05-04T20:00:40+00:00</updated>
66
<entry>
7-
<id>tag:entry-test</id>
7+
<id>https://test.com/entry?a=b&amp;c=d</id>
88
<title type="html"><![CDATA[entry title]]></title>
99
<updated>2019-05-04T21:00:40+00:00</updated>
10-
<link href="http://alternate.com" rel="alternate"/>
10+
<link href="http://alternate.com?a=b&amp;c=d" rel="alternate"/>
1111
<source>
12-
<id>tag:source</id>
12+
<id>https://test.com/source?a=b&amp;c=d</id>
1313
<title>source title</title>
1414
<updated>2019-03-04T20:00:40+00:00</updated>
1515
</source>

0 commit comments

Comments
 (0)