This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
forked from minkphp/Mink
/
Element.php
97 lines (83 loc) · 2.12 KB
/
Element.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
namespace Behat\Mink\Element;
use Behat\Mink\Session;
/*
* This file is part of the Behat\Mink.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Base element.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
abstract class Element implements ElementInterface
{
private $session;
/**
* Initialize element.
*
* @param Behat\Mink\Session $session
*/
public function __construct(Session $session)
{
$this->session = $session;
}
/**
* Returns element session.
*
* @return Behat\Mink\Session
*/
public function getSession()
{
return $this->session;
}
/**
* @see Behat\Mink\Element\ElementInterface::find()
*/
public function find($selector, $locator)
{
$items = $this->findAll($selector, $locator);
return count($items) ? current($items) : null;
}
/**
* @see Behat\Mink\Element\ElementInterface::findAll()
*/
public function findAll($selector, $locator)
{
return $this->getSession()->getDriver()->find(
$this->getSession()->getSelectorsHandler()->selectorToXpath($selector, $locator)
);
}
/**
* @see Behat\Mink\Element\ElementInterface::findAll()
*/
public function hasSelector($selector, $locator)
{
return null !== $this->find($selector, $locator);
}
/**
* Returns element text.
*
* @return string|null
*/
abstract public function getText();
/**
* Returns elemenelement text with trimmed tags and non-printable chars.
*
* @return string|null
*/
public function getPlainText()
{
$text = $this->getText();
if (null !== $text) {
$text = str_replace("\n", ' ', $text);
$text = preg_replace('/\<br *\/?\>/i', "\n", $text);
$text = strip_tags($text);
$text = preg_replace('/ {2,}/', ' ', $text);
return $text;
}
}
}