Skip to content

Commit ac486f6

Browse files
committed
some bugfixes
1 parent 69806b3 commit ac486f6

File tree

6 files changed

+36
-33
lines changed

6 files changed

+36
-33
lines changed

src/Adapters/Sassmeister.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public static function check(Request $request)
2424
*/
2525
public function getCode()
2626
{
27+
$this->width = null;
28+
$this->height = 480;
2729
$id = $this->request->getDirectoryPosition(1);
2830

2931
return "<p class=\"sassmeister\" data-gist-id=\"{$id}\" data-height=\"480\" data-theme=\"tomorrow\">".

src/Providers/Html.php

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function run()
3030
self::extractFromMeta($html, $this->bag);
3131

3232
if ($main = self::getMainElement($html)) {
33-
self::extractImages($main, $this->bag, $this->request->getDomain());
33+
$this->extractImages($main);
3434
}
3535

3636
//Title
@@ -273,50 +273,42 @@ protected static function extractFromMeta(\DOMDocument $html, Bag $bag)
273273
* Extract <img> elements.
274274
*
275275
* @param \DOMElement $html
276-
* @param Bag $bag
277-
* @param null|string $domain
278276
*/
279-
protected static function extractImages(\DOMElement $html, Bag $bag, $domain = null)
277+
protected function extractImages(\DOMElement $html)
280278
{
279+
$domain = $this->request->getDomain();
280+
281281
foreach ($html->getElementsByTagName('img') as $img) {
282282
if ($img->hasAttribute('src')) {
283-
$src = new Url($img->getAttribute('src'));
283+
$src = $this->request->createUrl($img->getAttribute('src'));
284284

285-
//Is src relative?
286-
if (!$src->getDomain()) {
287-
$bag->add('images', $src->getUrl());
285+
//Avoid external images
286+
if ($src->getContent() === null && $src->getDomain() !== $domain) {
288287
continue;
289288
}
290289

291-
//Avoid external images or in external links
292-
if ($domain !== null) {
293-
if ($src->getDomain() !== $domain) {
294-
continue;
295-
}
296-
297-
$parent = $img->parentNode;
290+
$parent = $img->parentNode;
298291

299-
while ($parent && isset($parent->tagName)) {
300-
if ($parent->tagName === 'a') {
301-
if ($parent->hasAttribute('href')) {
302-
$href = new Url($parent->getAttribute('href'));
292+
while ($parent && isset($parent->tagName)) {
293+
if ($parent->tagName === 'a') {
294+
if ($parent->hasAttribute('href')) {
295+
$href = $this->request->createUrl($parent->getAttribute('href'));
303296

304-
if ($href->getDomain() && $src->getDomain() !== $domain) {
305-
continue 2;
306-
}
307-
}
308-
if ($parent->hasAttribute('rel') && (string) $parent->getAttribute('rel') === 'nofollow') {
297+
if ($href->getDomain() !== $domain) {
309298
continue 2;
310299
}
311-
312-
break;
300+
}
301+
if ($parent->hasAttribute('rel') && (string) $parent->getAttribute('rel') === 'nofollow') {
302+
continue 2;
313303
}
314304

315-
$parent = $parent->parentNode;
305+
break;
316306
}
317307

318-
$bag->add('images', $src->getUrl());
308+
$parent = $parent->parentNode;
319309
}
310+
311+
$this->bag->add('images', $src->getUrl());
320312
}
321313
}
322314
}

src/Request.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,17 @@ public function getUrl()
8383

8484
/**
8585
* Creates and returns an Url clone.
86+
*
87+
* @param string|null $url
8688
*
8789
* @return Url
8890
*/
89-
public function createUrl()
91+
public function createUrl($url = null)
9092
{
93+
if ($url !== null) {
94+
return new Url($this->getAbsolute($url));
95+
}
96+
9197
return new Url($this->getUrl());
9298
}
9399

src/Url.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class Url
99
{
1010
protected $info;
1111

12+
public static $validate = false;
13+
1214
/**
1315
* Constructor. Sets the url.
1416
*
@@ -436,7 +438,9 @@ protected function buildUrl()
436438
*/
437439
protected function parseUrl($url)
438440
{
439-
if (!filter_var($url, FILTER_VALIDATE_URL)) {
441+
// do not validate urls because some real urls fails.
442+
// Example: http://jouey-.deviantart.com/art/market-153836478 fails.
443+
if (self::$validate && substr($url, 0, 5) !== 'data:' && !filter_var($url, FILTER_VALIDATE_URL)) {
440444
throw new Exceptions\InvalidUrlException("The url '{$url}' is not valid");
441445
}
442446

@@ -478,6 +482,8 @@ protected function parseUrl($url)
478482
*/
479483
public function getAbsolute($url)
480484
{
485+
$url = trim($url);
486+
481487
if (empty($url)) {
482488
return '';
483489
}

tests/SassmeisterTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ public function testGist()
88
'http://sassmeister.com/gist/1122e441f0434ec6eb2a',
99
[
1010
'title' => 'SassMeister | The Sass Playground!',
11-
'imageWidth' => 240,
12-
'imageHeight' => 240,
1311
'type' => 'rich',
1412
'providerName' => 'sassmeister',
1513
]

tests/ScribdTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ public function testOne()
88
'http://www.scribd.com/doc/110799637/Synthesis-of-Knowledge-Effects-of-Fire-and-Thinning-Treatments-on-Understory-Vegetation-in-Dry-U-S-Forests',
99
[
1010
'title' => 'Synthesis of Knowledge: Effects of Fire and Thinning Treatments on Understory Vegetation in Dry U.S. Forests',
11-
'description' => "A review of current literature on studies that address effects of prescribed fire and mechanical thinning treatments on understory vegetation, and the effects of prescribed burning on rare, threatened and endangered species. The studies' outcomes are presented in two sets of tables: (1) functional group results, and (2) species-specific results.",
1211
'imageWidth' => 111,
1312
'imageHeight' => 142,
1413
'type' => 'rich',

0 commit comments

Comments
 (0)