Skip to content

Commit

Permalink
Adds guard against invalid id selectors
Browse files Browse the repository at this point in the history
This fixes #24
  • Loading branch information
rhardih committed Nov 2, 2018
1 parent f5153a9 commit 3365951
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
28 changes: 17 additions & 11 deletions js/ekill.js
Expand Up @@ -126,19 +126,25 @@
} else {
let hitList = JSON.parse(item.ekillHitlistV2);
let hierarchy = ekill.generateElementHierarchy(element);
let selector = ekill.elementHierarchyToDOMString(hierarchy);

ekill.addHit(hitList, l.hostname, l.pathname, selector);
try {
let selector = ekill.elementHierarchyToDOMString(hierarchy);

c.storage.local.set({
"ekillHitlistV2": JSON.stringify(hitList)
}, _ => {
if (c.runtime.lastError) {
console.error(c.runtime.lastError);
} else {
callback();
}
});
ekill.addHit(hitList, l.hostname, l.pathname, selector);

c.storage.local.set({
"ekillHitlistV2": JSON.stringify(hitList)
}, _ => {
if (c.runtime.lastError) {
console.error(c.runtime.lastError);
} else {
callback();
}
});
} catch (e) {
console.log("ekill: removed element not saved");
console.error(e);
}
}
});
}
Expand Down
4 changes: 4 additions & 0 deletions test/index.html
Expand Up @@ -69,6 +69,10 @@
<p data-target="ehtd-06-target"></p>
<p></p>
</div>
<div id="ehtd-07">
<!-- invalid id -->
<div id=895d data-target="ehtd-07-target"></div>
</div>
</div>
</div>

Expand Down
8 changes: 8 additions & 0 deletions test/test.js
Expand Up @@ -134,6 +134,14 @@ describe("ekill", function() {
expect(ds).to.equal("#ehtd-06 > p:nth-of-type(1)");
expect(selected).to.equal(target);
});

it("should throw an error for invalid css id", function() {
let target = document.querySelector("[data-target=ehtd-07-target]");
let hierarchy = ekill.generateElementHierarchy(target);
let proc = _ => ekill.elementHierarchyToDOMString(hierarchy);

expect(proc).to.throw();
});
});

describe("#addHit", function() {
Expand Down

0 comments on commit 3365951

Please sign in to comment.