TableToken (EN) - bhsd-harry/wikiparser-node GitHub Wiki

Table of Contents

Other Languages

Introduction

Table. This class inherits all the properties and methods of the Token class which are not repeated here.

✅ Available in the Mini and Browser versions.
🌐 Available in the Browser version.

Properties

closed

✅ Expand

type: boolean
Whether the table is closed. Read-only in the Mini and Browser versions.

// closed
var {firstChild} = Parser.parse("{|\n!a");
assert.equal(firstChild, "{|\n!a");
assert.ok(!firstChild.closed);
// closed (main)
var {firstChild} = Parser.parse("{|\n!a");
firstChild.closed = true;
assert.equal(firstChild, "{|\n!a\n|}");

Methods

lint

✅ Expand

returns: LintError[]
Report potential grammar errors.

// lint
assert.deepStrictEqual(
	Parser.parse("{|\na\n|a\n|-\n|b||c").firstChild.lint(),
	[
		{
			rule: "fostered-content",
			severity: "warning",
			message: "content to be moved outside the table",
			startLine: 1,
			startCol: 0,
			startIndex: 3,
			endLine: 1,
			endCol: 1,
			endIndex: 4,
		},
		{
			rule: "unclosed-table",
			severity: "error",
			message: "unclosed table",
			startLine: 0,
			startCol: 0,
			startIndex: 0,
			endLine: 0,
			endCol: 2,
			endIndex: 2,
		},
		{
			rule: "table-layout",
			severity: "warning",
			message: "inconsistent table layout",
			startLine: 3,
			startCol: 0,
			startIndex: 8,
			endLine: 4,
			endCol: 5,
			endIndex: 16,
		},
	],
);
assert.deepStrictEqual(
	Parser.parse("{|\n{{#switch:|1=!|2={{!}}|3={{!-}}|4=<td></td>}}\n|}")
		.firstChild
		.lint(),
	[],
);
assert.deepStrictEqual(
	Parser.parse("{|\n{{{a|a}}}\n|-\n{{{b|!}}}\n|-\n{{{c}}}|\n|}", true)
		.firstChild
		.lint(),
	[
		{
			rule: "fostered-content",
			severity: "warning",
			message: "content to be moved outside the table",
			startLine: 1,
			startCol: 0,
			startIndex: 3,
			endLine: 1,
			endCol: 9,
			endIndex: 12,
		},
	],
);
assert.deepStrictEqual(
	Parser.parse("{|\n{{#if:|{{x}}|{{#invoke:y|y}}}}\n|}").firstChild.lint(),
	[
		{
			rule: "fostered-content",
			severity: "warning",
			message: "content to be moved outside the table",
			startLine: 1,
			startCol: 0,
			startIndex: 3,
			endLine: 1,
			endCol: 30,
			endIndex: 33,
		},
	],
);
assert.deepStrictEqual(Parser.parse("{|\n{{#if:|x}}\n|}").firstChild.lint(), [
	{
		rule: "fostered-content",
		severity: "warning",
		message: "content to be moved outside the table",
		startLine: 1,
		startCol: 0,
		startIndex: 3,
		endLine: 1,
		endCol: 10,
		endIndex: 13,
	},
]);

getAllRows

✅ Expand

returns: TdToken[]
Get all rows.

// getAllRows
var {firstChild} = Parser.parse("{|\n!a\n|-\n|b\n|}"),
	tr = firstChild.querySelector("tr");
assert.equal(firstChild, "{|\n!a\n|-\n|b\n|}");
assert.deepStrictEqual(firstChild.getAllRows(), [firstChild, tr]);

getNthRow

✅ Expand

param: number Row number
returns: TrToken
Get the nth row.

// getNthRow
var {firstChild} = Parser.parse("{|\n!a\n|-\n|b\n|}"),
	tr = firstChild.querySelector("tr");
assert.equal(firstChild, "{|\n!a\n|-\n|b\n|}");
assert.strictEqual(firstChild.getNthRow(0), firstChild);
assert.strictEqual(firstChild.getNthRow(1), tr);

getRowCount

✅ Expand

returns: number
Get the number of rows.

// getRowCount
var {firstChild} = Parser.parse("{|\n|a\n|-\n|}");
assert.equal(firstChild, "{|\n|a\n|-\n|}");
// The first row can be the table itself. Empty rows are not counted.
assert.strictEqual(firstChild.getRowCount(), 1);

json

🌐 Expand

Save the syntax tree as JSON.

// json (print)
var {lastChild} = Parser.parse(" {|");
assert.deepStrictEqual(lastChild.json(), {
	range: [1, 3],
	type: "table",
	closed: false,
	childNodes: [
		{
			range: [1, 3],
			type: "table-syntax",
			childNodes: [
				{
					range: [1, 3],
					data: "{|",
				},
			],
		},
		{
			range: [3, 3],
			type: "table-attrs",
			name: "table",
			childNodes: [],
		},
	],
});

getNthCol

Expand

param: number Column number
returns: TdToken
Get the nth column. Only for TableToken itself when used as a row.

// getNthCol (main)
var {firstChild} = Parser.parse("{|\n!a\n|}"),
	td = firstChild.querySelector("td");
assert.equal(firstChild, "{|\n!a\n|}");
assert.strictEqual(firstChild.getNthCol(0), td);

close

Expand

Close the table.

// close (main)
var {firstChild} = Parser.parse("{|\n!a");
assert.equal(firstChild, "{|\n!a");
firstChild.close();
assert.equal(firstChild, "{|\n!a\n|}");

cloneNode

Expand

returns: this
Deep clone the node.

// cloneNode (main)
var {firstChild} = Parser.parse("{|\n!a");
assert.equal(firstChild, "{|\n!a");
assert.deepStrictEqual(firstChild.cloneNode(), firstChild);

escape

Expand

Escape table syntax.

// escape (main)
var {firstChild} = Parser.parse("{|\n|-\n|a\n|}");
assert.equal(firstChild, "{|\n|-\n|a\n|}");
firstChild.escape();
assert.equal(firstChild, "{{{!}}\n{{!}}-\n{{!}}a\n{{!}}}");

getColCount

Expand

returns: number
Get the number of columns. Only for TableToken itself when used as a row.

// getColCount (main)
var {firstChild} = Parser.parse("{|\n|a\n|}");
assert.equal(firstChild, "{|\n|a\n|}");
assert.strictEqual(firstChild.getColCount(), 1);

getNextRow

Expand

returns: TrToken
Get the next row (the row directly after the table itself).

// getNextRow (main)
var {firstChild} = Parser.parse("{|\n|-\n|a\n|}"),
	tr = firstChild.querySelector("tr");
assert.equal(firstChild, "{|\n|-\n|a\n|}");
assert.strictEqual(firstChild.getNextRow(), tr);

toHtml

Expand

version added: 1.10.0

returns: string
Convert to HTML.

// toHtml (main)
var {firstChild} = Parser.parse("{|id=t\na\nb\n|c\n|-\nd\ne\n|-\n|f\n|}");
assert.strictEqual(
	firstChild.toHtml(),
	`a b d e<table id="t">
<tbody><tr>
<td>c
</td></tr>

<tr>
<td>f
</td></tr></tbody></table>`,
);
⚠️ **GitHub.com Fallback** ⚠️