Skip to content

Commit c74fa42

Browse files
committed
create sanitizeHTML function and use for sourceattribution
1 parent 38327b2 commit c74fa42

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

src/lib/svg_text_utils.js

+62
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,68 @@ function buildSVGText(containerNode, str) {
625625
return hasLink;
626626
}
627627

628+
/*
629+
* sanitizeHTML: port of buildSVGText aimed at providing a clean subset of HTML
630+
* @param {string} str: the html string to clean
631+
* @returns {string}: a cleaned and normalized version of the input,
632+
* supporting only a small subset of html
633+
*/
634+
exports.sanitizeHTML = function sanitizeHTML(str) {
635+
str = str.replace(NEWLINES, ' ');
636+
637+
var rootNode = document.createElement('p');
638+
var currentNode = rootNode;
639+
var nodeStack = [];
640+
641+
var parts = str.split(SPLIT_TAGS);
642+
for(var i = 0; i < parts.length; i++) {
643+
var parti = parts[i];
644+
var match = parti.match(ONE_TAG);
645+
var tagType = match && match[2].toLowerCase();
646+
647+
if(tagType in TAG_STYLES) {
648+
if(match[1]) {
649+
if(nodeStack.length) {
650+
currentNode = nodeStack.pop();
651+
}
652+
} else {
653+
var extra = match[4];
654+
655+
var css = getQuotedMatch(extra, STYLEMATCH);
656+
var nodeAttrs = css ? {style: css} : {};
657+
658+
if(tagType === 'a') {
659+
var href = getQuotedMatch(extra, HREFMATCH);
660+
661+
if(href) {
662+
var dummyAnchor = document.createElement('a');
663+
dummyAnchor.href = href;
664+
if(PROTOCOLS.indexOf(dummyAnchor.protocol) !== -1) {
665+
nodeAttrs.href = encodeURI(decodeURI(href));
666+
var target = getQuotedMatch(extra, TARGETMATCH);
667+
if(target) {
668+
nodeAttrs.target = target;
669+
}
670+
}
671+
}
672+
}
673+
674+
var newNode = document.createElement(tagType);
675+
currentNode.appendChild(newNode);
676+
d3.select(newNode).attr(nodeAttrs);
677+
678+
currentNode = newNode;
679+
nodeStack.push(newNode);
680+
}
681+
} else {
682+
currentNode.appendChild(
683+
document.createTextNode(convertEntities(parti))
684+
);
685+
}
686+
}
687+
return rootNode.innerHTML;
688+
};
689+
628690
exports.lineCount = function lineCount(s) {
629691
return s.selectAll('tspan.line').size() || 1;
630692
};

src/plots/mapbox/layers.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'use strict';
1010

1111
var Lib = require('../../lib');
12+
var sanitizeHTML = require('../../lib/svg_text_utils').sanitizeHTML;
1213
var convertTextOpts = require('./convert_text_opts');
1314
var constants = require('./constants');
1415

@@ -278,7 +279,9 @@ function convertSourceOpts(opts) {
278279

279280
sourceOpts[field] = source;
280281

281-
if(opts.sourceattribution) sourceOpts.attribution = opts.sourceattribution;
282+
if(opts.sourceattribution) {
283+
sourceOpts.attribution = sanitizeHTML(opts.sourceattribution);
284+
}
282285

283286
return sourceOpts;
284287
}

0 commit comments

Comments
 (0)