Andreas Traczyk | 43c0823 | 2018-10-31 13:42:09 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2016 SoapBox Innovations Inc. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | * of this software and associated documentation files (the "Software"), to deal |
| 6 | * in the Software without restriction, including without limitation the rights |
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | * copies of the Software, and to permit persons to whom the Software is |
| 9 | * furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | * THE SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | 'use strict'; |
| 24 | |
| 25 | ;(function (window, linkify) { |
| 26 | var linkifyString = function (linkify) { |
| 27 | 'use strict'; |
| 28 | |
| 29 | /** |
| 30 | Convert strings of text into linkable HTML text |
| 31 | */ |
| 32 | |
| 33 | var tokenize = linkify.tokenize; |
| 34 | var options = linkify.options; |
| 35 | var Options = options.Options; |
| 36 | |
| 37 | |
| 38 | function escapeText(text) { |
| 39 | return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'); |
| 40 | } |
| 41 | |
| 42 | function escapeAttr(href) { |
| 43 | return href.replace(/"/g, '"'); |
| 44 | } |
| 45 | |
| 46 | function attributesToString(attributes) { |
| 47 | if (!attributes) { |
| 48 | return ''; |
| 49 | } |
| 50 | var result = []; |
| 51 | |
| 52 | for (var attr in attributes) { |
| 53 | var val = attributes[attr] + ''; |
| 54 | result.push(attr + '="' + escapeAttr(val) + '"'); |
| 55 | } |
| 56 | return result.join(' '); |
| 57 | } |
| 58 | |
| 59 | function linkifyStr(str) { |
| 60 | var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; |
| 61 | |
| 62 | opts = new Options(opts); |
| 63 | |
| 64 | var tokens = tokenize(str); |
| 65 | var result = []; |
| 66 | |
| 67 | for (var i = 0; i < tokens.length; i++) { |
| 68 | var token = tokens[i]; |
| 69 | |
| 70 | if (token.type === 'nl' && opts.nl2br) { |
| 71 | result.push('<br>\n'); |
| 72 | continue; |
| 73 | } else if (!token.isLink || !opts.check(token)) { |
| 74 | result.push(escapeText(token.toString())); |
| 75 | continue; |
| 76 | } |
| 77 | |
| 78 | var _opts$resolve = opts.resolve(token); |
| 79 | |
| 80 | var formatted = _opts$resolve.formatted; |
| 81 | var formattedHref = _opts$resolve.formattedHref; |
| 82 | var tagName = _opts$resolve.tagName; |
| 83 | var className = _opts$resolve.className; |
| 84 | var target = _opts$resolve.target; |
| 85 | var attributes = _opts$resolve.attributes; |
| 86 | |
| 87 | |
| 88 | var link = '<' + tagName + ' href="' + escapeAttr(formattedHref) + '"'; |
| 89 | |
| 90 | if (className) { |
| 91 | link += ' class="' + escapeAttr(className) + '"'; |
| 92 | } |
| 93 | |
| 94 | if (target) { |
| 95 | link += ' target="' + escapeAttr(target) + '"'; |
| 96 | } |
| 97 | |
| 98 | if (attributes) { |
| 99 | link += ' ' + attributesToString(attributes); |
| 100 | } |
| 101 | |
| 102 | link += '>' + escapeText(formatted) + '</' + tagName + '>'; |
| 103 | result.push(link); |
| 104 | } |
| 105 | |
| 106 | return result.join(''); |
| 107 | } |
| 108 | |
| 109 | if (!String.prototype.linkify) { |
| 110 | String.prototype.linkify = function (opts) { |
| 111 | return linkifyStr(this, opts); |
| 112 | }; |
| 113 | } |
| 114 | |
| 115 | return linkifyStr; |
| 116 | }(linkify); |
| 117 | window.linkifyStr = linkifyString; |
| 118 | })(window, linkify); |