You are here

class PreparedTagElement in Extensible BBCode 8.3

Same name and namespace in other branches
  1. 4.0.x src/PreparedTagElement.php \Drupal\xbbcode\PreparedTagElement

Adapter for the tag element that marks markup as safe.

This is used to allow Twig in template tags to automatically filter unsafe strings while leaving safe markup intact.

The content, source and outer source of the tag are marked as safe. In the outer source, the argument string is run through XSS filtering to ensure it is indeed safe.

The option and attribute values are not safe, and therefore left as is.

Hierarchy

Expanded class hierarchy of PreparedTagElement

2 files declare their use of PreparedTagElement
TagFormBase.php in src/Form/TagFormBase.php
TagPluginBase.php in src/Plugin/TagPluginBase.php

File

src/PreparedTagElement.php, line 24

Namespace

Drupal\xbbcode
View source
class PreparedTagElement implements TagElementInterface {

  /**
   * The wrapped tag element.
   *
   * @var \Drupal\xbbcode\Parser\Tree\TagElementInterface
   */
  protected $tag;

  /**
   * The outer source.
   *
   * @var \Drupal\Component\Render\MarkupInterface
   */
  protected $outerSource;

  /**
   * PreparedTagElement constructor.
   *
   * @param \Drupal\xbbcode\Parser\Tree\TagElementInterface $tag
   *   The tag to be wrapped.
   */
  public function __construct(TagElementInterface $tag) {
    $this->tag = $tag;
  }

  /**
   * {@inheritdoc}
   */
  public function getName() : string {
    return $this->tag
      ->getName();
  }

  /**
   * {@inheritdoc}
   */
  public function getArgument() : string {
    return $this->tag
      ->getArgument();
  }

  /**
   * {@inheritdoc}
   */
  public function getAttribute(string $name) : ?string {
    return $this->tag
      ->getAttribute($name);
  }

  /**
   * {@inheritdoc}
   */
  public function setAttribute(string $name, string $value = NULL) : void {
    $this->tag
      ->setAttribute($name, $value);
  }

  /**
   * {@inheritdoc}
   */
  public function getAttributes() : array {
    return $this->tag
      ->getAttributes();
  }

  /**
   * {@inheritdoc}
   */
  public function setAttributes(array $attributes) : void {
    $this->tag
      ->setAttributes($attributes);
  }

  /**
   * {@inheritdoc}
   */
  public function getOption() : string {
    return $this->tag
      ->getOption();
  }

  /**
   * {@inheritdoc}
   */
  public function setOption(string $value) : void {
    $this->tag
      ->setOption($value);
  }

  /**
   * {@inheritdoc}
   */
  public function getContent() {
    return Markup::create($this->tag
      ->getContent());
  }

  /**
   * {@inheritdoc}
   */
  public function getSource() {
    return Markup::create($this->tag
      ->getSource());
  }

  /**
   * {@inheritdoc}
   */
  public function setSource(string $source) : void {
    $this->tag
      ->setSource($source);
  }

  /**
   * {@inheritdoc}
   */
  public function getOuterSource() {

    // Reconstruct the opening and closing tags, but render the content.
    if (!isset($this->outerSource)) {
      $name = $this->tag
        ->getName();

      // The argument string must be made safe before rendering.
      $argument = XssEscape::filterAdmin($this->tag
        ->getArgument());
      $content = $this->tag
        ->getContent();
      $this->outerSource = Markup::create("[{$name}{$argument}]{$content}[/{$name}]");
    }
    return $this->outerSource;
  }

  /**
   * {@inheritdoc}
   */
  public function getProcessor() : TagProcessorInterface {
    return $this->tag
      ->getProcessor();
  }

  /**
   * {@inheritdoc}
   */
  public function setProcessor(TagProcessorInterface $processor) : void {
    $this->tag
      ->setProcessor($processor);
  }

  /**
   * {@inheritdoc}
   */
  public function render() : OutputElementInterface {
    return $this->tag
      ->render();
  }

  /**
   * {@inheritdoc}
   */
  public function append(ElementInterface $element) : void {
    $this->tag
      ->append($element);
  }

  /**
   * {@inheritdoc}
   */
  public function getChildren() : array {
    return $this->tag
      ->getChildren();
  }

  /**
   * {@inheritdoc}
   */
  public function getRenderedChildren($force_render = TRUE) : array {
    return $this->tag
      ->getRenderedChildren($force_render);
  }

  /**
   * {@inheritdoc}
   */
  public function getDescendants() {
    return $this->tag
      ->getDescendants();
  }

  /**
   * {@inheritdoc}
   */
  public function getParent() : NodeElementInterface {
    return $this->tag
      ->getParent();
  }

  /**
   * {@inheritdoc}
   */
  public function setParent(NodeElementInterface $parent) : void {
    $this->tag
      ->setParent($parent);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PreparedTagElement::$outerSource protected property The outer source.
PreparedTagElement::$tag protected property The wrapped tag element.
PreparedTagElement::append public function Append an element to the children of this element. Overrides NodeElementInterface::append
PreparedTagElement::getArgument public function Retrieve the unparsed argument string. Overrides TagElementInterface::getArgument
PreparedTagElement::getAttribute public function Retrieve a particular attribute of the element. Overrides TagElementInterface::getAttribute
PreparedTagElement::getAttributes public function Return all attribute values. Overrides TagElementInterface::getAttributes
PreparedTagElement::getChildren public function Get all children of the element. Overrides NodeElementInterface::getChildren
PreparedTagElement::getContent public function Retrieve the rendered content of the element. Overrides NodeElementInterface::getContent
PreparedTagElement::getDescendants public function Retrieve the descendants of the node. Overrides NodeElementInterface::getDescendants
PreparedTagElement::getName public function Get the tag name of this element. Overrides TagElementInterface::getName
PreparedTagElement::getOption public function Retrieve the option-type attribute of the element. Overrides TagElementInterface::getOption
PreparedTagElement::getOuterSource public function Retrieve the content including the opening and closing tags. Overrides TagElementInterface::getOuterSource
PreparedTagElement::getParent public function Retrieve the parent of the current tag. Overrides TagElementInterface::getParent
PreparedTagElement::getProcessor public function Get the assigned processor. Overrides TagElementInterface::getProcessor
PreparedTagElement::getRenderedChildren public function Retrieve the rendered output of each child. Overrides NodeElementInterface::getRenderedChildren
PreparedTagElement::getSource public function Retrieve the content source of the tag. Overrides TagElementInterface::getSource
PreparedTagElement::render public function Render this element to a string. Overrides ElementInterface::render
PreparedTagElement::setAttribute public function Set an attribute of the element. Overrides TagElementInterface::setAttribute
PreparedTagElement::setAttributes public function Set all attribute values. Overrides TagElementInterface::setAttributes
PreparedTagElement::setOption public function Set the option-style attribute of the element. Overrides TagElementInterface::setOption
PreparedTagElement::setParent public function Set the parent of the current tag. Overrides TagElementInterface::setParent
PreparedTagElement::setProcessor public function Assign a processor to this tag element. Overrides TagElementInterface::setProcessor
PreparedTagElement::setSource public function Set the content source of the tag. Overrides TagElementInterface::setSource
PreparedTagElement::__construct public function PreparedTagElement constructor.