123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- namespace PHPHtmlParser\Dom;
- /**
- * Class TextNode
- *
- * @package PHPHtmlParser\Dom
- */
- class TextNode extends LeafNode
- {
- /**
- * This is a text node.
- *
- * @var Tag
- */
- protected $tag;
- /**
- * This is the text in this node.
- *
- * @var string
- */
- protected $text;
- /**
- * This is the converted version of the text.
- *
- * @var string
- */
- protected $convertedText = null;
- /**
- * Sets the text for this node.
- *
- * @param string $text
- * @param bool $removeDoubleSpace
- */
- public function __construct(string $text, $removeDoubleSpace = true)
- {
- if ($removeDoubleSpace) {
- // remove double spaces
- $text = mb_ereg_replace('\s+', ' ', $text);
- }
- // restore line breaks
- $text = str_replace(' ', "\n", $text);
- $this->text = $text;
- $this->tag = new Tag('text');
- parent::__construct();
- }
- /**
- * Returns the text of this node.
- *
- * @return string
- */
- public function text(): string
- {
- // convert charset
- if ( ! is_null($this->encode)) {
- if ( ! is_null($this->convertedText)) {
- // we already know the converted value
- return $this->convertedText;
- }
- $text = $this->encode->convert($this->text);
- // remember the conversion
- $this->convertedText = $text;
- return $text;
- } else {
- return $this->text;
- }
- }
- /**
- * Sets the text for this node.
- *
- * @var string $text
- * @return void
- */
- public function setText(string $text): void
- {
- $this->text = $text;
- if ( ! is_null($this->encode)) {
- $text = $this->encode->convert($text);
- // remember the conversion
- $this->convertedText = $text;
- }
- }
- /**
- * This node has no html, just return the text.
- *
- * @return string
- * @uses $this->text()
- */
- public function innerHtml(): string
- {
- return $this->text();
- }
- /**
- * This node has no html, just return the text.
- *
- * @return string
- * @uses $this->text()
- */
- public function outerHtml(): string
- {
- return $this->text();
- }
- /**
- * Call this when something in the node tree has changed. Like a child has been added
- * or a parent has been changed.
- */
- protected function clear(): void
- {
- $this->convertedText = null;
- }
- /**
- * Checks if the current node is a text node.
- *
- * @return bool
- */
- public function isTextNode(): bool
- {
- return true;
- }
- }
|