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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
support target and popup attributes in pseudo-html text links #1726
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 |
---|---|---|
|
@@ -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 ''; | ||
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; | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.