creeperyang/html-parser-lite

Problem parsing alt=""

Closed this issue · 2 comments

Hi, I'm using your parser for my project https://github.com/holtwick/hostic-dom and found an issue while parsing a line like this:

<img src="/assets/ocr@2x-97ede361.png" alt="" width="621" height="422">

The alt attribute will result in a string containing "" instead of an empty string.

Thanks for your lib, its pretty cool!

The solution is to check for != null instead of simply !, because an empty string will fail the || chain. In modern syntax this could be solved by ?? like in this fix proposal:

parseAttributes(tagName, input) {
    const attrs = {}
    input.replace(this.attrRe, (attr, name, c2, value, c4, valueInQuote, c6, valueInSingleQuote) => {
      attrs[name] = valueInSingleQuote ?? valueInQuote ?? value ?? true
    })
    return attrs
  }

The solution is to check for != null instead of simply !, because an empty string will fail the || chain. In modern syntax this could be solved by ?? like in this fix proposal:

parseAttributes(tagName, input) {
    const attrs = {}
    input.replace(this.attrRe, (attr, name, c2, value, c4, valueInQuote, c6, valueInSingleQuote) => {
      attrs[name] = valueInSingleQuote ?? valueInQuote ?? value ?? true
    })
    return attrs
  }

I will check it.