Skip to content

Commit 1e09977

Browse files
committed
Adding body styles partial allowing CSS styles to be set on the body tag of the requested page - #92
1 parent 5ba4950 commit 1e09977

File tree

5 files changed

+94
-2
lines changed

5 files changed

+94
-2
lines changed

src/JonnyW/PhantomJs/Http/AbstractRequest.php

+39
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ abstract class AbstractRequest
8585
*/
8686
protected $viewportHeight;
8787

88+
/**
89+
* Body styles.
90+
*
91+
* @var int
92+
* @access protected
93+
*/
94+
protected $bodyStyles;
95+
8896
/**
8997
* Internal constructor
9098
*
@@ -97,6 +105,7 @@ public function __construct($url = null, $method = RequestInterface::METHOD_GET,
97105
{
98106
$this->headers = array();
99107
$this->data = array();
108+
$this->bodyStyles = array();
100109
$this->delay = 0;
101110
$this->viewportWidth = 0;
102111
$this->viewportHeight = 0;
@@ -377,6 +386,36 @@ public function getHeaders($format = 'default')
377386
return $this->headers;
378387
}
379388

389+
/**
390+
* Set body styles
391+
*
392+
* @access public
393+
* @param array $styles
394+
* @return \JonnyW\PhantomJs\Http\AbstractRequest
395+
*/
396+
public function setBodyStyles(array $styles)
397+
{
398+
$this->bodyStyles = $styles;
399+
400+
return $this;
401+
}
402+
403+
/**
404+
* Get body styles
405+
*
406+
* @access public
407+
* @param string $format (default: 'default')
408+
* @return array
409+
*/
410+
public function getBodyStyles($format = 'default')
411+
{
412+
if ($format === 'json') {
413+
return json_encode($this->bodyStyles);
414+
}
415+
416+
return $this->bodyStyles;
417+
}
418+
380419
/**
381420
* Flatten data into single
382421
* dimensional array

src/JonnyW/PhantomJs/Http/RequestInterface.php

+16
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,20 @@ public function addHeaders(array $headers);
184184
* @return array
185185
*/
186186
public function getHeaders($format = 'default');
187+
188+
/**
189+
* Set body styles
190+
*
191+
* @access public
192+
* @param array $styles
193+
*/
194+
public function setBodyStyles(array $styles);
195+
196+
/**
197+
* Get body styles
198+
*
199+
* @access public
200+
* @return array
201+
*/
202+
public function getBodyStyles();
187203
}

src/JonnyW/PhantomJs/Resources/procedures/http_default.proc

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ phantom.onError = function(msg, trace) {
7575
* Open page
7676
*/
7777
page.open ('{{ input.getUrl() }}', '{{ input.getMethod() }}', '{{ input.getBody() }}', function (status) {
78+
[[ engine.load('page_body_styles') ]]
7879
[[ engine.load('page_open') ]]
7980
});
8081

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
page.evaluate(function() {
3+
4+
var styles = {{ input.getBodyStyles('json') }};
5+
6+
for(var property in styles) {
7+
document.body.style[property] = styles[property];
8+
}
9+
});

src/JonnyW/PhantomJs/Tests/Integration/ClientTest.php

+29-2
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public function testResponseContainsHeaders()
243243

244244
$this->assertNotEmpty($response->getHeaders());
245245
}
246-
246+
247247
/**
248248
* Test redirect URL is set in response
249249
* if request is redirected.
@@ -310,7 +310,7 @@ public function testCaptureRequestSavesFileToLocalDisk()
310310
$response = $client->getMessageFactory()->createResponse();
311311

312312
$request->setMethod('GET');
313-
$request->setUrl('http://jonnyw.kiwi/tests/test-console-error.php');
313+
$request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
314314
$request->setOutputFile($file);
315315

316316
$client->send($request, $response);
@@ -750,6 +750,33 @@ public function testDebugLogsDebugInfoToClientLog()
750750
$this->assertContains('[DEBUG]', $client->getLog());
751751
}
752752

753+
/**
754+
* Test test can set page
755+
* background color
756+
*
757+
* @access public
758+
* @return void
759+
*/
760+
public function testCanSetPageBackgroundColor()
761+
{
762+
$this->filename = 'test.jpg';
763+
$file = ($this->directory . '/' . $this->filename);
764+
765+
$client = $this->getClient();
766+
767+
$request = $client->getMessageFactory()->createCaptureRequest();
768+
$response = $client->getMessageFactory()->createResponse();
769+
770+
$request->setMethod('GET');
771+
$request->setUrl('http://jonnyw.kiwi/tests/test-capture.php');
772+
$request->setBodyStyles(array('backgroundColor' => 'red'));
773+
$request->setOutputFile($file);
774+
775+
$client->send($request, $response);
776+
777+
$this->assertContains('body style="background-color: red;"', $response->getContent());
778+
}
779+
753780
/** +++++++++++++++++++++++++++++++++++ **/
754781
/** ++++++++++ TEST ENTITIES ++++++++++ **/
755782
/** +++++++++++++++++++++++++++++++++++ **/

0 commit comments

Comments
 (0)