Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(utils): Fixed getOwnPropertyDescriptor to hasOwnProperty function #908

Merged
merged 4 commits into from
Jan 30, 2024
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
10 changes: 6 additions & 4 deletions dist/purify.cjs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/purify.cjs.js.map

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions dist/purify.es.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const stringMatch = unapply(String.prototype.match);
const stringReplace = unapply(String.prototype.replace);
const stringIndexOf = unapply(String.prototype.indexOf);
const stringTrim = unapply(String.prototype.trim);
const objectHasOwnProperty = unapply(Object.prototype.hasOwnProperty);
const regExpTest = unapply(RegExp.prototype.test);
const typeErrorCreate = unconstruct(TypeError);

Expand Down Expand Up @@ -120,7 +121,8 @@ function addToSet(set, array) {
*/
function cleanArray(array) {
for (let index = 0; index < array.length; index++) {
if (getOwnPropertyDescriptor(array, index) === undefined) {
const isPropertyExist = objectHasOwnProperty(array, index);
if (!isPropertyExist) {
array[index] = null;
}
}
Expand All @@ -136,7 +138,8 @@ function cleanArray(array) {
function clone(object) {
const newObject = create(null);
for (const [property, value] of entries(object)) {
if (getOwnPropertyDescriptor(object, property) !== undefined) {
const isPropertyExist = objectHasOwnProperty(object, property);
if (isPropertyExist) {
if (Array.isArray(value)) {
newObject[property] = cleanArray(value);
} else if (value && typeof value === 'object' && value.constructor === Object) {
Expand Down Expand Up @@ -169,8 +172,7 @@ function lookupGetter(object, prop) {
}
object = getPrototypeOf(object);
}
function fallbackValue(element) {
console.warn('fallback value for', element);
function fallbackValue() {
return null;
}
return fallbackValue;
Expand Down
2 changes: 1 addition & 1 deletion dist/purify.es.mjs.map

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions dist/purify.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/purify.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/purify.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/purify.min.js.map

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const stringReplace = unapply(String.prototype.replace);
const stringIndexOf = unapply(String.prototype.indexOf);
const stringTrim = unapply(String.prototype.trim);

const objectHasOwnProperty = unapply(Object.prototype.hasOwnProperty);
Copy link
Contributor Author

@ssi02014 ssi02014 Jan 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JavaScript does not protect the property name hasOwnProperty, so objects with properties with this name may return incorrect results.

If we use the prototype function the way we've used it, it's stable.

const objectHasOwnProperty = unapply(Object.prototype.hasOwnProperty);

const obj = {
  foo: 1,
  hasOwnProperty() {
    return false;
  },
};

// Normal behavior
console.log(objectHasOwnProperty(obj, "foo")); // true
console.log(objectHasOwnProperty(obj, "bar")); // false

console.log(Object.prototype.hasOwnProperty.call(obj, "foo")); // true
console.log(Object.prototype.hasOwnProperty.call(obj, "bar")); // false

// Abnormal behavior
console.log(obj.hasOwnProperty("foo")); // false
console.log(obj.hasOwnProperty("bar")); // false

Related
MDN-Using hasOwnProperty as a property name


const objectHasOwnProperty = unapply(Object.prototype.hasOwnProperty);

const obj2 = Object.create(null);
obj2.foo = 1;

// Normal behavior
console.log(objectHasOwnProperty(obj2, "foo")); // true
console.log(objectHasOwnProperty(obj2, "bar")); // false

console.log(Object.prototype.hasOwnProperty.call(obj2, "foo")); // true
console.log(Object.prototype.hasOwnProperty.call(obj2, "bar")); // false

// TypeError: obj2.hasOwnProperty is not a function
console.log(obj2.hasOwnProperty("foo"));

Related
MDN-Objects created with Object.create(null)
eslint no-prototype-builtins


const regExpTest = unapply(RegExp.prototype.test);

const typeErrorCreate = unconstruct(TypeError);
Expand Down Expand Up @@ -115,7 +117,9 @@ function addToSet(set, array, transformCaseFunc = stringToLowerCase) {
*/
function cleanArray(array) {
for (let index = 0; index < array.length; index++) {
if (getOwnPropertyDescriptor(array, index) === undefined) {
const isPropertyExist = objectHasOwnProperty(array, index);

if (!isPropertyExist) {
array[index] = null;
}
}
Expand All @@ -133,7 +137,9 @@ function clone(object) {
const newObject = create(null);

for (const [property, value] of entries(object)) {
if (getOwnPropertyDescriptor(object, property) !== undefined) {
const isPropertyExist = objectHasOwnProperty(object, property);

if (isPropertyExist) {
if (Array.isArray(value)) {
newObject[property] = cleanArray(value);
} else if (
Expand Down
Loading