css_completions.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. var propertyMap = {
  33. "background": {"#$0": 1},
  34. "background-color": {"#$0": 1, "transparent": 1, "fixed": 1},
  35. "background-image": {"url('/$0')": 1},
  36. "background-repeat": {"repeat": 1, "repeat-x": 1, "repeat-y": 1, "no-repeat": 1, "inherit": 1},
  37. "background-position": {"bottom":2, "center":2, "left":2, "right":2, "top":2, "inherit":2},
  38. "background-attachment": {"scroll": 1, "fixed": 1},
  39. "background-size": {"cover": 1, "contain": 1},
  40. "background-clip": {"border-box": 1, "padding-box": 1, "content-box": 1},
  41. "background-origin": {"border-box": 1, "padding-box": 1, "content-box": 1},
  42. "border": {"solid $0": 1, "dashed $0": 1, "dotted $0": 1, "#$0": 1},
  43. "border-color": {"#$0": 1},
  44. "border-style": {"solid":2, "dashed":2, "dotted":2, "double":2, "groove":2, "hidden":2, "inherit":2, "inset":2, "none":2, "outset":2, "ridged":2},
  45. "border-collapse": {"collapse": 1, "separate": 1},
  46. "bottom": {"px": 1, "em": 1, "%": 1},
  47. "clear": {"left": 1, "right": 1, "both": 1, "none": 1},
  48. "color": {"#$0": 1, "rgb(#$00,0,0)": 1},
  49. "cursor": {"default": 1, "pointer": 1, "move": 1, "text": 1, "wait": 1, "help": 1, "progress": 1, "n-resize": 1, "ne-resize": 1, "e-resize": 1, "se-resize": 1, "s-resize": 1, "sw-resize": 1, "w-resize": 1, "nw-resize": 1},
  50. "display": {"none": 1, "block": 1, "inline": 1, "inline-block": 1, "table-cell": 1},
  51. "empty-cells": {"show": 1, "hide": 1},
  52. "float": {"left": 1, "right": 1, "none": 1},
  53. "font-family": {"Arial":2,"Comic Sans MS":2,"Consolas":2,"Courier New":2,"Courier":2,"Georgia":2,"Monospace":2,"Sans-Serif":2, "Segoe UI":2,"Tahoma":2,"Times New Roman":2,"Trebuchet MS":2,"Verdana": 1},
  54. "font-size": {"px": 1, "em": 1, "%": 1},
  55. "font-weight": {"bold": 1, "normal": 1},
  56. "font-style": {"italic": 1, "normal": 1},
  57. "font-variant": {"normal": 1, "small-caps": 1},
  58. "height": {"px": 1, "em": 1, "%": 1},
  59. "left": {"px": 1, "em": 1, "%": 1},
  60. "letter-spacing": {"normal": 1},
  61. "line-height": {"normal": 1},
  62. "list-style-type": {"none": 1, "disc": 1, "circle": 1, "square": 1, "decimal": 1, "decimal-leading-zero": 1, "lower-roman": 1, "upper-roman": 1, "lower-greek": 1, "lower-latin": 1, "upper-latin": 1, "georgian": 1, "lower-alpha": 1, "upper-alpha": 1},
  63. "margin": {"px": 1, "em": 1, "%": 1},
  64. "margin-right": {"px": 1, "em": 1, "%": 1},
  65. "margin-left": {"px": 1, "em": 1, "%": 1},
  66. "margin-top": {"px": 1, "em": 1, "%": 1},
  67. "margin-bottom": {"px": 1, "em": 1, "%": 1},
  68. "max-height": {"px": 1, "em": 1, "%": 1},
  69. "max-width": {"px": 1, "em": 1, "%": 1},
  70. "min-height": {"px": 1, "em": 1, "%": 1},
  71. "min-width": {"px": 1, "em": 1, "%": 1},
  72. "overflow": {"hidden": 1, "visible": 1, "auto": 1, "scroll": 1},
  73. "overflow-x": {"hidden": 1, "visible": 1, "auto": 1, "scroll": 1},
  74. "overflow-y": {"hidden": 1, "visible": 1, "auto": 1, "scroll": 1},
  75. "padding": {"px": 1, "em": 1, "%": 1},
  76. "padding-top": {"px": 1, "em": 1, "%": 1},
  77. "padding-right": {"px": 1, "em": 1, "%": 1},
  78. "padding-bottom": {"px": 1, "em": 1, "%": 1},
  79. "padding-left": {"px": 1, "em": 1, "%": 1},
  80. "page-break-after": {"auto": 1, "always": 1, "avoid": 1, "left": 1, "right": 1},
  81. "page-break-before": {"auto": 1, "always": 1, "avoid": 1, "left": 1, "right": 1},
  82. "position": {"absolute": 1, "relative": 1, "fixed": 1, "static": 1},
  83. "right": {"px": 1, "em": 1, "%": 1},
  84. "table-layout": {"fixed": 1, "auto": 1},
  85. "text-decoration": {"none": 1, "underline": 1, "line-through": 1, "blink": 1},
  86. "text-align": {"left": 1, "right": 1, "center": 1, "justify": 1},
  87. "text-transform": {"capitalize": 1, "uppercase": 1, "lowercase": 1, "none": 1},
  88. "top": {"px": 1, "em": 1, "%": 1},
  89. "vertical-align": {"top": 1, "bottom": 1},
  90. "visibility": {"hidden": 1, "visible": 1},
  91. "white-space": {"nowrap": 1, "normal": 1, "pre": 1, "pre-line": 1, "pre-wrap": 1},
  92. "width": {"px": 1, "em": 1, "%": 1},
  93. "word-spacing": {"normal": 1},
  94. // opacity
  95. "filter": {"alpha(opacity=$0100)": 1},
  96. "text-shadow": {"$02px 2px 2px #777": 1},
  97. "text-overflow": {"ellipsis-word": 1, "clip": 1, "ellipsis": 1},
  98. // border radius
  99. "-moz-border-radius": 1,
  100. "-moz-border-radius-topright": 1,
  101. "-moz-border-radius-bottomright": 1,
  102. "-moz-border-radius-topleft": 1,
  103. "-moz-border-radius-bottomleft": 1,
  104. "-webkit-border-radius": 1,
  105. "-webkit-border-top-right-radius": 1,
  106. "-webkit-border-top-left-radius": 1,
  107. "-webkit-border-bottom-right-radius": 1,
  108. "-webkit-border-bottom-left-radius": 1,
  109. // dropshadows
  110. "-moz-box-shadow": 1,
  111. "-webkit-box-shadow": 1,
  112. // transformations
  113. "transform": {"rotate($00deg)": 1, "skew($00deg)": 1},
  114. "-moz-transform": {"rotate($00deg)": 1, "skew($00deg)": 1},
  115. "-webkit-transform": {"rotate($00deg)": 1, "skew($00deg)": 1 }
  116. };
  117. var CssCompletions = function() {
  118. };
  119. (function() {
  120. this.completionsDefined = false;
  121. this.defineCompletions = function() {
  122. //fill in missing properties
  123. if (document) {
  124. var style = document.createElement('c').style;
  125. for (var i in style) {
  126. if (typeof style[i] !== 'string')
  127. continue;
  128. var name = i.replace(/[A-Z]/g, function(x) {
  129. return '-' + x.toLowerCase();
  130. });
  131. if (!propertyMap.hasOwnProperty(name))
  132. propertyMap[name] = 1;
  133. }
  134. }
  135. this.completionsDefined = true;
  136. }
  137. this.getCompletions = function(state, session, pos, prefix) {
  138. if (!this.completionsDefined) {
  139. this.defineCompletions();
  140. }
  141. var token = session.getTokenAt(pos.row, pos.column);
  142. if (!token)
  143. return [];
  144. if (state==='ruleset'){
  145. //css attribute value
  146. var line = session.getLine(pos.row).substr(0, pos.column);
  147. if (/:[^;]+$/.test(line)) {
  148. /([\w\-]+):[^:]*$/.test(line);
  149. return this.getPropertyValueCompletions(state, session, pos, prefix);
  150. } else {
  151. return this.getPropertyCompletions(state, session, pos, prefix);
  152. }
  153. }
  154. return [];
  155. };
  156. this.getPropertyCompletions = function(state, session, pos, prefix) {
  157. var properties = Object.keys(propertyMap);
  158. return properties.map(function(property){
  159. return {
  160. caption: property,
  161. snippet: property + ': $0',
  162. meta: "property",
  163. score: Number.MAX_VALUE
  164. };
  165. });
  166. };
  167. this.getPropertyValueCompletions = function(state, session, pos, prefix) {
  168. var line = session.getLine(pos.row).substr(0, pos.column);
  169. var property = (/([\w\-]+):[^:]*$/.exec(line) || {})[1];
  170. if (!property)
  171. return [];
  172. var values = [];
  173. if (property in propertyMap && typeof propertyMap[property] === "object") {
  174. values = Object.keys(propertyMap[property]);
  175. }
  176. return values.map(function(value){
  177. return {
  178. caption: value,
  179. snippet: value,
  180. meta: "property value",
  181. score: Number.MAX_VALUE
  182. };
  183. });
  184. };
  185. }).call(CssCompletions.prototype);
  186. exports.CssCompletions = CssCompletions;
  187. });