Skip to content

Commit

Permalink
Merge pull request #3032 from VivaLaVia/fix/style-block-regex
Browse files Browse the repository at this point in the history
Update css block regex to allow final semicolon omission
  • Loading branch information
maxkfranz authored Jun 28, 2022
2 parents e0a2a28 + 0c22210 commit 89ba597
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
2 changes: 1 addition & 1 deletion documentation/md/style.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The style specified at [initialisation](#core/initialisation) can be in a functi

### String format

Note that the trailing semicolons for each property are mandatory. Parsing will certainly fail without them.
Note that the trailing semicolons for each property, except for the last, are mandatory. Parsing will certainly fail without them.

An example style file:

Expand Down
2 changes: 1 addition & 1 deletion src/style/string-sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ styfn.appendFromString = function( string ){
let nothingLeftToParse = blockRem.match( /^\s*$/ );
if( nothingLeftToParse ){ break; }

let propAndVal = blockRem.match( /^\s*(.+?)\s*:\s*(.+?)\s*;/ );
let propAndVal = blockRem.match( /^\s*(.+?)\s*:\s*(.+?)(?:\s*;|\s*$)/ );

if( !propAndVal ){
util.warn( 'Skipping parsing of block: Invalid formatting of style property and value definitions found in:' + blockStr );
Expand Down
100 changes: 100 additions & 0 deletions test/style-string-sheet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
const expect = require('chai').expect;
const cytoscape = require('../src/test.js');

describe('String-sheet parsing', function(){
let cy;
let consoleWarn;

this.beforeAll(function() {
consoleWarn = console.warn;
console.warn = function() {};
});

this.afterAll(function() {
console.warn = consoleWarn;
});

beforeEach(function() {
cy = cytoscape({
styleEnabled: true,
elements: {
nodes: [
{ data: { id: 'n1' } },
],
},
});
});

afterEach(function(){
cy.destroy();
});

[
'#n1 { color: rgb(1,1,1); background-color: rgb(1,1,1); }',
'#n1 { color: rgb(1,1,1); background-color: rgb(1,1,1) }',
'#n1 {color: rgb(1,1,1);background-color: rgb(1,1,1);}',
`#n1 {color: rgb(1,1,1);
background-color: rgb(1,1,1);}`,
]
.forEach((stringSheet) => {
it('style block applied when parsed with no validity errors', function(){
let style = cy.$('#n1').style();
expect( style ).to.have.property('color');
expect( style['color'] ).to.exist;
expect( style['color'] ).not.to.equal('rgb(1,1,1)');
expect( style ).to.have.property('background-color');
expect( style['background-color'] ).to.exist;
expect( style['background-color'] ).not.to.equal('rgb(1,1,1)');

cy.style().fromString(stringSheet);

style = cy.$('#n1').style();
expect( style['color'] ).to.equal('rgb(1,1,1)');
expect( style['background-color'] ).to.equal('rgb(1,1,1)');
});
});

[
'#n1 { color: rgb(1,1,1) }',
'#n1 { color: rgb(1,1,1); }',
]
.forEach((stringSheet) => {
it('style block with one property applied when parsed with no validity errors', function(){
let style = cy.$('#n1').style();
expect( style ).to.have.property('color');
expect( style['color'] ).to.exist;
expect( style['color'] ).not.to.equal('rgb(1,1,1)');


cy.style().fromString(stringSheet);

style = cy.$('#n1').style();
expect( style['color'] ).to.equal('rgb(1,1,1)');
});
});

[
'#n1 { color: rgb(1,1,1) background-color: rgb(1,1,1) }',
'#n1 { color: rgb(1,1,1) background-color: rgb(1,1,1); }',
`#n1 {color: rgb(1,1,1)
background-color: rgb(1,1,1);}`,
]
.forEach((stringSheet) => {
it('style block ignored when parsed with validity error', function(){
let style = cy.$('#n1').style();
expect( style ).to.have.property('color');
expect( style['color'] ).to.exist;
expect( style['color'] ).not.to.equal('rgb(1,1,1)');
expect( style ).to.have.property('background-color');
expect( style['background-color'] ).to.exist;
expect( style['background-color'] ).not.to.equal('rgb(1,1,1)');

cy.style().fromString(stringSheet);

style = cy.$('#n1').style();
expect( style['color'] ).not.to.equal('rgb(1,1,1)');
expect( style['background-color'] ).not.to.equal('rgb(1,1,1)');
});
});

});

0 comments on commit 89ba597

Please sign in to comment.