Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat(location): add configurable attr to force html5mode link rewriting #14976

Closed
wants to merge 1 commit into from

Conversation

onlywei
Copy link
Contributor

@onlywei onlywei commented Aug 1, 2016

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
Feature

What is the current behavior? (You can also link to an open issue here)
When rewriteLinks is disabled in Location's html5mode, all links are treated as if they should reload from the server.

Also see this discussion: #14959

What is the new behavior (if this is a feature change)?
This allows the user to add an ng-noserver attribute to the <a> tag, like this:
<a href="somewhere" ng-noserver>click me</a> to explicitly mark this link as one that should not reload from server, i.e. to use angular javascript routing instead.

Does this PR introduce a breaking change?
No

Please check if the PR fulfills these requirements

Other information:

See this discussion: #14959

When using html5mode, sometimes it is necessary to not use the tag
in order to support SVG icons. An example of this is in the discussion
linked above.

When we do this, sometimes we also want to disable "rewriteLinks" so that
we can still leave the Angular application via navigation.

This feature allows the user to explicitly mark an tag as a link that
should not refresh from server, even if rewriteLinks is disabled.

@@ -877,6 +879,8 @@ function $LocationProvider() {
if (elm[0] === $rootElement[0] || !(elm = elm.parent())[0]) return;
}

if (!html5Mode.rewriteLinks && isUndefined(elm.attr('ng-noserver'))) return;
Copy link
Member

@gkalpak gkalpak Aug 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not recognize prefixed forms (such as x-ng-... or data-ng-...) - and I think this is undesirable.

@gkalpak
Copy link
Member

gkalpak commented Aug 5, 2016

I think noserver is not a very intuitive name. Also URL-rewriting doesn't guarantee that no request will be done to the server afaict.

@onlywei
Copy link
Contributor Author

onlywei commented Aug 5, 2016

Thanks @gkalpak, I was hoping to get this kind of useful feedback. I've updated the code. Please review at your leisure.

* - **rewriteLinks** - `{boolean|String}` - (default: `true`) When html5Mode is enabled,
* enables/disables url rewriting for relative links. If set to a string, url rewriting will
* only happen on links with an attribute that matches the given string. For example, if set
* to "internalLink", then the URL will only be rewritten for `<a internal-link>` links.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no attribute name (de)normalization going on. In order to match <a internal-link> you need to set rewriteLinks to 'internal-link' (not 'internalLink').

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@gkalpak gkalpak changed the title feat(location): add ng-noserver attr to force html5mode link rewriting feat(location): add configurable attr to force html5mode link rewriting Aug 7, 2016
@gkalpak
Copy link
Member

gkalpak commented Aug 7, 2016

Btw, docs need to be updated in other places (e.g. the Using $location section of the developer guide).
Adding it here as a reminder, but feel free to defer that until the implementation has finalized.

@onlywei onlywei force-pushed the force-link-rewrite branch from 36e7a6d to 368d9bf Compare August 7, 2016 16:08
module(function($locationProvider) {
$locationProvider.html5Mode({
enabled: 'duh',
requireBase: 'probably',
rewriteLinks: 'nope'
rewriteLinks: false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change doesn't test what it should. You need to supply an unsupported type and verify that it didn't get copied to the actual config.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

See this discussion: angular#14959

When using html5mode, sometimes it is necessary to not use the <base> tag
in order to support SVG icons. An example of this is in the discussion
linked above.

When we do this, sometimes we also want to disable "rewriteLinks" so that
we can still leave the Angular application via navigation.

This feature allows the user to explicitly mark an <a> tag as a link that
should not refresh from server, even if rewriteLinks is disabled.
@onlywei onlywei force-pushed the force-link-rewrite branch from 368d9bf to 5eae33d Compare August 7, 2016 19:06
@gkalpak
Copy link
Member

gkalpak commented Aug 8, 2016

LGTM

@gkalpak
Copy link
Member

gkalpak commented Aug 8, 2016

I'll let @petebacondarwin do the final LGTM, since he is the $location master 😃

@onlywei
Copy link
Contributor Author

onlywei commented Aug 19, 2016

@petebacondarwin Ping. Please review when convenient.

@petebacondarwin
Copy link
Contributor

LGTM

@gkalpak gkalpak closed this in 3d686a9 Aug 22, 2016
gkalpak pushed a commit that referenced this pull request Aug 22, 2016
… attribute

In HTML5 mode, links can now be selectively rewritten, by setting `mode.rewriteLinks` to a string
(denoting an attribute name). Anchor elements that have the specified attribute will be rewritten,
while other links will remain untouched.

This can be useful in situations where it is desirable to use HTML5 mode without a `<base>` tag, but
still support rewriting specific links only. See #14959 for more details on a possible usecase.

Closes #14976
petebacondarwin pushed a commit to petebacondarwin/angular.js that referenced this pull request Nov 21, 2016
… attribute

In HTML5 mode, links can now be selectively rewritten, by setting `mode.rewriteLinks` to a string
(denoting an attribute name). Anchor elements that have the specified attribute will be rewritten,
while other links will remain untouched.

This can be useful in situations where it is desirable to use HTML5 mode without a `<base>` tag, but
still support rewriting specific links only. See angular#14959 for more details on a possible usecase.

Closes angular#14976
petebacondarwin pushed a commit to petebacondarwin/angular.js that referenced this pull request Nov 21, 2016
… attribute

In HTML5 mode, links can now be selectively rewritten, by setting `mode.rewriteLinks` to a string
(denoting an attribute name). Anchor elements that have the specified attribute will be rewritten,
while other links will remain untouched.

This can be useful in situations where it is desirable to use HTML5 mode without a `<base>` tag, but
still support rewriting specific links only. See angular#14959 for more details on a possible usecase.

Closes angular#14976
petebacondarwin pushed a commit to petebacondarwin/angular.js that referenced this pull request Nov 21, 2016
… attribute

In HTML5 mode, links can now be selectively rewritten, by setting `mode.rewriteLinks` to a string
(denoting an attribute name). Anchor elements that have the specified attribute will be rewritten,
while other links will remain untouched.

This can be useful in situations where it is desirable to use HTML5 mode without a `<base>` tag, but
still support rewriting specific links only. See angular#14959 for more details on a possible usecase.

Closes angular#14976
petebacondarwin pushed a commit to petebacondarwin/angular.js that referenced this pull request Nov 21, 2016
… attribute

In HTML5 mode, links can now be selectively rewritten, by setting `mode.rewriteLinks` to a string
(denoting an attribute name). Anchor elements that have the specified attribute will be rewritten,
while other links will remain untouched.

This can be useful in situations where it is desirable to use HTML5 mode without a `<base>` tag, but
still support rewriting specific links only. See angular#14959 for more details on a possible usecase.

Closes angular#14976
petebacondarwin pushed a commit to petebacondarwin/angular.js that referenced this pull request Nov 21, 2016
… attribute

In HTML5 mode, links can now be selectively rewritten, by setting `mode.rewriteLinks` to a string
(denoting an attribute name). Anchor elements that have the specified attribute will be rewritten,
while other links will remain untouched.

This can be useful in situations where it is desirable to use HTML5 mode without a `<base>` tag, but
still support rewriting specific links only. See angular#14959 for more details on a possible usecase.

Closes angular#14976
petebacondarwin pushed a commit that referenced this pull request Nov 23, 2016
… attribute

In HTML5 mode, links can now be selectively rewritten, by setting `mode.rewriteLinks` to a string
(denoting an attribute name). Anchor elements that have the specified attribute will be rewritten,
while other links will remain untouched.

This can be useful in situations where it is desirable to use HTML5 mode without a `<base>` tag, but
still support rewriting specific links only. See #14959 for more details on a possible usecase.

Closes #14976
petebacondarwin pushed a commit that referenced this pull request Nov 24, 2016
… attribute

In HTML5 mode, links can now be selectively rewritten, by setting `mode.rewriteLinks` to a string
(denoting an attribute name). Anchor elements that have the specified attribute will be rewritten,
while other links will remain untouched.

This can be useful in situations where it is desirable to use HTML5 mode without a `<base>` tag, but
still support rewriting specific links only. See #14959 for more details on a possible usecase.

Closes #14976
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants