UNPKG

3.56 kBJavaScriptView Raw
1import * as util from '../util';
2import Selector from '../selector';
3
4let styfn = {};
5
6styfn.appendFromString = function( string ){
7 let self = this;
8 let style = this;
9 let remaining = '' + string;
10 let selAndBlockStr;
11 let blockRem;
12 let propAndValStr;
13
14 // remove comments from the style string
15 remaining = remaining.replace( /[/][*](\s|.)+?[*][/]/g, '' );
16
17 function removeSelAndBlockFromRemaining(){
18 // remove the parsed selector and block from the remaining text to parse
19 if( remaining.length > selAndBlockStr.length ){
20 remaining = remaining.substr( selAndBlockStr.length );
21 } else {
22 remaining = '';
23 }
24 }
25
26 function removePropAndValFromRem(){
27 // remove the parsed property and value from the remaining block text to parse
28 if( blockRem.length > propAndValStr.length ){
29 blockRem = blockRem.substr( propAndValStr.length );
30 } else {
31 blockRem = '';
32 }
33 }
34
35 for(;;){
36 let nothingLeftToParse = remaining.match( /^\s*$/ );
37 if( nothingLeftToParse ){ break; }
38
39 let selAndBlock = remaining.match( /^\s*((?:.|\s)+?)\s*\{((?:.|\s)+?)\}/ );
40
41 if( !selAndBlock ){
42 util.warn( 'Halting stylesheet parsing: String stylesheet contains more to parse but no selector and block found in: ' + remaining );
43 break;
44 }
45
46 selAndBlockStr = selAndBlock[0];
47
48 // parse the selector
49 let selectorStr = selAndBlock[1];
50 if( selectorStr !== 'core' ){
51 let selector = new Selector( selectorStr );
52 if( selector.invalid ){
53 util.warn( 'Skipping parsing of block: Invalid selector found in string stylesheet: ' + selectorStr );
54
55 // skip this selector and block
56 removeSelAndBlockFromRemaining();
57 continue;
58 }
59 }
60
61 // parse the block of properties and values
62 let blockStr = selAndBlock[2];
63 let invalidBlock = false;
64 blockRem = blockStr;
65 let props = [];
66
67 for(;;){
68 let nothingLeftToParse = blockRem.match( /^\s*$/ );
69 if( nothingLeftToParse ){ break; }
70
71 let propAndVal = blockRem.match( /^\s*(.+?)\s*:\s*(.+?)\s*;/ );
72
73 if( !propAndVal ){
74 util.warn( 'Skipping parsing of block: Invalid formatting of style property and value definitions found in:' + blockStr );
75 invalidBlock = true;
76 break;
77 }
78
79 propAndValStr = propAndVal[0];
80 let propStr = propAndVal[1];
81 let valStr = propAndVal[2];
82
83 let prop = self.properties[ propStr ];
84 if( !prop ){
85 util.warn( 'Skipping property: Invalid property name in: ' + propAndValStr );
86
87 // skip this property in the block
88 removePropAndValFromRem();
89 continue;
90 }
91
92 let parsedProp = style.parse( propStr, valStr );
93
94 if( !parsedProp ){
95 util.warn( 'Skipping property: Invalid property definition in: ' + propAndValStr );
96
97 // skip this property in the block
98 removePropAndValFromRem();
99 continue;
100 }
101
102 props.push( {
103 name: propStr,
104 val: valStr
105 } );
106 removePropAndValFromRem();
107 }
108
109 if( invalidBlock ){
110 removeSelAndBlockFromRemaining();
111 break;
112 }
113
114 // put the parsed block in the style
115 style.selector( selectorStr );
116 for( let i = 0; i < props.length; i++ ){
117 let prop = props[ i ];
118 style.css( prop.name, prop.val );
119 }
120
121 removeSelAndBlockFromRemaining();
122 }
123
124 return style;
125};
126
127styfn.fromString = function( string ){
128 let style = this;
129
130 style.resetToDefault();
131 style.appendFromString( string );
132
133 return style;
134};
135
136export default styfn;