From a49084958df274efffcb47c79adc324a5d39a279 Mon Sep 17 00:00:00 2001 From: Ben Vinegar Date: Thu, 21 Apr 2016 17:25:46 -0700 Subject: [PATCH] Use query selector style for ui target breadcrumb format --- src/utils.js | 22 ++++++++++++++++------ test/integration/test.js | 6 +++--- test/utils.test.js | 10 +++++----- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/utils.js b/src/utils.js index 73bae2eab972..783c60771a24 100644 --- a/src/utils.js +++ b/src/utils.js @@ -153,21 +153,31 @@ function uuid4() { } /** - * Returns a simple, child-less string representation of a DOM element - * e.g. [HTMLElement] => + * 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(''); } diff --git a/test/integration/test.js b/test/integration/test.js index 0709c6dc52d0..ab595018b910 100644 --- a/test/integration/test.js +++ b/test/integration/test.js @@ -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, ''); + assert.equal(breadcrumbs[0].data.target, 'input#bar[name="foo"][placeholder="lol"]'); assert.equal(breadcrumbs[0].data.type, 'click'); } ); @@ -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, ''); + assert.equal(breadcrumbs[0].data.target, 'input#bar[name="foo"][placeholder="lol"]'); assert.equal(breadcrumbs[0].data.type, 'click'); } ); @@ -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, '
'); + assert.equal(breadcrumbs[0].data.target, 'div#a'); assert.equal(breadcrumbs[0].data.type, 'click'); } ); diff --git a/test/utils.test.js b/test/utils.test.js index d28e37038e86..2ed17cb07975 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -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#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#image-3[title="A picture of an apple"]'); }); });