Imangazaliev / DiDOM

Simple and fast HTML and XML parser
MIT License
2.2k stars 203 forks source link

Find all class, ID, Attribute, and tag in Document #200

Open mizanexpertofficial opened 1 year ago

mizanexpertofficial commented 1 year ago

Hey I try to find all class, id, attr, and tag in document.

Here is example in DOMDocument

Possible to do it via DiDOM?

 //get dom document
$dom = new \DOMDocument();
$dom->xpath = new \DOMXPath($dom);

//setup used selectors array
$this->used_selectors = array('tags' => array(), 'ids' => array(), 'classes' => array());

//search for used selectors in dom
 $classes = array();
     foreach ($dom->getElementsByTagName('*') as $tag) {

     //add tag
    $this->used_selectors['tags'][$tag->tagName] = 1;

     //add id
      if ($tag->hasAttribute('id')) {
        $this->used_selectors['ids'][$tag->getAttribute('id')] = 1;
      }

      //store tag classes
      if ($tag->hasAttribute('class')) {
          $class = $tag->getAttribute('class');
          $tag_classes = preg_split('/\s+/', $class);
            array_push($classes, ...$tag_classes);
        }
 }

//add classes
$classes = array_filter(array_unique($classes));
if ($classes) {
      $this->used_selectors['classes'] = array_fill_keys($classes, 1);
 }
scott8035 commented 1 year ago

What is not working for you? It's certainly possible to do this.

The "class" (and "style") attributes are handled specially by src/DiDom/Element.php. You can modify the "store tag classes" section like so:

//store tag classes
foreach ($tag->classes()->getAll() as $class ) {
    $this->used_selectors['classes'][$class]++;
}

I think that will do what you want for that part. Not sure what you were looking for otherwise.