Skip to content

AttributeToken (EN)

bhsd edited this page Feb 29, 2024 · 13 revisions
Table of Contents

Other Languages

Introduction

A single attribute of extension tags, HTML tags and tables.

✅ Available in the Mini and Browser versions.

Properties

name

✅ Expand

type: string
The name of the attribute in lowercase, read-only.

// name
var attr = Parser.parse('<REF name=a/>').querySelector('ext-attr');
assert.equal(attr, 'name=a');
assert.strictEqual(attr.name, 'name');

tag

✅ Expand

type: string
The name of the tag in lowercase, read-only.

// tag
var attr = Parser.parse('<REF name=a/>').querySelector('ext-attr');
assert.equal(attr, 'name=a');
assert.strictEqual(attr.tag, 'ref');

balanced

✅ Expand

type: boolean
Whether the quotes are matched.

// balanced
var attr = Parser.parse('<p id="a>').querySelector('html-attr');
assert.equal(attr, 'id="a');
assert(!attr.balanced);
attr.balanced = true;
assert.equal(attr, 'id="a"');

value

Expand

type: string | true
See getValue and setValue methods.

Methods

lint

✅ Expand

returns: LintError[]
Report potential grammar errors.

// lint
var [lang, tabindex, align, style] = Parser.parse(`<p xml:lang=zh tabindex=1 align=center style="cursor:url('cursor.png')>`)
	.querySelectorAll('html-attr');
assert.equal(lang, 'xml:lang=zh');
assert.equal(tabindex, 'tabindex=1');
assert.equal(align, 'align=center');
assert.equal(style, `style="cursor:url('cursor.png')`);
assert.deepStrictEqual(lang.lint(), [
	{
		rule: 'illegal-attr',
		severity: 'error',
		message: 'illegal attribute name',
		startLine: 0,
		startCol: 3,
		startIndex: 3,
		endLine: 0,
		endCol: 11,
		endIndex: 11,
	},
]);
assert.deepStrictEqual(tabindex.lint(), [
	{
		rule: 'illegal-attr',
		severity: 'error',
		message: 'nonzero tabindex',
		startLine: 0,
		startCol: 24,
		startIndex: 24,
		endLine: 0,
		endCol: 25,
		endIndex: 25,
		suggestions: [
			{
				desc: 'remove',
				range: [15, 25],
				text: '',
			},
			{
				desc: '0 tabindex',
				range: [24, 25],
				text: '0',
			}
		],
	},
]);
assert.deepStrictEqual(align.lint(), [
	{
		rule: 'obsolete-attr',
		severity: 'warning',
		message: 'obsolete attribute',
		startLine: 0,
		startCol: 26,
		startIndex: 26,
		endLine: 0,
		endCol: 31,
		endIndex: 31,
	},
]);
assert.deepStrictEqual(style.lint(), [
	{
		rule: 'unclosed-quote',
		severity: 'warning',
		message: 'unclosed quotes',
		startLine: 0,
		startCol: 45,
		startIndex: 45,
		endLine: 0,
		endCol: 70,
		endIndex: 70,
		fix: {
			range: [70, 70],
			text: '"',
		},
	},
	{
		rule: 'insecure-style',
		severity: 'error',
		message: 'insecure style',
		startLine: 0,
		startCol: 46,
		startIndex: 46,
		endLine: 0,
		endCol: 70,
		endIndex: 70,
	},
]);

getValue

✅ Expand

returns: string | true
Get the value of the attribute.

// getValue
var attr = Parser.parse('<p id=a>').querySelector('html-attr');
assert.equal(attr, 'id=a');
assert.strictEqual(attr.getValue(), 'a');

cloneNode

Expand

returns: this
Deep clone the node.

// cloneNode
var [ext, html, table] = Parser.parse('<ref name=a/><p id=b>\n{|id=c\n|}')
	.querySelectorAll('ext-attr, html-attr, table-attr');
assert.equal(ext, 'name=a');
assert.equal(html, 'id=b');
assert.equal(table, 'id=c');
assert.deepStrictEqual(ext.cloneNode(), ext);
assert.deepStrictEqual(html.cloneNode(), html);
assert.deepStrictEqual(table.cloneNode(), table);

escape

Expand

Escape equal signs. Used inside templates.

// escape
var attr = Parser.parse('<p id=a>').querySelector('html-attr');
assert.equal(attr, 'id=a');
attr.escape();
assert.equal(attr, 'id{{=}}a');

close

Expand

Close the quotes.

// close
var attr = Parser.parse('<p id="a>').querySelector('html-attr');
assert.equal(attr, 'id="a');
attr.close();
assert.equal(attr, 'id="a"');

setValue

Expand

param: string | boolean Attribute value
Set the value of the attribute.

// setValue
var attr = Parser.parse('<p id=a>').querySelector('html-attr');
assert.equal(attr, 'id=a');
attr.setValue('b');
assert.equal(attr, 'id="b"');
attr.setValue(false);
assert.strictEqual(attr.parentNode, undefined);

rename

Expand

param: string Attribute name
Rename the attribute.

// rename
var attr = Parser.parse('<p id=a>').querySelector('html-attr');
assert.equal(attr, 'id=a');
attr.rename('class');
assert.equal(attr, 'class=a');
Clone this wiki locally