Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. HtmlExtension.php

class HtmlExtension

XPath expression translator HTML extension.

This component is a port of the Python cssselect library, which is copyright Ian Bicking, @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>

@internal

Hierarchy

  • class \Symfony\Component\CssSelector\XPath\Extension\AbstractExtension implements \Symfony\Component\CssSelector\XPath\Extension\ExtensionInterface
    • class \Symfony\Component\CssSelector\XPath\Extension\HtmlExtension extends \Symfony\Component\CssSelector\XPath\Extension\AbstractExtension

Expanded class hierarchy of HtmlExtension

See also

https://github.com/SimonSapin/cssselect.

1 file declares its use of HtmlExtension
CssSelectorConverter.php in vendor/symfony/css-selector/CssSelectorConverter.php

File

vendor/symfony/css-selector/XPath/Extension/HtmlExtension.php, line 29

Namespace

Symfony\Component\CssSelector\XPath\Extension
View source
class HtmlExtension extends AbstractExtension {
    public function __construct(Translator $translator) {
        $translator->getExtension('node')
            ->setFlag(NodeExtension::ELEMENT_NAME_IN_LOWER_CASE, true)
            ->setFlag(NodeExtension::ATTRIBUTE_NAME_IN_LOWER_CASE, true);
    }
    public function getPseudoClassTranslators() : array {
        return [
            'checked' => $this->translateChecked(...),
            'link' => $this->translateLink(...),
            'disabled' => $this->translateDisabled(...),
            'enabled' => $this->translateEnabled(...),
            'selected' => $this->translateSelected(...),
            'invalid' => $this->translateInvalid(...),
            'hover' => $this->translateHover(...),
            'visited' => $this->translateVisited(...),
        ];
    }
    public function getFunctionTranslators() : array {
        return [
            'lang' => $this->translateLang(...),
        ];
    }
    public function translateChecked(XPathExpr $xpath) : XPathExpr {
        return $xpath->addCondition('(@checked ' . "and (name(.) = 'input' or name(.) = 'command')" . "and (@type = 'checkbox' or @type = 'radio'))");
    }
    public function translateLink(XPathExpr $xpath) : XPathExpr {
        return $xpath->addCondition("@href and (name(.) = 'a' or name(.) = 'link' or name(.) = 'area')");
    }
    public function translateDisabled(XPathExpr $xpath) : XPathExpr {
        return $xpath->addCondition('(' . '@disabled and' . '(' . "(name(.) = 'input' and @type != 'hidden')" . " or name(.) = 'button'" . " or name(.) = 'select'" . " or name(.) = 'textarea'" . " or name(.) = 'command'" . " or name(.) = 'fieldset'" . " or name(.) = 'optgroup'" . " or name(.) = 'option'" . ')' . ') or (' . "(name(.) = 'input' and @type != 'hidden')" . " or name(.) = 'button'" . " or name(.) = 'select'" . " or name(.) = 'textarea'" . ')' . ' and ancestor::fieldset[@disabled]');
        // todo: in the second half, add "and is not a descendant of that fieldset element's first legend element child, if any."
    }
    public function translateEnabled(XPathExpr $xpath) : XPathExpr {
        return $xpath->addCondition('(' . '@href and (' . "name(.) = 'a'" . " or name(.) = 'link'" . " or name(.) = 'area'" . ')' . ') or (' . '(' . "name(.) = 'command'" . " or name(.) = 'fieldset'" . " or name(.) = 'optgroup'" . ')' . ' and not(@disabled)' . ') or (' . '(' . "(name(.) = 'input' and @type != 'hidden')" . " or name(.) = 'button'" . " or name(.) = 'select'" . " or name(.) = 'textarea'" . " or name(.) = 'keygen'" . ')' . ' and not (@disabled or ancestor::fieldset[@disabled])' . ') or (' . "name(.) = 'option' and not(" . '@disabled or ancestor::optgroup[@disabled]' . ')' . ')');
    }
    
    /**
     * @throws ExpressionErrorException
     */
    public function translateLang(XPathExpr $xpath, FunctionNode $function) : XPathExpr {
        $arguments = $function->getArguments();
        foreach ($arguments as $token) {
            if (!($token->isString() || $token->isIdentifier())) {
                throw new ExpressionErrorException('Expected a single string or identifier for :lang(), got ' . implode(', ', $arguments));
            }
        }
        return $xpath->addCondition(\sprintf('ancestor-or-self::*[@lang][1][starts-with(concat(' . "translate(@%s, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '-')" . ', %s)]', 'lang', Translator::getXpathLiteral(strtolower($arguments[0]->getValue()) . '-')));
    }
    public function translateSelected(XPathExpr $xpath) : XPathExpr {
        return $xpath->addCondition("(@selected and name(.) = 'option')");
    }
    public function translateInvalid(XPathExpr $xpath) : XPathExpr {
        return $xpath->addCondition('0');
    }
    public function translateHover(XPathExpr $xpath) : XPathExpr {
        return $xpath->addCondition('0');
    }
    public function translateVisited(XPathExpr $xpath) : XPathExpr {
        return $xpath->addCondition('0');
    }
    public function getName() : string {
        return 'html';
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
AbstractExtension::getAttributeMatchingTranslators public function Returns attribute operation translators. Overrides ExtensionInterface::getAttributeMatchingTranslators 1
AbstractExtension::getCombinationTranslators public function Returns combination translators. Overrides ExtensionInterface::getCombinationTranslators 1
AbstractExtension::getNodeTranslators public function Returns node translators. Overrides ExtensionInterface::getNodeTranslators 1
HtmlExtension::getFunctionTranslators public function Returns function translators. Overrides AbstractExtension::getFunctionTranslators
HtmlExtension::getName public function Returns extension name. Overrides ExtensionInterface::getName
HtmlExtension::getPseudoClassTranslators public function Returns pseudo-class translators. Overrides AbstractExtension::getPseudoClassTranslators
HtmlExtension::translateChecked public function
HtmlExtension::translateDisabled public function
HtmlExtension::translateEnabled public function
HtmlExtension::translateHover public function
HtmlExtension::translateInvalid public function
HtmlExtension::translateLang public function
HtmlExtension::translateLink public function
HtmlExtension::translateSelected public function
HtmlExtension::translateVisited public function
HtmlExtension::__construct public function
RSS feed
Powered by Drupal