diff -u b/core/lib/Drupal.php b/core/lib/Drupal.php --- b/core/lib/Drupal.php +++ b/core/lib/Drupal.php @@ -500,7 +500,7 @@ * * This method is a convenience wrapper for the link generator service's * generate() method. For detailed documentation, see - * \Drupal\Core\Render\LinkGeneratorInterface::generate(). + * \Drupal\Core\Routing\LinkGeneratorInterface::generate(). * * @see \Drupal\Core\Render\LinkGeneratorInterface::generate() * @see \Drupal\Core\Url reverted: --- b/core/lib/Drupal/Core/Render/LinkGenerator.php +++ a/core/lib/Drupal/Core/Render/LinkGenerator.php @@ -2,10 +2,10 @@ /** * @file + * Contains \Drupal\Core\Utility\LinkGenerator. - * Contains \Drupal\Core\Render\LinkGenerator. */ +namespace Drupal\Core\Utility; -namespace Drupal\Core\Render; use Drupal\Component\Serialization\Json; use Drupal\Component\Utility\SafeMarkup; reverted: --- b/core/lib/Drupal/Core/Render/LinkGeneratorInterface.php +++ a/core/lib/Drupal/Core/Render/LinkGeneratorInterface.php @@ -2,10 +2,10 @@ /** * @file + * Contains \Drupal\Core\Utility\LinkGeneratorInterface. - * Contains \Drupal\Core\Render\LinkGeneratorInterface. */ +namespace Drupal\Core\Utility; -namespace Drupal\Core\Render; use Drupal\Core\Link; use Drupal\Core\Url; reverted: --- b/core/tests/Drupal/Tests/Core/Render/LinkGeneratorTest.php +++ a/core/tests/Drupal/Tests/Core/Render/LinkGeneratorTest.php @@ -2,18 +2,18 @@ /** * @file + * Contains \Drupal\Tests\Core\Utility\LinkGeneratorTest. - * Contains \Drupal\Tests\Core\Render\LinkGeneratorTest. */ namespace Drupal\Tests\Core\Render { use Drupal\Core\Language\Language; use Drupal\Core\Url; +use Drupal\Core\Utility\LinkGenerator; -use Drupal\Core\Render\LinkGenerator; use Drupal\Tests\UnitTestCase; /** + * @coversDefaultClass \Drupal\Core\Utility\LinkGenerator - * @coversDefaultClass \Drupal\Core\Render\LinkGenerator * @group Utility */ class LinkGeneratorTest extends UnitTestCase { @@ -21,7 +21,7 @@ /** * The tested link generator. * + * @var \Drupal\Core\Utility\LinkGenerator - * @var \Drupal\Core\Render\LinkGenerator */ protected $linkGenerator; @@ -73,7 +73,7 @@ /** * Provides test data for testing the link method. * + * @see \Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateHrefs() - * @see \Drupal\Tests\Core\Render\LinkGeneratorTest::testGenerateHrefs() * * @return array * Returns some test data. @@ -92,8 +92,8 @@ /** * Tests the link method with certain hrefs. * + * @see \Drupal\Core\Utility\LinkGenerator::generate() + * @see \Drupal\Tests\Core\Utility\LinkGeneratorTest::providerTestGenerate() - * @see \Drupal\Core\Render\LinkGenerator::generate() - * @see \Drupal\Tests\Core\Render\LinkGeneratorTest::providerTestGenerate() * * @dataProvider providerTestGenerateHrefs */ @@ -184,7 +184,7 @@ /** * Tests the link method with additional attributes. * + * @see \Drupal\Core\Utility\LinkGenerator::generate() - * @see \Drupal\Core\Render\LinkGenerator::generate() */ public function testGenerateAttributes() { $this->urlGenerator->expects($this->once()) @@ -212,7 +212,7 @@ /** * Tests the link method with passed query options. * + * @see \Drupal\Core\Utility\LinkGenerator::generate() - * @see \Drupal\Core\Render\LinkGenerator::generate() */ public function testGenerateQuery() { $this->urlGenerator->expects($this->once()) @@ -238,7 +238,7 @@ /** * Tests the link method with passed query options via parameters. * + * @see \Drupal\Core\Utility\LinkGenerator::generate() - * @see \Drupal\Core\Render\LinkGenerator::generate() */ public function testGenerateParametersAsQuery() { $this->urlGenerator->expects($this->once()) @@ -262,7 +262,7 @@ /** * Tests the link method with arbitrary passed options. * + * @see \Drupal\Core\Utility\LinkGenerator::generate() - * @see \Drupal\Core\Render\LinkGenerator::generate() */ public function testGenerateOptions() { $this->urlGenerator->expects($this->once()) @@ -288,7 +288,7 @@ /** * Tests the link method with a script tab. * + * @see \Drupal\Core\Utility\LinkGenerator::generate() - * @see \Drupal\Core\Render\LinkGenerator::generate() */ public function testGenerateXss() { $this->urlGenerator->expects($this->once()) @@ -314,7 +314,7 @@ /** * Tests the link method with html. * + * @see \Drupal\Core\Utility\LinkGenerator::generate() - * @see \Drupal\Core\Render\LinkGenerator::generate() */ public function testGenerateWithHtml() { $this->urlGenerator->expects($this->at(0)) @@ -360,7 +360,7 @@ /** * Tests the active class on the link method. * + * @see \Drupal\Core\Utility\LinkGenerator::generate() - * @see \Drupal\Core\Render\LinkGenerator::generate() * * @todo Test that the active class is added on the front page when generating * links to the front page when drupal_is_front_page() is converted to a only in patch2: unchanged: --- /dev/null +++ b/core/lib/Drupal/Core/Render/LinkGenerator.php @@ -0,0 +1,143 @@ +urlGenerator = $url_generator; + $this->moduleHandler = $module_handler; + } + + /** + * {@inheritdoc} + */ + public function generateFromLink(Link $link) { + return $this->generate($link->getText(), $link->getUrl()); + } + + /** + * {@inheritdoc} + * + * For anonymous users, the "active" class will be calculated on the server, + * because most sites serve each anonymous user the same cached page anyway. + * For authenticated users, the "active" class will be calculated on the + * client (through JavaScript), only data- attributes are added to links to + * prevent breaking the render cache. The JavaScript is added in + * system_page_attachments(). + * + * @see system_page_attachments() + */ + public function generate($text, Url $url) { + // Performance: avoid Url::toString() needing to retrieve the URL generator + // service from the container. + $url->setUrlGenerator($this->urlGenerator); + + // Start building a structured representation of our link to be altered later. + $variables = array( + // @todo Inject the service when drupal_render() is converted to one. + 'text' => is_array($text) ? drupal_render($text) : $text, + 'url' => $url, + 'options' => $url->getOptions(), + ); + + // Merge in default options. + $variables['options'] += array( + 'attributes' => array(), + 'query' => array(), + 'html' => FALSE, + 'language' => NULL, + 'set_active_class' => FALSE, + 'absolute' => FALSE, + ); + + // Add a hreflang attribute if we know the language of this link's url and + // hreflang has not already been set. + if (!empty($variables['options']['language']) && !isset($variables['options']['attributes']['hreflang'])) { + $variables['options']['attributes']['hreflang'] = $variables['options']['language']->getId(); + } + + // Set the "active" class if the 'set_active_class' option is not empty. + if (!empty($variables['options']['set_active_class']) && !$url->isExternal()) { + // Add a "data-drupal-link-query" attribute to let the + // drupal.active-link library know the query in a standardized manner. + if (!empty($variables['options']['query'])) { + $query = $variables['options']['query']; + ksort($query); + $variables['options']['attributes']['data-drupal-link-query'] = Json::encode($query); + } + + // Add a "data-drupal-link-system-path" attribute to let the + // drupal.active-link library know the path in a standardized manner. + if ($url->isRouted() && !isset($variables['options']['attributes']['data-drupal-link-system-path'])) { + // @todo System path is deprecated - use the route name and parameters. + $system_path = $url->getInternalPath(); + // Special case for the front page. + $variables['options']['attributes']['data-drupal-link-system-path'] = $system_path == '' ? '' : $system_path; + } + } + + // Remove all HTML and PHP tags from a tooltip, calling expensive strip_tags() + // only when a quick strpos() gives suspicion tags are present. + if (isset($variables['options']['attributes']['title']) && strpos($variables['options']['attributes']['title'], '<') !== FALSE) { + $variables['options']['attributes']['title'] = strip_tags($variables['options']['attributes']['title']); + } + + // Allow other modules to modify the structure of the link. + $this->moduleHandler->alter('link', $variables); + + // Move attributes out of options. generateFromRoute(() doesn't need them. + $attributes = new Attribute($variables['options']['attributes']); + unset($variables['options']['attributes']); + $url->setOptions($variables['options']); + + // The result of the url generator is a plain-text URL. Because we are using + // it here in an HTML argument context, we need to encode it properly. + $url = String::checkPlain($url->toString()); + + // Sanitize the link text if necessary. + $text = $variables['options']['html'] ? $variables['text'] : String::checkPlain($variables['text']); + return SafeMarkup::set('' . $text . ''); + } + +} only in patch2: unchanged: --- /dev/null +++ b/core/lib/Drupal/Core/Render/LinkGeneratorInterface.php @@ -0,0 +1,79 @@ +content types page', array('@url' => \Drupal::url('node.overview_types'))); + * @endcode + * This keeps the context of the link title ('settings' in the example) for + * translators. + * + * @param string|array $text + * The link text for the anchor tag as a translated string or render array. + * @param \Drupal\Core\Url $url + * The URL object used for the link. Amongst its options, the following may + * be set to affect the generated link: + * - attributes: An associative array of HTML attributes to apply to the + * anchor tag. If element 'class' is included, it must be an array; 'title' + * must be a string; other elements are more flexible, as they just need + * to work as an argument for the constructor of the class + * Drupal\Core\Template\Attribute($options['attributes']). + * - html: Whether $text is HTML or just plain-text. For + * example, to make an image tag into a link, this must be set to TRUE, or + * you will see the escaped HTML image tag. $text is not sanitized if + * 'html' is TRUE. The calling function must ensure that $text is already + * safe. Defaults to FALSE. + * - language: An optional language object. If the path being linked to is + * internal to the site, $options['language'] is used to determine whether + * the link is "active", or pointing to the current page (the language as + * well as the path must match). + * - 'set_active_class': Whether this method should compare the $route_name, + * $parameters, language and query options to the current URL to determine + * whether the link is "active". Defaults to FALSE. If TRUE, an "active" + * class will be applied to the link. It is important to use this + * sparingly since it is usually unnecessary and requires extra + * processing. + * + * @return string + * An HTML string containing a link to the given route and parameters. + * + * @throws \Symfony\Component\Routing\Exception\RouteNotFoundException + * Thrown when the named route doesn't exist. + * @throws \Symfony\Component\Routing\Exception\MissingMandatoryParametersException + * Thrown when some parameters are missing that are mandatory for the route. + * @throws \Symfony\Component\Routing\Exception\InvalidParameterException + * Thrown when a parameter value for a placeholder is not correct because it + * does not match the requirement. + */ + public function generate($text, Url $url); + + /** + * Renders a link from a link object. + * + * @param \Drupal\Core\Link $link + * A link object to convert to a string. + * + * @return string + * An HTML string containing a link to the given link. + */ + public function generateFromLink(Link $link); + +} only in patch2: unchanged: --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Render/LinkGeneratorTest.php @@ -0,0 +1,463 @@ + array(), + 'html' => FALSE, + 'language' => NULL, + 'set_active_class' => FALSE, + 'absolute' => FALSE, + ); + + /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + + $this->urlGenerator = $this->getMock('\Drupal\Core\Routing\UrlGenerator', array(), array(), '', FALSE); + $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'); + + $this->linkGenerator = new LinkGenerator($this->urlGenerator, $this->moduleHandler); + $this->urlAssembler = $this->getMock('\Drupal\Core\Utility\UnroutedUrlAssemblerInterface'); + } + + /** + * Provides test data for testing the link method. + * + * @see \Drupal\Tests\Core\Render\LinkGeneratorTest::testGenerateHrefs() + * + * @return array + * Returns some test data. + */ + public function providerTestGenerateHrefs() { + return array( + // Test that the url returned by the URL generator is used. + array('test_route_1', array(), FALSE, '/test-route-1'), + // Test that $parameters is passed to the URL generator. + array('test_route_2', array('value' => 'example'), FALSE, '/test-route-2/example'), + // Test that the 'absolute' option is passed to the URL generator. + array('test_route_3', array(), TRUE, 'http://example.com/test-route-3'), + ); + } + + /** + * Tests the link method with certain hrefs. + * + * @see \Drupal\Core\Render\LinkGenerator::generate() + * @see \Drupal\Tests\Core\Render\LinkGeneratorTest::providerTestGenerate() + * + * @dataProvider providerTestGenerateHrefs + */ + public function testGenerateHrefs($route_name, array $parameters, $absolute, $expected_url) { + $this->urlGenerator->expects($this->once()) + ->method('generateFromRoute') + ->with($route_name, $parameters, array('absolute' => $absolute) + $this->defaultOptions) + ->will($this->returnValue($expected_url)); + + $this->moduleHandler->expects($this->once()) + ->method('alter'); + + $url = new Url($route_name, $parameters, array('absolute' => $absolute)); + $url->setUrlGenerator($this->urlGenerator); + $result = $this->linkGenerator->generate('Test', $url); + $this->assertTag(array( + 'tag' => 'a', + 'attributes' => array('href' => $expected_url), + ), $result); + } + + /** + * Tests the generate() method with a route. + * + * @covers ::generate + */ + public function testGenerate() { + $this->urlGenerator->expects($this->once()) + ->method('generateFromRoute') + ->with('test_route_1', array(), array('fragment' => 'the-fragment') + $this->defaultOptions) + ->will($this->returnValue('/test-route-1#the-fragment')); + + $this->moduleHandler->expects($this->once()) + ->method('alter') + ->with('link', $this->isType('array')); + + $url = new Url('test_route_1', array(), array('fragment' => 'the-fragment')); + $url->setUrlGenerator($this->urlGenerator); + + $result = $this->linkGenerator->generate('Test', $url); + $this->assertTag(array( + 'tag' => 'a', + 'attributes' => array( + 'href' => '/test-route-1#the-fragment', + ), + 'content' => 'Test', + ), $result); + } + + /** + * Tests the generate() method with an external URL. + * + * The set_active_class option is set to TRUE to ensure this does not cause + * an error together with an external URL. + * + * @covers ::generate + */ + public function testGenerateExternal() { + $this->urlAssembler->expects($this->once()) + ->method('assemble') + ->with('http://drupal.org', array('set_active_class' => TRUE, 'external' => TRUE) + $this->defaultOptions) + ->will($this->returnArgument(0)); + + $this->moduleHandler->expects($this->once()) + ->method('alter') + ->with('link', $this->isType('array')); + + $this->urlAssembler->expects($this->once()) + ->method('assemble') + ->with('http://drupal.org', array('set_active_class' => TRUE, 'external' => TRUE) + $this->defaultOptions) + ->willReturnArgument(0); + + $url = Url::fromUri('http://drupal.org'); + $url->setUrlGenerator($this->urlGenerator); + $url->setUnroutedUrlAssembler($this->urlAssembler); + $url->setOption('set_active_class', TRUE); + + $result = $this->linkGenerator->generate('Drupal', $url); + $this->assertTag(array( + 'tag' => 'a', + 'attributes' => array( + 'href' => 'http://drupal.org', + ), + 'content' => 'Drupal', + ), $result); + } + + /** + * Tests the link method with additional attributes. + * + * @see \Drupal\Core\Render\LinkGenerator::generate() + */ + public function testGenerateAttributes() { + $this->urlGenerator->expects($this->once()) + ->method('generateFromRoute') + ->with('test_route_1', array(), $this->defaultOptions) + ->will($this->returnValue( + '/test-route-1' + )); + + // Test that HTML attributes are added to the anchor. + $url = new Url('test_route_1', array(), array( + 'attributes' => array('title' => 'Tooltip'), + )); + $url->setUrlGenerator($this->urlGenerator); + $result = $this->linkGenerator->generate('Test', $url); + $this->assertTag(array( + 'tag' => 'a', + 'attributes' => array( + 'href' => '/test-route-1', + 'title' => 'Tooltip', + ), + ), $result); + } + + /** + * Tests the link method with passed query options. + * + * @see \Drupal\Core\Render\LinkGenerator::generate() + */ + public function testGenerateQuery() { + $this->urlGenerator->expects($this->once()) + ->method('generateFromRoute') + ->with('test_route_1', array(), array('query' => array('test' => 'value')) + $this->defaultOptions) + ->will($this->returnValue( + '/test-route-1?test=value' + )); + + $url = new Url('test_route_1', array(), array( + 'query' => array('test' => 'value'), + )); + $url->setUrlGenerator($this->urlGenerator); + $result = $this->linkGenerator->generate('Test', $url); + $this->assertTag(array( + 'tag' => 'a', + 'attributes' => array( + 'href' => '/test-route-1?test=value', + ), + ), $result); + } + + /** + * Tests the link method with passed query options via parameters. + * + * @see \Drupal\Core\Render\LinkGenerator::generate() + */ + public function testGenerateParametersAsQuery() { + $this->urlGenerator->expects($this->once()) + ->method('generateFromRoute') + ->with('test_route_1', array('test' => 'value'), $this->defaultOptions) + ->will($this->returnValue( + '/test-route-1?test=value' + )); + + $url = new Url('test_route_1', array('test' => 'value'), array()); + $url->setUrlGenerator($this->urlGenerator); + $result = $this->linkGenerator->generate('Test', $url); + $this->assertTag(array( + 'tag' => 'a', + 'attributes' => array( + 'href' => '/test-route-1?test=value', + ), + ), $result); + } + + /** + * Tests the link method with arbitrary passed options. + * + * @see \Drupal\Core\Render\LinkGenerator::generate() + */ + public function testGenerateOptions() { + $this->urlGenerator->expects($this->once()) + ->method('generateFromRoute') + ->with('test_route_1', array(), array('key' => 'value') + $this->defaultOptions) + ->will($this->returnValue( + '/test-route-1?test=value' + )); + + $url = new Url('test_route_1', array(), array( + 'key' => 'value', + )); + $url->setUrlGenerator($this->urlGenerator); + $result = $this->linkGenerator->generate('Test', $url); + $this->assertTag(array( + 'tag' => 'a', + 'attributes' => array( + 'href' => '/test-route-1?test=value', + ), + ), $result); + } + + /** + * Tests the link method with a script tab. + * + * @see \Drupal\Core\Render\LinkGenerator::generate() + */ + public function testGenerateXss() { + $this->urlGenerator->expects($this->once()) + ->method('generateFromRoute') + ->with('test_route_4', array(), $this->defaultOptions) + ->will($this->returnValue( + '/test-route-4' + )); + + // Test that HTML link text is escaped by default. + $url = new Url('test_route_4'); + $url->setUrlGenerator($this->urlGenerator); + $result = $this->linkGenerator->generate("", $url); + $this->assertNotTag(array( + 'tag' => 'a', + 'attributes' => array('href' => '/test-route-4'), + 'child' => array( + 'tag' => 'script', + ), + ), $result); + } + + /** + * Tests the link method with html. + * + * @see \Drupal\Core\Render\LinkGenerator::generate() + */ + public function testGenerateWithHtml() { + $this->urlGenerator->expects($this->at(0)) + ->method('generateFromRoute') + ->with('test_route_5', array(), $this->defaultOptions) + ->will($this->returnValue( + '/test-route-5' + )); + $this->urlGenerator->expects($this->at(1)) + ->method('generateFromRoute') + ->with('test_route_5', array(), array('html' => TRUE) + $this->defaultOptions) + ->will($this->returnValue( + '/test-route-5' + )); + + // Test that HTML tags are stripped from the 'title' attribute. + $url = new Url('test_route_5', array(), array( + 'attributes' => array('title' => 'HTML Tooltip'), + )); + $url->setUrlGenerator($this->urlGenerator); + $result = $this->linkGenerator->generate('Test', $url); + $this->assertTag(array( + 'tag' => 'a', + 'attributes' => array( + 'href' => '/test-route-5', + 'title' => 'HTML Tooltip', + ), + ), $result); + + // Test that the 'html' option allows unsanitized HTML link text. + $url = new Url('test_route_5', array(), array('html' => TRUE)); + $url->setUrlGenerator($this->urlGenerator); + $result = $this->linkGenerator->generate('HTML output', $url); + $this->assertTag(array( + 'tag' => 'a', + 'attributes' => array('href' => '/test-route-5'), + 'child' => array( + 'tag' => 'em', + ), + ), $result); + } + + /** + * Tests the active class on the link method. + * + * @see \Drupal\Core\Render\LinkGenerator::generate() + * + * @todo Test that the active class is added on the front page when generating + * links to the front page when drupal_is_front_page() is converted to a + * service. + */ + public function testGenerateActive() { + $this->urlGenerator->expects($this->exactly(5)) + ->method('generateFromRoute') + ->will($this->returnValueMap(array( + array('test_route_1', array(), FALSE, '/test-route-1'), + array('test_route_3', array(), FALSE, '/test-route-3'), + array('test_route_4', array('object' => '1'), FALSE, '/test-route-4/1'), + ))); + + $this->urlGenerator->expects($this->exactly(4)) + ->method('getPathFromRoute') + ->will($this->returnValueMap(array( + array('test_route_1', array(), 'test-route-1'), + array('test_route_3', array(), 'test-route-3'), + array('test_route_4', array('object' => '1'), 'test-route-4/1'), + ))); + + $this->moduleHandler->expects($this->exactly(5)) + ->method('alter'); + + // Render a link. + $url = new Url('test_route_1', array(), array('set_active_class' => TRUE)); + $url->setUrlGenerator($this->urlGenerator); + $result = $this->linkGenerator->generate('Test', $url); + $this->assertTag(array( + 'tag' => 'a', + 'attributes' => array('data-drupal-link-system-path' => 'test-route-1'), + ), $result); + + // Render a link with the set_active_class option disabled. + $url = new Url('test_route_1', array(), array('set_active_class' => FALSE)); + $url->setUrlGenerator($this->urlGenerator); + $result = $this->linkGenerator->generate('Test', $url); + $this->assertNotTag(array( + 'tag' => 'a', + 'attributes' => array('data-drupal-link-system-path' => 'test-route-1'), + ), $result); + + // Render a link with an associated language. + $url = new Url('test_route_1', array(), array( + 'language' => new Language(array('id' => 'de')), + 'set_active_class' => TRUE, + )); + $url->setUrlGenerator($this->urlGenerator); + $result = $this->linkGenerator->generate('Test', $url); + $this->assertTag(array( + 'tag' => 'a', + 'attributes' => array( + 'data-drupal-link-system-path' => 'test-route-1', + 'hreflang' => 'de', + ), + ), $result); + + // Render a link with a query parameter. + $url = new Url('test_route_3', array(), array( + 'query' => array('value' => 'example_1'), + 'set_active_class' => TRUE, + )); + $url->setUrlGenerator($this->urlGenerator); + $result = $this->linkGenerator->generate('Test', $url); + $this->assertTag(array( + 'tag' => 'a', + 'attributes' => array( + 'data-drupal-link-system-path' => 'test-route-3', + 'data-drupal-link-query' => 'regexp:/.*value.*example_1.*/', + ), + ), $result); + + // Render a link with route parameters and a query parameter. + $url = new Url('test_route_4', array('object' => '1'), array( + 'query' => array('value' => 'example_1'), + 'set_active_class' => TRUE, + )); + $url->setUrlGenerator($this->urlGenerator); + $result = $this->linkGenerator->generate('Test', $url); + $this->assertTag(array( + 'tag' => 'a', + 'attributes' => array( + 'data-drupal-link-system-path' => 'test-route-4/1', + 'data-drupal-link-query' => 'regexp:/.*value.*example_1.*/', + ), + ), $result); + } + +} + +} +namespace { + // @todo Remove this once there is a service for drupal_is_front_page(). + if (!function_exists('drupal_is_front_page')) { + function drupal_is_front_page() { + return FALSE; + } + } +}