Skip to content

FileToken (EN)

bhsd edited this page Jan 3, 2024 · 13 revisions
Table of Contents

Other Languages

Introduction

File。

✅ Available in the Mini and Browser versions.

Properties

name

Expand

type: string
File name,read-only.

// name
var {firstChild} = Parser.parse('[[file:a]]');
assert.strictEqual(firstChild.name, 'File:A');

link

Expand

type: string | Title
File link.

// link
var {firstChild, lastChild} = Parser.parse([[file:a]][[file:b|link=c]]');
assert.equal(firstChild, '[[file:a]]');
assert.equal(lastChild, '[[file:b|link=c]]');
assert.deepStrictEqual(firstChild.link, firstChild.normalizeTitle('file:a'));
assert.deepStrictEqual(lastChild.link, lastChild.normalizeTitle('c'));
firstChild.link = '//c';
assert.equal(firstChild, '[[file:a|link=//c]]');

size

Expand

type: {width: string, height: string}
Image size.

// size
var {firstChild} = Parser.parse('[[file:a|x1px]]');
assert.deepStrictEqual(firstChild.size, {width: '', height: '1'});
firstChild.size = {width: '1', height: '1'};
assert.equal(firstChild, '[[file:a|1x1px]]');

width

Expand

type: number
Image width.

// width
var {firstChild} = Parser.parse('[[file:a|1x1px]]');
assert.strictEqual(firstChild.width, '1');
firstChild.width = undefined;
assert.equal(firstChild, '[[file:a|x1px]]');

height

Expand

type: number
Image height.

// height
var {firstChild} = Parser.parse('[[file:a|1x1px]]');
assert.strictEqual(firstChild.height, '1');
firstChild.height = undefined;
assert.equal(firstChild, '[[file:a|1px]]');

Methods

lint

✅ Expand

returns: LintError[]
Report potential grammar errors.

// lint
var {firstChild} = Parser.parse('[[file:a|frameless|framed|left|none|sub|top|b|c]]');
assert.deepStrictEqual(firstChild.lint(), [
	{
		severity: 'error',
		message: 'duplicated image caption parameter',
		startLine: 0,
		startCol: 44,
		startIndex: 44,
		endLine: 0,
		endCol: 45,
		endIndex: 45,
		excerpt: 'b',
	},
	{
		severity: 'error',
		message: 'duplicated image caption parameter',
		startLine: 0,
		startCol: 46,
		startIndex: 46,
		endLine: 0,
		endCol: 47,
		endIndex: 47,
		excerpt: 'c',
	},
	{
		severity: 'error',
		message: 'conflicting image frame parameter',
		startLine: 0,
		startCol: 9,
		startIndex: 9,
		endLine: 0,
		endCol: 18,
		endIndex: 18,
		excerpt: 'frameless',
	},
	{
		severity: 'error',
		message: 'conflicting image frame parameter',
		startLine: 0,
		startCol: 19,
		startIndex: 19,
		endLine: 0,
		endCol: 25,
		endIndex: 25,
		excerpt: 'framed',
	},
	{
		severity: 'error',
		message: 'conflicting image horizontal-alignment parameter',
		startLine: 0,
		startCol: 26,
		startIndex: 26,
		endLine: 0,
		endCol: 30,
		endIndex: 30,
		excerpt: 'left',
	},
	{
		severity: 'error',
		message: 'conflicting image horizontal-alignment parameter',
		startLine: 0,
		startCol: 31,
		startIndex: 31,
		endLine: 0,
		endCol: 35,
		endIndex: 35,
		excerpt: 'none',
	},
	{
		severity: 'error',
		message: 'conflicting image vertical-alignment parameter',
		startLine: 0,
		startCol: 36,
		startIndex: 36,
		endLine: 0,
		endCol: 39,
		endIndex: 39,
		excerpt: 'sub',
	},
	{
		severity: 'error',
		message: 'conflicting image vertical-alignment parameter',
		startLine: 0,
		startCol: 40,
		startIndex: 40,
		endLine: 0,
		endCol: 43,
		endIndex: 43,
		excerpt: 'top',
	},
])

getAllArgs

✅ Expand

returns: ImageParameterToken[]
Get all image parameter nodes.

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

getArgs

✅ Expand

param: string parameter name
returns: ImageParameterToken[]
Get specified image parameter nodes.

// getArgs
var {firstChild} = Parser.parse('[[file:a|link=b|链接=c]]');
assert.deepStrictEqual(firstChild.getArgs('link'), firstChild.children.slice(1));

getFrameArgs

✅ Expand

returns: ImageParameterToken[]
Get image frame parameter nodes.

// getFrameArgs
var {firstChild} = Parser.parse('[[file:a|thumb]]');
assert.deepStrictEqual(firstChild.getFrameArgs(), [firstChild.lastChild]);

getHorizAlignArgs

✅ Expand

returns: ImageParameterToken[]
Get image horizontal alignment parameter nodes.

// getHorizAlignArgs
var {firstChild} = Parser.parse('[[file:a|none]]');
assert.deepStrictEqual(firstChild.getHorizAlignArgs(), [firstChild.lastChild]);

getVertAlignArgs

✅ Expand

returns: ImageParameterToken[]
Get image vertical alignment parameter nodes.

// getVertAlignArgs
var {firstChild} = Parser.parse('[[file:a|top]]');
assert.deepStrictEqual(firstChild.getVertAlignArgs(), [firstChild.lastChild]);

cloneNode

Expand

returns: this
Deep clone the node.

// cloneNode
var {firstChild} = Parser.parse('[[file:a]]');
assert.deepStrictEqual(firstChild.cloneNode(), firstChild);

setTarget

Expand

param: string target file name
Set target file.

// setTarget
var {firstChild} = Parser.parse('[[file:a]]');
firstChild.setTarget('file:b');
assert.equal(firstChild, '[[file:b]]');

getArg

Expand

param: string parameter name
returns: ImageParameterToken
Get effective node of the specified image parameter.

// getArg
var {firstChild} = Parser.parse('[[file:a|link=b|链接=c]]');
assert.strictEqual(firstChild.getArg('link'), firstChild.lastChild);

hasArg

Expand

param: string parameter name
returns: boolean
Whether the image has the specified parameter.

// hasArg
var {firstChild} = Parser.parse('[[file:a|b]]');
assert(firstChild.hasArg('caption'));

removeArg

Expand

param: string parameter name
Remove the specified image parameter.

// removeArg
var {firstChild} = Parser.parse('[[file:a|link=b]]');
firstChild.removeArg('link');
assert.equal(firstChild, '[[file:a]]');

getKeys

Expand

returns(): Set<string>
Get image parameter names.

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

getValues

Expand

param: string parameter name
returns: (string | true)[]
Get specified image parameter values.

// getValues
var {firstChild} = Parser.parse('[[file:a|b|c]]');
assert.deepStrictEqual(firstChild.getValues('caption'), ['b', 'c']);

getValue

Expand

param: string parameter name
returns: string | true
Get effective value of the specified image parameter.

// getValue
var {firstChild} = Parser.parse('[[file:a|b|c]]');
assert.strictEqual(firstChild.getValue('caption'), 'c');

setValue

Expand

param: string parameter name
param: string | boolean parameter value
Set the specified image parameter.

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