From 1d2e8a7676ba74f73ea2d96bcd68b12541a46d03 Mon Sep 17 00:00:00 2001 From: Elias Winberg Date: Mon, 4 Jul 2016 20:19:13 +0200 Subject: [PATCH] Return undefined in .prop if given an invalid element or tag (#880) --- lib/api/attributes.js | 2 ++ test/api/attributes.js | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/lib/api/attributes.js b/lib/api/attributes.js index a0fec61409..199abb59d9 100644 --- a/lib/api/attributes.js +++ b/lib/api/attributes.js @@ -91,6 +91,8 @@ exports.attr = function(name, value) { }; var getProp = function (el, name) { + if (!el || !isTag(el)) return; + return el.hasOwnProperty(name) ? el[name] : rboolean.test(name) diff --git a/test/api/attributes.js b/test/api/attributes.js index c23a1ff4b7..21f29e786d 100644 --- a/test/api/attributes.js +++ b/test/api/attributes.js @@ -182,6 +182,11 @@ describe('$(...)', function() { it('(key, value) : should support chaining after setting props', function() { expect(checkbox.prop('checked', false)).to.equal(checkbox); }); + + it('(invalid element/tag) : prop should return undefined', function() { + expect($(undefined).prop('prop')).to.be(undefined); + expect($(null).prop('prop')).to.be(undefined); + }); }); describe('.data', function() {