parsers.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /*********************************************************************
  2. * These are commonly used parsers for CSS Values they take a string *
  3. * to parse and return a string after it's been converted, if needed *
  4. ********************************************************************/
  5. 'use strict';
  6. const namedColors = require('./named_colors.json');
  7. exports.TYPES = {
  8. INTEGER: 1,
  9. NUMBER: 2,
  10. LENGTH: 3,
  11. PERCENT: 4,
  12. URL: 5,
  13. COLOR: 6,
  14. STRING: 7,
  15. ANGLE: 8,
  16. KEYWORD: 9,
  17. NULL_OR_EMPTY_STR: 10,
  18. };
  19. // rough regular expressions
  20. var integerRegEx = /^[-+]?[0-9]+$/;
  21. var numberRegEx = /^[-+]?[0-9]*\.[0-9]+$/;
  22. var lengthRegEx = /^(0|[-+]?[0-9]*\.?[0-9]+(in|cm|em|mm|pt|pc|px|ex|rem|vh|vw))$/;
  23. var percentRegEx = /^[-+]?[0-9]*\.?[0-9]+%$/;
  24. var urlRegEx = /^url\(\s*([^)]*)\s*\)$/;
  25. var stringRegEx = /^("[^"]*"|'[^']*')$/;
  26. var colorRegEx1 = /^#[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]([0-9a-fA-F][0-9a-fA-F][0-9a-fA-F])?$/;
  27. var colorRegEx2 = /^rgb\(([^)]*)\)$/;
  28. var colorRegEx3 = /^rgba\(([^)]*)\)$/;
  29. var colorRegEx4 = /^hsla?\(\s*(-?\d+|-?\d*.\d+)\s*,\s*(-?\d+|-?\d*.\d+)%\s*,\s*(-?\d+|-?\d*.\d+)%\s*(,\s*(-?\d+|-?\d*.\d+)\s*)?\)/;
  30. var angleRegEx = /^([-+]?[0-9]*\.?[0-9]+)(deg|grad|rad)$/;
  31. // This will return one of the above types based on the passed in string
  32. exports.valueType = function valueType(val) {
  33. if (val === '' || val === null) {
  34. return exports.TYPES.NULL_OR_EMPTY_STR;
  35. }
  36. if (typeof val === 'number') {
  37. val = val.toString();
  38. }
  39. if (typeof val !== 'string') {
  40. return undefined;
  41. }
  42. if (integerRegEx.test(val)) {
  43. return exports.TYPES.INTEGER;
  44. }
  45. if (numberRegEx.test(val)) {
  46. return exports.TYPES.NUMBER;
  47. }
  48. if (lengthRegEx.test(val)) {
  49. return exports.TYPES.LENGTH;
  50. }
  51. if (percentRegEx.test(val)) {
  52. return exports.TYPES.PERCENT;
  53. }
  54. if (urlRegEx.test(val)) {
  55. return exports.TYPES.URL;
  56. }
  57. if (stringRegEx.test(val)) {
  58. return exports.TYPES.STRING;
  59. }
  60. if (angleRegEx.test(val)) {
  61. return exports.TYPES.ANGLE;
  62. }
  63. if (colorRegEx1.test(val)) {
  64. return exports.TYPES.COLOR;
  65. }
  66. var res = colorRegEx2.exec(val);
  67. var parts;
  68. if (res !== null) {
  69. parts = res[1].split(/\s*,\s*/);
  70. if (parts.length !== 3) {
  71. return undefined;
  72. }
  73. if (
  74. parts.every(percentRegEx.test.bind(percentRegEx)) ||
  75. parts.every(integerRegEx.test.bind(integerRegEx))
  76. ) {
  77. return exports.TYPES.COLOR;
  78. }
  79. return undefined;
  80. }
  81. res = colorRegEx3.exec(val);
  82. if (res !== null) {
  83. parts = res[1].split(/\s*,\s*/);
  84. if (parts.length !== 4) {
  85. return undefined;
  86. }
  87. if (
  88. parts.slice(0, 3).every(percentRegEx.test.bind(percentRegEx)) ||
  89. parts.every(integerRegEx.test.bind(integerRegEx))
  90. ) {
  91. if (numberRegEx.test(parts[3])) {
  92. return exports.TYPES.COLOR;
  93. }
  94. }
  95. return undefined;
  96. }
  97. if (colorRegEx4.test(val)) {
  98. return exports.TYPES.COLOR;
  99. }
  100. // could still be a color, one of the standard keyword colors
  101. val = val.toLowerCase();
  102. if (namedColors.includes(val)) {
  103. return exports.TYPES.COLOR;
  104. }
  105. switch (val) {
  106. // the following are deprecated in CSS3
  107. case 'activeborder':
  108. case 'activecaption':
  109. case 'appworkspace':
  110. case 'background':
  111. case 'buttonface':
  112. case 'buttonhighlight':
  113. case 'buttonshadow':
  114. case 'buttontext':
  115. case 'captiontext':
  116. case 'graytext':
  117. case 'highlight':
  118. case 'highlighttext':
  119. case 'inactiveborder':
  120. case 'inactivecaption':
  121. case 'inactivecaptiontext':
  122. case 'infobackground':
  123. case 'infotext':
  124. case 'menu':
  125. case 'menutext':
  126. case 'scrollbar':
  127. case 'threeddarkshadow':
  128. case 'threedface':
  129. case 'threedhighlight':
  130. case 'threedlightshadow':
  131. case 'threedshadow':
  132. case 'window':
  133. case 'windowframe':
  134. case 'windowtext':
  135. return exports.TYPES.COLOR;
  136. default:
  137. return exports.TYPES.KEYWORD;
  138. }
  139. };
  140. exports.parseInteger = function parseInteger(val) {
  141. var type = exports.valueType(val);
  142. if (type === exports.TYPES.NULL_OR_EMPTY_STR) {
  143. return val;
  144. }
  145. if (type !== exports.TYPES.INTEGER) {
  146. return undefined;
  147. }
  148. return String(parseInt(val, 10));
  149. };
  150. exports.parseNumber = function parseNumber(val) {
  151. var type = exports.valueType(val);
  152. if (type === exports.TYPES.NULL_OR_EMPTY_STR) {
  153. return val;
  154. }
  155. if (type !== exports.TYPES.NUMBER && type !== exports.TYPES.INTEGER) {
  156. return undefined;
  157. }
  158. return String(parseFloat(val));
  159. };
  160. exports.parseLength = function parseLength(val) {
  161. if (val === 0 || val === '0') {
  162. return '0px';
  163. }
  164. var type = exports.valueType(val);
  165. if (type === exports.TYPES.NULL_OR_EMPTY_STR) {
  166. return val;
  167. }
  168. if (type !== exports.TYPES.LENGTH) {
  169. return undefined;
  170. }
  171. return val;
  172. };
  173. exports.parsePercent = function parsePercent(val) {
  174. if (val === 0 || val === '0') {
  175. return '0%';
  176. }
  177. var type = exports.valueType(val);
  178. if (type === exports.TYPES.NULL_OR_EMPTY_STR) {
  179. return val;
  180. }
  181. if (type !== exports.TYPES.PERCENT) {
  182. return undefined;
  183. }
  184. return val;
  185. };
  186. // either a length or a percent
  187. exports.parseMeasurement = function parseMeasurement(val) {
  188. var length = exports.parseLength(val);
  189. if (length !== undefined) {
  190. return length;
  191. }
  192. return exports.parsePercent(val);
  193. };
  194. exports.parseUrl = function parseUrl(val) {
  195. var type = exports.valueType(val);
  196. if (type === exports.TYPES.NULL_OR_EMPTY_STR) {
  197. return val;
  198. }
  199. var res = urlRegEx.exec(val);
  200. // does it match the regex?
  201. if (!res) {
  202. return undefined;
  203. }
  204. var str = res[1];
  205. // if it starts with single or double quotes, does it end with the same?
  206. if ((str[0] === '"' || str[0] === "'") && str[0] !== str[str.length - 1]) {
  207. return undefined;
  208. }
  209. if (str[0] === '"' || str[0] === "'") {
  210. str = str.substr(1, str.length - 2);
  211. }
  212. var i;
  213. for (i = 0; i < str.length; i++) {
  214. switch (str[i]) {
  215. case '(':
  216. case ')':
  217. case ' ':
  218. case '\t':
  219. case '\n':
  220. case "'":
  221. case '"':
  222. return undefined;
  223. case '\\':
  224. i++;
  225. break;
  226. }
  227. }
  228. return 'url(' + str + ')';
  229. };
  230. exports.parseString = function parseString(val) {
  231. var type = exports.valueType(val);
  232. if (type === exports.TYPES.NULL_OR_EMPTY_STR) {
  233. return val;
  234. }
  235. if (type !== exports.TYPES.STRING) {
  236. return undefined;
  237. }
  238. var i;
  239. for (i = 1; i < val.length - 1; i++) {
  240. switch (val[i]) {
  241. case val[0]:
  242. return undefined;
  243. case '\\':
  244. i++;
  245. while (i < val.length - 1 && /[0-9A-Fa-f]/.test(val[i])) {
  246. i++;
  247. }
  248. break;
  249. }
  250. }
  251. if (i >= val.length) {
  252. return undefined;
  253. }
  254. return val;
  255. };
  256. exports.parseColor = function parseColor(val) {
  257. var type = exports.valueType(val);
  258. if (type === exports.TYPES.NULL_OR_EMPTY_STR) {
  259. return val;
  260. }
  261. var red,
  262. green,
  263. blue,
  264. hue,
  265. saturation,
  266. lightness,
  267. alpha = 1;
  268. var parts;
  269. var res = colorRegEx1.exec(val);
  270. // is it #aaa or #ababab
  271. if (res) {
  272. var hex = val.substr(1);
  273. if (hex.length === 3) {
  274. hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
  275. }
  276. red = parseInt(hex.substr(0, 2), 16);
  277. green = parseInt(hex.substr(2, 2), 16);
  278. blue = parseInt(hex.substr(4, 2), 16);
  279. return 'rgb(' + red + ', ' + green + ', ' + blue + ')';
  280. }
  281. res = colorRegEx2.exec(val);
  282. if (res) {
  283. parts = res[1].split(/\s*,\s*/);
  284. if (parts.length !== 3) {
  285. return undefined;
  286. }
  287. if (parts.every(percentRegEx.test.bind(percentRegEx))) {
  288. red = Math.floor((parseFloat(parts[0].slice(0, -1)) * 255) / 100);
  289. green = Math.floor((parseFloat(parts[1].slice(0, -1)) * 255) / 100);
  290. blue = Math.floor((parseFloat(parts[2].slice(0, -1)) * 255) / 100);
  291. } else if (parts.every(integerRegEx.test.bind(integerRegEx))) {
  292. red = parseInt(parts[0], 10);
  293. green = parseInt(parts[1], 10);
  294. blue = parseInt(parts[2], 10);
  295. } else {
  296. return undefined;
  297. }
  298. red = Math.min(255, Math.max(0, red));
  299. green = Math.min(255, Math.max(0, green));
  300. blue = Math.min(255, Math.max(0, blue));
  301. return 'rgb(' + red + ', ' + green + ', ' + blue + ')';
  302. }
  303. res = colorRegEx3.exec(val);
  304. if (res) {
  305. parts = res[1].split(/\s*,\s*/);
  306. if (parts.length !== 4) {
  307. return undefined;
  308. }
  309. if (parts.slice(0, 3).every(percentRegEx.test.bind(percentRegEx))) {
  310. red = Math.floor((parseFloat(parts[0].slice(0, -1)) * 255) / 100);
  311. green = Math.floor((parseFloat(parts[1].slice(0, -1)) * 255) / 100);
  312. blue = Math.floor((parseFloat(parts[2].slice(0, -1)) * 255) / 100);
  313. alpha = parseFloat(parts[3]);
  314. } else if (parts.slice(0, 3).every(integerRegEx.test.bind(integerRegEx))) {
  315. red = parseInt(parts[0], 10);
  316. green = parseInt(parts[1], 10);
  317. blue = parseInt(parts[2], 10);
  318. alpha = parseFloat(parts[3]);
  319. } else {
  320. return undefined;
  321. }
  322. if (isNaN(alpha)) {
  323. alpha = 1;
  324. }
  325. red = Math.min(255, Math.max(0, red));
  326. green = Math.min(255, Math.max(0, green));
  327. blue = Math.min(255, Math.max(0, blue));
  328. alpha = Math.min(1, Math.max(0, alpha));
  329. if (alpha === 1) {
  330. return 'rgb(' + red + ', ' + green + ', ' + blue + ')';
  331. }
  332. return 'rgba(' + red + ', ' + green + ', ' + blue + ', ' + alpha + ')';
  333. }
  334. res = colorRegEx4.exec(val);
  335. if (res) {
  336. const [, _hue, _saturation, _lightness, _alphaString = ''] = res;
  337. const _alpha = parseFloat(_alphaString.replace(',', '').trim());
  338. if (!_hue || !_saturation || !_lightness) {
  339. return undefined;
  340. }
  341. hue = parseFloat(_hue);
  342. saturation = parseInt(_saturation, 10);
  343. lightness = parseInt(_lightness, 10);
  344. if (_alpha && numberRegEx.test(_alpha)) {
  345. alpha = parseFloat(_alpha);
  346. }
  347. if (!_alphaString || alpha === 1) {
  348. return 'hsl(' + hue + ', ' + saturation + '%, ' + lightness + '%)';
  349. }
  350. return 'hsla(' + hue + ', ' + saturation + '%, ' + lightness + '%, ' + alpha + ')';
  351. }
  352. if (type === exports.TYPES.COLOR) {
  353. return val;
  354. }
  355. return undefined;
  356. };
  357. exports.parseAngle = function parseAngle(val) {
  358. var type = exports.valueType(val);
  359. if (type === exports.TYPES.NULL_OR_EMPTY_STR) {
  360. return val;
  361. }
  362. if (type !== exports.TYPES.ANGLE) {
  363. return undefined;
  364. }
  365. var res = angleRegEx.exec(val);
  366. var flt = parseFloat(res[1]);
  367. if (res[2] === 'rad') {
  368. flt *= 180 / Math.PI;
  369. } else if (res[2] === 'grad') {
  370. flt *= 360 / 400;
  371. }
  372. while (flt < 0) {
  373. flt += 360;
  374. }
  375. while (flt > 360) {
  376. flt -= 360;
  377. }
  378. return flt + 'deg';
  379. };
  380. exports.parseKeyword = function parseKeyword(val, valid_keywords) {
  381. var type = exports.valueType(val);
  382. if (type === exports.TYPES.NULL_OR_EMPTY_STR) {
  383. return val;
  384. }
  385. if (type !== exports.TYPES.KEYWORD) {
  386. return undefined;
  387. }
  388. val = val.toString().toLowerCase();
  389. var i;
  390. for (i = 0; i < valid_keywords.length; i++) {
  391. if (valid_keywords[i].toLowerCase() === val) {
  392. return valid_keywords[i];
  393. }
  394. }
  395. return undefined;
  396. };
  397. // utility to translate from border-width to borderWidth
  398. var dashedToCamelCase = function(dashed) {
  399. var i;
  400. var camel = '';
  401. var nextCap = false;
  402. for (i = 0; i < dashed.length; i++) {
  403. if (dashed[i] !== '-') {
  404. camel += nextCap ? dashed[i].toUpperCase() : dashed[i];
  405. nextCap = false;
  406. } else {
  407. nextCap = true;
  408. }
  409. }
  410. return camel;
  411. };
  412. exports.dashedToCamelCase = dashedToCamelCase;
  413. var is_space = /\s/;
  414. var opening_deliminators = ['"', "'", '('];
  415. var closing_deliminators = ['"', "'", ')'];
  416. // this splits on whitespace, but keeps quoted and parened parts together
  417. var getParts = function(str) {
  418. var deliminator_stack = [];
  419. var length = str.length;
  420. var i;
  421. var parts = [];
  422. var current_part = '';
  423. var opening_index;
  424. var closing_index;
  425. for (i = 0; i < length; i++) {
  426. opening_index = opening_deliminators.indexOf(str[i]);
  427. closing_index = closing_deliminators.indexOf(str[i]);
  428. if (is_space.test(str[i])) {
  429. if (deliminator_stack.length === 0) {
  430. if (current_part !== '') {
  431. parts.push(current_part);
  432. }
  433. current_part = '';
  434. } else {
  435. current_part += str[i];
  436. }
  437. } else {
  438. if (str[i] === '\\') {
  439. i++;
  440. current_part += str[i];
  441. } else {
  442. current_part += str[i];
  443. if (
  444. closing_index !== -1 &&
  445. closing_index === deliminator_stack[deliminator_stack.length - 1]
  446. ) {
  447. deliminator_stack.pop();
  448. } else if (opening_index !== -1) {
  449. deliminator_stack.push(opening_index);
  450. }
  451. }
  452. }
  453. }
  454. if (current_part !== '') {
  455. parts.push(current_part);
  456. }
  457. return parts;
  458. };
  459. /*
  460. * this either returns undefined meaning that it isn't valid
  461. * or returns an object where the keys are dashed short
  462. * hand properties and the values are the values to set
  463. * on them
  464. */
  465. exports.shorthandParser = function parse(v, shorthand_for) {
  466. var obj = {};
  467. var type = exports.valueType(v);
  468. if (type === exports.TYPES.NULL_OR_EMPTY_STR) {
  469. Object.keys(shorthand_for).forEach(function(property) {
  470. obj[property] = '';
  471. });
  472. return obj;
  473. }
  474. if (typeof v === 'number') {
  475. v = v.toString();
  476. }
  477. if (typeof v !== 'string') {
  478. return undefined;
  479. }
  480. if (v.toLowerCase() === 'inherit') {
  481. return {};
  482. }
  483. var parts = getParts(v);
  484. var valid = true;
  485. parts.forEach(function(part, i) {
  486. var part_valid = false;
  487. Object.keys(shorthand_for).forEach(function(property) {
  488. if (shorthand_for[property].isValid(part, i)) {
  489. part_valid = true;
  490. obj[property] = part;
  491. }
  492. });
  493. valid = valid && part_valid;
  494. });
  495. if (!valid) {
  496. return undefined;
  497. }
  498. return obj;
  499. };
  500. exports.shorthandSetter = function(property, shorthand_for) {
  501. return function(v) {
  502. var obj = exports.shorthandParser(v, shorthand_for);
  503. if (obj === undefined) {
  504. return;
  505. }
  506. //console.log('shorthandSetter for:', property, 'obj:', obj);
  507. Object.keys(obj).forEach(function(subprop) {
  508. // in case subprop is an implicit property, this will clear
  509. // *its* subpropertiesX
  510. var camel = dashedToCamelCase(subprop);
  511. this[camel] = obj[subprop];
  512. // in case it gets translated into something else (0 -> 0px)
  513. obj[subprop] = this[camel];
  514. this.removeProperty(subprop);
  515. // don't add in empty properties
  516. if (obj[subprop] !== '') {
  517. this._values[subprop] = obj[subprop];
  518. }
  519. }, this);
  520. Object.keys(shorthand_for).forEach(function(subprop) {
  521. if (!obj.hasOwnProperty(subprop)) {
  522. this.removeProperty(subprop);
  523. delete this._values[subprop];
  524. }
  525. }, this);
  526. // in case the value is something like 'none' that removes all values,
  527. // check that the generated one is not empty, first remove the property
  528. // if it already exists, then call the shorthandGetter, if it's an empty
  529. // string, don't set the property
  530. this.removeProperty(property);
  531. var calculated = exports.shorthandGetter(property, shorthand_for).call(this);
  532. if (calculated !== '') {
  533. this._setProperty(property, calculated);
  534. }
  535. };
  536. };
  537. exports.shorthandGetter = function(property, shorthand_for) {
  538. return function() {
  539. if (this._values[property] !== undefined) {
  540. return this.getPropertyValue(property);
  541. }
  542. return Object.keys(shorthand_for)
  543. .map(function(subprop) {
  544. return this.getPropertyValue(subprop);
  545. }, this)
  546. .filter(function(value) {
  547. return value !== '';
  548. })
  549. .join(' ');
  550. };
  551. };
  552. // isValid(){1,4} | inherit
  553. // if one, it applies to all
  554. // if two, the first applies to the top and bottom, and the second to left and right
  555. // if three, the first applies to the top, the second to left and right, the third bottom
  556. // if four, top, right, bottom, left
  557. exports.implicitSetter = function(property_before, property_after, isValid, parser) {
  558. property_after = property_after || '';
  559. if (property_after !== '') {
  560. property_after = '-' + property_after;
  561. }
  562. var part_names = ['top', 'right', 'bottom', 'left'];
  563. return function(v) {
  564. if (typeof v === 'number') {
  565. v = v.toString();
  566. }
  567. if (typeof v !== 'string') {
  568. return undefined;
  569. }
  570. var parts;
  571. if (v.toLowerCase() === 'inherit' || v === '') {
  572. parts = [v];
  573. } else {
  574. parts = getParts(v);
  575. }
  576. if (parts.length < 1 || parts.length > 4) {
  577. return undefined;
  578. }
  579. if (!parts.every(isValid)) {
  580. return undefined;
  581. }
  582. parts = parts.map(function(part) {
  583. return parser(part);
  584. });
  585. this._setProperty(property_before + property_after, parts.join(' '));
  586. if (parts.length === 1) {
  587. parts[1] = parts[0];
  588. }
  589. if (parts.length === 2) {
  590. parts[2] = parts[0];
  591. }
  592. if (parts.length === 3) {
  593. parts[3] = parts[1];
  594. }
  595. for (var i = 0; i < 4; i++) {
  596. var property = property_before + '-' + part_names[i] + property_after;
  597. this.removeProperty(property);
  598. if (parts[i] !== '') {
  599. this._values[property] = parts[i];
  600. }
  601. }
  602. return v;
  603. };
  604. };
  605. //
  606. // Companion to implicitSetter, but for the individual parts.
  607. // This sets the individual value, and checks to see if all four
  608. // sub-parts are set. If so, it sets the shorthand version and removes
  609. // the individual parts from the cssText.
  610. //
  611. exports.subImplicitSetter = function(prefix, part, isValid, parser) {
  612. var property = prefix + '-' + part;
  613. var subparts = [prefix + '-top', prefix + '-right', prefix + '-bottom', prefix + '-left'];
  614. return function(v) {
  615. if (typeof v === 'number') {
  616. v = v.toString();
  617. }
  618. if (typeof v !== 'string') {
  619. return undefined;
  620. }
  621. if (!isValid(v)) {
  622. return undefined;
  623. }
  624. v = parser(v);
  625. this._setProperty(property, v);
  626. var parts = [];
  627. for (var i = 0; i < 4; i++) {
  628. if (this._values[subparts[i]] == null || this._values[subparts[i]] === '') {
  629. break;
  630. }
  631. parts.push(this._values[subparts[i]]);
  632. }
  633. if (parts.length === 4) {
  634. for (i = 0; i < 4; i++) {
  635. this.removeProperty(subparts[i]);
  636. this._values[subparts[i]] = parts[i];
  637. }
  638. this._setProperty(prefix, parts.join(' '));
  639. }
  640. return v;
  641. };
  642. };
  643. var camel_to_dashed = /[A-Z]/g;
  644. var first_segment = /^\([^-]\)-/;
  645. var vendor_prefixes = ['o', 'moz', 'ms', 'webkit'];
  646. exports.camelToDashed = function(camel_case) {
  647. var match;
  648. var dashed = camel_case.replace(camel_to_dashed, '-$&').toLowerCase();
  649. match = dashed.match(first_segment);
  650. if (match && vendor_prefixes.indexOf(match[1]) !== -1) {
  651. dashed = '-' + dashed;
  652. }
  653. return dashed;
  654. };