-
Notifications
You must be signed in to change notification settings - Fork 1
/
sc-autolinker.html
82 lines (71 loc) · 2.26 KB
/
sc-autolinker.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE
The complete set of authors may be found at http://polymer.github.io/AUTHORS
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS
-->
<link rel="import" href="../polymer/polymer.html" />
<script src="../Autolinker.js/dist/Autolinker.min.js"></script>
<dom-module id="sc-autolinker">
<template>
<style>
#autolinked a, #autolinked a:-webkit-any-link {
color: inherit;
@apply(--sc-autolinker-link-style);
}
#autolinked {
@apply(--sc-autolinker-style);
}
</style>
<div id="autolinked"></div>
</template>
</dom-module>
<script>
(function () {
window.Polymer({
is: 'sc-autolinker',
properties: {
/**
* A string of text with links that should become clickable
*
* @default ''
*/
text: {
type: String,
observer: '_textChanged',
value: function() {
return this.text || this.textContent;
}
}
},
_textChanged: function() {
var text = (this.text || '').toString();
var native = window.cordova || window.capacitor;
// link handler
if (native && window.viewLink) {
text = text.replace(/href=['"]?(.*?)['"]/g,
'href="javascript://" onclick="window.viewLink(\'$1\');"');
}
// Otherwise simply enforce target="_blank"
else {
text = text
.replace(/(<[Aa] [^>]*?)target=[^ ]*([^>]*?>)/g, '$1$2')
.replace(/(<[Aa] )/g, '$1target="_blank" ');
console.log(text)
}
Polymer.dom(this.$.autolinked).innerHTML = Autolinker.link(text, {
replaceFn: function (match) {
if (native && window.viewLink) {
return '<a href="javascript://" onclick="window.viewLink(\''.concat(
match.getAnchorHref(), '\');">', match.getAnchorText(), '</a>');
}
}
});
this.fire('ready', {}, { bubbles: false });
}
});
})();
</script>