Skip to content

Added sourceIndex props #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 19, 2015
Merged
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
25 changes: 14 additions & 11 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ module.exports = function (input) {
code = value.charCodeAt(next);
} while (code <= 32);
token = value.slice(pos, next);
pos = next;

prev = tokens[tokens.length - 1];
if (code === closeParentheses && balanced) {
Expand All @@ -42,14 +41,16 @@ module.exports = function (input) {
} else if (code === slash || code === comma || code === colon) {
before = token;
} else {
tokens.push({ type: 'space', value: token });
tokens.push({ type: 'space', sourceIndex: pos, value: token });
}

pos = next;

// Quotes
} else if (code === singleQuote || code === doubleQuote) {
next = pos;
quote = code === singleQuote ? '\'' : '"';
token = { type: 'string', quote: quote };
token = { type: 'string', sourceIndex: pos, quote: quote };
do {
escape = false;
next = value.indexOf(quote, next + 1);
Expand All @@ -74,12 +75,13 @@ module.exports = function (input) {
// Dividers
} else if (code === slash || code === comma || code === colon) {
token = value[pos];
pos += 1;
code = value.charCodeAt(pos);

tokens.push({ type: 'div', value: token, before: before, after: '' });
tokens.push({ type: 'div', sourceIndex: pos - before.length, value: token, before: before, after: '' });
before = '';

pos += 1;
code = value.charCodeAt(pos);

// Open parentheses
} else if (openParentheses === code) {
// Whitespaces after open parentheses
Expand All @@ -88,7 +90,7 @@ module.exports = function (input) {
next += 1;
code = value.charCodeAt(next);
} while (code <= 32);
token = { type: 'function', value: name, before: value.slice(pos + 1, next) };
token = { type: 'function', sourceIndex: pos - name.length, value: name, before: value.slice(pos + 1, next) };
pos = next;

if (name === 'url' && code !== singleQuote && code !== doubleQuote) {
Expand All @@ -113,10 +115,10 @@ module.exports = function (input) {
whitespacePos -= 1;
code = value.charCodeAt(whitespacePos);
} while (code <= 32);
token.nodes = [{ type: 'word', value: value.slice(pos, whitespacePos + 1) }];
token.nodes = [{ type: 'word', sourceIndex: pos, value: value.slice(pos, whitespacePos + 1) }];
if (token.unclosed && whitespacePos + 1 !== next) {
token.after = '';
token.nodes.push({ type: 'space', value: value.slice(whitespacePos + 1, next) });
token.nodes.push({ type: 'space', sourceIndex: whitespacePos + 1, value: value.slice(whitespacePos + 1, next) });
} else {
token.after = value.slice(whitespacePos + 1, next);
}
Expand Down Expand Up @@ -161,13 +163,14 @@ module.exports = function (input) {
code === openParentheses || code === closeParentheses && balanced
));
token = value.slice(pos, next);
pos = next;

if (openParentheses === code) {
name = token;
} else {
tokens.push({ type: 'word', value: token });
tokens.push({ type: 'word', sourceIndex: pos, value: token });
}

pos = next;
}
}

Expand Down
28 changes: 14 additions & 14 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ test('ValueParser', function (t) {
});

t.deepEqual(result, [
{ type: 'function', value: 'fn', before: ' ', after: '', nodes: [] },
{ type: 'function', value: 'fn2', before: ' ', after: '', nodes: [
{ type: 'function', value: 'fn3', before: '', after: '', nodes: [] }
{ type: 'function', sourceIndex: 0, value: 'fn', before: ' ', after: '', nodes: [] },
{ type: 'function', sourceIndex: 6, value: 'fn2', before: ' ', after: '', nodes: [
{ type: 'function', sourceIndex: 11, value: 'fn3', before: '', after: '', nodes: [] }
] },
{ type: 'function', value: 'fn3', before: '', after: '', nodes: [] },
{ type: 'function', sourceIndex: 11, value: 'fn3', before: '', after: '', nodes: [] },
], 'should process all functions');


Expand All @@ -48,9 +48,9 @@ test('ValueParser', function (t) {
});

t.deepEqual(result, [
{ type: 'function', value: 'fn', before: ' ', after: '', nodes: [] },
{ type: 'function', value: 'fn2', before: ' ', after: '', nodes: [
{ type: 'function', value: 'fn3', before: '', after: '', nodes: [] }
{ type: 'function', sourceIndex: 0, value: 'fn', before: ' ', after: '', nodes: [] },
{ type: 'function', sourceIndex: 6, value: 'fn2', before: ' ', after: '', nodes: [
{ type: 'function', sourceIndex: 11, value: 'fn3', before: '', after: '', nodes: [] }
] },
], 'shouldn\'t process functions after falsy callback');

Expand All @@ -65,10 +65,10 @@ test('ValueParser', function (t) {
});

t.deepEqual(result, [
{ type: 'function', value: 'fn', before: ' ', after: '', nodes: [] },
{ type: 'space', value: ' '},
{ type: 'word', value: 'fn2', before: ' ', after: '', nodes: [
{ type: 'function', value: 'fn3', before: '', after: '', nodes: [] }
{ type: 'function', sourceIndex: 0, value: 'fn', before: ' ', after: '', nodes: [] },
{ type: 'space', sourceIndex: 5, value: ' '},
{ type: 'word', sourceIndex: 6, value: 'fn2', before: ' ', after: '', nodes: [
{ type: 'function', sourceIndex: 11, value: 'fn3', before: '', after: '', nodes: [] }
] },
], 'shouldn\'t process nodes with defined non-function type');

Expand All @@ -82,9 +82,9 @@ test('ValueParser', function (t) {
}, true);

t.deepEqual(result, [
{ type: 'function', value: 'fn3', before: '', after: '', nodes: [] },
{ type: 'function', value: 'fn2', before: ' ', after: '', nodes: [
{ type: 'function', value: 'fn3', before: '', after: '', nodes: [] }
{ type: 'function', sourceIndex: 5, value: 'fn3', before: '', after: '', nodes: [] },
{ type: 'function', sourceIndex: 0, value: 'fn2', before: ' ', after: '', nodes: [
{ type: 'function', sourceIndex: 5, value: 'fn3', before: '', after: '', nodes: [] }
] },
], 'should process all functions with reverse mode');
});
Expand Down
Loading