NodeUtility.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace andreskrey\Readability\Nodes;
  3. use andreskrey\Readability\Nodes\DOM\DOMDocument;
  4. use andreskrey\Readability\Nodes\DOM\DOMElement;
  5. use andreskrey\Readability\Nodes\DOM\DOMNode;
  6. /**
  7. * Class NodeUtility.
  8. */
  9. class NodeUtility
  10. {
  11. /**
  12. * Collection of regexps to check the node usability.
  13. *
  14. * @var array
  15. */
  16. public static $regexps = [
  17. 'unlikelyCandidates' => '/banner|breadcrumbs|combx|comment|community|cover-wrap|disqus|extra|foot|header|legends|menu|related|remark|replies|rss|shoutbox|sidebar|skyscraper|social|sponsor|supplemental|ad-break|agegate|pagination|pager|popup|yom-remote/i',
  18. 'okMaybeItsACandidate' => '/and|article|body|column|main|shadow/i',
  19. 'extraneous' => '/print|archive|comment|discuss|e[\-]?mail|share|reply|all|login|sign|single|utility/i',
  20. 'byline' => '/byline|author|dateline|writtenby|p-author/i',
  21. 'replaceFonts' => '/<(\/?)font[^>]*>/gi',
  22. 'normalize' => '/\s{2,}/',
  23. 'videos' => '/\/\/(www\.)?(dailymotion|youtube|youtube-nocookie|player\.vimeo)\.com/i',
  24. 'nextLink' => '/(next|weiter|continue|>([^\|]|$)|»([^\|]|$))/i',
  25. 'prevLink' => '/(prev|earl|old|new|<|«)/i',
  26. 'whitespace' => '/^\s*$/',
  27. 'hasContent' => '/\S$/',
  28. 'positive' => '/article|body|content|entry|hentry|h-entry|main|page|pagination|post|text|blog|story/i',
  29. 'negative' => '/hidden|^hid$| hid$| hid |^hid |banner|combx|comment|com-|contact|foot|footer|footnote|masthead|media|meta|outbrain|promo|related|scroll|share|shoutbox|sidebar|skyscraper|sponsor|shopping|tags|tool|widget/i',
  30. // \x{00A0} is the unicode version of &nbsp;
  31. 'onlyWhitespace' => '/\x{00A0}|\s+/u'
  32. ];
  33. /**
  34. * Imported from the Element class on league\html-to-markdown.
  35. *
  36. * @param $node
  37. *
  38. * @return DOMElement
  39. */
  40. public static function nextElement($node)
  41. {
  42. $next = $node;
  43. while ($next
  44. && $next->nodeName !== '#text'
  45. && trim($next->textContent)) {
  46. $next = $next->nextSibling;
  47. }
  48. return $next;
  49. }
  50. /**
  51. * Changes the node tag name. Since tagName on DOMElement is a read only value, this must be done creating a new
  52. * element with the new tag name and importing it to the main DOMDocument.
  53. *
  54. * @param string $value
  55. * @param bool $importAttributes
  56. *
  57. * @return DOMNode
  58. */
  59. public static function setNodeTag($node, $value, $importAttributes = false)
  60. {
  61. $new = new DOMDocument('1.0', 'utf-8');
  62. $new->appendChild($new->createElement($value));
  63. $children = $node->childNodes;
  64. /** @var $children \DOMNodeList $i */
  65. for ($i = 0; $i < $children->length; $i++) {
  66. $import = $new->importNode($children->item($i), true);
  67. $new->firstChild->appendChild($import);
  68. }
  69. if ($importAttributes) {
  70. // Import attributes from the original node.
  71. foreach ($node->attributes as $attribute) {
  72. $new->firstChild->setAttribute($attribute->nodeName, $attribute->nodeValue);
  73. }
  74. }
  75. // The import must be done on the firstChild of $new, since $new is a DOMDocument and not a DOMElement.
  76. $import = $node->ownerDocument->importNode($new->firstChild, true);
  77. $node->parentNode->replaceChild($import, $node);
  78. return $import;
  79. }
  80. /**
  81. * Removes the current node and returns the next node to be parsed (child, sibling or parent).
  82. *
  83. * @param DOMNode $node
  84. *
  85. * @return DOMNode
  86. */
  87. public static function removeAndGetNext($node)
  88. {
  89. $nextNode = self::getNextNode($node, true);
  90. $node->parentNode->removeChild($node);
  91. return $nextNode;
  92. }
  93. /**
  94. * Remove the selected node.
  95. *
  96. * @param $node DOMElement
  97. *
  98. * @return void
  99. **/
  100. public static function removeNode($node)
  101. {
  102. $parent = $node->parentNode;
  103. if ($parent) {
  104. $parent->removeChild($node);
  105. }
  106. }
  107. /**
  108. * Returns the next node. First checks for children (if the flag allows it), then for siblings, and finally
  109. * for parents.
  110. *
  111. * @param DOMNode $originalNode
  112. * @param bool $ignoreSelfAndKids
  113. *
  114. * @return DOMNode
  115. */
  116. public static function getNextNode($originalNode, $ignoreSelfAndKids = false)
  117. {
  118. /*
  119. * Traverse the DOM from node to node, starting at the node passed in.
  120. * Pass true for the second parameter to indicate this node itself
  121. * (and its kids) are going away, and we want the next node over.
  122. *
  123. * Calling this in a loop will traverse the DOM depth-first.
  124. */
  125. // First check for kids if those aren't being ignored
  126. if (!$ignoreSelfAndKids && $originalNode->firstChild) {
  127. return $originalNode->firstChild;
  128. }
  129. // Then for siblings...
  130. if ($originalNode->nextSibling) {
  131. return $originalNode->nextSibling;
  132. }
  133. // And finally, move up the parent chain *and* find a sibling
  134. // (because this is depth-first traversal, we will have already
  135. // seen the parent nodes themselves).
  136. do {
  137. $originalNode = $originalNode->parentNode;
  138. } while ($originalNode && !$originalNode->nextSibling);
  139. return ($originalNode) ? $originalNode->nextSibling : $originalNode;
  140. }
  141. }