algolia / algolia/diffable-html
Invalid multiline text node indentation
- 主要语言
- JavaScript
- 星标
- 38
- 派生
- 8
- PR 合并指标
- 30 天内没有已合并 PR
描述
Currently for input:
```
My text
Another line
```
The output is:
```
My text
Another line
```
so first line is properly formatted (indentation corrected), but following lines are not.
This is an issue for me, because we are building HTML using different template strings, helpers etc, so indentation is essentially unpredictable. We use `diffable-html` in tests to compare expected HTML with actual HTML, and both expected and actual are built from template strings. Therefore, we need proper normalization.
I created patch that works for me:
```
diff --git a/dist/diffable-html.js b/dist/diffable-html.js
index c732d9e3eca544b70f1d10c8291881e3a5925c60..c4978caa2cbcc1064e33c65668feb86c22802fb7 100644
--- a/dist/diffable-html.js
+++ b/dist/diffable-html.js
@@ -206,9 +206,17 @@ var format = function(html, ref) {
return;
}
- appendLineBreak();
- appendCurrentIndentation();
- append(trimmed);
+ const splitted = text.split('\n');
+ for (const line of splitted) {
+ const lineTrimmed = line.trim();
+ if (lineTrimmed.length === 0) {
+ continue;
+ }
+
+ appendLineBreak();
+ appendCurrentIndentation();
+ append(lineTrimmed);
+ }
},
onclosetag: function(tagname) {
var isVoidTag = isVoidTagName(tagname);
```
Essentially it uses current logic for `onText` but applies it to each line. It seems it fixed my problem.
Note: this patch removes empty lines in text nodes.
贡献指南
这个仓库没有索引到贡献指南
调研方向
报告的行为在 dist/diffable-html.js 的 onText callback 中处理;请先阅读该 handler 及其周围的格式化逻辑。重现提供的多行输入,并验证每一行的预期处理方式,包括是否应保留或删除空行。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- javascript
- 领域
- tooling
- Issue 类型
- 缺陷
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 48/100