Skip to content

Commit

Permalink
Teddy bug fixes (rooseveltframework#452)
Browse files Browse the repository at this point in the history
* bug fixes

* cleanup

* trailing space

* overwrite package-lock
  • Loading branch information
dishad authored Dec 22, 2020
1 parent 5eb9d25 commit b72149b
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 13 deletions.
19 changes: 14 additions & 5 deletions looping.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function parseLoop (charList, model, passes, endParse, fs, contextModels, curren
let containsTag = false // <loop> contains other teddy tags
let containsComment = false // <loop> contains teddy comments
let parsedTags = false // <loop> variable has been parsed more than once due to additional teddy tags
let inComment = false // boolean to determine whether for loop is assessing a teddy comment
let i
let j
const l = charList.length
Expand All @@ -42,7 +43,7 @@ function parseLoop (charList, model, passes, endParse, fs, contextModels, curren
params.through = teddyName.slice(9, teddyName.length - 1)
}
teddyName = ''
} else if (currentChar === '>' && (teddyName.length > 6 || typeof sol === 'undefined')) { // End of opening <loop>
} else if (currentChar === '>' && (teddyName.length > 6 && typeof sol === 'undefined')) { // End of opening <loop>
if (teddyName.slice(0, 3) === 'key') { // params.key
params.key = teddyName.slice(5, teddyName.length - 1)
} else if (teddyName.slice(0, 3) === 'val') { // params.val
Expand All @@ -51,7 +52,7 @@ function parseLoop (charList, model, passes, endParse, fs, contextModels, curren
sol = i // Save index location of '>' of <loop> tag
teddyName = ''
} else if (currentChar === '<') { // Found either an HTML tag or teddy tag
if (twoArraysEqual(charList.slice(i - tagLengths.loop + 1, i + 1), primaryTags.loop)) { // Found a nested <loop>
if (twoArraysEqual(charList.slice(i - tagLengths.loop + 1, i + 1), primaryTags.loop) && !inComment) { // Found a nested <loop>
isNested = true
nested++
} else if (validEndingTag(charList, i) && twoArraysEqual(charList.slice(i - tagLengths.cloop + 1, i + 1), primaryTags.cloop)) { // Found </loop>
Expand All @@ -73,12 +74,16 @@ function parseLoop (charList, model, passes, endParse, fs, contextModels, curren
containsTag = true
const nextTag = charList.lastIndexOf('>', i - 1)
i = nextTag + 1
} else if (charList.slice(i - 23, i).join('').includes(' if-')) { // Found one line if
} else if (charList.slice(i - 23, i).join('').includes('-fi ')) { // Found one line if
containsTag = true
}
} else if (currentChar === '{' && charList[i - 1] === '!') { // Found start of teddy server side comment
inComment = true
} else if (currentChar === '!' && charList[i - 1] === '}') { // Found end of teddy server side comment
inComment = false
} else { // Get all <loop> attributes and their declared values
if (currentChar.match(/\s/)) continue // skip whitespace
if (Object.keys(params).length < 3 && !Object.keys(params).includes('val')) { // Make sure we end up with params.through, params.val, params.key (optional)
if (!sol && Object.keys(params).length < 3 && (!Object.keys(params).includes('val') || !Object.keys(params).includes('key'))) { // Make sure we end up with params.through, params.val (optional), params.key (optional)
teddyName += currentChar
}
}
Expand Down Expand Up @@ -248,7 +253,11 @@ function getContext (model, str, thru) {
for (let i = 0; i < str.length; i++) { // Found index
if (getIndex) { // Get numerical index to select from
if (str[i] === ']') {
currentValue = currentValue[tempIndex]
if (currentValue[tempIndex]) { // We can get value from the index
currentValue = currentValue[tempIndex]
} else { // Need to convert object values into list to get current value from index
currentValue = Object.values(currentValue)[tempIndex]
}
getIndex = false
tempIndex = ''
} else {
Expand Down
3 changes: 1 addition & 2 deletions test/conditionals.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ describe('Conditionals', function () {
done()
})

// https://github.com/rooseveltframework/teddy/issues/431
it.skip('should evaluate one line ifs in loops examining the object member\'s value correctly (conditionals/oneLineInLoop.html)', function (done) {
it('should evaluate one line ifs in loops examining the object member\'s value correctly (conditionals/oneLineInLoop.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('conditionals/oneLineInLoop.html', model), '<p class=\'something-is-present\'>guy</p><p class=\'something-is-present\'>girl</p><p class=\'something-is-present\'>landscape</p><p class=\'something-is-not-present\'>guy</p><p class=\'something-is-present\'>girl</p><p class=\'something-is-not-present\'>landscape</p>')
done()
})
Expand Down
8 changes: 6 additions & 2 deletions test/looping.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ describe('Looping', function () {
done()
})

// #446
it.skip('should loop through a nested object correctly (looping/nestedObjects.html)', function (done) {
it('should loop through a nested object correctly (looping/nestedObjects.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('looping/nestedObjects.html', model), '<p>Thing With Name 1</p><p>Thing With Name 1: Subthing With Name 1</p><p>Thing With Name 1: Subthing With Name 2</p><p>Thing With Name 1: Subthing With Name 3</p><p>Thing With Name 2</p><p>Thing With Name 2: Subthing With Name 4</p><p>Thing With Name 2: Subthing With Name 5</p><p>Thing With Name 2: Subthing With Name 6</p><p>Thing With Name 3</p><p>Thing With Name 3: Subthing With Name 7</p><p>Thing With Name 3: Subthing With Name 8</p><p>Thing With Name 3: Subthing With Name 9</p>')
done()
})
Expand Down Expand Up @@ -180,6 +179,11 @@ describe('Looping', function () {
done()
})

it('should loop through {letters} keys correctly with no val attribute (looping/loopNoVal.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('looping/loopNoVal.html', model), '<p>0</p><p>1</p><p>2</p>')
done()
})

it('should ignore loops with missing attributes (looping/loopInvalidAttributes.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('looping/loopInvalidAttributes.html', model), '<div></div>')
done()
Expand Down
2 changes: 1 addition & 1 deletion test/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ describe('Misc', function () {
done()
})

it.skip('should not render Teddy code in server-side comments in loops (misc/serverSideCommentsWithTeddyCode.html)', function (done) {
it('should not render Teddy code in server-side comments in loops (misc/serverSideCommentsWithTeddyCode.html)', function (done) {
assert.equalIgnoreSpaces(teddy.render('misc/serverSideCommentsWithTeddyCode.html', model), '<div><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p></div>')
done()
})
Expand Down
7 changes: 7 additions & 0 deletions test/templates/looping/loopNoVal.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{!
should loop through {letters} key values correctly without val attribute
!}

<loop through='letters' key='key'>
<p>{key}</p> {! outputs 0, 1, 2 !}
</loop>
2 changes: 1 addition & 1 deletion test/templates/looping/nestedObjects.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
<loop through='nestedObj' key='key' val='thing'>
<p>{key}</p>
<loop through='thing' key='subkey' val='things'>
<p>{key}: {subKey}</p>
<p>{key}: {subkey}</p>
</loop>
</loop>
4 changes: 2 additions & 2 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,9 @@ function insertValue (str, val, start, end) {
function removeTeddyComment (charList) {
let nested = 0
let i
const l = charList.length
const l = charList.length - 2 // Loop at start of comment content

for (i = l - 2; i > 0; i--) {
for (i = l; i > 0; i--) {
if (charList[i] === '{' && charList[i - 1] === '!') { // Teddy comment within teddy comment
nested++
} else if (charList[i] === '!' && charList[i - 1] === '}') { // End of teddy comment
Expand Down

0 comments on commit b72149b

Please sign in to comment.