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

Table of Contents

Other Languages

Introduction

Table cell. 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

subtype

✅ Expand

type: 'td' | 'th' | 'caption'
Subtype of the cell. Read-only in the Mini and Browser versions.

// subtype
var [caption, th, td] = Parser.parse("{|\n|+\n!\n|\n|}").querySelectorAll("td");
assert.equal(caption, "\n|+");
assert.equal(th, "\n!");
assert.equal(td, "\n|");
assert.strictEqual(caption.subtype, "caption");
assert.strictEqual(th.subtype, "th");
assert.strictEqual(td.subtype, "td");
// subtype (main)
var [, th] = Parser.parse("{|\n|+\n!\n|\n|}").querySelectorAll("td");
th.subtype = "td";
assert.equal(th, "\n|");

rowspan

✅ Expand

type: number
Row span of the cell. Read-only in the Mini and Browser versions.

// rowspan
var td = Parser.parse("{|\n|\n|}").querySelector("td");
assert.equal(td, "\n|");
assert.strictEqual(td.rowspan, 1);
// rowspan (main)
var td = Parser.parse("{|\n|\n|}").querySelector("td");
td.rowspan = 2;
assert.equal(td, '\n|rowspan="2"|');

colspan

✅ Expand

type: number
Column span of the cell. Read-only in the Mini and Browser versions.

// colspan
var td = Parser.parse("{|\n|\n|}").querySelector("td");
assert.equal(td, "\n|");
assert.strictEqual(td.colspan, 1);
// colspan (main)
var td = Parser.parse("{|\n|\n|}").querySelector("td");
td.colspan = 2;
assert.equal(td, '\n|colspan="2"|');

innerText

Expand

type: string
Text content of the cell.

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

Methods

lint

✅ Expand

returns: LintError[]
Report potential grammar errors.

// lint
var td = Parser.parse("{|\n|\na||b\n|}").querySelector("td");
assert.equal(td, "\n|\na||b");
assert.deepStrictEqual(td.lint(), [
	{
		rule: "pipe-like",
		severity: "error",
		message: 'additional "|" in a table cell',
		startLine: 1,
		startCol: 1,
		startIndex: 4,
		endLine: 2,
		endCol: 4,
		endIndex: 9,
		fix: {
			range: [4, 9],
			text: "\na\n|b",
			desc: "newline",
		},
	},
]);

json

🌐 Expand

Save the syntax tree as JSON.

// json (print)
var th = Parser.parse("{|\n!rowspan=2|").querySelector("td");
assert.deepStrictEqual(th.json(), {
	range: [2, 14],
	type: "td",
	subtype: "th",
	rowspan: 2,
	childNodes: [
		{
			range: [2, 4],
			type: "table-syntax",
			childNodes: [
				{
					range: [2, 4],
					data: "\n!",
				},
			],
		},
		{
			range: [4, 13],
			type: "table-attrs",
			name: "th",
			childNodes: [
				{
					range: [4, 13],
					type: "table-attr",
					name: "rowspan",
					tag: "th",
					childNodes: [
						{
							range: [4, 11],
							type: "attr-key",
							childNodes: [
								{
									range: [4, 11],
									data: "rowspan",
								},
							],
						},
						{
							range: [12, 13],
							type: "attr-value",
							childNodes: [
								{
									range: [12, 13],
									data: "2",
								},
							],
						},
					],
				},
			],
		},
		{
			range: [14, 14],
			type: "td-inner",
			childNodes: [
				{
					range: [14, 14],
					data: "",
				},
			],
		},
	],
});

isIndependent

Expand

returns: boolean
Whether the cell is at the start of a line.

// isIndependent (main)
var [a, b] = Parser.parse("{|\n!a||b\n|}").querySelectorAll("td");
assert.equal(a, "\n!a");
assert.equal(b, "||b");
assert.ok(a.isIndependent());
assert.ok(!b.isIndependent());

cloneNode

Expand

returns: this
Deep clone the node.

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

toHtml

Expand

version added: 1.10.0

returns: string
Convert to HTML.

// toHtml (main)
var [caption, th, td] = Parser.parse("{|\n|+id=a|a\nb\n!id=c|c\n|id=d|d\n|}")
	.querySelectorAll("td");
assert.equal(caption, "\n|+id=a|a\nb");
assert.equal(th, "\n!id=c|c");
assert.equal(td, "\n|id=d|d");
assert.strictEqual(caption.toHtml(), '\n<caption id="a">a&#10;b</caption>');
assert.strictEqual(th.toHtml(), '\n<th id="c">c\n</th>');
assert.strictEqual(td.toHtml(), '\n<td id="d">d\n</td>');
⚠️ **GitHub.com Fallback** ⚠️