Skip to content

Commit

Permalink
Handle null check for getAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
winston0410 committed Aug 25, 2021
1 parent 53b9b63 commit d5e7d9b
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export const getProxiedObject = () => {
};

export const matchWithSelector = (element, selector) => {
if (element.type === "Fragment" || element.type.endsWith("Block")) {
if (element.type !== "Element") {
return false;
}

Expand All @@ -186,13 +186,12 @@ export const matchWithSelector = (element, selector) => {
}
}

if (element.attributes.length < 1) {
return false;
}

switch (selector.type) {
case "ClassSelector": {
const attr = getAttribute(element, "class");
if (!attr) {
return false
}
for (const className of attr.value[0].raw.split(" ")) {
if (className === selector.name) {
return true;
Expand All @@ -202,6 +201,9 @@ export const matchWithSelector = (element, selector) => {
}
case "AttributeSelector": {
const attr = getAttribute(element, selector.name.name);
if (!attr) {
return false
}
const attrValue = attr.value[0];
const unquoted = selector.value.value.replace(/(^["']|["']$)/g, "");
switch (selector.matcher) {
Expand Down Expand Up @@ -242,6 +244,9 @@ export const matchWithSelector = (element, selector) => {
}
case "IdSelector": {
const attr = getAttribute(element, "id");
if (!attr) {
return false
}
if (attr.value[0].raw === selector.name) {
return true;
}
Expand Down

0 comments on commit d5e7d9b

Please sign in to comment.