/** * Formats a string for HTML display by replacing variable placeholders. * * @param $string * A string containing placeholders. The string itself is not escaped, any * unsafe content must be in $args and inserted via placeholders. * @param $args * An associative array of replacements to make. Occurrences in $string of * any key in $args are replaced with the corresponding value, after * optional sanitization and formatting. The type of sanitization and * formatting depends on the first character of the key: * - @variable: Escaped to HTML using * \Drupal\Component\Utility\SafeMarkup::escape(). Use this as the * default choice for anything displayed on a page on the site. * - %variable: Escaped to HTML and formatted using String::placeholder(), * which makes the following HTML code: * @code * <em class="placeholder">text output here.</em> * @endcode * - !variable: Inserted as is, with no sanitization or formatting. Only * use this when the resulting string is being generated for one of: * - Non-HTML usage, such as a plain-text email. * - Non-direct HTML output, such as a plain-text variable that will be * printed as an HTML attribute value and therefore formatted with * String::checkPlain() as part of that. * - Some other special reason for suppressing sanitization. * * @return string * The formatted string, which is marked as safe unless sanitization of an * unsafe argument was suppressed (see above). * * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0. * Use \Drupal\Component\Utility\SafeMarkup::format() instead. */ public static function format($string, array $args = array()) { return SafeMarkup::format($string, $args); }
/** * Asserts that a select option in the current page is checked. * * @param string $id * ID of select field to assert. * @param string $option * Option to assert. * @param string $message * (optional) A message to display with the assertion. Do not translate * messages: use format_string() to embed variables in the message text, not * t(). If left blank, a default message will be displayed. * @param string $group * (optional) The group this message is in, which is displayed in a column * in test output. Use 'Debug' to indicate this is debugging output. Do not * translate this string. Defaults to 'Browser'; most tests do not override * this default. * * @return bool * TRUE on pass, FALSE on fail. * * @todo Remove function once core issue is resolved: https://www.drupal.org/node/2530092 */ protected function assertOptionSelected($id, $option, $message = '', $group = 'Browser') { $elements = $this->xpath('//select[contains(@id, :id)]//option[@value=:option]', array(':id' => $id, ':option' => $option)); return $this->assertTrue(isset($elements[0]) && !empty($elements[0]['selected']), $message ? $message : SafeMarkup::format('Option @option for field @id is selected.', array('@option' => $option, '@id' => $id)), $group); }
/** * Determines if an external URL points to this installation. * * @param string $url * A string containing an external URL, such as "http://example.com/foo". * @param string $base_url * The base URL string to check against, such as "http://example.com/" * * @return bool * TRUE if the URL has the same domain and base path. * * @throws \InvalidArgumentException * Exception thrown when a either $url or $bath_url are not fully qualified. */ public static function externalIsLocal($url, $base_url) { $url_parts = parse_url($url); $base_parts = parse_url($base_url); if (empty($base_parts['host']) || empty($url_parts['host'])) { throw new \InvalidArgumentException(SafeMarkup::format('A path was passed when a fully qualified domain was expected.')); } if (!isset($url_parts['path']) || !isset($base_parts['path'])) { return (!isset($base_parts['path']) || $base_parts['path'] == '/') && $url_parts['host'] == $base_parts['host']; } else { // When comparing base paths, we need a trailing slash to make sure a // partial URL match isn't occurring. Since base_path() always returns // with a trailing slash, we don't need to add the trailing slash here. return $url_parts['host'] == $base_parts['host'] && stripos($url_parts['path'], $base_parts['path']) === 0; } }