Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,31 @@ function uuid4() {
}

/**
* Returns a simple, child-less string representation of a DOM element
* e.g. [HTMLElement] => <input class="btn" />
* Returns a simple, query-selector representation of a DOM element
* e.g. [HTMLElement] => input#foo.btn[name=baz]
* @param HTMLElement
*/
function htmlElementAsString(elem) {
var out = ['<'];
var out = [];
out.push(elem.tagName.toLowerCase());
var attrWhitelist = ['id', 'type', 'name', 'value', 'class', 'placeholder', 'title', 'alt'];

if (elem.id) {
out.push('#' + elem.id);
}
var classes, i;
if (elem.className) {
classes = elem.className.split(' ');
for (i = 0; i < classes.length; i++) {
out.push('.' + classes[i]);
}
}
var attrWhitelist = ['type', 'name', 'value', 'placeholder', 'title', 'alt'];
each(attrWhitelist, function(index, key) {
var attr = elem.getAttribute(key);
if (attr) {
out.push(' ' + key + '="' + attr + '"');
out.push('[' + key + '="' + attr + '"]');
}
});
out.push(' />');
return out.join('');
}

Expand Down
6 changes: 3 additions & 3 deletions test/integration/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ describe('integration', function () {

assert.equal(breadcrumbs[0].type, 'ui_event');
// NOTE: attributes re-ordered. should this be expected?
assert.equal(breadcrumbs[0].data.target, '<input id="bar" name="foo" placeholder="lol" />');
assert.equal(breadcrumbs[0].data.target, 'input#bar[name="foo"][placeholder="lol"]');
assert.equal(breadcrumbs[0].data.type, 'click');
}
);
Expand Down Expand Up @@ -482,7 +482,7 @@ describe('integration', function () {

assert.equal(breadcrumbs[0].type, 'ui_event');
// NOTE: attributes re-ordered. should this be expected?
assert.equal(breadcrumbs[0].data.target, '<input id="bar" name="foo" placeholder="lol" />');
assert.equal(breadcrumbs[0].data.target, 'input#bar[name="foo"][placeholder="lol"]');
assert.equal(breadcrumbs[0].data.type, 'click');
}
);
Expand Down Expand Up @@ -530,7 +530,7 @@ describe('integration', function () {

assert.equal(breadcrumbs[0].type, 'ui_event');
// NOTE: attributes re-ordered. should this be expected?
assert.equal(breadcrumbs[0].data.target, '<div id="a" />');
assert.equal(breadcrumbs[0].data.target, 'div#a');
assert.equal(breadcrumbs[0].data.type, 'click');
}
);
Expand Down
10 changes: 5 additions & 5 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,26 +125,26 @@ describe('utils', function () {
it('should work', function () {
assert.equal(htmlElementAsString({
tagName: 'INPUT',
id: 'the-username',
className: 'form-control',
getAttribute: function (key){
return {
id: 'the-username',
name: 'username',
class: 'form-control',
placeholder: 'Enter your username'
}[key];
}
}), '<input id="the-username" name="username" class="form-control" placeholder="Enter your username" />');
}), 'input#the-username.form-control[name="username"][placeholder="Enter your username"]');

assert.equal(htmlElementAsString({
tagName: 'IMG',
id: 'image-3',
getAttribute: function (key){
return {
id: 'image-3',
title: 'A picture of an apple',
'data-something': 'This should be ignored' // skipping data-* attributes in first implementation
}[key];
}
}), '<img id="image-3" title="A picture of an apple" />');
}), 'img#image-3[title="A picture of an apple"]');
});
});

Expand Down