vue.js 源代码学习笔记 ----- text-parse.js
/* @flow */
import { cached } from 'shared/util'
import { parseFilters } from './filter-parser'
//找到{{abc}}这样的
const defaultTagRE = /\{\{((?:.|\n)+?)\}\}/g //.+的意义是最小匹配, 找到符合的马上结束
//正则的元字符 ^ $ . * + ? = ! : | \ / ( ) [ ] { }
const regexEscapeRE = /[-.*+?^${}()|[\]/\\]/g
//这个函数创建用户自定义模板符号, 比如传入 [ "[[", "]]" ], 模板就可以写成 [[abc.reverse()]]
const buildRegex = cached(delimiters => {
const open = delimiters[0].replace(regexEscapeRE, '\\$&') //$&代表这个匹配项, 这里元字符使用\\转义, 保证安全
const close = delimiters[1].replace(regexEscapeRE, '\\$&')
return new RegExp(open + '((?:.|\\n)+?)' + close, 'g') //格式跟 defaultTagRe类似
})
export function parseText (
text: string,
delimiters?: [string, string]
): string | void {
//拿到最终的模板正则
const tagRE = delimiters ? buildRegex(delimiters) : defaultTagRE
if (!tagRE.test(text)) {
return
}
const tokens = []
let lastIndex = tagRE.lastIndex = 0
let match, index
while ((match = tagRE.exec(text))) {
index = match.index
// push text token
if (index > lastIndex) {
tokens.push(JSON.stringify(text.slice(lastIndex, index)))
}
// tag token
const exp = parseFilters(match[1].trim())
tokens.push(`_s(${exp})`)
lastIndex = index + match[0].length
}
if (lastIndex < text.length) {
tokens.push(JSON.stringify(text.slice(lastIndex)))
}
return tokens.join('+')
}
vue.js 源代码学习笔记 ----- text-parse.js的更多相关文章
- vue.js 源代码学习笔记 ----- core scedule.js
/* @flow */ import type Watcher from './watcher' import config from '../config' import { callHook } ...
- vue.js 源代码学习笔记 ----- core array.js
/* * not type checking this file because flow doesn't play well with * dynamically accessing methods ...
- node.js day01学习笔记:认识node.js
Node.js(JavaScript,everywhere) 1.Node.js 介绍 1.1. 为什么要学习Node.js 企业需求 + 具有服务端开发经验更好 + front-end + back ...
- vue.js 源代码学习笔记 ----- html-parse.js
/** * Not type-checking this file because it's mostly vendor code. */ /*! * HTML Parser By John Resi ...
- vue.js 源代码学习笔记 ----- 工具方法 lang
/* @flow */ // Object.freeze 使得这个对象不能增加属性, 修改属性, 这样就保证了这个对象在任何时候都是空的 export const emptyObject = Obje ...
- vue.js 源代码学习笔记 ----- 工具方法 env
/* @flow */ /* globals MutationObserver */ import { noop } from 'shared/util' // can we use __proto_ ...
- vue.js 源代码学习笔记 ----- helpers.js
/* @flow */ import { parseFilters } from './parser/filter-parser' export function baseWarn (msg: str ...
- vue.js 源代码学习笔记 ----- 工具方法 share
/* @flow */ /** * Convert a value to a string that is actually rendered. { .. } [ .. ] 2 => '' */ ...
- vue.js 源代码学习笔记 ----- codegen.js
/* @flow */ import { genHandlers } from './events' import { baseWarn, pluckModuleFunction } from '.. ...
随机推荐
- java程序运行一段时间之后停止
原创文章,未经作者允许,禁止转载!!!!!!! 如何用java是一段代码运行一段时间之后自动停止运行? 就拿打印随机函数的代码来做例子吧,让程序随机打印1-10的数字,打印十秒钟后停止打印: publ ...
- 非线性方程(组):一维非线性方程(一)二分法、不动点迭代、牛顿法 [MATLAB]
1. 二分法(Bisection) 1) 原理 [介值定理] 对于连续的一元非线性函数,若其在两个点的取值异号,则在两点间必定存在零点. [迭代流程] 若左右两端取值不同,则取其中点,求其函数值,取中 ...
- 一次org.springframework.jdbc.BadSqlGrammarException ### Error querying database Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException问题排查过程
先说结论: 因为在表设计中有一个商品描述字段被设置为desc,但desc是mysql中的关键字,如select id,name,desc,price from product;这条sql语句在查询时的 ...
- Mac OS 终端下使用 Curl 命令下载文件
在 mac os下,如何通过命令行来下载网络文件?如果你没有安装或 wget 命令,那么可以使用 curl 工具来达到我们的目的. curl命令参数: curl 'url地址' curl [选项] ' ...
- c++第二十六天
p131~p135: 1.除非必要否则不使用后缀加加(减减),会有额外的性能开销. 2.混用解引用和递增运算符.*pointer++,后缀运算符优先于解引用运算符. 3.运算对象可按任意顺序求值,即使 ...
- InstallShield 2015 Premier的Basic MSI Project如何在卸载时删除残留的文件 (转)
转载:http://blog.csdn.net/zztoll/article/details/54018615#comments 先说下缘由,我在用InstallShield 2015 Premier ...
- duilib : 滑动显示的窗口实现以及 悬浮窗 (转载)
1. vc 判断窗口是否显示 BOOL IsWindowVisible(HWND hWnd); 2.悬浮窗 http://blog.csdn.net/lincyang/article/details ...
- #/bin/sh参数-e的含义
注:本博客欢迎转载和引用,但请保留原作者信息! 一.背景 今天遇到一个诡异的问题,一旦脚本中判断$?为非零,那么脚本就会自动退出的情况,仔细调试脚本的逻辑,并没有发现错误,因此作此文 二.解决 既然要 ...
- LeetCode——Rotate Image
1. Question You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ( ...
- python 输出环境变量
import os # Access all environment variables print('*---------------ENVIRON-------------------*') pr ...