Skip to content

Commit

Permalink
Fix #20
Browse files Browse the repository at this point in the history
Updated existing test to exhibit more complex cases.
Changed unclosed attribute quote test so only quote is missing,
not paren
  • Loading branch information
Thomas-git authored and jescalan committed Jun 10, 2019
1 parent d594017 commit 5404556
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
25 changes: 14 additions & 11 deletions lib/lexer.js
Expand Up @@ -144,14 +144,15 @@ module.exports = function Lexer(input, options = {}) {
// we're in attribute land until the *close paren*
while (char !== ')') {
// attempt to get the attribute key
// - it can't contain */* to avoid malformed html (*/* will be skipped)
// - if it has a value, it will end at the *=*
// - if it's boolean, it will end at the *paren* or *space* (next attr)
const key = collectUntil('=|)|\\s/')

// if we do have a key, add it to our tokens, otherwise move on
// if we do have a key, add it to our tokens, otherwise if we have a space or invalid '/' char in attr name move on (example : attr = 'foo')
if (key.length) {
addToken('attributeKey', key)
} else {
} else if (char && (char.match(/\s/) || char.match(/\//)) && !char.match(/\n/)) {
next()
}

Expand All @@ -168,17 +169,17 @@ module.exports = function Lexer(input, options = {}) {
next()
}

// now we grab the value, it ends at a *space*, *close paren*, or
// now we grab the value, it ends at a *space*, *close paren*, *newline* or
// a *close quote*. if it's quoted though, we don't match *space*.
// this is because you can have div(class='foo bar')
let regex = '\\s|)|\'|"'
if (quoted) {
regex = quoted
regex = quoted + '|\n'
}
val += collectUntil(regex)

// if there is no next character, we have a hanging open quote
if (!char) {
if (!char || (quoted && char.match(/\n/))) {
throw new SugarmlError({
message: `Unclosed attribute quote`,
location: {
Expand All @@ -191,13 +192,17 @@ module.exports = function Lexer(input, options = {}) {
}

// if there's a close quote, move past it
if (char.match(/['"]/)) next()
if (quoted) next()
}

// if we did match a value, push it to tokens
if (val.length) addToken('attributeValue', val)

if (!char) {
// if we have a *space*, move on to the next attribute
if (char && char.match(/\s/) && !char.match(/\n/)) next()

//If we run out of chars or char or next char are a new line when attribute list isn't closed, that's a syntax error
if (!char || char.match(/\n/) || (char !== ')' && nextChar() && nextChar().match(/\n/))) {
throw new SugarmlError({
message: `Unclosed attribute parentheses`,
location: {
Expand All @@ -209,11 +214,9 @@ module.exports = function Lexer(input, options = {}) {
})
}

// if we have a *space*, move on to the next attribute
if (char.match(/\s/)) next()
}
// done with attributes, move past the *close paren*
if (char === ')') next()
next()
}
}

Expand Down
6 changes: 5 additions & 1 deletion test/fixtures/unclosed-attribute-paren.sgr
@@ -1 +1,5 @@
a(b
ul
li(
img(src=foo
li: img(src='bar'
li(src='')
2 changes: 1 addition & 1 deletion test/fixtures/unclosed-attribute-quote.sgr
@@ -1 +1 @@
a(href='a
a(href='a)

0 comments on commit 5404556

Please sign in to comment.