@@ -8,71 +8,56 @@ module.exports = zone
8
8
var splice = [ ] . splice
9
9
10
10
function zone ( node , name , callback ) {
11
- var nodes = [ ]
12
- var start = null
13
- var scope = null
14
- var level = 0
15
- var position
11
+ var level
12
+ var marker
13
+ var scope
16
14
17
15
visit ( node , gather )
18
16
19
17
// Gather one dimensional zones.
20
18
function gather ( node , index , parent ) {
21
- var type = test ( node )
22
-
23
- if ( scope && parent === scope ) {
24
- if ( type === 'start' ) {
25
- level ++
26
- }
27
-
28
- if ( type === 'end' ) {
29
- level --
19
+ var info = commentMarker ( node )
20
+ var match =
21
+ info && info . name === name && info . attributes . match ( / ( s t a r t | e n d ) \b / )
22
+ var type = match && match [ 0 ]
23
+ var start
24
+ var result
25
+
26
+ if ( type ) {
27
+ if ( ! scope && type === 'start' ) {
28
+ level = 0
29
+ marker = node
30
+ scope = parent
30
31
}
31
32
32
- if ( type === 'end' && level === 0 ) {
33
- nodes = callback ( start , nodes , node , {
34
- start : index - nodes . length - 1 ,
35
- end : index ,
36
- parent : scope
37
- } )
38
-
39
- if ( nodes ) {
40
- splice . apply (
41
- scope . children ,
42
- [ position , index - position + 1 ] . concat ( nodes )
43
- )
33
+ if ( scope && parent === scope ) {
34
+ if ( type === 'start' ) {
35
+ level ++
36
+ } else {
37
+ level --
44
38
}
45
39
46
- start = null
47
- scope = null
48
- position = null
49
- nodes = [ ]
50
- } else {
51
- nodes . push ( node )
52
- }
53
- }
40
+ if ( type === 'end' && ! level ) {
41
+ start = scope . children . indexOf ( marker )
54
42
55
- if ( ! scope && type === 'start' ) {
56
- level = 1
57
- position = index
58
- start = node
59
- scope = parent
60
- }
61
- }
43
+ result = callback (
44
+ marker ,
45
+ scope . children . slice ( start + 1 , index ) ,
46
+ node ,
47
+ { start : start , end : index , parent : parent }
48
+ )
62
49
63
- // Test if `node` matches the bound settings.
64
- function test ( node ) {
65
- var marker = commentMarker ( node )
66
- var attributes
67
- var head
50
+ if ( result ) {
51
+ splice . apply (
52
+ scope . children ,
53
+ [ start , index - start + 1 ] . concat ( result )
54
+ )
55
+ }
68
56
69
- if ( ! marker || marker . name !== name ) {
70
- return null
57
+ marker = undefined
58
+ scope = undefined
59
+ }
60
+ }
71
61
}
72
-
73
- attributes = marker . attributes
74
- head = attributes . match ( / ( s t a r t | e n d ) \b / )
75
-
76
- return head ? head [ 0 ] : null
77
62
}
78
63
}
0 commit comments