harttle / md-padding

修复 Markdown 中的混排空格:中英文、数字、链接等。
https://harttle.land/md-padding/
56 stars 5 forks source link

When the scanning cursor is at the end of the input string, parsing should NOT stop #43

Open obgnail opened 4 hours ago

obgnail commented 4 hours ago

开发者你好,发现一个边界情况 BUG。

it("testcase", () => expect(padMarkdown("- ")).toEqual(`- `))

// Expected: "- "
// Received: ""

原因是 parse 函数的 while 的判断条件提前结束,导致后续的 state === State.UnorderedListItem && c === '\n' 无法执行

while (i < str.length) {
    ...
    else if (state === State.UnorderedListItem && c === '\n') {
      resolve(new UnorderedListItem(listPrefix, popNodes()), new Blank(c))
      i++
    }
}

由于 parse 的递归实现,会放大此 BUG 的影响:

it("testcase", () => {
  const src = "- <u>123</u>\n- <u>456</u>"
  expect(padMarkdown(src, { ignorePatterns: ['<u>.+?</u>'] })).toEqual(src)
})

// Expected: "- <u>123</u>\n- <u>456</u>"
// Received: "<u>123</u><u>456</u>"
obgnail commented 3 hours ago

related PR: #44