Skip to content

Commit ca0990e

Browse files
committed
fix getting DOM element by number id
id with number works (api that uses `GridStackElement` handle more string formats)
1 parent 9ed0eb1 commit ca0990e

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Change log
4444

4545
- fix `class="ui-draggable-disabled ui-resizable-disabled"` have been added back to static grid items, so existing CSS rule to style continue working [1435](https://github.com/gridstack/gridstack.js/issues/1435)
4646
- add `data-gs-staticGrid` attribute
47+
- fix getting DOM element by id with number works (api that uses `GridStackElement` handle more string formats)
4748

4849
## 2.1.0 (2020-10-28)
4950

spec/gridstack-spec.ts

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,70 @@ describe('gridstack', function() {
952952
expect(parseInt(widget.getAttribute('data-gs-height'), 10)).toBe(1);
953953
expect(widget.getAttribute('data-gs-id')).toBe('foo');
954954
});
955-
});
955+
});
956+
957+
describe('makeWidget()', function() {
958+
beforeEach(function() {
959+
document.body.insertAdjacentHTML('afterbegin', gridstackEmptyHTML);
960+
});
961+
afterEach(function() {
962+
document.body.removeChild(document.getElementById('gs-cont'));
963+
});
964+
it('passing element', function() {
965+
let grid = GridStack.init();
966+
let doc = document.implementation.createHTMLDocument();
967+
doc.body.innerHTML = '<div><div class="grid-stack-item-content"></div></div>';
968+
let el = doc.body.children[0] as HTMLElement;
969+
grid.el.appendChild(el);
970+
let widget = grid.makeWidget(el);
971+
expect(parseInt(widget.getAttribute('data-gs-x'), 10)).toBe(0);
972+
});
973+
it('passing class', function() {
974+
let grid = GridStack.init();
975+
let doc = document.implementation.createHTMLDocument();
976+
doc.body.innerHTML = '<div class="item"><div class="grid-stack-item-content"></div></div>';
977+
let el = doc.body.children[0] as HTMLElement;
978+
grid.el.appendChild(el);
979+
let widget = grid.makeWidget('.item');
980+
expect(parseInt(widget.getAttribute('data-gs-x'), 10)).toBe(0);
981+
});
982+
it('passing class no dot', function() {
983+
let grid = GridStack.init();
984+
let doc = document.implementation.createHTMLDocument();
985+
doc.body.innerHTML = '<div class="item"><div class="grid-stack-item-content"></div></div>';
986+
let el = doc.body.children[0] as HTMLElement;
987+
grid.el.appendChild(el);
988+
let widget = grid.makeWidget('item');
989+
expect(parseInt(widget.getAttribute('data-gs-x'), 10)).toBe(0);
990+
});
991+
it('passing id', function() {
992+
let grid = GridStack.init();
993+
let doc = document.implementation.createHTMLDocument();
994+
doc.body.innerHTML = '<div id="item"><div class="grid-stack-item-content"></div></div>';
995+
let el = doc.body.children[0] as HTMLElement;
996+
grid.el.appendChild(el);
997+
let widget = grid.makeWidget('#item');
998+
expect(parseInt(widget.getAttribute('data-gs-x'), 10)).toBe(0);
999+
});
1000+
it('passing id no #', function() {
1001+
let grid = GridStack.init();
1002+
let doc = document.implementation.createHTMLDocument();
1003+
doc.body.innerHTML = '<div id="item"><div class="grid-stack-item-content"></div></div>';
1004+
let el = doc.body.children[0] as HTMLElement;
1005+
grid.el.appendChild(el);
1006+
let widget = grid.makeWidget('item');
1007+
expect(parseInt(widget.getAttribute('data-gs-x'), 10)).toBe(0);
1008+
});
1009+
it('passing id as number', function() {
1010+
let grid = GridStack.init();
1011+
let doc = document.implementation.createHTMLDocument();
1012+
doc.body.innerHTML = '<div id="1"><div class="grid-stack-item-content"></div></div>';
1013+
let el = doc.body.children[0] as HTMLElement;
1014+
grid.el.appendChild(el);
1015+
let widget = grid.makeWidget('1');
1016+
expect(parseInt(widget.getAttribute('data-gs-x'), 10)).toBe(0);
1017+
});
1018+
});
9561019

9571020
describe('method getFloat()', function() {
9581021
beforeEach(function() {

src/gridstack.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,11 +1773,23 @@ export class GridStack {
17731773
/** @internal convert a potential selector into actual element */
17741774
private static getElement(els: GridStackElement = '.grid-stack-item'): GridItemHTMLElement {
17751775
if (typeof els === 'string') {
1776-
let el = document.querySelector(els);
1777-
if (!el && els[0] !== '.' && els[0] !== '#') {
1778-
el = document.querySelector('#' + els);
1779-
if (!el) { el = document.querySelector('.' + els) }
1776+
if (!els.length) { return null}
1777+
if (els[0] === '#') {
1778+
return document.getElementById(els.substring(1));
1779+
}
1780+
if (els[0] === '.') {
1781+
return document.querySelector(els);
1782+
}
1783+
1784+
// if we start with a digit, assume it's an id (error calling querySelector('#1')) as class are not valid CSS
1785+
if(!isNaN(+els[0])) { // start with digit
1786+
return document.getElementById(els);
17801787
}
1788+
1789+
// finally try string, then id then class
1790+
let el = document.querySelector(els);
1791+
if (!el) { el = document.getElementById(els) }
1792+
if (!el) { el = document.querySelector('.' + els) }
17811793
return el as GridItemHTMLElement;
17821794
}
17831795
return els;

0 commit comments

Comments
 (0)