Skip to content

Commit

Permalink
Merge 226ca3c into 1b33f7d
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongkai committed Jul 11, 2020
2 parents 1b33f7d + 226ca3c commit ca2223f
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

22 changes: 17 additions & 5 deletions src/view/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var warnEventListenMethod = require('./warn-event-listen-method');
var elementDisposeChildren = require('./element-dispose-children');
var createDataTypesChecker = require('../util/create-data-types-checker');
var warn = require('../util/warn');
var hotTags = require('../browser/hot-tags');



Expand Down Expand Up @@ -94,16 +95,18 @@ function Component(options) { // eslint-disable-line
if (!proto.hasOwnProperty('_cmptReady')) {
proto.components = clazz.components || proto.components || {};
var components = proto.components;
// 收集要跳过preheat的标签
proto._preHeatSkip = {};

for (var key in components) { // eslint-disable-line
var cmptClass = components[key];

if (typeof cmptClass === 'object' && !(cmptClass instanceof ComponentLoader)) {
components[key] = defineComponent(cmptClass);
}
else if (cmptClass === 'self') {
components[key] = clazz;
}
proto._preHeatSkip[key] = 1;
}

proto._cmptReady = 1;
Expand All @@ -120,22 +123,19 @@ function Component(options) { // eslint-disable-line
proto.aNode = parseComponentTemplate(clazz);
}
}
preheatANode(proto.aNode);


this.tagName = proto.aNode.tagName;
this.source = typeof options.source === 'string'
? parseTemplate(options.source).children[0]
: options.source;
preheatANode(this.source);
preheatANode(this.source, proto._preHeatSkip);


this.sourceSlotNameProps = [];
this.sourceSlots = {
named: {}
};


this.owner = options.owner;
this.scope = options.scope;
this.el = options.el;
Expand Down Expand Up @@ -244,6 +244,18 @@ function Component(options) { // eslint-disable-line

this.data = new Data(initData);

var getComponentType = clazz.getComponentType || proto.getComponentType;
// 这里只能收集getComponentType判断tagName的逻辑的hot标签予以跳过
if (getComponentType) {
for (var ht in hotTags) {
if (typeof getComponentType.call(this, { tagName: ht }, this.data) === 'function') {
proto._preHeatSkip[ht] = 1;
}
}
}

preheatANode(proto.aNode, proto._preHeatSkip);


this.tagName = this.tagName || 'div';
// #[begin] allua
Expand Down
15 changes: 12 additions & 3 deletions src/view/preheat-a-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ var Element = require('./element');
*
* @param {Object} aNode 要预热的ANode
*/
function preheatANode(aNode) {
function preheatANode(aNode, skipTags) {
var stack = [];
var skipTags = skipTags || {};

function recordHotspotData(expr, notContentData) {
var refs = analyseExprDataHotspot(expr);
Expand Down Expand Up @@ -87,7 +88,7 @@ function preheatANode(aNode) {
each(aNode.props, function (prop) {
aNode.hotspot.binds.push({
name: kebab2camel(prop.name),
expr: prop.noValue != null
expr: prop.noValue != null
? {type: ExprType.BOOL, value: true}
: prop.expr,
x: prop.x,
Expand Down Expand Up @@ -198,7 +199,15 @@ function preheatANode(aNode) {
}

if (hotTags[aNode.tagName]) {
aNode.Clazz = Element;
if (skipTags[aNode.tagName]) {
// #[begin] error
/* eslint-disable max-len */
warn('\`' + aNode.tagName + '\` is a reserved tag name. Using this to identify component may cause unknown exceptions.');
/* eslint-enable max-len */
// #[end]
} else {
aNode.Clazz = Element;
}
}
else {
switch (aNode.tagName) {
Expand Down
63 changes: 63 additions & 0 deletions test/component.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5550,5 +5550,68 @@ describe("Component", function () {
});
});
});
it("identify subcomponent with reserved hot tag", function() {
var Label = san.defineComponent({
template: '<span title="{{text}}">{{text}}</span>'
});

var MyComponent = san.defineComponent({
components: {
'b': Label
},

template: '<div><b text="{{name}}"></b></div>',

initData: function() {
return { name: 'erik' };
}
});

var myComponent = new MyComponent();

var wrap = document.createElement('div');
document.body.appendChild(wrap);
myComponent.attach(wrap);

var span = wrap.getElementsByTagName('span')[0];
expect(span.innerText).toBe('erik');
expect(span.title).toBe('erik');

myComponent.dispose();
document.body.removeChild(wrap);

});

it("identify subcomponent with reserved hot tag with getComponentType", function() {
var Label = san.defineComponent({
template: '<span title="{{text}}">{{text}}</span>'
});
var MyComponent = san.defineComponent({
getComponentType: function(aNode) {
if (aNode.tagName === 'b') {
return Label;
}
},

template: '<div><b text="{{name}}"></b></div>',

initData: function() {
return { name: 'erik' };
}
});

var myComponent = new MyComponent();

var wrap = document.createElement('div');
document.body.appendChild(wrap);
myComponent.attach(wrap);

var span = wrap.getElementsByTagName('span')[0];
expect(span.innerText).toBe('erik');
expect(span.title).toBe('erik');

myComponent.dispose();
document.body.removeChild(wrap);
});
});

0 comments on commit ca2223f

Please sign in to comment.