mirror of
https://github.com/tenrok/BBob.git
synced 2026-05-27 14:45:03 +03:00
9a6986965e
* feat: add file:// * feat: add changeset
123 lines
3.3 KiB
TypeScript
123 lines
3.3 KiB
TypeScript
import {
|
|
attrsToString,
|
|
attrValue,
|
|
appendToNode,
|
|
getNodeLength,
|
|
getUniqAttr,
|
|
isTagNode,
|
|
isStringNode,
|
|
isEOL,
|
|
TagNode,
|
|
} from '../src';
|
|
|
|
describe('@bbob/plugin-helper/helpers', () => {
|
|
test('appendToNode', () => {
|
|
const value = 'test';
|
|
const node = {content: []} as TagNode;
|
|
|
|
appendToNode(node, value);
|
|
expect(node.content.pop()).toBe(value);
|
|
});
|
|
|
|
test('getNodeLength', () => {
|
|
const node = {
|
|
tag: 'test',
|
|
content: [
|
|
'123',
|
|
{
|
|
tag: 'test2',
|
|
content: ['123']
|
|
}
|
|
]
|
|
} as TagNode;
|
|
|
|
expect(getNodeLength(node)).toBe(6)
|
|
});
|
|
|
|
test('isTagNode', () => {
|
|
const node = {
|
|
tag: 'test',
|
|
content: []
|
|
} as TagNode;
|
|
|
|
expect(isTagNode(node)).toBe(true)
|
|
});
|
|
|
|
test('isStringNode', () => {
|
|
const node = {
|
|
tag: 'test',
|
|
content: ['123']
|
|
};
|
|
|
|
expect(isStringNode(node.content[0])).toBe(true);
|
|
});
|
|
|
|
test('attrValue boolean', () => {
|
|
expect(attrValue('test', true)).toBe('test');
|
|
});
|
|
|
|
test('attrValue number', () => {
|
|
expect(attrValue('test', 123)).toBe('test="123"');
|
|
});
|
|
|
|
test('attrValue string', () => {
|
|
expect(attrValue('test', 'hello')).toBe('test="hello"');
|
|
});
|
|
|
|
test('attrValue object', () => {
|
|
const attrs = {tag: 'test'};
|
|
|
|
expect(attrValue('test', attrs)).toBe('test="{"tag":"test"}"');
|
|
});
|
|
|
|
test('isEOL', () => {
|
|
expect(isEOL('\n')).toBe(true)
|
|
});
|
|
|
|
test('attrsToString', () => {
|
|
expect(attrsToString({
|
|
tag: 'test',
|
|
foo: 'bar',
|
|
disabled: true
|
|
})).toBe(` tag="test" foo="bar" disabled`)
|
|
});
|
|
|
|
test('attrsToString undefined', () => {
|
|
expect(attrsToString(undefined)).toBe('')
|
|
});
|
|
|
|
describe('attrsToString escape', () => {
|
|
test(`javascript:alert("hello")`, () => {
|
|
expect(attrsToString({
|
|
onclick: `javascript:alert('hello')`,
|
|
href: `javascript:alert('hello')`,
|
|
})).toBe(` onclick="javascript%3Aalert('hello')" href="javascript%3Aalert('hello')"`)
|
|
});
|
|
test(`JAVASCRIPT:alert("hello")`, () => {
|
|
expect(attrsToString({
|
|
onclick: `JAVASCRIPT:alert('hello')`,
|
|
href: `JAVASCRIPT:alert('hello')`,
|
|
})).toBe(` onclick="JAVASCRIPT%3Aalert('hello')" href="JAVASCRIPT%3Aalert('hello')"`)
|
|
});
|
|
test(`file:alert("hello")`, () => {
|
|
expect(attrsToString({
|
|
href: `file:///shared/customer_info/customer-name`,
|
|
})).toBe(` href="file%3A///shared/customer_info/customer-name"`)
|
|
});
|
|
test(`<tag>`, () => {
|
|
expect(attrsToString({
|
|
onclick: `<tag>`,
|
|
href: `<tag>`,
|
|
})).toBe(` onclick="<tag>" href="<tag>"`)
|
|
});
|
|
});
|
|
|
|
test('getUniqAttr with unq attr', () => {
|
|
expect(getUniqAttr({foo: true, 'http://bar.com': 'http://bar.com'})).toBe('http://bar.com')
|
|
});
|
|
|
|
test('getUniqAttr without unq attr', () => {
|
|
expect(getUniqAttr({foo: true})).toBe(null)
|
|
})
|
|
});
|