You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

514 lines
16 KiB

2 years ago
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.unescapeValue = unescapeValue;
  4. exports["default"] = void 0;
  5. var _cssesc = _interopRequireDefault(require("cssesc"));
  6. var _unesc = _interopRequireDefault(require("../util/unesc"));
  7. var _namespace = _interopRequireDefault(require("./namespace"));
  8. var _types = require("./types");
  9. var _CSSESC_QUOTE_OPTIONS;
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  11. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  12. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  13. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
  14. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  15. var deprecate = require("util-deprecate");
  16. var WRAPPED_IN_QUOTES = /^('|")([^]*)\1$/;
  17. var warnOfDeprecatedValueAssignment = deprecate(function () {}, "Assigning an attribute a value containing characters that might need to be escaped is deprecated. " + "Call attribute.setValue() instead.");
  18. var warnOfDeprecatedQuotedAssignment = deprecate(function () {}, "Assigning attr.quoted is deprecated and has no effect. Assign to attr.quoteMark instead.");
  19. var warnOfDeprecatedConstructor = deprecate(function () {}, "Constructing an Attribute selector with a value without specifying quoteMark is deprecated. Note: The value should be unescaped now.");
  20. function unescapeValue(value) {
  21. var deprecatedUsage = false;
  22. var quoteMark = null;
  23. var unescaped = value;
  24. var m = unescaped.match(WRAPPED_IN_QUOTES);
  25. if (m) {
  26. quoteMark = m[1];
  27. unescaped = m[2];
  28. }
  29. unescaped = (0, _unesc["default"])(unescaped);
  30. if (unescaped !== value) {
  31. deprecatedUsage = true;
  32. }
  33. return {
  34. deprecatedUsage: deprecatedUsage,
  35. unescaped: unescaped,
  36. quoteMark: quoteMark
  37. };
  38. }
  39. function handleDeprecatedContructorOpts(opts) {
  40. if (opts.quoteMark !== undefined) {
  41. return opts;
  42. }
  43. if (opts.value === undefined) {
  44. return opts;
  45. }
  46. warnOfDeprecatedConstructor();
  47. var _unescapeValue = unescapeValue(opts.value),
  48. quoteMark = _unescapeValue.quoteMark,
  49. unescaped = _unescapeValue.unescaped;
  50. if (!opts.raws) {
  51. opts.raws = {};
  52. }
  53. if (opts.raws.value === undefined) {
  54. opts.raws.value = opts.value;
  55. }
  56. opts.value = unescaped;
  57. opts.quoteMark = quoteMark;
  58. return opts;
  59. }
  60. var Attribute = /*#__PURE__*/function (_Namespace) {
  61. _inheritsLoose(Attribute, _Namespace);
  62. function Attribute(opts) {
  63. var _this;
  64. if (opts === void 0) {
  65. opts = {};
  66. }
  67. _this = _Namespace.call(this, handleDeprecatedContructorOpts(opts)) || this;
  68. _this.type = _types.ATTRIBUTE;
  69. _this.raws = _this.raws || {};
  70. Object.defineProperty(_this.raws, 'unquoted', {
  71. get: deprecate(function () {
  72. return _this.value;
  73. }, "attr.raws.unquoted is deprecated. Call attr.value instead."),
  74. set: deprecate(function () {
  75. return _this.value;
  76. }, "Setting attr.raws.unquoted is deprecated and has no effect. attr.value is unescaped by default now.")
  77. });
  78. _this._constructed = true;
  79. return _this;
  80. }
  81. /**
  82. * Returns the Attribute's value quoted such that it would be legal to use
  83. * in the value of a css file. The original value's quotation setting
  84. * used for stringification is left unchanged. See `setValue(value, options)`
  85. * if you want to control the quote settings of a new value for the attribute.
  86. *
  87. * You can also change the quotation used for the current value by setting quoteMark.
  88. *
  89. * Options:
  90. * * quoteMark {'"' | "'" | null} - Use this value to quote the value. If this
  91. * option is not set, the original value for quoteMark will be used. If
  92. * indeterminate, a double quote is used. The legal values are:
  93. * * `null` - the value will be unquoted and characters will be escaped as necessary.
  94. * * `'` - the value will be quoted with a single quote and single quotes are escaped.
  95. * * `"` - the value will be quoted with a double quote and double quotes are escaped.
  96. * * preferCurrentQuoteMark {boolean} - if true, prefer the source quote mark
  97. * over the quoteMark option value.
  98. * * smart {boolean} - if true, will select a quote mark based on the value
  99. * and the other options specified here. See the `smartQuoteMark()`
  100. * method.
  101. **/
  102. var _proto = Attribute.prototype;
  103. _proto.getQuotedValue = function getQuotedValue(options) {
  104. if (options === void 0) {
  105. options = {};
  106. }
  107. var quoteMark = this._determineQuoteMark(options);
  108. var cssescopts = CSSESC_QUOTE_OPTIONS[quoteMark];
  109. var escaped = (0, _cssesc["default"])(this._value, cssescopts);
  110. return escaped;
  111. };
  112. _proto._determineQuoteMark = function _determineQuoteMark(options) {
  113. return options.smart ? this.smartQuoteMark(options) : this.preferredQuoteMark(options);
  114. }
  115. /**
  116. * Set the unescaped value with the specified quotation options. The value
  117. * provided must not include any wrapping quote marks -- those quotes will
  118. * be interpreted as part of the value and escaped accordingly.
  119. */
  120. ;
  121. _proto.setValue = function setValue(value, options) {
  122. if (options === void 0) {
  123. options = {};
  124. }
  125. this._value = value;
  126. this._quoteMark = this._determineQuoteMark(options);
  127. this._syncRawValue();
  128. }
  129. /**
  130. * Intelligently select a quoteMark value based on the value's contents. If
  131. * the value is a legal CSS ident, it will not be quoted. Otherwise a quote
  132. * mark will be picked that minimizes the number of escapes.
  133. *
  134. * If there's no clear winner, the quote mark from these options is used,
  135. * then the source quote mark (this is inverted if `preferCurrentQuoteMark` is
  136. * true). If the quoteMark is unspecified, a double quote is used.
  137. *
  138. * @param options This takes the quoteMark and preferCurrentQuoteMark options
  139. * from the quoteValue method.
  140. */
  141. ;
  142. _proto.smartQuoteMark = function smartQuoteMark(options) {
  143. var v = this.value;
  144. var numSingleQuotes = v.replace(/[^']/g, '').length;
  145. var numDoubleQuotes = v.replace(/[^"]/g, '').length;
  146. if (numSingleQuotes + numDoubleQuotes === 0) {
  147. var escaped = (0, _cssesc["default"])(v, {
  148. isIdentifier: true
  149. });
  150. if (escaped === v) {
  151. return Attribute.NO_QUOTE;
  152. } else {
  153. var pref = this.preferredQuoteMark(options);
  154. if (pref === Attribute.NO_QUOTE) {
  155. // pick a quote mark that isn't none and see if it's smaller
  156. var quote = this.quoteMark || options.quoteMark || Attribute.DOUBLE_QUOTE;
  157. var opts = CSSESC_QUOTE_OPTIONS[quote];
  158. var quoteValue = (0, _cssesc["default"])(v, opts);
  159. if (quoteValue.length < escaped.length) {
  160. return quote;
  161. }
  162. }
  163. return pref;
  164. }
  165. } else if (numDoubleQuotes === numSingleQuotes) {
  166. return this.preferredQuoteMark(options);
  167. } else if (numDoubleQuotes < numSingleQuotes) {
  168. return Attribute.DOUBLE_QUOTE;
  169. } else {
  170. return Attribute.SINGLE_QUOTE;
  171. }
  172. }
  173. /**
  174. * Selects the preferred quote mark based on the options and the current quote mark value.
  175. * If you want the quote mark to depend on the attribute value, call `smartQuoteMark(opts)`
  176. * instead.
  177. */
  178. ;
  179. _proto.preferredQuoteMark = function preferredQuoteMark(options) {
  180. var quoteMark = options.preferCurrentQuoteMark ? this.quoteMark : options.quoteMark;
  181. if (quoteMark === undefined) {
  182. quoteMark = options.preferCurrentQuoteMark ? options.quoteMark : this.quoteMark;
  183. }
  184. if (quoteMark === undefined) {
  185. quoteMark = Attribute.DOUBLE_QUOTE;
  186. }
  187. return quoteMark;
  188. };
  189. _proto._syncRawValue = function _syncRawValue() {
  190. var rawValue = (0, _cssesc["default"])(this._value, CSSESC_QUOTE_OPTIONS[this.quoteMark]);
  191. if (rawValue === this._value) {
  192. if (this.raws) {
  193. delete this.raws.value;
  194. }
  195. } else {
  196. this.raws.value = rawValue;
  197. }
  198. };
  199. _proto._handleEscapes = function _handleEscapes(prop, value) {
  200. if (this._constructed) {
  201. var escaped = (0, _cssesc["default"])(value, {
  202. isIdentifier: true
  203. });
  204. if (escaped !== value) {
  205. this.raws[prop] = escaped;
  206. } else {
  207. delete this.raws[prop];
  208. }
  209. }
  210. };
  211. _proto._spacesFor = function _spacesFor(name) {
  212. var attrSpaces = {
  213. before: '',
  214. after: ''
  215. };
  216. var spaces = this.spaces[name] || {};
  217. var rawSpaces = this.raws.spaces && this.raws.spaces[name] || {};
  218. return Object.assign(attrSpaces, spaces, rawSpaces);
  219. };
  220. _proto._stringFor = function _stringFor(name, spaceName, concat) {
  221. if (spaceName === void 0) {
  222. spaceName = name;
  223. }
  224. if (concat === void 0) {
  225. concat = defaultAttrConcat;
  226. }
  227. var attrSpaces = this._spacesFor(spaceName);
  228. return concat(this.stringifyProperty(name), attrSpaces);
  229. }
  230. /**
  231. * returns the offset of the attribute part specified relative to the
  232. * start of the node of the output string.
  233. *
  234. * * "ns" - alias for "namespace"
  235. * * "namespace" - the namespace if it exists.
  236. * * "attribute" - the attribute name
  237. * * "attributeNS" - the start of the attribute or its namespace
  238. * * "operator" - the match operator of the attribute
  239. * * "value" - The value (string or identifier)
  240. * * "insensitive" - the case insensitivity flag;
  241. * @param part One of the possible values inside an attribute.
  242. * @returns -1 if the name is invalid or the value doesn't exist in this attribute.
  243. */
  244. ;
  245. _proto.offsetOf = function offsetOf(name) {
  246. var count = 1;
  247. var attributeSpaces = this._spacesFor("attribute");
  248. count += attributeSpaces.before.length;
  249. if (name === "namespace" || name === "ns") {
  250. return this.namespace ? count : -1;
  251. }
  252. if (name === "attributeNS") {
  253. return count;
  254. }
  255. count += this.namespaceString.length;
  256. if (this.namespace) {
  257. count += 1;
  258. }
  259. if (name === "attribute") {
  260. return count;
  261. }
  262. count += this.stringifyProperty("attribute").length;
  263. count += attributeSpaces.after.length;
  264. var operatorSpaces = this._spacesFor("operator");
  265. count += operatorSpaces.before.length;
  266. var operator = this.stringifyProperty("operator");
  267. if (name === "operator") {
  268. return operator ? count : -1;
  269. }
  270. count += operator.length;
  271. count += operatorSpaces.after.length;
  272. var valueSpaces = this._spacesFor("value");
  273. count += valueSpaces.before.length;
  274. var value = this.stringifyProperty("value");
  275. if (name === "value") {
  276. return value ? count : -1;
  277. }
  278. count += value.length;
  279. count += valueSpaces.after.length;
  280. var insensitiveSpaces = this._spacesFor("insensitive");
  281. count += insensitiveSpaces.before.length;
  282. if (name === "insensitive") {
  283. return this.insensitive ? count : -1;
  284. }
  285. return -1;
  286. };
  287. _proto.toString = function toString() {
  288. var _this2 = this;
  289. var selector = [this.rawSpaceBefore, '['];
  290. selector.push(this._stringFor('qualifiedAttribute', 'attribute'));
  291. if (this.operator && (this.value || this.value === '')) {
  292. selector.push(this._stringFor('operator'));
  293. selector.push(this._stringFor('value'));
  294. selector.push(this._stringFor('insensitiveFlag', 'insensitive', function (attrValue, attrSpaces) {
  295. if (attrValue.length > 0 && !_this2.quoted && attrSpaces.before.length === 0 && !(_this2.spaces.value && _this2.spaces.value.after)) {
  296. attrSpaces.before = " ";
  297. }
  298. return defaultAttrConcat(attrValue, attrSpaces);
  299. }));
  300. }
  301. selector.push(']');
  302. selector.push(this.rawSpaceAfter);
  303. return selector.join('');
  304. };
  305. _createClass(Attribute, [{
  306. key: "quoted",
  307. get: function get() {
  308. var qm = this.quoteMark;
  309. return qm === "'" || qm === '"';
  310. },
  311. set: function set(value) {
  312. warnOfDeprecatedQuotedAssignment();
  313. }
  314. /**
  315. * returns a single (`'`) or double (`"`) quote character if the value is quoted.
  316. * returns `null` if the value is not quoted.
  317. * returns `undefined` if the quotation state is unknown (this can happen when
  318. * the attribute is constructed without specifying a quote mark.)
  319. */
  320. }, {
  321. key: "quoteMark",
  322. get: function get() {
  323. return this._quoteMark;
  324. }
  325. /**
  326. * Set the quote mark to be used by this attribute's value.
  327. * If the quote mark changes, the raw (escaped) value at `attr.raws.value` of the attribute
  328. * value is updated accordingly.
  329. *
  330. * @param {"'" | '"' | null} quoteMark The quote mark or `null` if the value should be unquoted.
  331. */
  332. ,
  333. set: function set(quoteMark) {
  334. if (!this._constructed) {
  335. this._quoteMark = quoteMark;
  336. return;
  337. }
  338. if (this._quoteMark !== quoteMark) {
  339. this._quoteMark = quoteMark;
  340. this._syncRawValue();
  341. }
  342. }
  343. }, {
  344. key: "qualifiedAttribute",
  345. get: function get() {
  346. return this.qualifiedName(this.raws.attribute || this.attribute);
  347. }
  348. }, {
  349. key: "insensitiveFlag",
  350. get: function get() {
  351. return this.insensitive ? 'i' : '';
  352. }
  353. }, {
  354. key: "value",
  355. get: function get() {
  356. return this._value;
  357. }
  358. /**
  359. * Before 3.0, the value had to be set to an escaped value including any wrapped
  360. * quote marks. In 3.0, the semantics of `Attribute.value` changed so that the value
  361. * is unescaped during parsing and any quote marks are removed.
  362. *
  363. * Because the ambiguity of this semantic change, if you set `attr.value = newValue`,
  364. * a deprecation warning is raised when the new value contains any characters that would
  365. * require escaping (including if it contains wrapped quotes).
  366. *
  367. * Instead, you should call `attr.setValue(newValue, opts)` and pass options that describe
  368. * how the new value is quoted.
  369. */
  370. ,
  371. set: function set(v) {
  372. if (this._constructed) {
  373. var _unescapeValue2 = unescapeValue(v),
  374. deprecatedUsage = _unescapeValue2.deprecatedUsage,
  375. unescaped = _unescapeValue2.unescaped,
  376. quoteMark = _unescapeValue2.quoteMark;
  377. if (deprecatedUsage) {
  378. warnOfDeprecatedValueAssignment();
  379. }
  380. if (unescaped === this._value && quoteMark === this._quoteMark) {
  381. return;
  382. }
  383. this._value = unescaped;
  384. this._quoteMark = quoteMark;
  385. this._syncRawValue();
  386. } else {
  387. this._value = v;
  388. }
  389. }
  390. }, {
  391. key: "attribute",
  392. get: function get() {
  393. return this._attribute;
  394. },
  395. set: function set(name) {
  396. this._handleEscapes("attribute", name);
  397. this._attribute = name;
  398. }
  399. }]);
  400. return Attribute;
  401. }(_namespace["default"]);
  402. exports["default"] = Attribute;
  403. Attribute.NO_QUOTE = null;
  404. Attribute.SINGLE_QUOTE = "'";
  405. Attribute.DOUBLE_QUOTE = '"';
  406. var CSSESC_QUOTE_OPTIONS = (_CSSESC_QUOTE_OPTIONS = {
  407. "'": {
  408. quotes: 'single',
  409. wrap: true
  410. },
  411. '"': {
  412. quotes: 'double',
  413. wrap: true
  414. }
  415. }, _CSSESC_QUOTE_OPTIONS[null] = {
  416. isIdentifier: true
  417. }, _CSSESC_QUOTE_OPTIONS);
  418. function defaultAttrConcat(attrValue, attrSpaces) {
  419. return "" + attrSpaces.before + attrValue + attrSpaces.after;
  420. }