-
-
Notifications
You must be signed in to change notification settings - Fork 97
扩展渲染table支持单元格渲染,单元格模板{{}}只有一个的时list数据渲染每个单元格,超过一个时渲染行 #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @dazhu6666, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 此拉取请求扩展了 Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request extends table rendering functionality, adding support for cell-by-cell rendering and enabling horizontal rendering mode when a template has a single placeholder. However, it introduces potential security vulnerabilities in the MiniWord library, including a high-severity arbitrary file read vulnerability due to unsanitized file paths in MiniWordPicture objects, which could lead to information disclosure. Additionally, a medium-severity Regular Expression Denial of Service (ReDoS) vulnerability exists because user-controlled input is used directly in regex construction without proper escaping. Indirect insecure deserialization is also noted as a potential risk. Beyond security concerns, the current implementation has several issues: there's room for performance optimization when detecting rendering mode, the horizontal rendering logic appears to only work for nested tables and cannot handle plain text, and data loss occurs at the end when the total number of data items is not a multiple of the number of columns. Specific code suggestions have been provided for performance and data loss, but deeper refactoring is needed to generalize the content capture mechanism for the nested table issue.
| var newTable = newTr.Descendants<Table>().FirstOrDefault(); | ||
| if (newTable != null) | ||
| elementList.Add(newTable.CloneNode(true)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| } | ||
|
|
||
| tr.Remove(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在水平渲染模式下(isHorizontal为true),如果数据列表中的项目总数不是表格列数的整数倍,循环结束后elementList中剩余的项目将会被丢弃,不会被渲染到文档中,这会导致数据丢失。您需要在循环之后添加逻辑来处理这些剩余的项目,为它们生成最后一行。
}
if (isHorizontal && elementList.Any())
{
var templateTr = tr.CloneNode(true);
var cells = templateTr.Elements<TableCell>().ToList();
for (int i = 0; i < cells.Count; i++)
{
var tCell = cells[i];
tCell.RemoveAllChildren();
if (i < elementList.Count)
{
tCell.Append(elementList[i]);
}
}
table.Append(templateTr);
}
tr.Remove();
src/MiniWord/MiniWord.Implment.cs
Outdated
| var trs = table.Descendants<TableRow>().ToArray(); // remember toarray or system will loop OOM; | ||
| var regexStr = "(?<={{).*?\\..*?(?=}})"; | ||
| //计算是否只有一个cell存在指令,如果超过1个则纵向渲染,否则横向渲染。 | ||
| bool isHorizontal = table.Elements<TableRow>().SelectMany(s => s.Elements<TableCell>()).Count(w => Regex.Matches(w.InnerText, regexStr).Count > 0) <= 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为了提高性能,建议优化此处的LINQ查询。当前的Count()会遍历所有单元格,即使在找到多个匹配项后也是如此。使用Where(...).Take(2).Count()可以在找到两个匹配的单元格后提前终止,从而提高效率。
另外,正则表达式regexStr在循环中被多次使用。为了获得更好的性能,建议将其定义为static readonly Regex字段并使用RegexOptions.Compiled进行编译。
bool isHorizontal = table.Elements<TableRow>().SelectMany(s => s.Elements<TableCell>()).Where(w => Regex.IsMatch(w.InnerText, regexStr)).Take(2).Count() <= 1;…只提取子级的tr。 不修改效果是出现大量的无效tr
1.新增扩展

模板
效果

2.修复bug
修复 当渲染table时使用 Descendants 会自动提取所有的tableRow,包括嵌套的tableRow,改用Elements,只提取子级的tr。
不修改效果是出现大量的无效tr