commit
18c72bf15c
37 changed files with 774 additions and 365 deletions
@ -0,0 +1,18 @@ |
||||
import { JSONTextSettings, SettingSource } from '../../src/shared/SettingData'; |
||||
import Settings from '../../src/shared/settings/Settings'; |
||||
|
||||
export default class SettingRepository { |
||||
constructor( |
||||
private readonly browser: any, |
||||
) { |
||||
} |
||||
|
||||
async saveJSON(settings: Settings): Promise<void> { |
||||
await this.browser.storage.local.set({ |
||||
settings: { |
||||
source: SettingSource.JSON, |
||||
json: JSONTextSettings.fromSettings(settings).toJSONText(), |
||||
} |
||||
}); |
||||
} |
||||
} |
@ -0,0 +1,58 @@ |
||||
import * as path from 'path'; |
||||
import * as assert from 'assert'; |
||||
|
||||
import TestServer from './lib/TestServer'; |
||||
import { Builder, Lanthan } from 'lanthan'; |
||||
import { WebDriver } from 'selenium-webdriver'; |
||||
import Page from './lib/Page'; |
||||
import Settings from '../src/shared/settings/Settings'; |
||||
import SettingRepository from './lib/SettingRepository'; |
||||
|
||||
describe("partial blacklist test", () => { |
||||
let server = new TestServer().receiveContent('/*', |
||||
`<!DOCTYPE html><html lang="en"><body style="width:10000px; height:10000px"></body></html>`, |
||||
); |
||||
let lanthan: Lanthan; |
||||
let webdriver: WebDriver; |
||||
let browser: any; |
||||
|
||||
before(async() => { |
||||
lanthan = await Builder |
||||
.forBrowser('firefox') |
||||
.spyAddon(path.join(__dirname, '..')) |
||||
.build(); |
||||
webdriver = lanthan.getWebDriver(); |
||||
browser = lanthan.getWebExtBrowser(); |
||||
await server.start(); |
||||
|
||||
let url = server.url().replace('http://', ''); |
||||
await new SettingRepository(browser).saveJSON(Settings.fromJSON({ |
||||
keymaps: { |
||||
j: { type: 'scroll.vertically', count: 1 }, |
||||
k: { type: 'scroll.vertically', count: -1 }, |
||||
}, |
||||
blacklist: [ |
||||
{ 'url': url, 'keys': ['k'] } |
||||
] |
||||
})); |
||||
}); |
||||
|
||||
after(async() => { |
||||
await server.stop(); |
||||
if (lanthan) { |
||||
await lanthan.quit(); |
||||
} |
||||
}); |
||||
|
||||
it('should disable keys in the partial blacklist', async () => { |
||||
let page = await Page.navigateTo(webdriver, server.url('/')); |
||||
|
||||
await page.sendKeys('j'); |
||||
let scrollY = await page.getScrollY(); |
||||
assert.strictEqual(scrollY, 64); |
||||
|
||||
await page.sendKeys('k'); |
||||
scrollY = await page.getScrollY(); |
||||
assert.strictEqual(scrollY, 64); |
||||
}); |
||||
}); |
@ -1,86 +0,0 @@ |
||||
export default { |
||||
source: 'json', |
||||
json: `{
|
||||
"keymaps": { |
||||
"0": { "type": "scroll.home" }, |
||||
":": { "type": "command.show" }, |
||||
"o": { "type": "command.show.open", "alter": false }, |
||||
"O": { "type": "command.show.open", "alter": true }, |
||||
"t": { "type": "command.show.tabopen", "alter": false }, |
||||
"T": { "type": "command.show.tabopen", "alter": true }, |
||||
"w": { "type": "command.show.winopen", "alter": false }, |
||||
"W": { "type": "command.show.winopen", "alter": true }, |
||||
"b": { "type": "command.show.buffer" }, |
||||
"a": { "type": "command.show.addbookmark", "alter": true }, |
||||
"k": { "type": "scroll.vertically", "count": -1 }, |
||||
"j": { "type": "scroll.vertically", "count": 1 }, |
||||
"h": { "type": "scroll.horizonally", "count": -1 }, |
||||
"l": { "type": "scroll.horizonally", "count": 1 }, |
||||
"<C-U>": { "type": "scroll.pages", "count": -0.5 }, |
||||
"<C-D>": { "type": "scroll.pages", "count": 0.5 }, |
||||
"<C-B>": { "type": "scroll.pages", "count": -1 }, |
||||
"<C-F>": { "type": "scroll.pages", "count": 1 }, |
||||
"gg": { "type": "scroll.top" }, |
||||
"G": { "type": "scroll.bottom" }, |
||||
"$": { "type": "scroll.end" }, |
||||
"d": { "type": "tabs.close" }, |
||||
"D": { "type": "tabs.close", "select": "left" }, |
||||
"x$": { "type": "tabs.close.right" }, |
||||
"!d": { "type": "tabs.close.force" }, |
||||
"u": { "type": "tabs.reopen" }, |
||||
"K": { "type": "tabs.prev", "count": 1 }, |
||||
"J": { "type": "tabs.next", "count": 1 }, |
||||
"gT": { "type": "tabs.prev", "count": 1 }, |
||||
"gt": { "type": "tabs.next", "count": 1 }, |
||||
"g0": { "type": "tabs.first" }, |
||||
"g$": { "type": "tabs.last" }, |
||||
"<C-6>": { "type": "tabs.prevsel" }, |
||||
"r": { "type": "tabs.reload", "cache": false }, |
||||
"R": { "type": "tabs.reload", "cache": true }, |
||||
"zp": { "type": "tabs.pin.toggle" }, |
||||
"zd": { "type": "tabs.duplicate" }, |
||||
"zi": { "type": "zoom.in" }, |
||||
"zo": { "type": "zoom.out" }, |
||||
"zz": { "type": "zoom.neutral" }, |
||||
"f": { "type": "follow.start", "newTab": false }, |
||||
"F": { "type": "follow.start", "newTab": true, "background": false }, |
||||
"m": { "type": "mark.set.prefix" }, |
||||
"'": { "type": "mark.jump.prefix" }, |
||||
"H": { "type": "navigate.history.prev" }, |
||||
"L": { "type": "navigate.history.next" }, |
||||
"[[": { "type": "navigate.link.prev" }, |
||||
"]]": { "type": "navigate.link.next" }, |
||||
"gu": { "type": "navigate.parent" }, |
||||
"gU": { "type": "navigate.root" }, |
||||
"gi": { "type": "focus.input" }, |
||||
"gf": { "type": "page.source" }, |
||||
"gh": { "type": "page.home" }, |
||||
"gH": { "type": "page.home", "newTab": true }, |
||||
"y": { "type": "urls.yank" }, |
||||
"p": { "type": "urls.paste", "newTab": false }, |
||||
"P": { "type": "urls.paste", "newTab": true }, |
||||
"/": { "type": "find.start" }, |
||||
"n": { "type": "find.next" }, |
||||
"N": { "type": "find.prev" }, |
||||
"<S-Esc>": { "type": "addon.toggle.enabled" } |
||||
}, |
||||
"search": { |
||||
"default": "google", |
||||
"engines": { |
||||
"google": "http://127.0.0.1:12321/google?q={}", |
||||
"yahoo": "http://127.0.0.1:12321/yahoo?q={}", |
||||
"bing": "http://127.0.0.1:12321/bind?q={}", |
||||
"duckduckgo": "http://127.0.0.1:12321/duplicate?q={}", |
||||
"twitter": "http://127.0.0.1:12321/twitter?q={}", |
||||
"wikipedia": "http://127.0.0.1:12321/wikipedia?q={}" |
||||
} |
||||
}, |
||||
"properties": { |
||||
"hintchars": "abcdefghijklmnopqrstuvwxyz", |
||||
"smoothscroll": false, |
||||
"complete": "sbh" |
||||
}, |
||||
"blacklist": [ |
||||
] |
||||
}`,
|
||||
}; |
@ -0,0 +1,9 @@ |
||||
export default interface AddressRepository { |
||||
getCurrentURL(): URL |
||||
} |
||||
|
||||
export class AddressRepositoryImpl implements AddressRepository { |
||||
getCurrentURL(): URL { |
||||
return new URL(window.location.href); |
||||
} |
||||
} |
@ -0,0 +1,28 @@ |
||||
.form-partial-blacklist-form { |
||||
@mixin row-base { |
||||
display: flex; |
||||
|
||||
.column-url { |
||||
flex: 5; |
||||
min-width: 0; |
||||
} |
||||
.column-keys { |
||||
flex: 1; |
||||
min-width: 0; |
||||
} |
||||
.column-delete { |
||||
flex: 1; |
||||
min-width: 0; |
||||
} |
||||
} |
||||
|
||||
&-header { |
||||
@include row-base; |
||||
|
||||
font-weight: bold; |
||||
} |
||||
|
||||
&-row { |
||||
@include row-base; |
||||
} |
||||
} |
@ -0,0 +1,79 @@ |
||||
import './PartialBlacklistForm.scss'; |
||||
import AddButton from '../ui/AddButton'; |
||||
import DeleteButton from '../ui/DeleteButton'; |
||||
import React from 'react'; |
||||
import Blacklist, { BlacklistItem } from '../../../shared/settings/Blacklist'; |
||||
|
||||
interface Props { |
||||
value: Blacklist; |
||||
onChange: (value: Blacklist) => void; |
||||
onBlur: () => void; |
||||
} |
||||
|
||||
class PartialBlacklistForm extends React.Component<Props> { |
||||
public static defaultProps: Props = { |
||||
value: new Blacklist([]), |
||||
onChange: () => {}, |
||||
onBlur: () => {}, |
||||
}; |
||||
|
||||
render() { |
||||
return <div className='form-partial-blacklist-form'> |
||||
<div className='form-partial-blacklist-form-header'> |
||||
<div className='column-url'>URL</div> |
||||
<div className='column-keys'>Keys</div> |
||||
</div> |
||||
{ |
||||
this.props.value.items.map((item, index) => { |
||||
if (!item.partial) { |
||||
return null; |
||||
} |
||||
return <div key={index} className='form-partial-blacklist-form-row'> |
||||
<input data-index={index} type='text' name='url' |
||||
className='column-url' value={item.pattern} |
||||
onChange={this.bindValue.bind(this)} |
||||
onBlur={this.props.onBlur} |
||||
/> |
||||
<input data-index={index} type='text' name='keys' |
||||
className='column-keys' value={item.keys.join(',')} |
||||
onChange={this.bindValue.bind(this)} |
||||
onBlur={this.props.onBlur} |
||||
/> |
||||
<DeleteButton data-index={index} name='delete' |
||||
onClick={this.bindValue.bind(this)} |
||||
onBlur={this.props.onBlur} |
||||
/> |
||||
</div>; |
||||
}) |
||||
} |
||||
<AddButton name='add' style={{ float: 'right' }} |
||||
onClick={this.bindValue.bind(this)} /> |
||||
</div>; |
||||
} |
||||
|
||||
bindValue(e: any) { |
||||
let name = e.target.name; |
||||
let index = e.target.getAttribute('data-index'); |
||||
let items = this.props.value.items; |
||||
|
||||
if (name === 'url') { |
||||
let current = items[index]; |
||||
items[index] = new BlacklistItem(e.target.value, true, current.keys); |
||||
} else if (name === 'keys') { |
||||
let current = items[index]; |
||||
items[index] = new BlacklistItem( |
||||
current.pattern, true, e.target.value.split(',')); |
||||
} else if (name === 'add') { |
||||
items.push(new BlacklistItem('', true, [])); |
||||
} else if (name === 'delete') { |
||||
items.splice(index, 1); |
||||
} |
||||
|
||||
this.props.onChange(new Blacklist(items)); |
||||
if (name === 'delete') { |
||||
this.props.onBlur(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
export default PartialBlacklistForm; |
@ -1,39 +1,125 @@ |
||||
export type BlacklistJSON = string[]; |
||||
import Key from './Key'; |
||||
|
||||
const fromWildcard = (pattern: string): RegExp => { |
||||
export type BlacklistItemJSON = string | { |
||||
url: string, |
||||
keys: string[], |
||||
}; |
||||
|
||||
export type BlacklistJSON = BlacklistItemJSON[]; |
||||
|
||||
const regexFromWildcard = (pattern: string): RegExp => { |
||||
let regexStr = '^' + pattern.replace(/\*/g, '.*') + '$'; |
||||
return new RegExp(regexStr); |
||||
}; |
||||
|
||||
const isArrayOfString = (raw: any): boolean => { |
||||
if (!Array.isArray(raw)) { |
||||
return false; |
||||
} |
||||
for (let x of Array.from(raw)) { |
||||
if (typeof x !== 'string') { |
||||
return false; |
||||
} |
||||
} |
||||
return true; |
||||
}; |
||||
|
||||
export class BlacklistItem { |
||||
public readonly pattern: string; |
||||
|
||||
private regex: RegExp; |
||||
|
||||
public readonly partial: boolean; |
||||
|
||||
public readonly keys: string[]; |
||||
|
||||
private readonly keyEntities: Key[]; |
||||
|
||||
constructor( |
||||
pattern: string, |
||||
partial: boolean, |
||||
keys: string[] |
||||
) { |
||||
this.pattern = pattern; |
||||
this.regex = regexFromWildcard(pattern); |
||||
this.partial = partial; |
||||
this.keys = keys; |
||||
this.keyEntities = this.keys.map(Key.fromMapKey); |
||||
} |
||||
|
||||
static fromJSON(raw: any): BlacklistItem { |
||||
if (typeof raw === 'string') { |
||||
return new BlacklistItem(raw, false, []); |
||||
} else if (typeof raw === 'object' && raw !== null) { |
||||
if (!('url' in raw)) { |
||||
throw new TypeError( |
||||
`missing field "url" of blacklist item: ${JSON.stringify(raw)}`); |
||||
} |
||||
if (typeof raw.url !== 'string') { |
||||
throw new TypeError( |
||||
`invalid field "url" of blacklist item: ${JSON.stringify(raw)}`); |
||||
} |
||||
if (!('keys' in raw)) { |
||||
throw new TypeError( |
||||
`missing field "keys" of blacklist item: ${JSON.stringify(raw)}`); |
||||
} |
||||
if (!isArrayOfString(raw.keys)) { |
||||
throw new TypeError( |
||||
`invalid field "keys" of blacklist item: ${JSON.stringify(raw)}`); |
||||
} |
||||
return new BlacklistItem(raw.url as string, true, raw.keys as string[]); |
||||
} |
||||
throw new TypeError( |
||||
`invalid format of blacklist item: ${JSON.stringify(raw)}`); |
||||
} |
||||
|
||||
toJSON(): BlacklistItemJSON { |
||||
if (!this.partial) { |
||||
return this.pattern; |
||||
} |
||||
return { url: this.pattern, keys: this.keys }; |
||||
} |
||||
|
||||
matches(url: URL): boolean { |
||||
return this.pattern.includes('/') |
||||
? this.regex.test(url.host + url.pathname) |
||||
: this.regex.test(url.host); |
||||
} |
||||
|
||||
includeKey(url: URL, key: Key): boolean { |
||||
if (!this.matches(url)) { |
||||
return false; |
||||
} |
||||
if (!this.partial) { |
||||
return true; |
||||
} |
||||
return this.keyEntities.some(k => k.equals(key)); |
||||
} |
||||
} |
||||
|
||||
export default class Blacklist { |
||||
constructor( |
||||
private blacklist: string[], |
||||
public readonly items: BlacklistItem[], |
||||
) { |
||||
} |
||||
|
||||
static fromJSON(json: any): Blacklist { |
||||
if (!Array.isArray(json)) { |
||||
throw new TypeError(`"blacklist" is not an array of string`); |
||||
throw new TypeError('blacklist is not an array: ' + JSON.stringify(json)); |
||||
} |
||||
for (let x of json) { |
||||
if (typeof x !== 'string') { |
||||
throw new TypeError(`"blacklist" is not an array of string`); |
||||
} |
||||
} |
||||
return new Blacklist(json); |
||||
let items = Array.from(json).map(item => BlacklistItem.fromJSON(item)); |
||||
return new Blacklist(items); |
||||
} |
||||
|
||||
toJSON(): BlacklistJSON { |
||||
return this.blacklist; |
||||
return this.items.map(item => item.toJSON()); |
||||
} |
||||
|
||||
includes(url: string): boolean { |
||||
let u = new URL(url); |
||||
return this.blacklist.some((item) => { |
||||
if (!item.includes('/')) { |
||||
return fromWildcard(item).test(u.host); |
||||
} |
||||
return fromWildcard(item).test(u.host + u.pathname); |
||||
}); |
||||
includesEntireBlacklist(url: URL): boolean { |
||||
return this.items.some(item => !item.partial && item.matches(url)); |
||||
} |
||||
|
||||
includeKey(url: URL, key: Key) { |
||||
return this.items.some(item => item.includeKey(url, key)); |
||||
} |
||||
} |
||||
|
@ -0,0 +1,133 @@ |
||||
import KeymapUseCase from '../../../src/content/usecases/KeymapUseCase'; |
||||
import {expect} from 'chai'; |
||||
import SettingRepository from "../../../src/content/repositories/SettingRepository"; |
||||
import Settings from "../../../src/shared/settings/Settings"; |
||||
import AddonEnabledRepository from "../../../src/content/repositories/AddonEnabledRepository"; |
||||
import {KeymapRepositoryImpl} from "../../../src/content/repositories/KeymapRepository"; |
||||
import Key from "../../../src/shared/settings/Key"; |
||||
import AddressRepository from "../../../src/content/repositories/AddressRepository"; |
||||
|
||||
class MockSettingRepository implements SettingRepository { |
||||
constructor( |
||||
private readonly settings: Settings, |
||||
) { |
||||
} |
||||
|
||||
get(): Settings { |
||||
return this.settings; |
||||
} |
||||
|
||||
set(_setting: Settings): void { |
||||
throw new Error('TODO'); |
||||
} |
||||
} |
||||
|
||||
class MockAddonEnabledRepository implements AddonEnabledRepository { |
||||
constructor( |
||||
private readonly enabled: boolean, |
||||
) { |
||||
} |
||||
|
||||
get(): boolean { |
||||
return this.enabled; |
||||
} |
||||
|
||||
set(_on: boolean): void { |
||||
throw new Error('TODO'); |
||||
} |
||||
} |
||||
|
||||
class MockAddressRepository implements AddressRepository { |
||||
constructor( |
||||
private url: URL, |
||||
) { |
||||
} |
||||
|
||||
getCurrentURL(): URL { |
||||
return this.url; |
||||
} |
||||
} |
||||
|
||||
|
||||
describe('KeymapUseCase', () => { |
||||
it('returns matched operation', () => { |
||||
let settings = Settings.fromJSON({ |
||||
keymaps: { |
||||
k: {type: 'scroll.vertically', count: -1}, |
||||
j: {type: 'scroll.vertically', count: 1}, |
||||
gg: {type: 'scroll.top'}, |
||||
}, |
||||
}); |
||||
let sut = new KeymapUseCase( |
||||
new KeymapRepositoryImpl(), |
||||
new MockSettingRepository(settings), |
||||
new MockAddonEnabledRepository(true), |
||||
new MockAddressRepository(new URL('https://example.com')), |
||||
); |
||||
|
||||
expect(sut.nextOp(Key.fromMapKey('k'))).to.deep.equal({type: 'scroll.vertically', count: -1}); |
||||
expect(sut.nextOp(Key.fromMapKey('j'))).to.deep.equal({type: 'scroll.vertically', count: 1}); |
||||
expect(sut.nextOp(Key.fromMapKey('g'))).to.be.null; |
||||
expect(sut.nextOp(Key.fromMapKey('g'))).to.deep.equal({type: 'scroll.top'}); |
||||
expect(sut.nextOp(Key.fromMapKey('z'))).to.be.null; |
||||
}); |
||||
|
||||
it('returns only ADDON_ENABLE and ADDON_TOGGLE_ENABLED operation', () => { |
||||
let settings = Settings.fromJSON({ |
||||
keymaps: { |
||||
k: {type: 'scroll.vertically', count: -1}, |
||||
a: {type: 'addon.enable'}, |
||||
b: {type: 'addon.toggle.enabled'}, |
||||
}, |
||||
}); |
||||
let sut = new KeymapUseCase( |
||||
new KeymapRepositoryImpl(), |
||||
new MockSettingRepository(settings), |
||||
new MockAddonEnabledRepository(false), |
||||
new MockAddressRepository(new URL('https://example.com')), |
||||
); |
||||
|
||||
expect(sut.nextOp(Key.fromMapKey('k'))).to.be.null; |
||||
expect(sut.nextOp(Key.fromMapKey('a'))).to.deep.equal({type: 'addon.enable'}); |
||||
expect(sut.nextOp(Key.fromMapKey('b'))).to.deep.equal({type: 'addon.toggle.enabled'}); |
||||
}); |
||||
|
||||
it('blocks keys in the partial blacklist', () => { |
||||
let settings = Settings.fromJSON({ |
||||
keymaps: { |
||||
k: {type: 'scroll.vertically', count: -1}, |
||||
j: {type: 'scroll.vertically', count: 1}, |
||||
gg: {"type": "scroll.top"}, |
||||
G: {"type": "scroll.bottom"}, |
||||
}, |
||||
blacklist: [ |
||||
{ url: "example.com", keys: ['g'] }, |
||||
{ url: "example.org", keys: ['<S-G>'] } |
||||
], |
||||
}); |
||||
|
||||
let sut = new KeymapUseCase( |
||||
new KeymapRepositoryImpl(), |
||||
new MockSettingRepository(settings), |
||||
new MockAddonEnabledRepository(true), |
||||
new MockAddressRepository(new URL('https://example.com')), |
||||
); |
||||
|
||||
expect(sut.nextOp(Key.fromMapKey('k'))).to.deep.equal({type: 'scroll.vertically', count: -1}); |
||||
expect(sut.nextOp(Key.fromMapKey('j'))).to.deep.equal({type: 'scroll.vertically', count: 1}); |
||||
expect(sut.nextOp(Key.fromMapKey('g'))).to.be.null; |
||||
expect(sut.nextOp(Key.fromMapKey('g'))).to.be.null; |
||||
expect(sut.nextOp(Key.fromMapKey('G'))).to.deep.equal({type: 'scroll.bottom'}); |
||||
|
||||
sut = new KeymapUseCase( |
||||
new KeymapRepositoryImpl(), |
||||
new MockSettingRepository(settings), |
||||
new MockAddonEnabledRepository(true), |
||||
new MockAddressRepository(new URL('https://example.org')), |
||||
); |
||||
|
||||
expect(sut.nextOp(Key.fromMapKey('g'))).to.be.null; |
||||
expect(sut.nextOp(Key.fromMapKey('g'))).to.deep.equal({type: 'scroll.top'}); |
||||
expect(sut.nextOp(Key.fromMapKey('G'))).to.be.null; |
||||
}); |
||||
}); |
@ -1,77 +1,158 @@ |
||||
import Blacklist from '../../../src/shared/settings/Blacklist'; |
||||
import Blacklist, { BlacklistItem } from '../../../src/shared/settings/Blacklist'; |
||||
import { expect } from 'chai'; |
||||
|
||||
describe('Blacklist', () => { |
||||
describe('fromJSON', () => { |
||||
it('returns empty array by empty settings', () => { |
||||
let blacklist = Blacklist.fromJSON([]); |
||||
expect(blacklist.toJSON()).to.be.empty; |
||||
import Key from '../../../src/shared/settings/Key'; |
||||
|
||||
describe('BlacklistItem', () => { |
||||
describe('#fromJSON', () => { |
||||
it('parses string pattern', () => { |
||||
let item = BlacklistItem.fromJSON('example.com'); |
||||
expect(item.pattern).to.equal('example.com'); |
||||
expect(item.partial).to.be.false; |
||||
}); |
||||
|
||||
it('returns blacklist by valid settings', () => { |
||||
let blacklist = Blacklist.fromJSON([ |
||||
'github.com', |
||||
'circleci.com', |
||||
]); |
||||
|
||||
expect(blacklist.toJSON()).to.deep.equal([ |
||||
'github.com', |
||||
'circleci.com', |
||||
]); |
||||
it('parses partial blacklist item', () => { |
||||
let item = BlacklistItem.fromJSON({ url: 'example.com', keys: ['j', 'k']}); |
||||
expect(item.pattern).to.equal('example.com'); |
||||
expect(item.partial).to.be.true; |
||||
expect(item.keys).to.deep.equal(['j', 'k']); |
||||
}); |
||||
|
||||
it('throws a TypeError by invalid settings', () => { |
||||
expect(() => Blacklist.fromJSON(null)).to.throw(TypeError); |
||||
expect(() => Blacklist.fromJSON({})).to.throw(TypeError); |
||||
expect(() => Blacklist.fromJSON([1,2,3])).to.throw(TypeError); |
||||
it('throws a TypeError', () => { |
||||
expect(() => BlacklistItem.fromJSON(null)).to.throw(TypeError); |
||||
expect(() => BlacklistItem.fromJSON(100)).to.throw(TypeError); |
||||
expect(() => BlacklistItem.fromJSON({})).to.throw(TypeError); |
||||
expect(() => BlacklistItem.fromJSON({url: 'google.com'})).to.throw(TypeError); |
||||
expect(() => BlacklistItem.fromJSON({keys: ['a']})).to.throw(TypeError); |
||||
expect(() => BlacklistItem.fromJSON({url: 'google.com', keys: 10})).to.throw(TypeError); |
||||
expect(() => BlacklistItem.fromJSON({url: 'google.com', keys: ['a', 'b', 3]})).to.throw(TypeError); |
||||
}); |
||||
}); |
||||
|
||||
describe('#includes', () => { |
||||
it('matches by *', () => { |
||||
let blacklist = new Blacklist(['*']); |
||||
|
||||
expect(blacklist.includes('https://github.com/abc')).to.be.true; |
||||
describe('#matches', () => { |
||||
it('matches by "*"', () => { |
||||
let item = BlacklistItem.fromJSON('*'); |
||||
expect(item.matches(new URL('https://github.com/abc'))).to.be.true; |
||||
}); |
||||
|
||||
it('matches by hostname', () => { |
||||
let blacklist = new Blacklist(['github.com']); |
||||
|
||||
expect(blacklist.includes('https://github.com')).to.be.true; |
||||
expect(blacklist.includes('https://gist.github.com')).to.be.false; |
||||
expect(blacklist.includes('https://github.com/ueokande')).to.be.true; |
||||
expect(blacklist.includes('https://github.org')).to.be.false; |
||||
expect(blacklist.includes('https://google.com/search?q=github.org')).to.be.false; |
||||
let item = BlacklistItem.fromJSON('github.com'); |
||||
expect(item.matches(new URL('https://github.com'))).to.be.true; |
||||
expect(item.matches(new URL('https://gist.github.com'))).to.be.false; |
||||
expect(item.matches(new URL('https://github.com/ueokande'))).to.be.true; |
||||
expect(item.matches(new URL('https://github.org'))).to.be.false; |
||||
expect(item.matches(new URL('https://google.com/search?q=github.org'))).to.be.false; |
||||
}); |
||||
|
||||
it('matches by hostname with wildcard', () => { |
||||
let blacklist = new Blacklist(['*.github.com']); |
||||
let item = BlacklistItem.fromJSON('*.github.com'); |
||||
|
||||
expect(blacklist.includes('https://github.com')).to.be.false; |
||||
expect(blacklist.includes('https://gist.github.com')).to.be.true; |
||||
}) |
||||
expect(item.matches(new URL('https://github.com'))).to.be.false; |
||||
expect(item.matches(new URL('https://gist.github.com'))).to.be.true; |
||||
}); |
||||
|
||||
it('matches by path', () => { |
||||
let blacklist = new Blacklist(['github.com/abc']); |
||||
let item = BlacklistItem.fromJSON('github.com/abc'); |
||||
|
||||
expect(blacklist.includes('https://github.com/abc')).to.be.true; |
||||
expect(blacklist.includes('https://github.com/abcdef')).to.be.false; |
||||
expect(blacklist.includes('https://gist.github.com/abc')).to.be.false; |
||||
}) |
||||
expect(item.matches(new URL('https://github.com/abc'))).to.be.true; |
||||
expect(item.matches(new URL('https://github.com/abcdef'))).to.be.false; |
||||
expect(item.matches(new URL('https://gist.github.com/abc'))).to.be.false; |
||||
}); |
||||
|
||||
it('matches by path with wildcard', () => { |
||||
let blacklist = new Blacklist(['github.com/abc*']); |
||||
let item = BlacklistItem.fromJSON('github.com/abc*'); |
||||
|
||||
expect(blacklist.includes('https://github.com/abc')).to.be.true; |
||||
expect(blacklist.includes('https://github.com/abcdef')).to.be.true; |
||||
expect(blacklist.includes('https://gist.github.com/abc')).to.be.false; |
||||
}) |
||||
expect(item.matches(new URL('https://github.com/abc'))).to.be.true; |
||||
expect(item.matches(new URL('https://github.com/abcdef'))).to.be.true; |
||||
expect(item.matches(new URL('https://gist.github.com/abc'))).to.be.false; |
||||
}); |
||||
|
||||
it('matches address and port', () => { |
||||
let blacklist = new Blacklist(['127.0.0.1:8888']); |
||||
let item = BlacklistItem.fromJSON('127.0.0.1:8888'); |
||||
|
||||
expect(item.matches(new URL('http://127.0.0.1:8888/'))).to.be.true; |
||||
expect(item.matches(new URL('http://127.0.0.1:8888/hello'))).to.be.true; |
||||
}); |
||||
|
||||
it('matches with partial blacklist', () => { |
||||
let item = BlacklistItem.fromJSON({ url: 'google.com', keys: ['j', 'k'] }); |
||||
|
||||
expect(item.matches(new URL('https://google.com'))).to.be.true; |
||||
expect(item.matches(new URL('https://yahoo.com'))).to.be.false; |
||||
}) |
||||
}); |
||||
|
||||
describe('#includesPartialKeys', () => { |
||||
it('matches with partial keys', () => { |
||||
let item = BlacklistItem.fromJSON({url: 'google.com', keys: ['j', 'k', '<C-U>']}); |
||||
|
||||
expect(item.includeKey(new URL('http://google.com/maps'), Key.fromMapKey('j'))).to.be.true; |
||||
expect(item.includeKey(new URL('http://google.com/maps'), Key.fromMapKey('<C-U>'))).to.be.true; |
||||
expect(item.includeKey(new URL('http://google.com/maps'), Key.fromMapKey('z'))).to.be.false; |
||||
expect(item.includeKey(new URL('http://google.com/maps'), Key.fromMapKey('u'))).to.be.false; |
||||
expect(item.includeKey(new URL('http://maps.google.com/'), Key.fromMapKey('j'))).to.be.false; |
||||
}) |
||||
}); |
||||
}); |
||||
|
||||
describe('Blacklist', () => { |
||||
describe('#fromJSON', () => { |
||||
it('parses string list', () => { |
||||
let blacklist = Blacklist.fromJSON(['example.com', 'example.org']); |
||||
expect(blacklist.toJSON()).to.deep.equals([ |
||||
'example.com', 'example.org', |
||||
]); |
||||
}); |
||||
|
||||
it('parses mixed blacklist', () => { |
||||
let blacklist = Blacklist.fromJSON([ |
||||
{ url: 'example.com', keys: ['j', 'k']}, |
||||
'example.org', |
||||
]); |
||||
expect(blacklist.toJSON()).to.deep.equals([ |
||||
{ url: 'example.com', keys: ['j', 'k']}, |
||||
'example.org', |
||||
]); |
||||
}); |
||||
|
||||
expect(blacklist.includes('http://127.0.0.1:8888/')).to.be.true; |
||||
expect(blacklist.includes('http://127.0.0.1:8888/hello')).to.be.true; |
||||
it('parses empty blacklist', () => { |
||||
let blacklist = Blacklist.fromJSON([]); |
||||
expect(blacklist.toJSON()).to.deep.equals([]); |
||||
}); |
||||
|
||||
it('throws a TypeError', () => { |
||||
expect(() => Blacklist.fromJSON(null)).to.throw(TypeError); |
||||
expect(() => Blacklist.fromJSON(100)).to.throw(TypeError); |
||||
expect(() => Blacklist.fromJSON({})).to.throw(TypeError); |
||||
expect(() => Blacklist.fromJSON([100])).to.throw(TypeError); |
||||
expect(() => Blacklist.fromJSON([{}])).to.throw(TypeError); |
||||
}) |
||||
}) |
||||
}); |
||||
|
||||
describe('#includesEntireBlacklist', () => { |
||||
it('matches a url with entire blacklist', () => { |
||||
let blacklist = Blacklist.fromJSON(['google.com', '*.github.com']); |
||||
expect(blacklist.includesEntireBlacklist(new URL('https://google.com'))).to.be.true; |
||||
expect(blacklist.includesEntireBlacklist(new URL('https://github.com'))).to.be.false; |
||||
expect(blacklist.includesEntireBlacklist(new URL('https://gist.github.com'))).to.be.true; |
||||
}); |
||||
|
||||
it('does not matches with partial blacklist', () => { |
||||
let blacklist = Blacklist.fromJSON(['google.com', { url: 'yahoo.com', keys: ['j', 'k'] }]); |
||||
expect(blacklist.includesEntireBlacklist(new URL('https://google.com'))).to.be.true; |
||||
expect(blacklist.includesEntireBlacklist(new URL('https://yahoo.com'))).to.be.false; |
||||
}); |
||||
}); |
||||
|
||||
describe('#includesKeys', () => { |
||||
it('matches with entire blacklist or keys in the partial blacklist', () => { |
||||
let blacklist = Blacklist.fromJSON([ |
||||
'google.com', |
||||
{ url: 'github.com', keys: ['j', 'k'] }, |
||||
]); |
||||
|
||||
expect(blacklist.includeKey(new URL('https://google.com'), Key.fromMapKey('j'))).to.be.true; |
||||
expect(blacklist.includeKey(new URL('https://github.com'), Key.fromMapKey('j'))).to.be.true; |
||||
expect(blacklist.includeKey(new URL('https://github.com'), Key.fromMapKey('a'))).to.be.false; |
||||
}); |
||||
}); |
||||
}); |
||||
|
Reference in new issue