public static function cleanChildTokens(Configuration $configuration, array &$children, LoggerInterface $logger)
 {
     if ($configuration->get('clean-strategy') == Configuration::CLEAN_STRATEGY_NONE) {
         return true;
     }
     foreach ($children as $key => $child) {
         if ($child instanceof Cleanable) {
             $isClean = $child->clean($logger);
             if (!$isClean && $configuration->get('clean-strategy') !== Configuration::CLEAN_STRATEGY_LENIENT) {
                 unset($children[$key]);
             }
         }
     }
     return true;
 }
 public function clean($html)
 {
     // Tokenize.
     $tokenizer = new Tokenizer($this->configuration);
     $tokenContainer = $tokenizer->tokenize($html);
     // Remove unwanted tokens.
     $tokenContainer->remove($this->logger);
     // Build output generator.
     $outputClassName = 'Groundskeeper\\Output\\' . ucfirst($this->configuration->get('output'));
     $outputGenerator = new $outputClassName();
     // Clean
     $i = 0;
     do {
         if ($i > 0) {
             $tokenContainer = $tokenizer->tokenize($cleanedOutput);
         }
         $preCleaningOutput = $outputGenerator($tokenContainer);
         $tokenContainer->clean($this->logger);
         $cleanedOutput = $outputGenerator($tokenContainer);
         ++$i;
     } while ($i < 5 && $preCleaningOutput != $cleanedOutput);
     return $cleanedOutput;
 }
 /**
  * Required by the Removable interface.
  */
 public function remove(LoggerInterface $logger)
 {
     $hasRemovableElements = $this->configuration->get('element-blacklist') != '';
     $hasRemovableTypes = $this->configuration->get('type-blacklist') != '';
     foreach ($this->children as $child) {
         // Check types.
         if ($hasRemovableTypes && !$this->configuration->isAllowedType($child->getType())) {
             $logger->debug('Removing ' . $child . ' on type blacklist.');
             $this->removeChild($child);
             continue;
         }
         // Check elements.
         if ($hasRemovableElements && $child instanceof Element && !$this->configuration->isAllowedElement($child->getName())) {
             $logger->debug('Removing ' . $child . ' on element blacklist.');
             $this->removeChild($child);
             continue;
         }
         // Check children.
         if ($child instanceof Removable) {
             $child->remove($logger);
         }
     }
 }
示例#4
0
 private function cleanAttributeInteger(Configuration $configuration, Element $element, LoggerInterface $logger)
 {
     if ($this->value === true || $this->value == '') {
         if ($configuration->get('clean-strategy') !== Configuration::CLEAN_STRATEGY_LENIENT) {
             $logger->debug('Within ' . $element . ', the value for the attribute "' . $this->name . '" is required to be an positive, non-zero integer.  The value is invalid, therefore the attribute has been removed.');
         }
         return false;
     }
     if (!is_int($this->value)) {
         $origonalValue = (string) $this->value;
         $this->value = (int) $this->value;
         if ($origonalValue != (string) $this->value) {
             $logger->debug('Within ' . $element . ', the value for the attribute "' . $this->name . '" is required to be an positive, non-zero integer.  The value has been converted to an integer.');
         }
     }
     if ($this->value <= 0 && $configuration->get('clean-strategy') !== Configuration::CLEAN_STRATEGY_LENIENT) {
         $logger->debug('Within ' . $element . ', the value for the attribute "' . $this->value . '" is required to be an positive, non-zero integer.  The value is invalid, therefore the attribute has been removed.');
         return false;
     }
     return true;
 }