Skip to content

FileToken

bhsd edited this page Nov 24, 2023 · 23 revisions

FileToken

文件。这个类继承了 LinkToken 类。

原型属性

link: string|Title

  • 链接目标。
var root = Parser.parse("[[file:a|link=[[talk:b]]]]"),
    file = root.firstChild;
assert.deepStrictEqual(file.link, root.normalizeTitle('Talk:B'));
file.link = '//c';
assert.strictEqual(root.toString(), '[[file:a|link=//c]]');

size: {width: string, height: string}

  • 图片尺寸。
var root = Parser.parse('[[file:a|x1px]]'),
    file = root.firstChild;
assert.deepStrictEqual(file.size, {width: '', height: '1'});

width: number

  • 图片宽度。
var root = Parser.parse('[[file:a|1x1px]]'),
    file = root.firstChild;
assert.strictEqual(file.width, '1');
file.width = undefined;
assert.strictEqual(root.toString(), '[[file:a|x1px]]');

height: number

  • 图片高度。
var root = Parser.parse('[[file:a|1x1px]]'),
    file = root.firstChild;
assert.strictEqual(file.height, '1');
file.height = undefined;
assert.strictEqual(root.toString(), '[[file:a|1px]]');

原型方法

getAllArgs(): ImageParameterToken[]

var root = Parser.parse('[[file:a|thumb|1px|link=b|alt=c|d]]'),
    file = root.firstChild;
assert.deepStrictEqual(file.getAllArgs(), file.children.slice(1));

getFrameArgs(): ImageParameterToken[]

  • 获取图片框架参数,即缩略图无框有框等。每张图片只有第 1 个框架参数会生效。
var root = Parser.parse('[[file:a|thumb|1px|link=b|alt=c|d]]'),
    file = root.firstChild;
assert.deepStrictEqual(file.getFrameArgs(), file.children.slice(1, 2));

getArgs(key: string): ImageParameterToken[]>

var root = Parser.parse('[[file:a|link=b|链接=c]]'), // 这里故意使用一个错误语法的例子,请勿模仿
    file = root.firstChild;
assert.deepStrictEqual(file.getArgs('link'), file.children.slice(1));

hasArg(key: string): boolean

var root = Parser.parse('[[file:a|b]]'),
    file = root.firstChild;
assert(file.hasArg('caption') === true);

getArg(key: string): ImageParameterToken

var root = Parser.parse('[[file:a|link=b|链接=c]]'), // 这里故意使用一个错误语法的例子,请勿模仿
    file = root.firstChild;
assert.strictEqual(file.getArg('link'), file.lastChild);

removeArg(key: string): void

var root = Parser.parse('[[file:a|link=b|链接=c]]'), // 这里故意使用一个错误语法的例子,请勿模仿
    file = root.firstChild;
file.removeArg('link');
assert.strictEqual(root.toString(), '[[file:a]]');

getKeys(): string[]

var root = Parser.parse('[[file:a|thumb|1px|link=b|alt=c|d]]'),
    file = root.firstChild;
assert.deepStrictEqual(file.getKeys(), ['thumbnail', 'width', 'link', 'alt', 'caption']);

getValue(key: string): string|true

var root = Parser.parse('[[file:a|thumb|100px]]'),
    file = root.firstChild;
assert(file.getValue('thumbnail') === true);
assert.strictEqual(file.getValue('width'), '100');

setValue(key: string, value: string|boolean): void

var root = Parser.parse('[[file:a|thumb]]'),
    file = root.firstChild;
file.setValue('thumbnail', false);
file.setValue('width', '100');
file.setValue('framed', true);
assert.strictEqual(root.toString(), '[[file:a|100px|framed]]');
Clone this wiki locally