Skip to content

Commit

Permalink
[scss|sass] Fix bug with empty uri nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
bgriffith committed Sep 28, 2017
1 parent 78b3006 commit 63a9215
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 70 deletions.
66 changes: 31 additions & 35 deletions src/sass/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -5364,10 +5364,29 @@ function checkUri(i) {
* @return {Node} Specific type of URI node
*/
function getUri() {
const uriType = tokens[pos].uriType;
const startPos = pos;
const type = NodeType.UriType;
const token = tokens[startPos];
const line = token.ln;
const column = token.col;
let content = [];
let end;

const uriType = tokens[startPos].uriType;

// Skip `url` and `(`.
pos += 2;

if (uriType === 1) content = content.concat(getUri1());
else if (uriType === 2) content = content.concat(getUri2());
else end = getLastPosition(content, line, column, 4);

if (!end) end = getLastPosition(content, line, column, 1);

if (uriType === 1) return getUri1();
if (uriType === 2) return getUri2();
// Skip `)`.
pos++;

return newNode(type, content, line, column, end);
}

/**
Expand Down Expand Up @@ -5497,38 +5516,23 @@ function checkUri1(i) {
/**
* Get a raw (without quotes) URI
node
* @return {Node}
* @return {Array}
*/
function getUri1() {
const startPos = pos;
const type = NodeType.UriType;
const token = tokens[startPos];
const line = token.ln;
const column = token.col;
let content = [];

// Skip `url` and `(`
pos += 2;

if (checkSC(pos)) content = content.concat(getSC());

while (pos < tokens[startPos + 2].uri_end) {
while (pos < tokens[startPos].uri_end) {
if (checkInterpolation(pos)) content.push(getInterpolation());
else if (checkUriRaw(pos)) content.push(getUriRaw());
else break;
}

if (checkSC(pos)) content = content.concat(getSC());

// Check that we are at the end of the uri
if (pos < tokens[startPos + 1].right) return 0;

const end = getLastPosition(content, line, column, 1);

// Skip `)`
pos++;

return newNode(type, content, line, column, end);
return content;
}

/**
Expand All @@ -5553,38 +5557,30 @@ function checkUri2(i) {
else break;
}

// Check that we are at the end of the uri
if (i < tokens[start - 1].right) return 0;

tokens[start].uri_end = i;

return i - start;
}

/**
* Get a non-raw (with quotes) URI node
* @return {Node}
* @return {Array}
*/
function getUri2() {
const startPos = pos;
const token = tokens[startPos];
const line = token.ln;
const column = token.col;
let content = [];

// Skip `url` and `(`
pos += 2;

while (pos < tokens[startPos + 2].uri_end) {
while (pos < tokens[startPos].uri_end) {
if (checkSC(pos)) content = content.concat(getSC());
else if (checkUnary(pos)) content.push(getUnary());
else if (_checkValue(pos)) content.push(_getValue());
else break;
}

const end = getLastPosition(content, line, column, 1);

// Skip `)`
pos++;

return newNode(NodeType.UriType, content, line, column, end);
return content;
}

/**
Expand Down
66 changes: 31 additions & 35 deletions src/scss/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -4715,10 +4715,29 @@ function checkUri(i) {
* @return {Node} Specific type of URI node
*/
function getUri() {
const uriType = tokens[pos].uriType;
const startPos = pos;
const type = NodeType.UriType;
const token = tokens[startPos];
const line = token.ln;
const column = token.col;
let content = [];
let end;

const uriType = tokens[startPos].uriType;

// Skip `url` and `(`.
pos += 2;

if (uriType === 1) content = content.concat(getUri1());
else if (uriType === 2) content = content.concat(getUri2());
else end = getLastPosition(content, line, column, 4);

if (!end) end = getLastPosition(content, line, column, 1);

if (uriType === 1) return getUri1();
if (uriType === 2) return getUri2();
// Skip `)`.
pos++;

return newNode(type, content, line, column, end);
}

/**
Expand Down Expand Up @@ -4848,38 +4867,23 @@ function checkUri1(i) {
/**
* Get a raw (without quotes) URI
node
* @return {Node}
* @return {Array}
*/
function getUri1() {
const startPos = pos;
const type = NodeType.UriType;
const token = tokens[startPos];
const line = token.ln;
const column = token.col;
let content = [];

// Skip `url` and `(`
pos += 2;

if (checkSC(pos)) content = content.concat(getSC());

while (pos < tokens[startPos + 2].uri_end) {
while (pos < tokens[startPos].uri_end) {
if (checkInterpolation(pos)) content.push(getInterpolation());
else if (checkUriRaw(pos)) content.push(getUriRaw());
else break;
}

if (checkSC(pos)) content = content.concat(getSC());

// Check that we are at the end of the uri
if (pos < tokens[startPos + 1].right) return 0;

const end = getLastPosition(content, line, column, 1);

// Skip `)`
pos++;

return newNode(type, content, line, column, end);
return content;
}

/**
Expand All @@ -4904,38 +4908,30 @@ function checkUri2(i) {
else break;
}

// Check that we are at the end of the uri
if (i < tokens[start - 1].right) return 0;

tokens[start].uri_end = i;

return i - start;
}

/**
* Get a non-raw (with quotes) URI node
* @return {Node}
* @return {Array}
*/
function getUri2() {
const startPos = pos;
const token = tokens[startPos];
const line = token.ln;
const column = token.col;
let content = [];

// Skip `url` and `(`
pos += 2;

while (pos < tokens[startPos + 2].uri_end) {
while (pos < tokens[startPos].uri_end) {
if (checkSC(pos)) content = content.concat(getSC());
else if (checkUnary(pos)) content.push(getUnary());
else if (_checkValue(pos)) content.push(_getValue());
else break;
}

const end = getLastPosition(content, line, column, 1);

// Skip `)`
pos++;

return newNode(NodeType.UriType, content, line, column, end);
return content;
}

/**
Expand Down
13 changes: 13 additions & 0 deletions test/sass/uri/10.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "uri",
"content": [],
"syntax": "sass",
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 5
}
}
1 change: 1 addition & 0 deletions test/sass/uri/10.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
url()
2 changes: 2 additions & 0 deletions test/sass/uri/test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ describe 'sass/uri >>', ->
it '6', -> this.shouldBeOk()
it '7', -> this.shouldBeOk()
it '8', -> this.shouldBeOk()
it '9', -> this.shouldBeOk()
it '10', -> this.shouldBeOk()

it 'interp.0', -> this.shouldBeOk()
it 'interp.1', -> this.shouldBeOk()
Expand Down
13 changes: 13 additions & 0 deletions test/scss/uri/10.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "uri",
"content": [],
"syntax": "scss",
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 5
}
}
1 change: 1 addition & 0 deletions test/scss/uri/10.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
url()
2 changes: 2 additions & 0 deletions test/scss/uri/test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ describe 'scss/uri >>', ->
it '6', -> this.shouldBeOk()
it '7', -> this.shouldBeOk()
it '8', -> this.shouldBeOk()
it '9', -> this.shouldBeOk()
it '10', -> this.shouldBeOk()

it 'interp.0', -> this.shouldBeOk()
it 'interp.1', -> this.shouldBeOk()
Expand Down

0 comments on commit 63a9215

Please sign in to comment.