Refactor attribute binding logic: streamline detection of bindings and improve handling of non-standard attributes

This commit is contained in:
Astrian Zheng 2025-05-15 09:14:24 +10:00
parent ae48c89291
commit 45af436648
Signed by: Astrian
SSH Key Fingerprint: SHA256:rVnhx3DAKjujCwWE13aDl7uV6+9U1MvydLkNRXJrBiA

View File

@ -233,25 +233,17 @@ export default (options: ComponentOptions) => {
const element = currentNode as Element const element = currentNode as Element
// Traverse all attributes // Traverse all attributes
// Detect :attr="" bindings, such as :src="imageUrl"
Array.from(element.attributes).forEach(attr => { Array.from(element.attributes).forEach(attr => {
const value = attr.value if (attr.name.startsWith(':')) {
if (value.includes('{{')) { const attrName = attr.name.substring(1) // Remove ':'
// Extract the expression const expr = attr.value.trim()
const matches = value.match(/\{\{\s*([^}]+)\s*\}\}/g)
if (matches) {
matches.forEach(match => {
const expr = match.replace(/\{\{\s*|\s*\}\}/g, '').trim()
// For attribute bindings, we need a special update function // Remove the attribute, as it is not a standard HTML attribute
this._setupAttributeBinding(element, attr.name, expr, value) element.removeAttribute(attr.name)
// Record dependency relationship // Set up attribute binding
if (!this._stateToElementsMap[expr]) { this._setupAttributeBinding(element, attrName, expr, attr.value)
this._stateToElementsMap[expr] = new Set()
}
this._stateToElementsMap[expr].add(element as HTMLElement)
})
}
} }
}) })