Skip to content

Commit

Permalink
EWPP-1860: Prepending inpage nav ids with ref-.
Browse files Browse the repository at this point in the history
  • Loading branch information
upchuk committed Feb 1, 2022
1 parent 2606497 commit fd1e473
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
7 changes: 7 additions & 0 deletions js/inpage_navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@
return false;
}

if (!slug[0].match("^[a-zA-Z]*$")) {
// In case the slug doesn't start with letters, append a string to
// ensure that the resulting slug is always a valid query selector.
slug = 'ref-' + slug;
originalSlug = slug;
}

// If an element with the generated slug as ID already exists, mark the slug as seen.
if (!this.seenIds.hasOwnProperty(slug) && document.querySelector('#' + slug)) {
this.seenIds[slug] = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@
{# An element that will generate an ID that can represent a property of the Object prototype. #}
<h3 class="heading">Length</h3>

<h3 class="heading">2022, a new year</h3>

</div>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

/**
* Test Inpage navigation block plugin.
*
* @group batch3
*/
class InPageNavigationBlockTest extends WebDriverTestBase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function testLibrary(): void {
// container.
// CSS selector uses "descendent-or-self" as prefix, so we need to use
// XPath to exclude the container from the selection.
$this->assertCount(10, $container->findAll('xpath', '//*[@id]'));
$this->assertCount(11, $container->findAll('xpath', '//*[@id]'));

$inner = $container->find('xpath', '/div[@data-inpage-navigation-source-area="h3"]');
// Since an element with ID "details" already exists in the page, the
Expand All @@ -91,10 +91,12 @@ public function testLibrary(): void {
// easier to assert the content.
$this->assertEquals('Title with <strong>HTML tags</strong>', $container->find('xpath', '/h3[@id="title-with-html-tags"]')->getHtml());

// Last 3 assertions for the ID generation.
// Last 4 assertions for the ID generation.
$this->assertEquals('strip--unwanted-characters', $container->find('css', '.strip')->getAttribute('id'));
$this->assertEquals('constructor', $container->find('xpath', '/h3[@class="heading"][text()="Reserved keyword"]')->getAttribute('id'));
$this->assertEquals('length', $container->find('xpath', '/h3[@class="heading"][text()="Length"]')->getAttribute('id'));
// The heading starting with a non-alpha character gets a ref- prepended.
$this->assertEquals('ref-2022-a-new-year', $container->find('xpath', '/h3[@class="heading"][text()="2022, a new year"]')->getAttribute('id'));

$navigation = $assert_session->elementExists('css', '#block-inpage-navigation nav[data-ecl-inpage-navigation]');
$assert = new InPageNavigationAssert();
Expand Down Expand Up @@ -137,6 +139,10 @@ public function testLibrary(): void {
'label' => 'Length',
'href' => '#length',
],
[
'label' => '2022, a new year',
'href' => '#ref-2022-a-new-year',
],
],
];
$assert->assertPattern($expected, $navigation->getOuterHtml());
Expand Down

0 comments on commit fd1e473

Please sign in to comment.