|
"use strict"; |
|
|
|
exports.__esModule = true; |
|
exports.unescapeValue = unescapeValue; |
|
exports["default"] = void 0; |
|
|
|
var _cssesc = _interopRequireDefault(require("cssesc")); |
|
|
|
var _unesc = _interopRequireDefault(require("../util/unesc")); |
|
|
|
var _namespace = _interopRequireDefault(require("./namespace")); |
|
|
|
var _types = require("./types"); |
|
|
|
var _CSSESC_QUOTE_OPTIONS; |
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } |
|
|
|
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); } } |
|
|
|
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } |
|
|
|
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); } |
|
|
|
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } |
|
|
|
var deprecate = require("util-deprecate"); |
|
|
|
var WRAPPED_IN_QUOTES = /^('|")([^]*)\1$/; |
|
var warnOfDeprecatedValueAssignment = deprecate(function () {}, "Assigning an attribute a value containing characters that might need to be escaped is deprecated. " + "Call attribute.setValue() instead."); |
|
var warnOfDeprecatedQuotedAssignment = deprecate(function () {}, "Assigning attr.quoted is deprecated and has no effect. Assign to attr.quoteMark instead."); |
|
var warnOfDeprecatedConstructor = deprecate(function () {}, "Constructing an Attribute selector with a value without specifying quoteMark is deprecated. Note: The value should be unescaped now."); |
|
|
|
function unescapeValue(value) { |
|
var deprecatedUsage = false; |
|
var quoteMark = null; |
|
var unescaped = value; |
|
var m = unescaped.match(WRAPPED_IN_QUOTES); |
|
|
|
if (m) { |
|
quoteMark = m[1]; |
|
unescaped = m[2]; |
|
} |
|
|
|
unescaped = (0, _unesc["default"])(unescaped); |
|
|
|
if (unescaped !== value) { |
|
deprecatedUsage = true; |
|
} |
|
|
|
return { |
|
deprecatedUsage: deprecatedUsage, |
|
unescaped: unescaped, |
|
quoteMark: quoteMark |
|
}; |
|
} |
|
|
|
function handleDeprecatedContructorOpts(opts) { |
|
if (opts.quoteMark !== undefined) { |
|
return opts; |
|
} |
|
|
|
if (opts.value === undefined) { |
|
return opts; |
|
} |
|
|
|
warnOfDeprecatedConstructor(); |
|
|
|
var _unescapeValue = unescapeValue(opts.value), |
|
quoteMark = _unescapeValue.quoteMark, |
|
unescaped = _unescapeValue.unescaped; |
|
|
|
if (!opts.raws) { |
|
opts.raws = {}; |
|
} |
|
|
|
if (opts.raws.value === undefined) { |
|
opts.raws.value = opts.value; |
|
} |
|
|
|
opts.value = unescaped; |
|
opts.quoteMark = quoteMark; |
|
return opts; |
|
} |
|
|
|
var Attribute = function (_Namespace) { |
|
_inheritsLoose(Attribute, _Namespace); |
|
|
|
function Attribute(opts) { |
|
var _this; |
|
|
|
if (opts === void 0) { |
|
opts = {}; |
|
} |
|
|
|
_this = _Namespace.call(this, handleDeprecatedContructorOpts(opts)) || this; |
|
_this.type = _types.ATTRIBUTE; |
|
_this.raws = _this.raws || {}; |
|
Object.defineProperty(_this.raws, 'unquoted', { |
|
get: deprecate(function () { |
|
return _this.value; |
|
}, "attr.raws.unquoted is deprecated. Call attr.value instead."), |
|
set: deprecate(function () { |
|
return _this.value; |
|
}, "Setting attr.raws.unquoted is deprecated and has no effect. attr.value is unescaped by default now.") |
|
}); |
|
_this._constructed = true; |
|
return _this; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var _proto = Attribute.prototype; |
|
|
|
_proto.getQuotedValue = function getQuotedValue(options) { |
|
if (options === void 0) { |
|
options = {}; |
|
} |
|
|
|
var quoteMark = this._determineQuoteMark(options); |
|
|
|
var cssescopts = CSSESC_QUOTE_OPTIONS[quoteMark]; |
|
var escaped = (0, _cssesc["default"])(this._value, cssescopts); |
|
return escaped; |
|
}; |
|
|
|
_proto._determineQuoteMark = function _determineQuoteMark(options) { |
|
return options.smart ? this.smartQuoteMark(options) : this.preferredQuoteMark(options); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
; |
|
|
|
_proto.setValue = function setValue(value, options) { |
|
if (options === void 0) { |
|
options = {}; |
|
} |
|
|
|
this._value = value; |
|
this._quoteMark = this._determineQuoteMark(options); |
|
|
|
this._syncRawValue(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; |
|
|
|
_proto.smartQuoteMark = function smartQuoteMark(options) { |
|
var v = this.value; |
|
var numSingleQuotes = v.replace(/[^']/g, '').length; |
|
var numDoubleQuotes = v.replace(/[^"]/g, '').length; |
|
|
|
if (numSingleQuotes + numDoubleQuotes === 0) { |
|
var escaped = (0, _cssesc["default"])(v, { |
|
isIdentifier: true |
|
}); |
|
|
|
if (escaped === v) { |
|
return Attribute.NO_QUOTE; |
|
} else { |
|
var pref = this.preferredQuoteMark(options); |
|
|
|
if (pref === Attribute.NO_QUOTE) { |
|
|
|
var quote = this.quoteMark || options.quoteMark || Attribute.DOUBLE_QUOTE; |
|
var opts = CSSESC_QUOTE_OPTIONS[quote]; |
|
var quoteValue = (0, _cssesc["default"])(v, opts); |
|
|
|
if (quoteValue.length < escaped.length) { |
|
return quote; |
|
} |
|
} |
|
|
|
return pref; |
|
} |
|
} else if (numDoubleQuotes === numSingleQuotes) { |
|
return this.preferredQuoteMark(options); |
|
} else if (numDoubleQuotes < numSingleQuotes) { |
|
return Attribute.DOUBLE_QUOTE; |
|
} else { |
|
return Attribute.SINGLE_QUOTE; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
; |
|
|
|
_proto.preferredQuoteMark = function preferredQuoteMark(options) { |
|
var quoteMark = options.preferCurrentQuoteMark ? this.quoteMark : options.quoteMark; |
|
|
|
if (quoteMark === undefined) { |
|
quoteMark = options.preferCurrentQuoteMark ? options.quoteMark : this.quoteMark; |
|
} |
|
|
|
if (quoteMark === undefined) { |
|
quoteMark = Attribute.DOUBLE_QUOTE; |
|
} |
|
|
|
return quoteMark; |
|
}; |
|
|
|
_proto._syncRawValue = function _syncRawValue() { |
|
var rawValue = (0, _cssesc["default"])(this._value, CSSESC_QUOTE_OPTIONS[this.quoteMark]); |
|
|
|
if (rawValue === this._value) { |
|
if (this.raws) { |
|
delete this.raws.value; |
|
} |
|
} else { |
|
this.raws.value = rawValue; |
|
} |
|
}; |
|
|
|
_proto._handleEscapes = function _handleEscapes(prop, value) { |
|
if (this._constructed) { |
|
var escaped = (0, _cssesc["default"])(value, { |
|
isIdentifier: true |
|
}); |
|
|
|
if (escaped !== value) { |
|
this.raws[prop] = escaped; |
|
} else { |
|
delete this.raws[prop]; |
|
} |
|
} |
|
}; |
|
|
|
_proto._spacesFor = function _spacesFor(name) { |
|
var attrSpaces = { |
|
before: '', |
|
after: '' |
|
}; |
|
var spaces = this.spaces[name] || {}; |
|
var rawSpaces = this.raws.spaces && this.raws.spaces[name] || {}; |
|
return Object.assign(attrSpaces, spaces, rawSpaces); |
|
}; |
|
|
|
_proto._stringFor = function _stringFor(name, spaceName, concat) { |
|
if (spaceName === void 0) { |
|
spaceName = name; |
|
} |
|
|
|
if (concat === void 0) { |
|
concat = defaultAttrConcat; |
|
} |
|
|
|
var attrSpaces = this._spacesFor(spaceName); |
|
|
|
return concat(this.stringifyProperty(name), attrSpaces); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; |
|
|
|
_proto.offsetOf = function offsetOf(name) { |
|
var count = 1; |
|
|
|
var attributeSpaces = this._spacesFor("attribute"); |
|
|
|
count += attributeSpaces.before.length; |
|
|
|
if (name === "namespace" || name === "ns") { |
|
return this.namespace ? count : -1; |
|
} |
|
|
|
if (name === "attributeNS") { |
|
return count; |
|
} |
|
|
|
count += this.namespaceString.length; |
|
|
|
if (this.namespace) { |
|
count += 1; |
|
} |
|
|
|
if (name === "attribute") { |
|
return count; |
|
} |
|
|
|
count += this.stringifyProperty("attribute").length; |
|
count += attributeSpaces.after.length; |
|
|
|
var operatorSpaces = this._spacesFor("operator"); |
|
|
|
count += operatorSpaces.before.length; |
|
var operator = this.stringifyProperty("operator"); |
|
|
|
if (name === "operator") { |
|
return operator ? count : -1; |
|
} |
|
|
|
count += operator.length; |
|
count += operatorSpaces.after.length; |
|
|
|
var valueSpaces = this._spacesFor("value"); |
|
|
|
count += valueSpaces.before.length; |
|
var value = this.stringifyProperty("value"); |
|
|
|
if (name === "value") { |
|
return value ? count : -1; |
|
} |
|
|
|
count += value.length; |
|
count += valueSpaces.after.length; |
|
|
|
var insensitiveSpaces = this._spacesFor("insensitive"); |
|
|
|
count += insensitiveSpaces.before.length; |
|
|
|
if (name === "insensitive") { |
|
return this.insensitive ? count : -1; |
|
} |
|
|
|
return -1; |
|
}; |
|
|
|
_proto.toString = function toString() { |
|
var _this2 = this; |
|
|
|
var selector = [this.rawSpaceBefore, '[']; |
|
selector.push(this._stringFor('qualifiedAttribute', 'attribute')); |
|
|
|
if (this.operator && (this.value || this.value === '')) { |
|
selector.push(this._stringFor('operator')); |
|
selector.push(this._stringFor('value')); |
|
selector.push(this._stringFor('insensitiveFlag', 'insensitive', function (attrValue, attrSpaces) { |
|
if (attrValue.length > 0 && !_this2.quoted && attrSpaces.before.length === 0 && !(_this2.spaces.value && _this2.spaces.value.after)) { |
|
attrSpaces.before = " "; |
|
} |
|
|
|
return defaultAttrConcat(attrValue, attrSpaces); |
|
})); |
|
} |
|
|
|
selector.push(']'); |
|
selector.push(this.rawSpaceAfter); |
|
return selector.join(''); |
|
}; |
|
|
|
_createClass(Attribute, [{ |
|
key: "quoted", |
|
get: function get() { |
|
var qm = this.quoteMark; |
|
return qm === "'" || qm === '"'; |
|
}, |
|
set: function set(value) { |
|
warnOfDeprecatedQuotedAssignment(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}, { |
|
key: "quoteMark", |
|
get: function get() { |
|
return this._quoteMark; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
, |
|
set: function set(quoteMark) { |
|
if (!this._constructed) { |
|
this._quoteMark = quoteMark; |
|
return; |
|
} |
|
|
|
if (this._quoteMark !== quoteMark) { |
|
this._quoteMark = quoteMark; |
|
|
|
this._syncRawValue(); |
|
} |
|
} |
|
}, { |
|
key: "qualifiedAttribute", |
|
get: function get() { |
|
return this.qualifiedName(this.raws.attribute || this.attribute); |
|
} |
|
}, { |
|
key: "insensitiveFlag", |
|
get: function get() { |
|
return this.insensitive ? 'i' : ''; |
|
} |
|
}, { |
|
key: "value", |
|
get: function get() { |
|
return this._value; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
, |
|
set: function set(v) { |
|
if (this._constructed) { |
|
var _unescapeValue2 = unescapeValue(v), |
|
deprecatedUsage = _unescapeValue2.deprecatedUsage, |
|
unescaped = _unescapeValue2.unescaped, |
|
quoteMark = _unescapeValue2.quoteMark; |
|
|
|
if (deprecatedUsage) { |
|
warnOfDeprecatedValueAssignment(); |
|
} |
|
|
|
if (unescaped === this._value && quoteMark === this._quoteMark) { |
|
return; |
|
} |
|
|
|
this._value = unescaped; |
|
this._quoteMark = quoteMark; |
|
|
|
this._syncRawValue(); |
|
} else { |
|
this._value = v; |
|
} |
|
} |
|
}, { |
|
key: "attribute", |
|
get: function get() { |
|
return this._attribute; |
|
}, |
|
set: function set(name) { |
|
this._handleEscapes("attribute", name); |
|
|
|
this._attribute = name; |
|
} |
|
}]); |
|
|
|
return Attribute; |
|
}(_namespace["default"]); |
|
|
|
exports["default"] = Attribute; |
|
Attribute.NO_QUOTE = null; |
|
Attribute.SINGLE_QUOTE = "'"; |
|
Attribute.DOUBLE_QUOTE = '"'; |
|
var CSSESC_QUOTE_OPTIONS = (_CSSESC_QUOTE_OPTIONS = { |
|
"'": { |
|
quotes: 'single', |
|
wrap: true |
|
}, |
|
'"': { |
|
quotes: 'double', |
|
wrap: true |
|
} |
|
}, _CSSESC_QUOTE_OPTIONS[null] = { |
|
isIdentifier: true |
|
}, _CSSESC_QUOTE_OPTIONS); |
|
|
|
function defaultAttrConcat(attrValue, attrSpaces) { |
|
return "" + attrSpaces.before + attrValue + attrSpaces.after; |
|
} |