attribute.js 15 KB

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