AstElement (EN) - bhsd-harry/wikiparser-node GitHub Wiki
Table of Contents
- Introduction
- Properties
-
Methods
- append ✅
- caretPositionFromIndex ✅
- caretPositionFromPoint
- closest ✅
- elementFromIndex ✅
- elementFromPoint ✅
- elementsFromIndex
- elementsFromPoint
- getElementById
- getElementByTypes
- getElementsByClassName
- getElementsByTagName
- insertAt ✅
- insertBefore
- json 🌐
- lint ✅
- matches
- normalize
- prepend
- print 🌐
- querySelector ✅
- querySelectorAll ✅
- removeAt ✅
- removeChild
- replaceChildren
- setText ✅
- text ✅
AstElement is the parent class of non-text nodes. It is modeled after the HTMLElement class, and has properties and methods very similar to the HTMLElement class. This class inherits all the properties and methods of the AstNode class which are not repeated here.
✅ Available in the Mini and Browser versions.
🌐 Available in the Browser version.
Expand
type: number
Total number of non-text child nodes, read-only.
// childElementCount (main)
assert.strictEqual(Parser.parse([[a]]b').childElementCount, 1);Expand
type: Token[]
Total number of non-text child nodes, read-only.
// children (main)
var root = Parser.parse([[a]]b'),
{firstChild} = root;
assert.equal(firstChild, '[[a]]');
assert.deepStrictEqual(root.children, [firstChild]);Expand
type: number
Inner height, only exists when the innerText property is defined, read-only.
// clientHeight (main)
var root = Parser.parse('<pre>a\nb</pre>'),
{firstChild} = root;
assert.equal(firstChild, '<pre>a\nb</pre>');
assert.strictEqual(firstChild.innerText, 'a\nb');
assert.strictEqual(root.innerText, undefined);
assert.strictEqual(firstChild.clientHeight, 2);
assert.strictEqual(root.clientHeight, undefined);Expand
type: number
Inner width, only exists when the innerText property is defined, read-only.
// clientWidth (main)
var root = Parser.parse('<pre>ab\nc</pre>'),
{firstChild} = root;
assert.equal(firstChild, '<pre>ab\nc</pre>');
assert.strictEqual(firstChild.innerText, 'ab\nc');
assert.strictEqual(root.innerText, undefined);
assert.strictEqual(firstChild.clientWidth, 1);
assert.strictEqual(root.clientWidth, undefined);Expand
type: Token[]
All embedded templates and modules, read-only.
// embeds (main)
var root = Parser.parse('{{a}}{{#invoke:b|c}}'),
{firstChild, lastChild} = root;
assert.equal(firstChild, '{{a}}');
assert.equal(lastChild, '{{#invoke:b|c}}');
assert.deepStrictEqual(root.embeds, [firstChild, lastChild]);Expand
type: Token
First non-text child node, read-only.
// firstElementChild (main)
var root = Parser.parse('b[[a]]'),
{lastChild} = root;
assert.equal(lastChild, '[[a]]');
assert.strictEqual(root.firstElementChild, lastChild);hidden
Expand
type: boolean
Whether the node is invisible, read-only.
// hidden (main)
var root = Parser.parse('<!--a-->__nocc__'),
{firstChild, lastChild} = root;
assert.equal(firstChild, '<!--a-->');
assert.equal(lastChild, '__nocc__');
assert.ok(firstChild.hidden);
assert.ok(lastChild.hidden);
assert.ok(root.hidden);Expand
type: Token[]
All image nodes, read-only.
// images (main)
var root = Parser.parse(
[[file:a]]<gallery>b</gallery><imagemap>file:c</imagemap>',
),
{
childNodes: [
file,
{lastChild: {firstChild: gallery}},
{lastChild: {firstChild: imagemap}},
],
} = root;
assert.equal(file, '[[file:a]]');
assert.equal(gallery, 'b');
assert.equal(imagemap, 'file:c');
assert.deepStrictEqual(root.images, [file, gallery, imagemap]);Expand
type: Token
Last non-text child node, read-only.
// lastElementChild (main)
var root = Parser.parse([[a]]b'),
{firstChild} = root;
assert.equal(firstChild, '[[a]]');
assert.strictEqual(root.lastElementChild, firstChild);✅ Expand
type: number
Total number of child nodes.
// length
assert.strictEqual(Parser.parse([[a]]b').length, 2);// length (main)
var root = Parser.parse([[a]]b');
root.length = 1;
assert.equal(root, '[[a]]');Expand
type: Token[]
All internal links, external links, free external links and image links, read-only.
// links (main)
var root = Parser.parse(
'#redirect [[x]]\n[[a]][//b]http://c PMID 1[[file:d|link=[[e]]]]',
),
{
childNodes: [
{lastChild: redirect},
link,
extLink,
freeExtLink,,
magicLink,
{lastChild: imageLink},
],
} = root;
assert.equal(redirect, '[[x]]');
assert.equal(link, '[[a]]');
assert.equal(extLink, '[//b]');
assert.equal(freeExtLink, 'http://c');
assert.equal(magicLink, 'PMID 1');
assert.equal(imageLink, 'link=[[e]]');
assert.deepStrictEqual(root.links, [
redirect,
link,
extLink,
freeExtLink,
magicLink,
imageLink,
]);Expand
type: string
Equivalent to the text method, read-only.
// outerText (main)
assert.strictEqual(
Parser.parse('<!--a-->b{{c|__nocc__}}').outerText,
'b{{c|}}',
);Expand
type: Token
Parent node, alias of the parentNode property, read-only.
// parentElement (main)
var root = Parser.parse('[[a]]'),
{firstChild} = root;
assert.equal(firstChild, '[[a]]');
assert.strictEqual(firstChild.parentElement, root);
assert.strictEqual(root.parentElement, undefined);✅ Expand
param: AstNode | string Child node to be inserted
Batch insert child nodes at the end.
// append
var root = Parser.parse('a');
root.append('b', 'c');
assert.equal(root, 'abc');✅ Expand
param: number Character position
returns: {offsetNode: AstNode, offset: number}
Find the given position.
// caretPositionFromIndex
var root = Parser.parse([[a]]bb[[c]]'),
{firstChild, lastChild} = root,
{nextSibling, firstChild: {firstChild: target}} = firstChild;
assert.equal(firstChild, '[[a]]');
assert.equal(target, 'a');
assert.equal(nextSibling, 'bb');
assert.equal(lastChild, '[[c]]');
assert.deepStrictEqual(root.caretPositionFromIndex(1), {
offsetNode: firstChild,
offset: 1,
});
assert.deepStrictEqual(root.caretPositionFromIndex(2), {
offsetNode: target,
offset: 0,
});
assert.deepStrictEqual(root.caretPositionFromIndex(5), {
offsetNode: firstChild,
offset: 5,
});
assert.deepStrictEqual(root.caretPositionFromIndex(6), {
offsetNode: nextSibling,
offset: 1,
});
assert.deepStrictEqual(root.caretPositionFromIndex(7), {
offsetNode: lastChild,
offset: 0,
});
assert.deepStrictEqual(root.caretPositionFromIndex(-1), {
offsetNode: lastChild,
offset: 4,
});
assert.deepStrictEqual(root.caretPositionFromIndex(13), undefined);Expand
param: number Character column
param: number Character row
returns: {offsetNode: AstNode, offset: number}
Find the given position.
// caretPositionFromPoint (main)
var root = Parser.parse([[a]]\nb[[c]]'),
{firstChild, lastChild} = root,
{nextSibling, firstChild: {firstChild: target}} = firstChild;
assert.equal(firstChild, '[[a]]');
assert.equal(target, 'a');
assert.equal(nextSibling, '\nb');
assert.equal(lastChild, '[[c]]');
assert.deepStrictEqual(root.caretPositionFromPoint(1, 0), {
offsetNode: firstChild,
offset: 1,
});
assert.deepStrictEqual(root.caretPositionFromPoint(2, 0), {
offsetNode: target,
offset: 0,
});
assert.deepStrictEqual(root.caretPositionFromPoint(5, 0), {
offsetNode: firstChild,
offset: 5,
});
assert.deepStrictEqual(root.caretPositionFromPoint(0, 1), {
offsetNode: nextSibling,
offset: 1,
});
assert.deepStrictEqual(root.caretPositionFromPoint(1, 1), {
offsetNode: lastChild,
offset: 0,
});
assert.deepStrictEqual(root.caretPositionFromPoint(0, 2), undefined);✅ Expand
param: string Selector
Closest ancestor node.
// closest
var root = Parser.parse('[[a]]'),
{firstChild: link} = root,
{firstChild} = link;
assert.equal(link, '[[a]]');
assert.strictEqual(firstChild.closest('root'), root);
assert.strictEqual(firstChild.closest('root, link'), link);// closest (main)
var root = Parser.parse('[[a]]'),
{firstChild: link} = root,
{firstChild} = link;
assert.equal(link, '[[a]]');
assert.strictEqual(firstChild.closest('link#A'), link);
assert.strictEqual(firstChild.closest('root, link#B'), root);✅ Expand
param: number Character position
returns: Token
Find the outermost node at the given position.
// elementFromIndex
var root = Parser.parse([[a]]bb'),
{firstChild, lastChild} = root,
{firstChild: target} = firstChild;
assert.equal(firstChild, '[[a]]');
assert.equal(target, 'a');
assert.equal(lastChild, 'bb');
assert.strictEqual(root.elementFromIndex(1), firstChild);
assert.strictEqual(root.elementFromIndex(2), target);
assert.strictEqual(root.elementFromIndex(6), root);✅ Expand
param: number Character column
param: number Character row
returns: Token
Find the outermost node at the given position.
// elementFromPoint
var root = Parser.parse([[a]]\nb'),
{firstChild, lastChild} = root,
{firstChild: target} = firstChild;
assert.equal(firstChild, '[[a]]');
assert.equal(target, 'a');
assert.equal(lastChild, '\nb');
assert.strictEqual(root.elementFromPoint(1, 0), firstChild);
assert.strictEqual(root.elementFromPoint(2, 0), target);
assert.strictEqual(root.elementFromPoint(0, 1), root);Expand
param: number Character position
returns: Token[]
Find all nodes at the given position.
// elementsFromIndex (main)
var root = Parser.parse([[a]]bb'),
{firstChild, lastChild} = root;
assert.equal(firstChild, '[[a]]');
assert.equal(lastChild, 'bb');
assert.deepStrictEqual(root.elementsFromIndex(1), [root, firstChild]);
assert.deepStrictEqual(root.elementsFromIndex(6), [root]);
assert.deepStrictEqual(root.elementsFromIndex(8), []);Expand
param: number Character column
param: number Character row
returns: Token[]
Find all nodes at the given position.
// elementsFromPoint (main)
var root = Parser.parse([[a]]\nb'),
{firstChild, lastChild} = root;
assert.equal(firstChild, '[[a]]');
assert.equal(lastChild, '\nb');
assert.deepStrictEqual(root.elementsFromPoint(1, 0), [root, firstChild]);
assert.deepStrictEqual(root.elementsFromPoint(0, 1), [root]);Expand
param: string id
returns: Token
Select by id.
// getElementById (main)
var root = Parser.parse('<p id=a><b id=a>'),
{firstChild} = root;
assert.equal(firstChild, '<p id=a>');
assert.strictEqual(root.getElementById('a'), firstChild);Expand
param: string Token types delimited by ,
returns: Token
Select by token type.
// getElementByTypes (main)
var root = Parser.parse([[a]]<i>'),
{firstChild} = root;
assert.equal(firstChild, '[[a]]');
assert.strictEqual(root.getElementByTypes('html, link'), firstChild);Expand
param: string Class name
returns: Token[]
Select by class name.
// getElementsByClassName (main)
var root = Parser.parse(`{|class="a b"
|}<p class="a c"><pre class="a d"></pre>`),
{childNodes: [table, html, ext]} = root;
assert.equal(table, '{|class="a b"\n|}');
assert.equal(table.childNodes[1], 'class="a b"');
assert.equal(html, '<p class="a c">');
assert.equal(html.firstChild, ' class="a c"');
assert.equal(ext, '<pre class="a d"></pre>');
assert.equal(ext.firstChild, ' class="a d"');
assert.deepStrictEqual(root.getElementsByClassName('a'), [
table,
table.childNodes[1],
html,
html.firstChild,
ext,
ext.firstChild,
]);Expand
param: string Tag name
returns: Token[]
Select by tag name.
// getElementsByTagName (main)
var root = Parser.parse('<pre></pre></pre>'),
{firstChild, lastChild} = root;
assert.equal(firstChild, '<pre></pre>');
assert.equal(lastChild, '</pre>');
assert.deepStrictEqual(root.getElementsByTagName('pre'), [
firstChild,
lastChild,
]);✅ Expand
param: AstNode | string Child node to be inserted
param: number Position of the child node to be inserted
returns: AstNode
Insert a child node at the specified position.
// insertAt
var root = Parser.parse('a');
root.insertAt('b', 0);
assert.equal(root, 'ba');// insertAt (main)
var a = Parser.parse('a'),
b = Parser.parse('b');
a.insertAt(b.firstChild);
assert.equal(a, 'ab');
assert.equal(b, '');Expand
param: AstNode Child node to be inserted
param: AstNode Reference node
Insert a child node before the specified position.
// insertBefore (main)
var root = Parser.parse('a');
root.insertBefore('b');
assert.equal(root, 'ab');
root.insertBefore('c', root.lastChild);
assert.equal(root, 'acb');🌐 Expand
param: string file name
Save the syntax tree as JSON. The saved JSON files are in the printed/ path of WikiParser-Node.
// json
assert.deepStrictEqual(Parser.parse('<!-- a -->').json(), {
range: [0, 10],
type: 'root',
childNodes: [
{
range: [0, 10],
type: 'comment',
closed: true,
childNodes: [
{
range: [4, 7],
data: ' a ',
},
],
},
],
});✅ Expand
returns: LintError[] & {output?: string}
Report potential grammar errors. See the documentation for each type of nodes for details.
Expand
param: string Selector
returns: boolean
Check if it matches the selector.
// matches (main)
var {firstChild} = Parser.parse('<br/>');
assert.ok(firstChild.matches('root > :is(#br[selfClosing])'));Expand
Merge adjacent text child nodes.
// normalize
var root = Parser.parse('[[a]]');
root.append('');
assert.strictEqual(root.length, 2);
root.normalize();
assert.strictEqual(root.length, 1);// normalize (main)
var root = Parser.parse('a');
root.prepend('b');
assert.strictEqual(root.length, 2);
root.normalize();
assert.strictEqual(root.length, 1);Expand
param: AstNode | string Child nodes to be inserted
Batch insert child nodes at the beginning.
// prepend (main)
var root = Parser.parse('a');
root.prepend('b', 'c');
assert.equal(root, 'bca');🌐 Expand
returns: string
Output in HTML format. See the documentation for each type of nodes for details.
// print
var {firstChild} = Parser.parse('<!-- a -->');
assert.equal(firstChild, '<!-- a -->');
assert.strictEqual(
firstChild.print(),
'<span class="wpb-comment"><!-- a --></span>',
);✅ Expand
param: string Selector
returns: Token
First descendant node that matches the selector.
// querySelector
var root = Parser.parse('a<p><i>'),
[, p] = root.childNodes;
assert.equal(p, '<p>');
assert.strictEqual(root.querySelector('html'), p);✅ Expand
param: string Selector
returns: Token[]
All descendant nodes that match the selector.
// querySelectorAll
var root = Parser.parse('{{{|<p>}}}a<i>'),
{firstChild: {lastChild: {firstChild}}, lastChild} = root;
assert.equal(firstChild, '<p>');
assert.equal(lastChild, '<i>');
assert.deepStrictEqual(root.querySelectorAll('html'), [
firstChild,
lastChild,
]);✅ Expand
param: number Position of the child node to be removed
returns: AstNode
Remove a child node at the specified position.
// removeAt
var root = Parser.parse([[a]]b');
assert.equal(root.removeAt(1), 'b');
assert.equal(root, '[[a]]');Expand
param: AstNode Child node to be removed
Remove a child node.
// removeChild (main)
var root = Parser.parse('a[[b]]');
assert.equal(root.removeChild(root.firstChild), 'a');
assert.equal(root, '[[b]]');Expand
param: AstNode | string New child nodes
Replace all child nodes.
// replaceChildren (main)
var root = Parser.parse('a[[b]]');
root.replaceChildren('c', 'd');
assert.equal(root, 'cd');✅ Expand
param: string New text
param: number Position of the text child node to be modified
returns: string
Modify the text child node.
// setText
var root = Parser.parse([[a]]b');
root.setText('c', -1);
assert.equal(root, [[a]]c');✅ Expand
returns: string
Visible text. See the documentation for each type of nodes for details.
// text
assert.strictEqual(Parser.parse('<!--a-->b{{c|__nocc__}}').text(), 'b{{c|}}');