TextNode.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace PHPHtmlParser\Dom;
  3. /**
  4. * Class TextNode
  5. *
  6. * @package PHPHtmlParser\Dom
  7. */
  8. class TextNode extends LeafNode
  9. {
  10. /**
  11. * This is a text node.
  12. *
  13. * @var Tag
  14. */
  15. protected $tag;
  16. /**
  17. * This is the text in this node.
  18. *
  19. * @var string
  20. */
  21. protected $text;
  22. /**
  23. * This is the converted version of the text.
  24. *
  25. * @var string
  26. */
  27. protected $convertedText = null;
  28. /**
  29. * Sets the text for this node.
  30. *
  31. * @param string $text
  32. * @param bool $removeDoubleSpace
  33. */
  34. public function __construct(string $text, $removeDoubleSpace = true)
  35. {
  36. if ($removeDoubleSpace) {
  37. // remove double spaces
  38. $text = mb_ereg_replace('\s+', ' ', $text);
  39. }
  40. // restore line breaks
  41. $text = str_replace('&#10;', "\n", $text);
  42. $this->text = $text;
  43. $this->tag = new Tag('text');
  44. parent::__construct();
  45. }
  46. /**
  47. * Returns the text of this node.
  48. *
  49. * @return string
  50. */
  51. public function text(): string
  52. {
  53. // convert charset
  54. if ( ! is_null($this->encode)) {
  55. if ( ! is_null($this->convertedText)) {
  56. // we already know the converted value
  57. return $this->convertedText;
  58. }
  59. $text = $this->encode->convert($this->text);
  60. // remember the conversion
  61. $this->convertedText = $text;
  62. return $text;
  63. } else {
  64. return $this->text;
  65. }
  66. }
  67. /**
  68. * Sets the text for this node.
  69. *
  70. * @var string $text
  71. * @return void
  72. */
  73. public function setText(string $text): void
  74. {
  75. $this->text = $text;
  76. if ( ! is_null($this->encode)) {
  77. $text = $this->encode->convert($text);
  78. // remember the conversion
  79. $this->convertedText = $text;
  80. }
  81. }
  82. /**
  83. * This node has no html, just return the text.
  84. *
  85. * @return string
  86. * @uses $this->text()
  87. */
  88. public function innerHtml(): string
  89. {
  90. return $this->text();
  91. }
  92. /**
  93. * This node has no html, just return the text.
  94. *
  95. * @return string
  96. * @uses $this->text()
  97. */
  98. public function outerHtml(): string
  99. {
  100. return $this->text();
  101. }
  102. /**
  103. * Call this when something in the node tree has changed. Like a child has been added
  104. * or a parent has been changed.
  105. */
  106. protected function clear(): void
  107. {
  108. $this->convertedText = null;
  109. }
  110. /**
  111. * Checks if the current node is a text node.
  112. *
  113. * @return bool
  114. */
  115. public function isTextNode(): bool
  116. {
  117. return true;
  118. }
  119. }