Skip to content

Commit

Permalink
incorrect shorthand parsing #6
Browse files Browse the repository at this point in the history
  • Loading branch information
tbela99 committed Aug 13, 2023
1 parent de70a7e commit e8dc908
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 115 deletions.
71 changes: 43 additions & 28 deletions dist/index-umd-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -1639,6 +1639,10 @@
str = options.removeComments ? '' : node.val;
}
else if (node.typ == 'Declaration') {
if (node.val.length == 0) {
console.error(`invalid declaration`, node);
return '';
}
str = `${node.nam}:${options.indent}${node.val.reduce(reducer, '').trimEnd()};`;
}
else if (node.typ == 'AtRule' && !('chi' in node)) {
Expand Down Expand Up @@ -2031,6 +2035,9 @@
this.pattern = config.pattern.split(/\s/);
}
add(declaration) {
for (const val of declaration.val) {
Object.defineProperty(val, 'propertyName', { enumerable: false, writable: true, value: declaration.nam });
}
if (declaration.nam == this.config.shorthand) {
this.declarations = new Map;
this.declarations.set(declaration.nam, declaration);
Expand Down Expand Up @@ -2064,7 +2071,7 @@
i--;
continue;
}
if (matchType(acc[i], props)) {
if (('propertyName' in acc[i] && acc[i].propertyName == property) || matchType(acc[i], props)) {
if ('prefix' in props && props.previous != null && !(props.previous in tokens)) {
return acc;
}
Expand Down Expand Up @@ -2198,10 +2205,12 @@
}
else {
let count = 0;
let match;
const separator = this.config.separator;
const tokens = {};
// @ts-ignore
/* const valid: string[] =*/ Object.entries(this.config.properties).reduce((acc, curr) => {
/* const valid: string[] =*/
Object.entries(this.config.properties).reduce((acc, curr) => {
if (!this.declarations.has(curr[0])) {
if (curr[1].required) {
acc.push(curr[0]);
Expand All @@ -2210,33 +2219,39 @@
}
let current = 0;
const props = this.config.properties[curr[0]];
const declaration = this.declarations.get(curr[0]);
// @ts-ignore
for (const val of (declaration instanceof PropertySet ? [...declaration][0] : declaration).val) {
if (separator != null && separator.typ == val.typ && eq(separator, val)) {
current++;
if (tokens[curr[0]].length == current) {
tokens[curr[0]].push([]);
const properties = this.declarations.get(curr[0]);
for (const declaration of [(properties instanceof PropertySet ? [...properties][0] : properties)]) {
// @ts-ignore
for (const val of declaration.val) {
if (separator != null && separator.typ == val.typ && eq(separator, val)) {
current++;
if (tokens[curr[0]].length == current) {
tokens[curr[0]].push([]);
}
continue;
}
continue;
}
if (val.typ == 'Whitespace' || val.typ == 'Comment') {
continue;
}
if (props.multiple && props.separator != null && props.separator.typ == val.typ && eq(props.separator, val)) {
continue;
}
if (matchType(val, curr[1])) {
if (!(curr[0] in tokens)) {
tokens[curr[0]] = [[]];
if (val.typ == 'Whitespace' || val.typ == 'Comment') {
continue;
}
if (props.multiple && props.separator != null && props.separator.typ == val.typ && eq(props.separator, val)) {
continue;
}
match = matchType(val, curr[1]);
if (isShorthand) {
isShorthand = match;
}
if (('propertyName' in val && val.propertyName == property) || match) {
if (!(curr[0] in tokens)) {
tokens[curr[0]] = [[]];
}
// is default value
tokens[curr[0]][current].push(val);
// continue;
}
else {
acc.push(curr[0]);
break;
}
// is default value
tokens[curr[0]][current].push(val);
// continue;
}
else {
acc.push(curr[0]);
break;
}
}
if (count == 0) {
Expand All @@ -2245,7 +2260,7 @@
return acc;
}, []);
count++;
if (Object.entries(this.config.properties).some(entry => {
if (!isShorthand || Object.entries(this.config.properties).some(entry => {
// missing required property
return entry[1].required && !(entry[0] in tokens);
}) || !Object.values(tokens).every(v => v.length == count)) {
Expand Down
71 changes: 43 additions & 28 deletions dist/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,10 @@ function doRender(data, options, reducer, level = 0, indents = []) {
str = options.removeComments ? '' : node.val;
}
else if (node.typ == 'Declaration') {
if (node.val.length == 0) {
console.error(`invalid declaration`, node);
return '';
}
str = `${node.nam}:${options.indent}${node.val.reduce(reducer, '').trimEnd()};`;
}
else if (node.typ == 'AtRule' && !('chi' in node)) {
Expand Down Expand Up @@ -2029,6 +2033,9 @@ class PropertyMap {
this.pattern = config.pattern.split(/\s/);
}
add(declaration) {
for (const val of declaration.val) {
Object.defineProperty(val, 'propertyName', { enumerable: false, writable: true, value: declaration.nam });
}
if (declaration.nam == this.config.shorthand) {
this.declarations = new Map;
this.declarations.set(declaration.nam, declaration);
Expand Down Expand Up @@ -2062,7 +2069,7 @@ class PropertyMap {
i--;
continue;
}
if (matchType(acc[i], props)) {
if (('propertyName' in acc[i] && acc[i].propertyName == property) || matchType(acc[i], props)) {
if ('prefix' in props && props.previous != null && !(props.previous in tokens)) {
return acc;
}
Expand Down Expand Up @@ -2196,10 +2203,12 @@ class PropertyMap {
}
else {
let count = 0;
let match;
const separator = this.config.separator;
const tokens = {};
// @ts-ignore
/* const valid: string[] =*/ Object.entries(this.config.properties).reduce((acc, curr) => {
/* const valid: string[] =*/
Object.entries(this.config.properties).reduce((acc, curr) => {
if (!this.declarations.has(curr[0])) {
if (curr[1].required) {
acc.push(curr[0]);
Expand All @@ -2208,33 +2217,39 @@ class PropertyMap {
}
let current = 0;
const props = this.config.properties[curr[0]];
const declaration = this.declarations.get(curr[0]);
// @ts-ignore
for (const val of (declaration instanceof PropertySet ? [...declaration][0] : declaration).val) {
if (separator != null && separator.typ == val.typ && eq(separator, val)) {
current++;
if (tokens[curr[0]].length == current) {
tokens[curr[0]].push([]);
const properties = this.declarations.get(curr[0]);
for (const declaration of [(properties instanceof PropertySet ? [...properties][0] : properties)]) {
// @ts-ignore
for (const val of declaration.val) {
if (separator != null && separator.typ == val.typ && eq(separator, val)) {
current++;
if (tokens[curr[0]].length == current) {
tokens[curr[0]].push([]);
}
continue;
}
continue;
}
if (val.typ == 'Whitespace' || val.typ == 'Comment') {
continue;
}
if (props.multiple && props.separator != null && props.separator.typ == val.typ && eq(props.separator, val)) {
continue;
}
if (matchType(val, curr[1])) {
if (!(curr[0] in tokens)) {
tokens[curr[0]] = [[]];
if (val.typ == 'Whitespace' || val.typ == 'Comment') {
continue;
}
if (props.multiple && props.separator != null && props.separator.typ == val.typ && eq(props.separator, val)) {
continue;
}
match = matchType(val, curr[1]);
if (isShorthand) {
isShorthand = match;
}
if (('propertyName' in val && val.propertyName == property) || match) {
if (!(curr[0] in tokens)) {
tokens[curr[0]] = [[]];
}
// is default value
tokens[curr[0]][current].push(val);
// continue;
}
else {
acc.push(curr[0]);
break;
}
// is default value
tokens[curr[0]][current].push(val);
// continue;
}
else {
acc.push(curr[0]);
break;
}
}
if (count == 0) {
Expand All @@ -2243,7 +2258,7 @@ class PropertyMap {
return acc;
}, []);
count++;
if (Object.entries(this.config.properties).some(entry => {
if (!isShorthand || Object.entries(this.config.properties).some(entry => {
// missing required property
return entry[1].required && !(entry[0] in tokens);
}) || !Object.values(tokens).every(v => v.length == count)) {
Expand Down
67 changes: 39 additions & 28 deletions dist/lib/parser/declaration/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class PropertyMap {
this.pattern = config.pattern.split(/\s/);
}
add(declaration) {
for (const val of declaration.val) {
Object.defineProperty(val, 'propertyName', { enumerable: false, writable: true, value: declaration.nam });
}
if (declaration.nam == this.config.shorthand) {
this.declarations = new Map;
this.declarations.set(declaration.nam, declaration);
Expand Down Expand Up @@ -52,7 +55,7 @@ class PropertyMap {
i--;
continue;
}
if (matchType(acc[i], props)) {
if (('propertyName' in acc[i] && acc[i].propertyName == property) || matchType(acc[i], props)) {
if ('prefix' in props && props.previous != null && !(props.previous in tokens)) {
return acc;
}
Expand Down Expand Up @@ -186,10 +189,12 @@ class PropertyMap {
}
else {
let count = 0;
let match;
const separator = this.config.separator;
const tokens = {};
// @ts-ignore
/* const valid: string[] =*/ Object.entries(this.config.properties).reduce((acc, curr) => {
/* const valid: string[] =*/
Object.entries(this.config.properties).reduce((acc, curr) => {
if (!this.declarations.has(curr[0])) {
if (curr[1].required) {
acc.push(curr[0]);
Expand All @@ -198,33 +203,39 @@ class PropertyMap {
}
let current = 0;
const props = this.config.properties[curr[0]];
const declaration = this.declarations.get(curr[0]);
// @ts-ignore
for (const val of (declaration instanceof PropertySet ? [...declaration][0] : declaration).val) {
if (separator != null && separator.typ == val.typ && eq(separator, val)) {
current++;
if (tokens[curr[0]].length == current) {
tokens[curr[0]].push([]);
const properties = this.declarations.get(curr[0]);
for (const declaration of [(properties instanceof PropertySet ? [...properties][0] : properties)]) {
// @ts-ignore
for (const val of declaration.val) {
if (separator != null && separator.typ == val.typ && eq(separator, val)) {
current++;
if (tokens[curr[0]].length == current) {
tokens[curr[0]].push([]);
}
continue;
}
continue;
}
if (val.typ == 'Whitespace' || val.typ == 'Comment') {
continue;
}
if (props.multiple && props.separator != null && props.separator.typ == val.typ && eq(props.separator, val)) {
continue;
}
if (matchType(val, curr[1])) {
if (!(curr[0] in tokens)) {
tokens[curr[0]] = [[]];
if (val.typ == 'Whitespace' || val.typ == 'Comment') {
continue;
}
if (props.multiple && props.separator != null && props.separator.typ == val.typ && eq(props.separator, val)) {
continue;
}
match = matchType(val, curr[1]);
if (isShorthand) {
isShorthand = match;
}
if (('propertyName' in val && val.propertyName == property) || match) {
if (!(curr[0] in tokens)) {
tokens[curr[0]] = [[]];
}
// is default value
tokens[curr[0]][current].push(val);
// continue;
}
else {
acc.push(curr[0]);
break;
}
// is default value
tokens[curr[0]][current].push(val);
// continue;
}
else {
acc.push(curr[0]);
break;
}
}
if (count == 0) {
Expand All @@ -233,7 +244,7 @@ class PropertyMap {
return acc;
}, []);
count++;
if (Object.entries(this.config.properties).some(entry => {
if (!isShorthand || Object.entries(this.config.properties).some(entry => {
// missing required property
return entry[1].required && !(entry[0] in tokens);
}) || !Object.values(tokens).every(v => v.length == count)) {
Expand Down
4 changes: 4 additions & 0 deletions dist/lib/renderer/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ function doRender(data, options, reducer, level = 0, indents = []) {
str = options.removeComments ? '' : node.val;
}
else if (node.typ == 'Declaration') {
if (node.val.length == 0) {
console.error(`invalid declaration`, node);
return '';
}
str = `${node.nam}:${options.indent}${node.val.reduce(reducer, '').trimEnd()};`;
}
else if (node.typ == 'AtRule' && !('chi' in node)) {
Expand Down
Loading

0 comments on commit e8dc908

Please sign in to comment.