Skip to content

Commit

Permalink
mv merge source attrs function from component to single file
Browse files Browse the repository at this point in the history
  • Loading branch information
errorrik committed Dec 8, 2023
1 parent 30ff80e commit 525b893
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 51 deletions.
57 changes: 6 additions & 51 deletions src/view/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var preheatEl = require('./preheat-el');
var parseComponentTemplate = require('./parse-component-template');
var preheatANode = require('./preheat-a-node');
var LifeCycle = require('./life-cycle');
var mergeANodeSourceAttrs = require('./merge-a-node-source-attrs');
var getANodeProp = require('./get-a-node-prop');
var isDataChangeByElement = require('./is-data-change-by-element');
var getEventListener = require('./get-event-listener');
Expand Down Expand Up @@ -325,7 +326,7 @@ function Component(options) { // eslint-disable-line

if (aNode.Clazz || this.components[aNode.tagName]) {
if (!aNode.Clazz && this.attrs && this.inheritAttrs) {
aNode = aNodeMergeSourceAttrs(aNode, this.source);
aNode = mergeANodeSourceAttrs(aNode, this.source);
}
this._rootNode = createHydrateNode(aNode, this, this.data, this, hydrateWalker);
this._rootNode._getElAsRootNode && (this.el = this._rootNode._getElAsRootNode());
Expand All @@ -349,7 +350,7 @@ function Component(options) { // eslint-disable-line

if (aNode.Clazz || this.components[aNode.tagName]) {
if (!aNode.Clazz && this.attrs && this.inheritAttrs) {
aNode = aNodeMergeSourceAttrs(aNode, this.source);
aNode = mergeANodeSourceAttrs(aNode, this.source);
}
hydrateWalker = new DOMChildrenWalker(this.el.parentNode, this.el);
this._rootNode = createHydrateNode(aNode, this, this.data, this, hydrateWalker);
Expand Down Expand Up @@ -772,7 +773,7 @@ Component.prototype._update = function (changes) {
}
}
});
debugger

each(me.attrs, function (bindItem) {
if (changeExprCompare(changeExpr, bindItem.expr, me.scope)) {
me.data.set(
Expand Down Expand Up @@ -963,7 +964,7 @@ Component.prototype._repaintChildren = function () {

var aNode = this.aNode;
if (!aNode.Clazz && this.attrs && this.inheritAttrs) {
aNode = aNodeMergeSourceAttrs(aNode, this.source);
aNode = mergeANodeSourceAttrs(aNode, this.source);
}

this._rootNode = createNode(aNode, this, this.data, this);
Expand Down Expand Up @@ -1053,52 +1054,6 @@ Component.prototype._getElAsRootNode = function () {
return this.el;
};

function aNodeMergeSourceAttrs(aNode, source) {
var startIndex = 0;

var mergedANode = {
directives: aNode.directives,
props: aNode.props,
events: aNode.events,
children: aNode.children,
tagName: aNode.tagName,
attrs: [],
vars: aNode.vars,
_ht: aNode._ht,
_i: aNode._i,
_dp: aNode._dp,
_xp: aNode._xp,
_pi: aNode._pi,
_b: aNode._b,
_ce: aNode._ce
};

var aNodeAttrIndex = {};
if (aNode.attrs) {
startIndex = aNode.attrs.length;
for (var i = 0; i < startIndex; i++) {
var attr = aNode.attrs[i];
aNodeAttrIndex[attr.name] = i;
mergedANode.attrs[i] = attr;
}
}

for (var i = 0; i < source.attrs.length; i++) {
var attr = source.attrs[i];

if (aNodeAttrIndex[attr.name] == null) {
mergedANode.attrs[startIndex] = {
name: attr.name,
expr: attr._data,
_data: attr._data
};
startIndex++;
}
}

return mergedANode;
}

/**
* 将组件attach到页面
*
Expand All @@ -1121,7 +1076,7 @@ Component.prototype.attach = function (parentEl, beforeEl) {
// aNode.Clazz 在 preheat 阶段为 if/else/for/fragment 等特殊标签或指令预热生成
// 这里不能用 this.components[aNode.tagName] 判断,因为可能特殊指令和组件在同一个节点上并存
if (!aNode.Clazz && this.attrs && this.inheritAttrs) {
aNode = aNodeMergeSourceAttrs(aNode, this.source);
aNode = mergeANodeSourceAttrs(aNode, this.source);
}

this._rootNode = this._rootNode || createNode(aNode, this, this.data, this);
Expand Down
55 changes: 55 additions & 0 deletions src/view/merge-a-node-source-attrs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright (c) Baidu Inc. All rights reserved.
*
* This source code is licensed under the MIT license.
* See LICENSE file in the project root for license information.
*
* @file 合并传入 ANode 的 attrs 声明,用于深层传递
*/

function mergeANodeSourceAttrs(aNode, source) {
var startIndex = 0;

var mergedANode = {
directives: aNode.directives,
props: aNode.props,
events: aNode.events,
children: aNode.children,
tagName: aNode.tagName,
attrs: [],
vars: aNode.vars,
_ht: aNode._ht,
_i: aNode._i,
_dp: aNode._dp,
_xp: aNode._xp,
_pi: aNode._pi,
_b: aNode._b,
_ce: aNode._ce
};

var aNodeAttrIndex = {};
if (aNode.attrs) {
startIndex = aNode.attrs.length;
for (var i = 0; i < startIndex; i++) {
var attr = aNode.attrs[i];
aNodeAttrIndex[attr.name] = i;
mergedANode.attrs[i] = attr;
}
}

for (var i = 0; i < source.attrs.length; i++) {
var attr = source.attrs[i];

if (aNodeAttrIndex[attr.name] == null) {
mergedANode.attrs[startIndex++] = {
name: attr.name,
expr: attr._data,
_data: attr._data
};
}
}

return mergedANode;
}

exports = module.exports = warnSetHTML;

0 comments on commit 525b893

Please sign in to comment.