Skip to content

Commit 0db9f21

Browse files
committed
Merge pull request #561 from getsentry/fix-target-bugs
Fix htmlElementAsString choking on bad input args
2 parents 386daaf + 0ac161f commit 0db9f21

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/utils.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,18 +198,24 @@ function htmlTreeAsString(elem) {
198198
*/
199199
function htmlElementAsString(elem) {
200200
var out = [],
201+
className,
201202
classes,
202203
key,
203204
attr,
204205
i;
205206

207+
if (!elem || !elem.tagName) {
208+
return '';
209+
}
210+
206211
out.push(elem.tagName.toLowerCase());
207212
if (elem.id) {
208213
out.push('#' + elem.id);
209214
}
210215

211-
if (elem.className) {
212-
classes = elem.className.split(' ');
216+
className = elem.className;
217+
if (className && isString(className)) {
218+
classes = className.split(' ');
213219
for (i = 0; i < classes.length; i++) {
214220
out.push('.' + classes[i]);
215221
}

test/utils.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,32 @@ describe('utils', function () {
201201
}
202202
}), 'img#image-3[title="A picture of an apple"]');
203203
});
204+
205+
it('should return an empty string if the input element is falsy', function () {
206+
assert.equal(htmlElementAsString(null), '');
207+
assert.equal(htmlElementAsString(0), '');
208+
assert.equal(htmlElementAsString(undefined), '');
209+
});
210+
211+
it('should return an empty string if the input element has no tagName property', function () {
212+
assert.equal(htmlElementAsString({
213+
id: 'the-username',
214+
className: 'form-control'
215+
}), '');
216+
});
217+
218+
it('should gracefully handle when className is not a string (e.g. SVGAnimatedString', function () {
219+
assert.equal(htmlElementAsString({
220+
tagName: 'INPUT',
221+
id: 'the-username',
222+
className: {}, // not a string
223+
getAttribute: function (key){
224+
return {
225+
name: 'username'
226+
}[key];
227+
}
228+
}), 'input#the-username[name="username"]');
229+
});
204230
});
205231

206232
describe('parseUrl', function () {

0 commit comments

Comments
 (0)