Skip to content

Commit bc4b020

Browse files
committed
替换 完美解决 <t>aa{</t><t>{</t>
1 parent b2a9377 commit bc4b020

2 files changed

Lines changed: 32 additions & 54 deletions

File tree

src/MiniWord/Extensions/OpenXmlExtension.cs

Lines changed: 30 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ internal static List<Tuple<string, List<Run>, List<Text>>> GetContinuousString(t
3838
var text = run.GetFirstChild<Text>();
3939
runs.Add(run);
4040
texts.Add(text);
41-
sb.Append(text.InnerText);
41+
sb.Append(text.Text);
4242
}
4343
else
4444
{
@@ -73,9 +73,9 @@ internal static List<Tuple<string, List<Run>, List<Text>>> GetContinuousString(t
7373
if (runs.Any())
7474
tuples.Add(new Tuple<string, List<Run>, List<Text>>(sb.ToString(), runs, texts));
7575

76-
sb = new StringBuilder();
77-
runs = new List<Run>();
78-
texts = new List<Text>();
76+
sb = null;
77+
runs = null;
78+
texts = null;
7979

8080
return tuples;
8181
}
@@ -93,64 +93,42 @@ internal static void TrimStringToInContinuousString(this IEnumerable<Text> texts
9393
//优化块为:[AB][C][DE][FGH][]
9494
*/
9595

96-
var index = string.Concat(texts.SelectMany(o => o.Text)).IndexOf(text);
97-
if (index == -1)
96+
var allTxtx = string.Concat(texts.SelectMany(o => o.Text));
97+
var indexState = allTxtx.IndexOf(text);
98+
if (indexState == -1)
9899
return;
99100

100-
int i = -1;
101-
int addLengg = 0;
102-
bool isbr = false;
103-
foreach (var textWord in texts)
101+
int indexEnd = indexState + text.Length - 1;
102+
List<Tuple<int, char>> yl = new List<Tuple<int, char>>(allTxtx.Length);
103+
int iRun = 0;
104+
int iIndex = 0;
105+
int iRunOf = -1;
106+
foreach (var item in texts)
104107
{
105-
if (addLengg > 0)
108+
foreach (var item2 in item.Text)
106109
{
107-
isbr = true;
108-
var leng = textWord.Text.Length;
109-
110-
if (addLengg - leng > 0)
111-
{
112-
addLengg -= leng;
113-
textWord.Text = "";
114-
}
115-
else if (addLengg - leng == 0)
110+
if (indexState <= iIndex && iIndex <= indexEnd)
116111
{
117-
textWord.Text = "";
118-
break;
112+
if (iRunOf == -1)
113+
iRunOf = iRun;
114+
115+
yl.Add(new Tuple<int, char>(iRunOf, item2));
119116
}
120117
else
121118
{
122-
textWord.Text = textWord.Text.Substring(addLengg);
123-
}
124-
}
125-
else if (isbr)
126-
{
127-
break;
128-
}
129-
else
130-
{
131-
i += textWord.Text.Length;
132-
//开始包含
133-
if (i >= index)
134-
{
135-
//全部包含
136-
if (textWord.Text.Contains(text))
137-
{
138-
break;
139-
}
140-
//部分包含
141-
else
142-
{
143-
var str1 = textWord.Text.Substring(0, i - index + 1);
144-
if (i == index)
145-
str1 = "";
146-
147-
var str2 = str1 + text;
148-
149-
addLengg = str2.Length - textWord.Text.Length;
150-
textWord.Text = str2;
151-
}
119+
yl.Add(new Tuple<int, char>(iRun, item2));
152120
}
121+
122+
iIndex++;
153123
}
124+
iRun++;
125+
}
126+
127+
int i = 0;
128+
foreach (var item in texts)
129+
{
130+
item.Text = string.Concat(yl.Where(o => o.Item1 == i).Select(o => o.Item2));
131+
i++;
154132
}
155133

156134
}

src/MiniWord/MiniWord.Implment.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ private static void SaveAsByTemplateImpl(Stream stream, byte[] template, Diction
4343
private static void Generate(this OpenXmlElement xmlElement, WordprocessingDocument docx, Dictionary<string, object> tags)
4444
{
4545
// avoid {{tag}} like <t>{</t><t>{</t>
46-
AvoidSplitTagText(xmlElement);
46+
//AvoidSplitTagText(xmlElement);
4747
// avoid {{tag}} like <t>aa{</t><t>{</t> test in...
48-
//AvoidSplitTagText(xmlElement, tags.Select(o => "{{" + o.Key + "}}"));
48+
AvoidSplitTagText(xmlElement, tags.Select(o => "{{" + o.Key + "}}"));
4949

5050
//Tables
5151
var tables = xmlElement.Descendants<Table>().ToArray();

0 commit comments

Comments
 (0)