Skip to content

Commit a490849

Browse files
committed
Use query selector style for ui target breadcrumb format
1 parent 5920f12 commit a490849

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

src/utils.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,21 +153,31 @@ function uuid4() {
153153
}
154154

155155
/**
156-
* Returns a simple, child-less string representation of a DOM element
157-
* e.g. [HTMLElement] => <input class="btn" />
156+
* Returns a simple, query-selector representation of a DOM element
157+
* e.g. [HTMLElement] => input#foo.btn[name=baz]
158158
* @param HTMLElement
159159
*/
160160
function htmlElementAsString(elem) {
161-
var out = ['<'];
161+
var out = [];
162162
out.push(elem.tagName.toLowerCase());
163-
var attrWhitelist = ['id', 'type', 'name', 'value', 'class', 'placeholder', 'title', 'alt'];
163+
164+
if (elem.id) {
165+
out.push('#' + elem.id);
166+
}
167+
var classes, i;
168+
if (elem.className) {
169+
classes = elem.className.split(' ');
170+
for (i = 0; i < classes.length; i++) {
171+
out.push('.' + classes[i]);
172+
}
173+
}
174+
var attrWhitelist = ['type', 'name', 'value', 'placeholder', 'title', 'alt'];
164175
each(attrWhitelist, function(index, key) {
165176
var attr = elem.getAttribute(key);
166177
if (attr) {
167-
out.push(' ' + key + '="' + attr + '"');
178+
out.push('[' + key + '="' + attr + '"]');
168179
}
169180
});
170-
out.push(' />');
171181
return out.join('');
172182
}
173183

test/integration/test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ describe('integration', function () {
441441

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

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

531531
assert.equal(breadcrumbs[0].type, 'ui_event');
532532
// NOTE: attributes re-ordered. should this be expected?
533-
assert.equal(breadcrumbs[0].data.target, '<div id="a" />');
533+
assert.equal(breadcrumbs[0].data.target, 'div#a');
534534
assert.equal(breadcrumbs[0].data.type, 'click');
535535
}
536536
);

test/utils.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,26 +125,26 @@ describe('utils', function () {
125125
it('should work', function () {
126126
assert.equal(htmlElementAsString({
127127
tagName: 'INPUT',
128+
id: 'the-username',
129+
className: 'form-control',
128130
getAttribute: function (key){
129131
return {
130-
id: 'the-username',
131132
name: 'username',
132-
class: 'form-control',
133133
placeholder: 'Enter your username'
134134
}[key];
135135
}
136-
}), '<input id="the-username" name="username" class="form-control" placeholder="Enter your username" />');
136+
}), 'input#the-username.form-control[name="username"][placeholder="Enter your username"]');
137137

138138
assert.equal(htmlElementAsString({
139139
tagName: 'IMG',
140+
id: 'image-3',
140141
getAttribute: function (key){
141142
return {
142-
id: 'image-3',
143143
title: 'A picture of an apple',
144144
'data-something': 'This should be ignored' // skipping data-* attributes in first implementation
145145
}[key];
146146
}
147-
}), '<img id="image-3" title="A picture of an apple" />');
147+
}), 'img#image-3[title="A picture of an apple"]');
148148
});
149149
});
150150

0 commit comments

Comments
 (0)