Last active
October 10, 2017 09:00
-
-
Save zhenchuan/0e12c1bc76a0703a7cd590031fa97748 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def data = """ | |
<div id="app"> | |
<h1>{{msg}}</h1> | |
<input type="text" m-model="msg"/> | |
</div> | |
""".toCharArray() | |
def state = [idx:0,tokens:[],data:data] | |
def text(state){ | |
def s = state.idx | |
def value = "" | |
while(state.idx<state.data.size() && state.data[state.idx]!="<"){ | |
value += state.data[state.idx] | |
state.idx = state.idx + 1 | |
} | |
println "text ==${value}==" | |
state.tokens << "text ==${value}==" | |
} | |
def tag(state){ | |
def s = state.idx | |
def data = state.data | |
def value = "" | |
def endTag = false | |
while(s<data.size() && data[s-1]!=">"){ | |
if(data[s+1]=="/"){ | |
endTag = true | |
} | |
value += data[s] | |
s = s + 1 | |
state.idx = s | |
} | |
println "tag ==${endTag} ${value}==" | |
state.tokens << "tag ==${endTag} ${value}==" | |
} | |
while(state.idx < data.length){ | |
if(data[state.idx]!="<"){ | |
text(state) | |
continue; | |
} | |
tag(state) | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def data = """ | |
<div id="app"> | |
<h1>{{msg}}</h1> | |
<h11> | |
<h22>{{msg}}</h22> | |
</h11> | |
</div> | |
""".toCharArray() | |
def tokens = [ | |
["tag":"div",open:true], | |
["tag":"h1",open:true], | |
["tag":"h1",open:false], | |
["tag":"h11",open:true], | |
["tag":"h22",open:true], | |
["tag":"h22",open:false], | |
["tag":"h11",open:false], | |
["tag":"div",open:false] | |
] | |
def state = [cur:0 , tokens:tokens] | |
def traverse(state){ | |
//当前tag | |
def tag = state.tokens[state.cur] | |
def newNode = [tag:tag,children:[]] | |
//下一个tag | |
state.cur = state.cur + 1 | |
def nextTag = state.tokens[state.cur] | |
while(nextTag.open == true){ | |
nextTag = traverse(state) | |
newNode.children << nextTag | |
nextTag = state.tokens[state.cur] | |
} | |
state.cur = state.cur + 1 //过滤掉end tag | |
println newNode | |
return newNode | |
} | |
traverse(state) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment