-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Core] adds a Filterclass, that modifies HTML for being exportet
(#5862)
- Loading branch information
1 parent
a0e326c
commit 474f37c
Showing
6 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
module/Core/src/Core/Factory/Filter/HtmlAbsPathFilterFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
/** | ||
* YAWIK | ||
* | ||
* @filesource | ||
* @copyright (c) 2013-2015 Cross Solution (http://cross-solution.de) | ||
* @license MIT | ||
* @author weitz@cross-solution.de | ||
*/ | ||
|
||
namespace Core\Factory\Filter; | ||
|
||
use Zend\ServiceManager\FactoryInterface; | ||
use Zend\ServiceManager\ServiceLocatorInterface; | ||
use Core\Filter\HtmlAbsPathFilter; | ||
|
||
class HtmlAbsPathFilterFactory implements FactoryInterface | ||
{ | ||
public function createService(ServiceLocatorInterface $serviceLocator) | ||
{ | ||
$request = $serviceLocator->getServiceLocator()->get('request'); | ||
$uri = $request->getUri(); | ||
$filter = new HtmlAbsPathFilter(); | ||
$filter->setUri($uri); | ||
return $filter; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
/** | ||
* YAWIK | ||
* | ||
* @filesource | ||
* @copyright (c) 2013-2015 Cross Solution (http://cross-solution.de) | ||
* @license MIT | ||
* @author weitz@cross-solution.de | ||
*/ | ||
|
||
namespace Core\Filter; | ||
|
||
use Zend\Filter\FilterInterface; | ||
use Zend\Filter\Exception; | ||
use Zend\Dom\Query; | ||
|
||
use Zend\Uri\Http; | ||
|
||
|
||
class HtmlAbsPathFilter implements FilterInterface { | ||
|
||
protected $uri; | ||
|
||
public function setUri($uri) { | ||
$this->uri = $uri; | ||
return $this; | ||
} | ||
|
||
public function filter($html) | ||
{ | ||
$baseUri = new Http($this->uri); | ||
|
||
$dom = new Query($html); | ||
$results = $dom->execute('*[@href],*[@src]'); | ||
foreach ($results as $result) { | ||
$attributeMap = $result->attributes; | ||
foreach ($attributeMap as $attribute) { | ||
$name = $attribute->name; | ||
if ($name == 'href' || $name == 'src') { | ||
$value = $result->getAttribute($name); | ||
$uri = new Http($value); | ||
$h = $uri->resolve($baseUri); | ||
$result->setAttribute($name, $h); | ||
} | ||
} | ||
} | ||
$document = $results->getDocument(); | ||
$documentHTML = $document->saveHTML(); | ||
return $documentHTML; | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
module/Core/test/CoreTest/Filter/HtmlAbsPathFilterTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
/** | ||
* YAWIK | ||
* | ||
* @filesource | ||
* @copyright (c) 2013-2015 Cross Solution (http://cross-solution.de) | ||
* @license MIT | ||
* @author weitz@cross-solution.de | ||
*/ | ||
|
||
namespace CoreTest\Filter; | ||
|
||
use Core\Filter\HtmlAbsPathFilter; | ||
|
||
|
||
class HtmlAbsPathFilterTest extends \PHPUnit_Framework_TestCase { | ||
|
||
protected $htmlTests = array( | ||
array( | ||
'original' => | ||
' | ||
<html> | ||
<head> | ||
<link rel="stylesheet" type="text/css" href="http://php.net/cached.php?t=1421837618&f=/fonts/Fira/fira.css" media="screen"> | ||
<link rel="stylesheet" type="text/css" href="//php.net/cached.php?t=1421837618&f=/fonts/Fira/fira.css" media="screen"> | ||
<link rel="stylesheet" type="text/css" href="localCSS.css" media="screen"> | ||
</head> | ||
<body> | ||
<div> | ||
<table> | ||
<tr> | ||
<td class="foo"> | ||
<div> | ||
Lorem ipsum <span class="bar"> | ||
<a href="/foo/bar" id="one" onClick="JavaScript:void()">One</a> | ||
<a href="/foo/baz" id="two">Two</a> | ||
<a src="/foo/bat" id="three">Three</a> | ||
<img src="/foo/bla" /> | ||
<img src="../foo/bla"> | ||
<img src="foo/bla"> | ||
<img src="../foo/bli"> | ||
</span> | ||
</div> | ||
</td> | ||
</tr> | ||
</table> | ||
</div> | ||
</body> | ||
</html> | ||
', | ||
), | ||
array( | ||
'original' => | ||
' | ||
<html> | ||
<head> | ||
<link rel="stylesheet" type="text/css" href=\'http://php.net/cached.php?t=1421837618&f=/fonts/Fira/fira.css\' media="screen"> | ||
<link rel="stylesheet" type="text/css" href=\'//php.net/cached.php?t=1421837618&f=/fonts/Fira/fira.css\' media="screen"> | ||
<link rel="stylesheet" type="text/css" href=\'localCSS.css\' media="screen"> | ||
</head> | ||
<body> | ||
<table> | ||
<tr> | ||
<td class="foo"> | ||
<div> | ||
Lorem ipsum <span class="bar"> | ||
<a href="/foo/bar" id="one" onClick="JavaScript:void()">One</a> | ||
</span> | ||
</td> | ||
</tr> | ||
</table> | ||
</div> | ||
</body> | ||
</html> | ||
', | ||
|
||
) | ||
); | ||
|
||
|
||
public function testFilter() | ||
{ | ||
$filter = new HtmlAbsPathFilter(); | ||
foreach ($this->htmlTests as $test) { | ||
|
||
$filter->setUri('http://aaa.bbb.cc/ddd/'); | ||
$f = $filter->filter($test['original']); | ||
preg_match_all ( '/(?:href|src)\s*=\s*"([^"]*)"/', $f, $matches); | ||
foreach ($matches[1] as $uri) { | ||
$this->assertRegExp("/^https?:\/\//", $uri); | ||
|
||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters