doc = $doc; $this->xp = new \DOMXPath( $doc ); $this->url = $url; $this->enabled = array_flip( $enabled ); } /** * Is a rule on? * * @param string $rule Rule id. * @return bool */ public function on( $rule ) { return isset( $this->enabled[ $rule ] ); } /** * Query elements, dropping anything in scripts, templates, the admin bar and so on. * * @param string $expr XPath. * @param \DOMNode|null $ctx Context node. * @return \DOMElement[] */ public function q( $expr, $ctx = null ) { $res = $ctx ? $this->xp->query( $expr, $ctx ) : $this->xp->query( $expr ); $out = array(); if ( ! $res ) { return $out; } foreach ( $res as $node ) { if ( $node instanceof \DOMElement && ! Html::in_ignored( $node ) ) { $out[] = $node; } } return $out; } /** * Record a finding. * * @param string $rule Rule id. * @param \DOMElement $el Element. * @param array $detail Extra detail. * @return void */ public function add( $rule, \DOMElement $el, array $detail = array() ) { if ( ! $this->on( $rule ) ) { return; } $this->counts[ $rule ] = isset( $this->counts[ $rule ] ) ? $this->counts[ $rule ] + 1 : 1; if ( $this->counts[ $rule ] > 60 ) { return; } $meta = Registry::get( $rule ); $this->findings[] = array( 'rule' => $rule, 'selector' => Html::selector( $el ), 'snippet' => Html::snippet( $el ), 'detail' => $detail, 'severity' => $meta ? $meta['severity'] : 'moderate', ); } }