Refactor attribute binding logic: streamline detection of bindings and improve handling of non-standard attributes
This commit is contained in:
parent
ae48c89291
commit
45af436648
24
src/main.ts
24
src/main.ts
|
@ -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)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user