Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

ternary operator #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ tmp
docs/docs/*.json
docs/coverage.html
build/
.tags
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be a separate commit?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@paulcpederson - can you explain why this needs to be a separate commit?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JamieSlome my thinking four years ago was probably that it seems like an unrelated change. But honestly I don't really remember...

26 changes: 23 additions & 3 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -617,24 +617,42 @@ exports.parse = function (swig, source, opts, tags, filters) {
return token;
}

function isTernary(chunk){
var ternexp = RegExp('^'+varOpen+'\\s*([\\w"\']+)\\s*\\?\\s*([\\w"\']+)\\s*:\\s*([\\w"\']+)\\s*'+varClose+'$|^'+varOpen+'\\s*([\\w"\']+)\\s*\\?:\\s*([\\w"\']+)\\s*'+varClose+'$','i');
return chunk.match(ternexp)
}

/*!
* Loop over the source, split via the tag/var/comment regular expression splitter.
* Send each chunk to the appropriate parser.
*/
utils.each(source.split(splitter), function (chunk) {
var chunksToParser = function (chunk) {
var token, lines, stripPrev, prevToken, prevChildToken;

if (!chunk) {
return;
}

// Ternary operator
var ternary = isTernary(chunk);
if(ternary !== null){
var ternarySource;
if(ternary[1] === undefined){
ternarySource = '{% if '+ternary[4]+' %}{{'+ternary[4]+'}}{% else %}{{'+ternary[5]+'}}{% endif %}';
} else {
ternarySource = '{% if '+ternary[1]+' %}{{'+ternary[2]+'}}{% else %}{{'+ternary[3]+'}}{% endif %}';
}
utils.each(ternarySource.split(splitter), chunksToParser);
return;
}

// Is a variable?
if (!inRaw && utils.startsWith(chunk, varOpen) && utils.endsWith(chunk, varClose)) {
stripPrev = varStripBefore.test(chunk);
stripNext = varStripAfter.test(chunk);
token = parseVariable(chunk.replace(varStrip, ''), line);
// Is a tag?
} else if (utils.startsWith(chunk, tagOpen) && utils.endsWith(chunk, tagClose)) {
} else if(utils.startsWith(chunk, tagOpen) && utils.endsWith(chunk, tagClose)) {
stripPrev = tagStripBefore.test(chunk);
stripNext = tagStripAfter.test(chunk);
token = parseTag(chunk.replace(tagStrip, ''), line);
Expand Down Expand Up @@ -687,7 +705,9 @@ exports.parse = function (swig, source, opts, tags, filters) {

lines = chunk.match(/\n/g);
line += lines ? lines.length : 0;
});
};

utils.each(source.split(splitter), chunksToParser);

return {
name: opts.filename,
Expand Down