8000 text and directive parser · bencode/vue@7da1cb8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7da1cb8

Browse files
committed
text and directive parser
1 parent d734b61 commit 7da1cb8

File tree

7 files changed

+44
-41
lines changed

7 files changed

+44
-41
lines changed

src/compiler/compile-props.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var _ = require('../util')
2-
var dirParser = require('../parsers/directive')
2+
import { parseDirective } from '../parsers/directive'
33
var propDef = require('../directives/internal/prop')
44
var propBindingModes = require('../config')._propBindingModes
55
var empty = {}
@@ -63,7 +63,7 @@ module.exports = function compileProps (el, propOptions) {
6363
if (value !== null) {
6464
// has dynamic binding!
6565
prop.raw = value
66-
parsed = dirParser.parse(value)
66+
parsed = parseDirective(value)
6767
value = parsed.expression
6868
prop.filters = parsed.filters
6969
// check binding type

src/compiler/compile.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ var _ = require('../util')
22
var publicDirectives = require('../directives/public')
33
var internalDirectives = require('../directives/internal')
44
var compileProps = require('./compile-props')
5-
var textParser = require('../parsers/text')
6-
var dirParser = require('../parsers/directive')
5+
import { parseText, tokensToExp } from '../parsers/text'
6+
import { parseDirective } from '../parsers/directive'
77
var templateParser = require('../parsers/template')
88
import { resolveAsset } from '../util'
99

@@ -289,9 +289,9 @@ function compileElement (el, options) {
289289
// textarea treats its text content as the initial value.
290290
// just bind it as an attr directive for value.
291291
if (el.tagName === 'TEXTAREA') {
292-
var tokens = textParser.parse(el.value)
292+
var tokens = parseText(el.value)
293293
if (tokens) {
294-
el.setAttribute(':value', textParser.tokensToExp(tokens))
294+
el.setAttribute(':value', tokensToExp(tokens))
295295
el.value = ''
296296
}
297297
}
@@ -330,7 +330,7 @@ function compileTextNode (node, options) {
330330
return removeText
331331
}
332332

333-
var tokens = textParser.parse(node.wholeText)
333+
var tokens = parseText(node.wholeText)
334334
if (!tokens) {
335335
return null
336336
}
@@ -395,7 +395,7 @@ function processTextToken (token, options) {
395395
}
396396
function setTokenType (type) {
397397
if (token.descriptor) return
398-
var parsed = dirParser.parse(token.value)
398+
var parsed = parseDirective(token.value)
399399
token.descriptor = {
400400
name: type,
401401
def: publicDirectives[type],
@@ -592,7 +592,7 @@ skip.terminal = true
592592
*/
593593

594594
function makeTerminalNodeLinkFn (el, dirName, value, options, def) {
595-
var parsed = dirParser.parse(value)
595+
var parsed = parseDirective(value)
596596
var descriptor = {
597597
name: dirName,
598598
expression: parsed.expression,
@@ -631,7 +631,7 @@ function compileDirectives (attrs, options) {
631631
attr = attrs[i]
632632
name = rawName = attr.name
633633
value = rawValue = attr.value
634-
tokens = textParser.parse(value)
634+
tokens = parseText(value)
635635
// reset arg
636636
arg = null
637637
// check modifiers
@@ -640,7 +640,7 @@ function compileDirectives (attrs, options) {
640640

641641
// attribute interpolations
642642
if (tokens) {
643-
value = textParser.tokensToExp(tokens)
643+
value = tokensToExp(tokens)
644644
arg = name
645645
pushDir('bind', publicDirectives.bind, true)
646646
// warn against mixing mustaches with v-bind
@@ -715,7 +715,7 @@ function compileDirectives (attrs, options) {
715715
*/
716716

717717
function pushDir (dirName, def, interp) {
718-
var parsed = dirParser.parse(value)
718+
var parsed = parseDirective(value)
719719
dirs.push({
720720
name: dirName,
721721
attr: rawName,

src/instance/api/data.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
var _ = require('../../util')
2-
var Watcher = require('../../watcher')
3-
var textParser = require('../../parsers/text')
4-
var dirParser = require('../../parsers/directive')
1+
import Watcher from '../../watcher'
2+
import { del } from '../../util'
3+
import { parseText } from '../../parsers/text'
4+
import { parseDirective } from '../../parsers/directive'
55
import { getPath } from '../../parsers/path'
66
import { isSimplePath, parseExpression } from '../../parsers/expression'
7-
var filterRE = /[^|]\|[^|]/
7+
8+
const filterRE = /[^|]\|[^|]/
89

910
export default function (Vue) {
1011

@@ -55,7 +56,7 @@ export default function (Vue) {
5556
*/
5657

5758
Vue.prototype.$delete = function (key) {
58-
_.del(this._data, key)
59+
del(this._data, key)
5960
}
6061

6162
/**
@@ -74,7 +75,7 @@ export default function (Vue) {
7475
var vm = this
7576
var parsed
7677
if (typeof expOrFn === 'string') {
77-
parsed = dirParser.parse(expOrFn)
78+
parsed = parseDirective(expOrFn)
7879
expOrFn = parsed.expression
7980
}
8081
var watcher = new Watcher(vm, expOrFn, cb, {
@@ -100,7 +101,7 @@ export default function (Vue) {
100101
Vue.prototype.$eval = function (text, asStatement) {
101102
// check for filters.
102103
if (filterRE.test(text)) {
103-
var dir = dirParser.parse(text)
104+
var dir = parseDirective(text)
104105
// the filter regex check might give false positive
105106
// for pipes inside strings, so it's possible that
106107
// we don't get any filters here
@@ -122,7 +123,7 @@ export default function (Vue) {
122123
*/
123124

124125
Vue.prototype.$interpolate = function (text) {
125-
var tokens = textParser.parse(text)
126+
var tokens = parseText(text)
126127
var vm = this
127128
if (tokens) {
128129
if (tokens.length === 1) {

src/parsers/directive.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
var _ = require('../util')
2-
var Cache = require('../cache')
3-
var cache = new Cache(1000)
4-
var filterTokenRE = /[^\s'"]+|'[^']*'|"[^"]*"/g
5-
var reservedArgRE = /^in$|^-?\d+/
1+
import { toNumber, stripQuotes } from '../util'
2+
import Cache from '../cache'
3+
4+
const cache = new Cache(1000)
5+
const filterTokenRE = /[^\s'"]+|'[^']*'|"[^"]*"/g
6+
const reservedArgRE = /^in$|^-?\d+/
67

78
/**
89
* Parser state
@@ -43,11 +44,11 @@ function pushFilter () {
4344
function processFilterArg (arg) {
4445
if (reservedArgRE.test(arg)) {
4546
return {
46-
value: _.toNumber(arg),
47+
value: toNumber(arg),
4748
dynamic: false
4849
}
4950
} else {
50-
var stripped = _.stripQuotes(arg)
51+
var stripped = stripQuotes(arg)
5152
var dynamic = stripped === arg
5253
return {
5354
value: dynamic ? arg : stripped,
@@ -74,7 +75,7 @@ function processFilterArg (arg) {
7475
* @return {Object}
7576
*/
7677

77-
exports.parse = function (s) {
78+
export function parseDirective (s) {
7879

7980
var hit = cache.get(s)
8081
if (hit) {

src/parsers/text.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Cache from '../cache'
22
import config from '../config'
3-
import dirParser from './directive'
3+
import { parseDirective } from '../parsers/directive'
4+
45
const regexEscapeRE = /[-.*+?^${}()|[\]\/\\]/g
56
let cache, tagRE, htmlRE
67

@@ -43,7 +44,7 @@ export function compileRegex () {
4344
* - {Boolean} [oneTime]
4445
*/
4546

46-
export function parse (text) {
47+
export function parseText (text) {
4748
if (!cache) {
4849
compileRegex()
4950
}
@@ -146,7 +147,7 @@ function inlineFilters (exp, single) {
146147
? exp
147148
: '(' + exp + ')'
148149
} else {
149-
var dir = dirParser.parse(exp)
150+
var dir = parseDirective(exp)
150151
if (!dir.filters) {
151152
return '(' + exp + ')'
152153
} else {

test/unit/specs/parsers/directive_spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
var parse = require('../../../../src/parsers/directive').parse
1+
var parse = require('../../../../src/parsers/directive').parseDirective
22

3-
describe('New Directive Parser', function () {
3+
describe('Directive Parser', function () {
44

55
it('simple', function () {
66
var res = parse('exp')

test/unit/specs/parsers/text_spec.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var testCases = [
5353
]
5454

5555
function assertParse (test) {
56-
var res = textParser.parse(test.text)
56+
var res = textParser.parseText(test.text)
5757
var exp = test.expected
5858
if (!Array.isArray(exp)) {
5959
expect(res).toBe(exp)
@@ -75,8 +75,8 @@ describe('Text Parser', function () {
7575
})
7676

7777
it('cache', function () {
78-
var res1 = textParser.parse('{{a}}')
79-
var res2 = textParser.parse('{{a}}')
78+
var res1 = textParser.parseText('{{a}}')
79+
var res2 = textParser.parseText('{{a}}')
8080
expect(res1).toBe(res2)
8181
})
8282

@@ -96,23 +96,23 @@ describe('Text Parser', function () {
9696
})
9797

9898
it('tokens to expression', function () {
99-
var tokens = textParser.parse('view-{{test + 1}}-test-{{ok + "|"}}')
99+
var tokens = textParser.parseText('view-{{test + 1}}-test-{{ok + "|"}}')
100100
var exp = textParser.tokensToExp(tokens)
101101
expect(exp).toBe('"view-"+(test + 1)+"-test-"+(ok + "|")')
102102
})
103103

104104
it('tokens to expression, single expression', function () {
105-
var tokens = textParser.parse('{{test}}')
105+
var tokens = textParser.parseText('{{test}}')
106106
var exp = textParser.tokensToExp(tokens)
107107
// should not have parens so it can be treated as a
108108
// simple path by the expression parser
109109
expect(exp).toBe('test')
110110
})
111111

112112
it('tokens to expression with filters, multiple expressions', function () {
113-
var tokens = textParser.parse('a {{b | c d | f}} e')
113+
var tokens = textParser.parseText('a {{b | c d | f}} e')
114114
var exp = textParser.tokensToExp(tokens)
115-
var filters = dirParser.parse('b | c d | f').filters
115+
var filters = dirParser.parseDirective('b | c d | f').filters
116116
expect(exp).toBe(
117117
'"a "+this._applyFilters(b,null,' +
118118
JSON.stringify(filters) +

0 commit comments

Comments
 (0)
0