@@ -19,7 +19,7 @@ def parser(self, init=True):
1919 value = ""
2020 output .children .append (self .parser (init = False ))
2121 output .children [- 1 ].parent = output
22- output .children [- 1 ].before = output .children [ - 2 ]
22+ output .children [- 1 ].key = len ( output .children ) - 1
2323 elif char == "}" :
2424 if value != "" :
2525 output .children .append (value )
@@ -38,8 +38,26 @@ class JsData:
3838 def __init__ (self ):
3939 self .children : list = []
4040 self .parent : JsData = None
41- self .after = None
42- self .before = None
41+ self .key = None
42+
43+
44+ @property
45+ def before (self ):
46+ if self .parent is None :
47+ return None
48+ if self .key > 0 :
49+ return self .parent .children [self .key - 1 ]
50+ return None
51+
52+ @property
53+ def after (self ):
54+ if self .parent is None :
55+ return None
56+ if self .key < len (self .parent .children ) - 1 :
57+ return self .parent .children [self .key + 1 ]
58+ return None
59+
60+
4361
4462 def __repr__ (self ):
4563 return json .dumps (self .to_list ())
@@ -58,10 +76,25 @@ class JsSearchData:
5876 def __init__ (self ):
5977 self .children : list = []
6078 self .parent : JsData = None
61- self .after = None
62- self .before = None
6379 self .data = None
64-
80+ self .key = None
81+
82+ @property
83+ def before (self ):
84+ if self .parent is None :
85+ return None
86+ if self .key > 0 :
87+ return self .parent .children [self .key - 1 ]
88+ return None
89+
90+ @property
91+ def after (self ):
92+ if self .parent is None :
93+ return None
94+ if self .key < len (self .parent .children ) - 1 :
95+ return self .parent .children [self .key + 1 ]
96+ return None
97+
6598 def __repr__ (self ):
6699 return json .dumps (self .children )
67100
@@ -86,11 +119,8 @@ def search_js(text: JsData, search: str) -> list[JsSearchData]:
86119 output .append (JsSearchData ())
87120 output [- 1 ].children = text .children
88121 output [- 1 ].parent = text
89- if len (text .children ) > key + 1 :
90- output [- 1 ].after = text .children [key + 1 ]
91- if len (text .children ) > 0 :
92- output [- 1 ].before = text .children [key - 1 ]
93122 output [- 1 ].data = search
123+ output [- 1 ].key = key
94124 break
95125 return output
96126
@@ -108,11 +138,8 @@ def search_js_reg(text: JsData, search: str) -> list[JsSearchData]:
108138 output .append (JsSearchData ())
109139 output [- 1 ].children = text .children [key ]
110140 output [- 1 ].parent = text
111- if len (text .children ) > key + 1 :
112- output [- 1 ].after = text .children [key + 1 ]
113- if len (text .children ) > 0 :
114- output [- 1 ].before = text .children [key - 1 ]
115141 output [- 1 ].data = find
142+ output [- 1 ].key = key
116143 break
117144 return output
118145
0 commit comments