Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Angular] *scRouterLink breaks link generation #815

Merged
merged 4 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';

import { generalLinkField as eeLinkData } from '../testData/ee-data';
import { RouterLinkDirective } from './router-link.directive';
import { RouterLinkDirective, stripQueryParams } from './router-link.directive';
import { RouterTestingModule } from '@angular/router/testing';
import { LinkField } from './rendering-field';

Expand Down Expand Up @@ -164,6 +164,19 @@ describe('<a *scRouterLink />', () => {
expect(rendered.nativeElement.target).toBe('_blank');
expect(rendered.nativeElement.title).toBe('footip');
});

it('should strip query string from href', () => {
const field = {
href: '/lorem?sc_site=lorem_ipsum',
text: 'ipsum',
};
comp.field = field;
field.href = stripQueryParams(field.href);
fixture.detectChanges();

const rendered = de.query(By.css('a'));
expect(rendered.nativeElement.href).toBe('http://localhost:9876/lorem');
CobyPear marked this conversation as resolved.
Show resolved Hide resolved
});
});

@Component({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { Router } from '@angular/router';
import { LinkDirective } from './link.directive';
import { LinkField } from './rendering-field';

export const stripQueryParams = (href: string) => href.split('?')[0];

@Directive({ selector: '[scRouterLink]' })
export class RouterLinkDirective extends LinkDirective {
@Input('scRouterLinkEditable') editable = true;
Expand All @@ -36,8 +38,9 @@ export class RouterLinkDirective extends LinkDirective {
this.renderer.setAttribute(node, key, propValue);

if (key === 'href') {
const path = stripQueryParams(propValue);
this.renderer.listen(node, 'click', (event) => {
this.router.navigate([propValue]);
this.router.navigate([path]);
event.preventDefault();
});
}
Expand Down