JSFiddle - React, Tailwind, and code Playground

by Roman Zharikov

HTML

Start
<p>Paragraph</p>
<table>
  <tr>
    <th>1</th>
    <th>2</th>
  </tr>
  <tr>
    <td>
      <ul>
        <li>li</li>
        <li>li</li>
        <li>li</li>
      </ul>
    </td>
    <td>
      <p>Paragraph 2</p>
    </td>
  </tr>
</table>
<ul>
  <li>li</li>
</ul>

JavaScript

(function() {
  window.recSearchTags = function(d, t) {
    var buffer = [],
      rec = function(buffer, target, t) {
        for (var i = 0; i < target.children.length; i++) {
          var el = target.children[i];
          if (buffer.indexOf(el) !== -1)
            break;
          if (el.tagName === t.toUpperCase())
            buffer.push(el);
          if (el.hasChildNodes())
            rec(buffer, el, t);
        }
      };
    for (var i = 0; i < d.body.children.length; i++)
      rec(buffer, d.body, t);
    return buffer;
  }
  
  // вызовы
  console.log(recSearchTags(document, 'table'))
  console.log(recSearchTags(document, 'td'));
})();