MiniWord
MiniWord copied to clipboard
一种可能导致文本替换失效的情况
遇到很奇怪的情况,即使整个 {{tagname}} 使用格式刷,文本替换还是不生效。
查看document.xml,里面的实际情况为:
<w:r>
<w:rPr>
<w:rFonts w:hint="eastAsia" w:ascii="宋体" w:hAnsi="宋体" w:cs="宋体"/>
<w:sz w:val="48"/>
<w:szCs w:val="48"/>
</w:rPr>
<w:t>{</w:t>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:ascii="宋体" w:hAnsi="宋体" w:cs="宋体"/>
<w:sz w:val="48"/>
<w:szCs w:val="48"/>
</w:rPr>
<w:t>{type}}</w:t>
</w:r>
Samples中的Demo4也测试了类似的情况。
我写了下面一个小方法,(模仿AvoidSplitTagText),尝试处理这种情况:
private static void MergeTagInMultiText(OpenXmlElement xmlElement)
{
var texts = new LinkedList<Text>(xmlElement.Descendants<Text>());
var node = texts.First;
while (node != null)
{
if (node.Next == null) goto next;
var next = node.Next.Value;
if (!node.Value.InnerText.EndsWith("{{") && node.Value.InnerText.EndsWith("{"))
{
if (next.InnerText.StartsWith("{") && !next.InnerText.StartsWith("{{"))
{
node.Value.Text = node.Value.InnerText + "{";
next.Text = next.InnerText.Substring(1) ;
}
}
else if (node.Value.InnerText.EndsWith("}") && !node.Value.InnerText.EndsWith("}}"))
{
if (next.InnerText.StartsWith("}") && !next.InnerText.StartsWith("}}"))
{
node.Value.Text = node.Value.InnerText + "}";
next.Text = next.InnerText.Substring(1);
}
}
next:
node = node.Next;
}
}
在AvoidSplitTagText之前使用这个方法处理了一下,之前不能渲染的地方可以正常渲染了(自己一个简单的例子)。
感觉大概是这种思路,将相邻的格子里面落单的}或者{跟前后的大部队合并起来,只是可能实现的粗糙了一点。
ok