Skip to content

Commit

Permalink
chore: Improve option handling, attribute parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 committed Sep 3, 2020
1 parent a6a7276 commit fa32fd8
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function parse(selector: string, options?: Options): Selector[][] {
function parseSelector(
subselects: Selector[][],
selector: string,
options?: Options
options: Options = {}
): string {
let tokens: Selector[] = [];
let sawWS = false;
Expand Down Expand Up @@ -212,30 +212,36 @@ function parseSelector(
});
} else if (firstChar === "[") {
selector = selector.substr(1);
const data = selector.match(reAttr);
if (!data) {
const attributeMatch = selector.match(reAttr);
if (!attributeMatch) {
throw new Error(
`Malformed attribute selector: ${selector}`
);
}
selector = selector.substr(data[0].length);
let name = unescapeCSS(data[1]);

if (
!options ||
("lowerCaseAttributeNames" in options
? options.lowerCaseAttributeNames
: !options.xmlMode)
) {
const [
completeSelector,
baseName,
actionType,
,
quotedValue = "",
value = quotedValue,
ignoreCase,
] = attributeMatch;

selector = selector.substr(completeSelector.length);
let name = unescapeCSS(baseName);

if (options.lowerCaseAttributeNames ?? !options.xmlMode) {
name = name.toLowerCase();
}

tokens.push({
type: "attribute",
name,
action: actionTypes[data[2]],
value: unescapeCSS(data[4] || data[5] || ""),
ignoreCase: !!data[6],
action: actionTypes[actionType],
value: unescapeCSS(value),
ignoreCase: !!ignoreCase,
});
} else if (firstChar === ":") {
if (selector.charAt(1) === ":") {
Expand Down Expand Up @@ -318,12 +324,7 @@ function parseSelector(
} else if (reName.test(selector)) {
let name = getName();

if (
!options ||
("lowerCaseTags" in options
? options.lowerCaseTags
: !options.xmlMode)
) {
if (options.lowerCaseTags ?? !options.xmlMode) {
name = name.toLowerCase();
}

Expand Down

0 comments on commit fa32fd8

Please sign in to comment.