-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle dynamic to property for links
- Loading branch information
Showing
14 changed files
with
163 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import isAbsoluteUrl from '../utils/is-absolute-url' | ||
|
||
const setAttribute = (attrs, name, value) => { | ||
if (attrs[name] === undefined) { | ||
attrs[name] = value | ||
} | ||
} | ||
|
||
const HTTP_RE = /^https?:\/\// | ||
|
||
export default { | ||
name: 'SaberLink', | ||
|
||
functional: true, | ||
|
||
render(h, { data, children, parent }) { | ||
const attrs = { ...data.attrs } | ||
const isExternal = typeof attrs.to === 'string' && isAbsoluteUrl(attrs.to) | ||
|
||
if (isExternal) { | ||
if (HTTP_RE.test(attrs.to)) { | ||
setAttribute(attrs, 'rel', 'noopener noreferrer') | ||
|
||
if (attrs.openLinkInNewTab !== false) { | ||
setAttribute(attrs, 'target', '_blank') | ||
} | ||
} | ||
attrs.href = attrs.to | ||
delete attrs.to | ||
} else { | ||
if (typeof attrs.to === 'string') { | ||
attrs.to = parent.$saber.getPageLink(attrs.to) | ||
} else { | ||
const { route } = parent.$router.resolve(attrs.to) | ||
attrs.to = parent.$saber.getPageLink(route.fullPath) | ||
} | ||
} | ||
|
||
delete attrs.openLinkInNewTab | ||
|
||
return h( | ||
isExternal ? 'a' : 'router-link', | ||
{ | ||
...data, | ||
attrs | ||
}, | ||
children | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export default url => { | ||
if (typeof url !== 'string') { | ||
throw new TypeError(`Expected a \`string\`, got \`${typeof url}\``) | ||
} | ||
|
||
// Don't match Windows paths `c:\` | ||
if (/^[a-zA-Z]:\\/.test(url)) { | ||
return false | ||
} | ||
|
||
// Scheme: https://tools.ietf.org/html/rfc3986#section-3.1 | ||
// Absolute URL: https://tools.ietf.org/html/rfc3986#section-4.3 | ||
return /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(url) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,10 +17,10 @@ test('basic', async () => { | |
<saber-link :to="foo">foo</saber-link> | ||
`) | ||
expect(html).toBe(` | ||
<saber-link :to="$saber.getPageLink('foo')">foo</saber-link> | ||
<a target="_blank" rel="noopener noreferrer" href="https://example.com">foo</a> | ||
<a href="mailto:[email protected]">foo</a> | ||
<saber-link :to="$saber.getPageLink('/foo')">foo</saber-link> | ||
<saber-link to="foo">foo</saber-link> | ||
<saber-link to="https://example.com">foo</saber-link> | ||
<saber-link to="mailto:[email protected]">foo</saber-link> | ||
<saber-link to="/foo">foo</saber-link> | ||
<saber-link :to="foo">foo</saber-link> | ||
`) | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters