-
Notifications
You must be signed in to change notification settings - Fork 2
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
feat: allow custom attributes to be dynamic #26
Conversation
WalkthroughThe recent changes to the Changes
TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (attribute === ':') return false | ||
if (!attribute.startsWith(':')) return false | ||
if (Attributes.FORBIDDEN_ATTRIBUTES.includes(attribute)) return false | ||
if (Events.isValidEvent(attribute)) return false | ||
if (Attributes.CUSTOM_ATTRIBUTES.includes(attribute)) return true | ||
|
||
return true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question the logic change in isValidAttribute
.
The method now includes a check for attribute === ':'
, which correctly returns false
. However, the subsequent line always returns true
, which could inadvertently allow invalid attributes. Please verify if this is the intended behavior or consider revising it to ensure only valid attributes are accepted.
static isValidNativeAttribute(attribute, element) { | ||
if (!Attributes.isValidAttribute(attribute)) return false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Suggest a minor optimization in isValidNativeAttribute
.
Consider caching the result of attribute.replace(':', '').split('.')
since it's used multiple times, which could improve performance slightly.
- const [nativeAttr] = attribute.replace(':', '').split('.');
+ const [nativeAttr] = attribute.startsWith(':') ? attribute.slice(1).split('.') : attribute.split('.');
Also applies to: 33-35
const element = this.entity.element | ||
const expr = element.getAttribute(attr) | ||
if (!expr) return | ||
|
||
try { | ||
const newValue = await this._interpret(expr) | ||
const nativeAttr = kebabToCamelCase(attr.slice(1)) | ||
|
||
if (attr.startsWith(':data-')) { | ||
if ( | ||
this.entity.element.dataset[nativeAttr.slice(4)] !== newValue && | ||
newValue != null | ||
) { | ||
const datasetAttr = | ||
nativeAttr[4].toLowerCase() + nativeAttr.slice(5) | ||
this.entity.element.dataset[datasetAttr] = newValue | ||
|
||
if (Attributes.isValidNativeAttribute(attr, element)) { | ||
const nativeAttr = kebabToCamelCase(attr.slice(1)) | ||
|
||
if (attr.startsWith(':data-')) { | ||
if ( | ||
element.dataset[nativeAttr.slice(4)] !== newValue && | ||
newValue != null | ||
) { | ||
const datasetAttr = | ||
nativeAttr[4].toLowerCase() + nativeAttr.slice(5) | ||
element.dataset[datasetAttr] = newValue | ||
} | ||
} else if (element[nativeAttr] !== newValue && newValue != null) { | ||
element[nativeAttr] = newValue | ||
} | ||
} else if ( | ||
this.entity.element[nativeAttr] !== newValue && | ||
newValue != null | ||
) | ||
this.entity.element[nativeAttr] = newValue | ||
} else { | ||
element.setAttribute(attr.slice(1), newValue) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clarify handling of :data-
attributes in evaluateOtherAttributes
.
The method now includes handling for :data-
attributes, which is a good improvement. However, the transformation of the attribute name from the dataset convention to the DOM API should be clarified or commented to avoid confusion for future maintainers.
🚀 PR was released in |
No description provided.