sky-core-styles.sky 4.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
SKY MODULE
<!-- this is part of sky:core -->
<script>
 // "internals" is an object only made visible to this module that exports stuff implemented in C++
 module.exports.registerProperty = internals.registerProperty;
 internals.registerLayoutManager('none', null);
 module.exports.LayoutManager = internals.LayoutManager;
 module.exports.InlineLayoutManager = internals.InlineLayoutManager;
 internals.registerLayoutManager('inline', internals.InlineLayoutManager);
 module.exports.ParagraphLayoutManager = internals.ParagraphLayoutManager;
 internals.registerLayoutManager('paragraph', internals.ParagraphLayoutManager);
 module.exports.BlockLayoutManager = internals.BlockLayoutManager;
 internals.registerLayoutManager('block', internals.BlockLayoutManager);

 let displayTypes = new Map();
 module.exports.registerLayoutManager = function registerLayoutManager(displayValue, layoutManagerConstructor) {
   // TODO(ianh): apply rules for type-checking displayValue is a String
   // TODO(ianh): apply rules for type-checking layoutManagerConstructor implements the LayoutManagerConstructor interface (or is null)
   if (displayTypes.has(displayValue))
     throw new Error();
   displayTypes.set(displayValue, layoutManagerConstructor);
 };

24 25
 module.exports.DisplayStyleGrammar = new StyleGrammar(); // value is null or a LayoutManagerConstructor
 module.exports.DisplayStyleGrammar.addParser((tokens) => {
26 27 28 29 30 31 32 33 34 35 36 37 38 39
   let token = tokens.next();
   if (token.done)
     throw new Error();
   if (token.value.kind != 'identifier')
     throw new Error();
   if (!displayTypes.has(token.value.value))
     throw new Error();
   return {
     value: displayTypes.get(token.value.value),
   }
 });

 internals.registerProperty({
   name: 'display',
40
   type: module.exports.DisplayStyleGrammar,
41 42 43 44 45
   inherits: false,
   initialValue: internals.BlockLayoutManager,
   needsLayout: true,
 });

46 47
 module.exports.PositiveLengthStyleGrammar = new StyleGrammar(); // value is a ParsedValue whose value (once resolved) is a number in 96dpi pixels, >=0
 module.exports.PositiveLengthStyleGrammar.addParser((tokens) => {
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
   // just handle "<number>px"
   let token = tokens.next();
   if (token.done)
     throw new Error();
   if (token.value.kind != 'dimension')
     throw new Error();
   if (token.value.unit != 'px')
     throw new Error();
   if (token.value.value < 0)
     throw new Error();
   return {
     value: token.value.value;
   };
 });

 internals.registerProperty({
   name: 'min-width',
65
   type: module.exports.PositiveLengthStyleGrammar,
66 67 68 69 70 71
   inherits: false,
   initialValue: 0,
   needsLayout: true,
 });
 internals.registerProperty({
   name: 'min-height',
72
   type: module.exports.PositiveLengthStyleGrammar,
73 74 75 76 77
   inherits: false,
   initialValue: 0,
   needsLayout: true,
 });

78 79
 module.exports.PositiveLengthOrAutoStyleGrammar = new StyleGrammar(); // value is a ParsedValue whose value (once resolved) is either a number in 96dpi pixels (>=0) or null (meaning 'auto')
 module.exports.PositiveLengthOrAutoStyleGrammar.addParser((tokens) => {
80 81 82 83 84 85 86 87 88 89 90 91
   // handle 'auto'
   let token = tokens.next();
   if (token.done)
     throw new Error();
   if (token.value.kind != 'identifier')
     throw new Error();
   if (token.value.value != 'auto')
     throw new Error();
   return {
     value: null,
   };
 });
92 93
 module.exports.PositiveLengthOrAutoStyleGrammar.addParser((tokens) => {
   return module.exports.PositiveLengthStyleGrammar.parse(tokens);
94 95 96 97
 });

 internals.registerProperty({
   name: 'width',
98
   type: module.exports.PositiveLengthOrAutoStyleGrammar,
99 100 101 102 103 104
   inherits: false,
   initialValue: null,
   needsLayout: true,
 });
 internals.registerProperty({
   name: 'height',
105
   type: module.exporets.PositiveLengthOrAutoStyleGrammar,
106 107 108 109 110
   inherits: false,
   initialValue: null,
   needsLayout: true,
 });

111 112
 module.exports.PositiveLengthOrInfinityStyleGrammar = new StyleGrammar(); // value is a ParsedValue whose value (once resolved) is either a number in 96dpi pixels (>=0) or Infinity
 module.exports.PositiveLengthOrInfinityStyleGrammar.addParser((tokens) => {
113 114 115 116 117 118 119 120 121 122 123 124
   // handle 'infinity'
   let token = tokens.next();
   if (token.done)
     throw new Error();
   if (token.value.kind != 'identifier')
     throw new Error();
   if (token.value.value != 'infinity')
     throw new Error();
   return {
     value: Infinity,
   };
 });
125 126
 module.exports.PositiveLengthOrInfinityStyleGrammar.addParser((tokens) => {
   return module.exports.PositiveLengthStyleGrammar.parse(tokens);
127 128 129 130
 });

 internals.registerProperty({
   name: 'width',
131
   type: module.exports.PositiveLengthOrInfinityStyleGrammar,
132 133 134 135 136 137
   inherits: false,
   initialValue: Infinity,
   needsLayout: true,
 });
 internals.registerProperty({
   name: 'height',
138
   type: module.exporets.PositiveLengthOrInfinityStyleGrammar,
139 140 141 142 143
   inherits: false,
   initialValue: Infinity,
   needsLayout: true,
 });
</script>