Skip to content
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*.idea*
example
npm-debug.log
yarn-error.log
*/tmp/*
test/tmp/*
test/runner/dom*
Expand Down
156 changes: 116 additions & 40 deletions dist/regular.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
@author leeluolee
@version 0.6.0-beta.10
@version 0.6.0
@homepage http://regularjs.github.io
*/
(function webpackUniversalModuleDefinition(root, factory) {
Expand Down Expand Up @@ -782,15 +782,19 @@ return /******/ (function(modules) { // webpackBootstrap
return group.inject || group.$inject;
}

_.blankReg = /^\s*$/;
_.blankReg = /^\s*$/;

_.getCompileFn = function(source, ctx, options){
return function( passedOptions ){
return function( passedOptions, transform ){
if( passedOptions && options ) _.extend( passedOptions , options );
else passedOptions = options;

if(typeof transform === 'function') {
passedOptions = transform(passedOptions);
}

return ctx.$compile(source, passedOptions )
}
return ctx.$compile.bind(ctx,source, options)
}

// remove directive param from AST
Expand Down Expand Up @@ -925,13 +929,6 @@ return /******/ (function(modules) { // webpackBootstrap

}








/* WEBPACK VAR INJECTION */}.call(exports, (function() { return this; }()), __webpack_require__(4).setImmediate))

/***/ }),
Expand Down Expand Up @@ -1719,7 +1716,6 @@ return /******/ (function(modules) { // webpackBootstrap


// modify在compile之后调用, 这样就无需处理SSR相关逻辑

if( oldModify ){
oldModify(this);
}
Expand Down Expand Up @@ -2236,7 +2232,7 @@ return /******/ (function(modules) { // webpackBootstrap
var conflictTag = {"}": "{", "]": "["}, map1, map2;
// some macro for lexer
var macro = {
'NAME': /(?:[:_A-Za-z][-\.:_0-9A-Za-z]*)/,
'NAME': /(?:[:_A-Za-z\$][-\.:_0-9A-Za-z]*)/,
'IDENT': /[\$_A-Za-z][_0-9A-Za-z\$]*/,
'SPACE': /[\r\n\t\f ]/
}
Expand Down Expand Up @@ -2885,9 +2881,45 @@ return /******/ (function(modules) { // webpackBootstrap

// {{~}}
op.inc = op.include = function(){
var content = this.expression();
var first = this.ll(1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个貌似有问题,是假设了 使用 this.$body 就是fragment类型 么? 理论上使用this.$body 和使用 其他 fragment类型的变量是一样的处理方式。 比如 <Scope title = {~ <div> {title} </div>} /> 配和 {#inc title}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

@fengzilong fengzilong Oct 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是指这里的一堆if判断嘛,这个是为了限制复杂表达式的
比如{#inc x + y with { item: item }},复杂表达式x + y不允许和scoped include一起使用

Copy link
Member Author

@fengzilong fengzilong Oct 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段if/else对后面的scoped include实现没帮助的,只影响下面的this.error,去掉也没事,目前的实现已经支持 {#inc title} 这种类型的fragment了

Copy link
Member

@leeluolee leeluolee Oct 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以精简一次pr的内容,即使有坑,但之前也是没这个逻辑的,可以换个pr处理。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

var second = this.ll(2);
var third = this.ll(3);
var fourth = this.ll(4);
var scope, locals, content;

if(
first.type === 'IDENT' &&
second.type === 'IDENT' &&
second.value === 'with'
) {
scope = first.value; // {#inc <var> with ...}
} else if (
first.type === 'IDENT' &&
first.value === 'this' &&
second.type === '.' &&
third.type === 'IDENT' &&
third.value === '$body' &&
fourth.type === 'IDENT' &&
fourth.value === 'with'
) {
scope = 'this.$body'; // {#inc this.$body with ...}
}

content = this.expression();

if (this.eat('IDENT', 'with')) {
if (!scope) {
this.error(
'Use scoped include only with named include or this.$body, ' +
'complex expression is not allowed here',
first.pos
)
}

locals = this.expression();
}
this.match('END');
return node.template(content);
return node.template(content, scope, locals);
}

// {{#if}}
Expand Down Expand Up @@ -3441,10 +3473,12 @@ return /******/ (function(modules) { // webpackBootstrap
text: text
}
},
template: function(template){
template: function(template, scope, locals){
return {
type: 'template',
content: template
content: template,
scope: scope,
locals: locals
}
}
}
Expand Down Expand Up @@ -4458,36 +4492,73 @@ return /******/ (function(modules) { // webpackBootstrap

// {#include } or {#inc template}
walkers.template = function(ast, options){
var content = ast.content, compiled;
var content = ast.content, scope = ast.scope, locals = ast.locals, compiled;
var placeholder = document.createComment('inlcude');
var compiled, namespace = options.namespace, extra = options.extra;
var group = new Group([placeholder]);
var cursor = options.cursor;

var localsFn, scopeName = this.$scope || '$scope';

insertPlaceHolder(placeholder, cursor);

if(content){
var self = this;
this.$watch(content, function(value){
var removed = group.get(1), type= typeof value;
if( removed){
removed.destroy(true);
group.children.pop();
}
if(!value) return;

group.push( compiled = type === 'function' ? value(cursor? {cursor: cursor}: null): self.$compile( type !== 'object'? String(value): value, {
// watch locals
if(scope && locals){
localsFn = this._touchExpr(locals).get.bind(this, this, extra);
}

this.$watch(content, update, OPTIONS.INIT);

cursor = null;
}

// add $scope for options
function addScope(options) {
var opts = {};

_.extend(opts, options);
opts.extra = _.createObject(options.extra);

if (scope && locals) {
opts.extra[ scopeName ] = localsFn();
self.$watch(localsFn, function (n, o) {
opts.extra[ scopeName ] = n;
}, {
deep: true,
sync: true
});
} else {
opts.extra[ scopeName ] = {};
}

return opts;
}

function update( value ){
var removed = group.get(1), type= typeof value;
if( removed){
removed.destroy(true);
group.children.pop();
}
if(!value) return;

compiled = type === 'function' ?
value(cursor? {cursor: cursor}: null, addScope) :
self.$compile( type !== 'object' ? String(value): value, addScope({
record: true,
outer: options.outer,
namespace: namespace,
cursor: cursor,
extra: extra}) );
if(placeholder.parentNode && !cursor) {
compiled.$inject(placeholder, 'before')
}
}, OPTIONS.INIT);
cursor = null;
extra: extra
}));
group.push(compiled);
if(placeholder.parentNode && !cursor) {
compiled.$inject(placeholder, 'before')
}
}

return group;
};

Expand Down Expand Up @@ -4610,7 +4681,7 @@ return /******/ (function(modules) { // webpackBootstrap
}else{
node = document.createTextNode("");
}

this.$watch(ast, function(newval){
dom.text(node, _.toText(newval));
}, OPTIONS.STABLE_INIT )
Expand Down Expand Up @@ -4762,12 +4833,18 @@ return /******/ (function(modules) { // webpackBootstrap
extra = options.extra,
namespace = options.namespace,
refDirective = walkers.Regular.directive('ref'),
ref, self = this, is;

ref, self = this, is, $scope;
var data = {}, events;

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

if (attr.name === '$scope') {
$scope = attr.value;
continue;
}

// consider disabled equlasto disabled={true}

shared.prepareAttr( attr, attr.name === 'ref' && refDirective );
Expand Down Expand Up @@ -4802,7 +4879,7 @@ return /******/ (function(modules) { // webpackBootstrap
namespace: namespace,
extra: extra,
outer: options.outer
})
})
}

// @if is r-component . we need to find the target Component
Expand Down Expand Up @@ -4846,6 +4923,7 @@ return /******/ (function(modules) { // webpackBootstrap
$parent: (isolate & 2)? null: this,
$root: this.$root,
$outer: options.outer,
$scope: $scope,
_body: {
ctx: this,
ast: ast.children
Expand All @@ -4857,10 +4935,8 @@ return /******/ (function(modules) { // webpackBootstrap
extra: options.extra
}


var component = new Component(definition, options), reflink;


if(ref && this.$refs){
reflink = refDirective.link;
var refDestroy = reflink.call(this, component, ref);
Expand Down
8 changes: 4 additions & 4 deletions dist/regular.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ gulp.task('v', function(fn){


gulp.task('watch', ["build", 'testbundle'], function(){
gulp.watch(['test/spec/*.js', 'lib/**/*.js'], ['jshint','testbundle'])
gulp.watch(['test/spec/*.js', 'lib/**/*.js'], ['jshint', 'build', 'testbundle'])
})


Expand Down
2 changes: 1 addition & 1 deletion lib/parser/Lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var createFSM = require("./FSM");
var conflictTag = {"}": "{", "]": "["}, map1, map2;
// some macro for lexer
var macro = {
'NAME': /(?:[:_A-Za-z][-\.:_0-9A-Za-z]*)/,
'NAME': /(?:[:_A-Za-z\$][-\.:_0-9A-Za-z]*)/,
'IDENT': /[\$_A-Za-z][_0-9A-Za-z\$]*/,
'SPACE': /[\r\n\t\f ]/
}
Expand Down
40 changes: 38 additions & 2 deletions lib/parser/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,45 @@ op.interplation = function(){

// {{~}}
op.inc = op.include = function(){
var content = this.expression();
var first = this.ll(1);
var second = this.ll(2);
var third = this.ll(3);
var fourth = this.ll(4);
var scope, locals, content;

if(
first.type === 'IDENT' &&
second.type === 'IDENT' &&
second.value === 'with'
) {
scope = first.value; // {#inc <var> with ...}
} else if (
first.type === 'IDENT' &&
first.value === 'this' &&
second.type === '.' &&
third.type === 'IDENT' &&
third.value === '$body' &&
fourth.type === 'IDENT' &&
fourth.value === 'with'
) {
scope = 'this.$body'; // {#inc this.$body with ...}
}

content = this.expression();

if (this.eat('IDENT', 'with')) {
if (!scope) {
this.error(
'Use scoped include only with named include or this.$body, ' +
'complex expression is not allowed here',
first.pos
)
}

locals = this.expression();
}
this.match('END');
return node.template(content);
return node.template(content, scope, locals);
}

// {{#if}}
Expand Down
6 changes: 4 additions & 2 deletions lib/parser/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ module.exports = {
text: text
}
},
template: function(template){
template: function(template, scope, locals){
return {
type: 'template',
content: template
content: template,
scope: scope,
locals: locals
}
}
}
1 change: 0 additions & 1 deletion lib/render/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ var Regular = function(definition, options){


// modify在compile之后调用, 这样就无需处理SSR相关逻辑

if( oldModify ){
oldModify(this);
}
Expand Down
Loading