Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2k
support target and popup attributes in pseudo-html text links#1726
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -282,11 +282,36 @@ var SPLIT_TAGS = /(<[^<>]*>)/; | ||
| var ONE_TAG = /<(\/?)([^ >]*)(\s+(.*))?>/i; | ||
| // Style and href: pull them out of either single or double quotes. | ||
| // Because we hack in other attributes with style (sub & sup), drop any trailing | ||
| // semicolon in user-supplied styles so we can consistently append the tag-dependent style | ||
| /* | ||
| * style and href: pull them out of either single or double quotes. Also | ||
| * - target: (_blank|_self|_parent|_top|framename) | ||
| * note that you can't use target to get a popup but if you use popup, | ||
| * a `framename` will be passed along as the name of the popup window. | ||
| * per the spec, cannot contain whitespace. | ||
| * for backward compatibility we default to '_blank' | ||
| * - popup: a custom one for us to enable popup (new window) links. String | ||
| * for window.open -> strWindowFeatures, like 'menubar=yes,width=500,height=550' | ||
| * note that at least in Chrome, you need to give at least one property | ||
| * in this string or the page will open in a new tab anyway. We follow this | ||
| * convention and will not make a popup if this string is empty. | ||
| * per the spec, cannot contain whitespace. | ||
| * | ||
| * Because we hack in other attributes with style (sub & sup), drop any trailing | ||
| * semicolon in user-supplied styles so we can consistently append the tag-dependent style | ||
| */ | ||
| var STYLEMATCH = /(^|[\s"'])style\s*=\s*("([^"]*);?"|'([^']*);?')/i; | ||
| var HREFMATCH = /(^|[\s"'])href\s*=\s*("([^"]*)"|'([^']*)')/i; | ||
| var TARGETMATCH = /(^|[\s"'])target\s*=\s*("([^"\s]*)"|'([^'\s]*)')/i; | ||
| var POPUPMATCH = /(^|[\s"'])popup\s*=\s*("([^"\s]*)"|'([^'\s]*)')/i; | ||
| // dedicated matcher for these quoted regexes, that can return their results | ||
| // in two different places | ||
| function getQuotedMatch(_str, re) { | ||
| if(!_str) return null; | ||
| var match = _str.match(re); | ||
| return match && (match[3] || match[4]); | ||
| } | ||
| var COLORMATCH = /(^|;)\s*color:/; | ||
| @@ -297,14 +322,14 @@ exports.plainText = function(_str) { | ||
| }; | ||
| function replaceFromMapObject(_str, list) { | ||
| var out = _str || ''; | ||
| if(!_str) return ''; | ||
CollaboratorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🐎 no need to loop over all these replacements on an empty string | ||
| for(var i = 0; i < list.length; i++) { | ||
| var item = list[i]; | ||
| out = out.replace(item.regExp, item.sub); | ||
| _str = _str.replace(item.regExp, item.sub); | ||
| } | ||
| return out; | ||
| return _str; | ||
| } | ||
| function convertEntities(_str) { | ||
| @@ -354,8 +379,7 @@ function convertToSVG(_str) { | ||
| // anchor is the only tag that doesn't turn into a tspan | ||
| if(tag === 'a') { | ||
| var hrefMatch = extra && extra.match(HREFMATCH); | ||
| var href = hrefMatch && (hrefMatch[3] || hrefMatch[4]); | ||
| var href = getQuotedMatch(extra, HREFMATCH); | ||
| out = '<a'; | ||
| @@ -364,7 +388,34 @@ function convertToSVG(_str) { | ||
| var dummyAnchor = document.createElement('a'); | ||
| dummyAnchor.href = href; | ||
| if(PROTOCOLS.indexOf(dummyAnchor.protocol) !== -1) { | ||
| out += ' xlink:show="new" xlink:href="' + encodeForHTML(href) + '"'; | ||
| href = encodeForHTML(href); | ||
| // look for target and popup specs | ||
| var target = encodeForHTML(getQuotedMatch(extra, TARGETMATCH)) || '_blank'; | ||
| var popup = encodeForHTML(getQuotedMatch(extra, POPUPMATCH)); | ||
| /* | ||
| * xlink:show is for backward compatibility only, | ||
| * newer browsers allow target just like html links. | ||
| */ | ||
| var xlinkShow = (target === '_blank' || target.charAt(0) !== '_') ? | ||
| 'new' : 'replace'; | ||
| out += ' xlink:show="' + xlinkShow + '" target="' + target + | ||
| '" xlink:href="' + href + '"'; | ||
| if(popup) { | ||
| /* | ||
| * Add the window.open call to create a popup | ||
| * Even when popup is specified, we leave the original | ||
| * href and target in place in case javascript is | ||
| * unavailable in this context (like downloaded svg) | ||
| * and for accessibility and so users can see where | ||
| * the link will lead. | ||
| */ | ||
| out += ' onclick="window.open(\'' + href + '\',\'' + target + | ||
| '\',\'' + popup + '\');return false;"'; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| @@ -380,8 +431,7 @@ function convertToSVG(_str) { | ||
| // now add style, from both the tag name and any extra css | ||
| // Most of the svg css that users will care about is just like html, | ||
| // but font color is different (uses fill). Let our users ignore this. | ||
| var cssMatch = extra && extra.match(STYLEMATCH); | ||
| var css = cssMatch && (cssMatch[3] || cssMatch[4]); | ||
| var css = getQuotedMatch(extra, STYLEMATCH); | ||
| if(css) { | ||
| css = encodeForHTML(css.replace(COLORMATCH, '$1 fill:')); | ||
| if(tagStyle) css += ';' + tagStyle; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems to be a point of browser inconsistency whether it still works with whitespace... alternatively I guess we could accept whitespace in the input but strip it from the output, but this was easier.