From 65e7f755633f2947c77668f659a93ca6b1ebce06 Mon Sep 17 00:00:00 2001
From: "Frederic G. MARAND" <fgm@osinet.fr>
Date: Sun, 28 Sep 2014 18:28:36 +0200
Subject: [PATCH] Issue #2339475 by fgm: Introduce a mutable HTML fragment
 interface.

---
 .../Core/Page/DefaultHtmlFragmentRenderer.php      |    4 +-
 core/lib/Drupal/Core/Page/HtmlFragment.php         |   48 ++----------
 .../Core/Page/HtmlMutableFragmentInterface.php     |   80 ++++++++++++++++++++
 3 files changed, 87 insertions(+), 45 deletions(-)
 create mode 100644 core/lib/Drupal/Core/Page/HtmlMutableFragmentInterface.php

diff --git a/core/lib/Drupal/Core/Page/DefaultHtmlFragmentRenderer.php b/core/lib/Drupal/Core/Page/DefaultHtmlFragmentRenderer.php
index 164f7f5..74570e4 100644
--- a/core/lib/Drupal/Core/Page/DefaultHtmlFragmentRenderer.php
+++ b/core/lib/Drupal/Core/Page/DefaultHtmlFragmentRenderer.php
@@ -135,10 +135,10 @@ public function preparePage(HtmlPage $page, &$page_array) {
   /**
    * Apply the default meta tags to the page object.
    *
-   * @param \Drupal\Core\Page\HtmlPage $page
+   * @param \Drupal\Core\Page\HtmlMutableFragmentInterface $page
    *   The html page.
    */
-  protected function setDefaultMetaTags(HtmlPage $page) {
+  protected function setDefaultMetaTags(HtmlMutableFragmentInterface $page) {
     // Add default elements. Make sure the Content-Type comes first because the
     // IE browser may be vulnerable to XSS via encoding attacks from any content
     // that comes before this META tag, such as a TITLE tag.
diff --git a/core/lib/Drupal/Core/Page/HtmlFragment.php b/core/lib/Drupal/Core/Page/HtmlFragment.php
index a4e0e29..6b1e578 100644
--- a/core/lib/Drupal/Core/Page/HtmlFragment.php
+++ b/core/lib/Drupal/Core/Page/HtmlFragment.php
@@ -15,7 +15,7 @@
 /**
  * Basic mutable implementation of an HTML fragment.
  */
-class HtmlFragment implements CacheableInterface, HtmlFragmentInterface {
+class HtmlFragment implements CacheableInterface, HtmlFragmentInterface, HtmlMutableFragmentInterface {
 
   /**
    * An array of Link elements.
@@ -71,12 +71,7 @@ public function __construct($content = '', array $cache_info = array()) {
   }
 
   /**
-   * Adds a link element to the page.
-   *
-   * @param \Drupal\Core\Page\LinkElement $link
-   *   A link element to enqueue.
-   *
-   * @return $this
+   * {@inheritdoc}
    */
   public function addLinkElement(LinkElement $link) {
     $this->links[] = $link;
@@ -104,12 +99,7 @@ public function getFeedLinkElements() {
   }
 
   /**
-   * Adds a meta element to the page.
-   *
-   * @param \Drupal\Core\Page\MetaElement $meta
-   *   A meta element to add.
-   *
-   * @return $this
+   * {@inheritdoc}
    */
   public function addMetaElement(MetaElement $meta) {
     $this->metatags[] = $meta;
@@ -124,18 +114,7 @@ public function &getMetaElements() {
   }
 
   /**
-   * Sets the response content.
-   *
-   * This should be the bulk of the page content, and will ultimately be placed
-   * within the <body> tag in final HTML output.
-   *
-   * Valid types are strings, numbers, and objects that implement a __toString()
-   * method.
-   *
-   * @param mixed $content
-   *   The content for this fragment.
-   *
-   * @return $this
+   * {@inheritdoc}
    */
   public function setContent($content) {
     $this->content = $content;
@@ -150,24 +129,7 @@ public function getContent() {
   }
 
   /**
-   * Sets the title of this HtmlFragment.
-   *
-   * Handling of this title varies depending on what is consuming this
-   * HtmlFragment object. If it's a block, it may only be used as the block's
-   * title; if it's at the page level, it will be used in a number of places,
-   * including the html <head> title.
-   *
-   * @param string $title
-   *   Value to assign to the page title.
-   * @param int $output
-   *   (optional) normally should be left as Title::CHECK_PLAIN. Only set to
-   *   Title::PASS_THROUGH if you have already removed any possibly dangerous
-   *   code from $title using a function like
-   *   \Drupal\Component\Utility\String::checkPlain() or
-   *   \Drupal\Component\Utility\Xss::filterAdmin(). With this flag the string
-   *   will be passed through unchanged.
-   *
-   * @return $this
+   * {@inheritdoc}
    */
   public function setTitle($title, $output = Title::CHECK_PLAIN) {
     if ($output == Title::CHECK_PLAIN) {
diff --git a/core/lib/Drupal/Core/Page/HtmlMutableFragmentInterface.php b/core/lib/Drupal/Core/Page/HtmlMutableFragmentInterface.php
new file mode 100644
index 0000000..7c13587
--- /dev/null
+++ b/core/lib/Drupal/Core/Page/HtmlMutableFragmentInterface.php
@@ -0,0 +1,80 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Core\Page\HtmlMutableFragmentInterface.
+ */
+
+namespace Drupal\Core\Page;
+
+/**
+ * Mutator interface for the domain object for a portion of an HTML page, including related data.
+ *
+ * Related data includes any additional information relevant to a fragment of
+ * HTML that would not be part of the HTML string itself. That includes, for
+ * example, required CSS files, Javascript files, link tags, meta tags, and the
+ * title of a page or page section.
+ *
+ * @ingroup menu
+ */
+interface HtmlMutableFragmentInterface {
+
+  /**
+   * Adds a link element to the page.
+   *
+   * @param \Drupal\Core\Page\LinkElement $link
+   *   A link element to enqueue.
+   *
+   * @return $this
+   */
+  public function addLinkElement(LinkElement $link);
+
+  /**
+   * Adds a meta element to the page.
+   *
+   * @param \Drupal\Core\Page\MetaElement $meta
+   *   A meta element to add.
+   *
+   * @return $this
+   */
+  public function addMetaElement(MetaElement $meta);
+
+  /**
+   * Sets the response content.
+   *
+   * This should be the bulk of the page content, and will ultimately be placed
+   * within the <body> tag in final HTML output.
+   *
+   * Valid types are strings, numbers, and objects that implement a __toString()
+   * method.
+   *
+   * @param mixed $content
+   *   The content for this fragment.
+   *
+   * @return $this
+   */
+  public function setContent($content);
+
+  /**
+   * Sets the title of this HtmlFragment.
+   *
+   * Handling of this title varies depending on what is consuming this
+   * HtmlFragment object. If it's a block, it may only be used as the block's
+   * title; if it's at the page level, it will be used in a number of places,
+   * including the html <head> title.
+   *
+   * @param string $title
+   *   Value to assign to the page title.
+   * @param int $output
+   *   (optional) normally should be left as Title::CHECK_PLAIN. Only set to
+   *   Title::PASS_THROUGH if you have already removed any possibly dangerous
+   *   code from $title using a function like
+   *   \Drupal\Component\Utility\String::checkPlain() or
+   *   \Drupal\Component\Utility\Xss::filterAdmin(). With this flag the string
+   *   will be passed through unchanged.
+   *
+   * @return $this
+   */
+  public function setTitle($title, $output = Title::CHECK_PLAIN);
+
+}
-- 
1.7.9.5

