style_builders.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. "use strict";
  2. /**
  3. * @license
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. Object.defineProperty(exports, "__esModule", { value: true });
  7. exports.concatStyles = exports.safeStyle = void 0;
  8. require("../environment/dev");
  9. var string_literal_1 = require("../internals/string_literal");
  10. var style_impl_1 = require("../internals/style_impl");
  11. /**
  12. * Creates a SafeStyle object from a template literal (without any embedded
  13. * expressions).
  14. *
  15. * ` style` should be in the format
  16. * ` name: value; [name: value; ...]` and must not have any < or >
  17. * characters in it. This is so that SafeStyle's contract is preserved,
  18. * allowing the SafeStyle to correctly be interpreted as a sequence of CSS
  19. * declarations and without affecting the syntactic structure of any
  20. * surrounding CSS and HTML.
  21. *
  22. * This function is a template literal tag function. It should be called with
  23. * a template literal that does not contain any expressions. For example,
  24. * safeStyle`foo`;
  25. * This function first checks if it is called with a literal template, and
  26. * then performs basic sanity checks on the format of ` style`
  27. * but does not constrain the format of ` name} and {@code value`, except
  28. * for disallowing tag characters.
  29. *
  30. * @param templateObj This contains the literal part of the template literal.
  31. */
  32. function safeStyle(templateObj) {
  33. if (process.env.NODE_ENV !== 'production') {
  34. (0, string_literal_1.assertIsTemplateObject)(templateObj, false, 'safeStyle is a template literal tag function ' +
  35. 'that only accepts template literals without expressions. ' +
  36. 'For example, safeStyle`foo`;');
  37. }
  38. var style = templateObj[0];
  39. if (process.env.NODE_ENV !== 'production') {
  40. if (/[<>]/.test(style)) {
  41. throw new Error('Forbidden characters in style string: ' + style);
  42. }
  43. if (!/;$/.test(style)) {
  44. throw new Error('Style string does not end with ";": ' + style);
  45. }
  46. if (!/:/.test(style)) {
  47. throw new Error('Style string should contain one or more ":": ' + style);
  48. }
  49. }
  50. return (0, style_impl_1.createStyle)(style);
  51. }
  52. exports.safeStyle = safeStyle;
  53. /** Creates a `SafeStyle` value by concatenating multiple `SafeStyle`s. */
  54. function concatStyles(styles) {
  55. return (0, style_impl_1.createStyle)(styles.map(style_impl_1.unwrapStyle).join(''));
  56. }
  57. exports.concatStyles = concatStyles;