Skip to content

Commit 9fb3c21

Browse files
authored
Merge branch 'master' into issue-114
2 parents a462c96 + 26e4ed4 commit 9fb3c21

File tree

3 files changed

+70
-11
lines changed

3 files changed

+70
-11
lines changed

Mf2/Parser.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -588,13 +588,13 @@ public function parseP(\DOMElement $p) {
588588

589589
$this->resolveChildUrls($p);
590590

591-
if ($p->tagName == 'img' and $p->getAttribute('alt') !== '') {
591+
if ($p->tagName == 'img' and $p->hasAttribute('alt')) {
592592
$pValue = $p->getAttribute('alt');
593-
} elseif ($p->tagName == 'area' and $p->getAttribute('alt') !== '') {
593+
} elseif ($p->tagName == 'area' and $p->hasAttribute('alt')) {
594594
$pValue = $p->getAttribute('alt');
595-
} elseif ($p->tagName == 'abbr' and $p->getAttribute('title') !== '') {
595+
} elseif ($p->tagName == 'abbr' and $p->hasAttribute('title')) {
596596
$pValue = $p->getAttribute('title');
597-
} elseif (in_array($p->tagName, array('data', 'input')) and $p->getAttribute('value') !== '') {
597+
} elseif (in_array($p->tagName, array('data', 'input')) and $p->hasAttribute('value')) {
598598
$pValue = $p->getAttribute('value');
599599
} else {
600600
$pValue = unicodeTrim($this->innerText($p));
@@ -611,13 +611,13 @@ public function parseP(\DOMElement $p) {
611611
* @todo make this adhere to value-class
612612
*/
613613
public function parseU(\DOMElement $u) {
614-
if (($u->tagName == 'a' or $u->tagName == 'area') and $u->getAttribute('href') !== null) {
614+
if (($u->tagName == 'a' or $u->tagName == 'area') and $u->hasAttribute('href')) {
615615
$uValue = $u->getAttribute('href');
616-
} elseif (in_array($u->tagName, array('img', 'audio', 'video', 'source')) and $u->getAttribute('src') !== null) {
616+
} elseif (in_array($u->tagName, array('img', 'audio', 'video', 'source')) and $u->hasAttribute('src')) {
617617
$uValue = $u->getAttribute('src');
618618
} elseif ($u->tagName == 'video' and !$u->hasAttribute('src') and $u->hasAttribute('poster')) {
619619
$uValue = $u->getAttribute('poster');
620-
} elseif ($u->tagName == 'object' and $u->getAttribute('data') !== null) {
620+
} elseif ($u->tagName == 'object' and $u->hasAttribute('data')) {
621621
$uValue = $u->getAttribute('data');
622622
}
623623

@@ -629,9 +629,9 @@ public function parseU(\DOMElement $u) {
629629

630630
if ($classTitle !== null) {
631631
return $classTitle;
632-
} elseif ($u->tagName == 'abbr' and $u->getAttribute('title') !== null) {
632+
} elseif ($u->tagName == 'abbr' and $u->hasAttribute('title')) {
633633
return $u->getAttribute('title');
634-
} elseif (in_array($u->tagName, array('data', 'input')) and $u->getAttribute('value') !== null) {
634+
} elseif (in_array($u->tagName, array('data', 'input')) and $u->hasAttribute('value')) {
635635
return $u->getAttribute('value');
636636
} else {
637637
return unicodeTrim($this->textContent($u));
@@ -1130,7 +1130,7 @@ public function parseImpliedPhoto(\DOMElement $e) {
11301130

11311131
if ($el->tagName == 'img') {
11321132
return $el->getAttribute('src');
1133-
} else if ($el->tagName == 'object' && $el->getAttribute('data') != '') {
1133+
} else if ($el->tagName == 'object' && $el->hasAttribute('data')) {
11341134
return $el->getAttribute('data');
11351135
}
11361136

tests/Mf2/ParsePTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ public function testParsePHandlesData() {
6767
$this->assertEquals('Example User', $output['items'][0]['properties']['name'][0]);
6868
}
6969

70+
/**
71+
* @group parseP
72+
*/
73+
public function testParsePHandlesDataWithBlankValueAttribute() {
74+
$input = '<div class="h-card"><data class="p-name" value="">Example User</data></div>';
75+
$parser = new Parser($input);
76+
$output = $parser->parse();
77+
78+
$this->assertArrayHasKey('name', $output['items'][0]['properties']);
79+
$this->assertEquals('', $output['items'][0]['properties']['name'][0]);
80+
}
81+
7082
/**
7183
* @group parseP
7284
*/

tests/Mf2/ParseUTest.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,31 @@ public function testParseUHandlesA() {
2525
$this->assertArrayHasKey('url', $output['items'][0]['properties']);
2626
$this->assertEquals('http://example.com', $output['items'][0]['properties']['url'][0]);
2727
}
28-
28+
29+
/**
30+
* @group parseU
31+
*/
32+
public function testParseUHandlesEmptyHrefAttribute() {
33+
$input = '<div class="h-card"><a class="u-url" href="">Awesome example website</a></div>';
34+
$parser = new Parser($input, "http://example.com/");
35+
$output = $parser->parse();
36+
37+
$this->assertArrayHasKey('url', $output['items'][0]['properties']);
38+
$this->assertEquals('http://example.com/', $output['items'][0]['properties']['url'][0]);
39+
}
40+
41+
/**
42+
* @group parseU
43+
*/
44+
public function testParseUHandlesMissingHrefAttribute() {
45+
$input = '<div class="h-card"><a class="u-url">Awesome example website</a></div>';
46+
$parser = new Parser($input, "http://example.com/");
47+
$output = $parser->parse();
48+
49+
$this->assertArrayHasKey('url', $output['items'][0]['properties']);
50+
$this->assertEquals('Awesome example website', $output['items'][0]['properties']['url'][0]);
51+
}
52+
2953
/**
3054
* @group parseU
3155
*/
@@ -73,6 +97,18 @@ public function testParseUHandlesAbbr() {
7397
$this->assertArrayHasKey('photo', $output['items'][0]['properties']);
7498
$this->assertEquals('http://example.com/someimage.png', $output['items'][0]['properties']['photo'][0]);
7599
}
100+
101+
/**
102+
* @group parseU
103+
*/
104+
public function testParseUHandlesAbbrNoTitle() {
105+
$input = '<div class="h-card"><abbr class="u-photo">no title attribute</abbr></div>';
106+
$parser = new Parser($input);
107+
$output = $parser->parse();
108+
109+
$this->assertArrayHasKey('photo', $output['items'][0]['properties']);
110+
$this->assertEquals('no title attribute', $output['items'][0]['properties']['photo'][0]);
111+
}
76112

77113
/**
78114
* @group parseU
@@ -161,6 +197,17 @@ public function testParseUHandlesVideo() {
161197
$this->assertEquals('http://example.com/video.mp4', $output['items'][0]['properties']['video'][0]);
162198
}
163199

200+
/**
201+
* @group parseU
202+
*/
203+
public function testParseUHandlesVideoNoSrc() {
204+
$input = '<div class="h-entry"><video class="u-video">no video support</video></div>';
205+
$parser = new Parser($input);
206+
$output = $parser->parse();
207+
208+
$this->assertArrayHasKey('video', $output['items'][0]['properties']);
209+
$this->assertEquals('no video support', $output['items'][0]['properties']['video'][0]);
210+
}
164211

165212
/**
166213
* @group parseU

0 commit comments

Comments
 (0)