AstText - bhsd-harry/wikiparser-node GitHub Wiki
目录
纯文本节点。仿照 Text 类设计,属性和方法也和 Text 类非常相似。这个类继承了 AstNode 类的全部属性和方法,这里不再列出。
✅ 在 Mini 和 Browser 版本中可用。
🌐 在 Browser 版本中可用。
从 AstNode 继承的属性
- bold
- eof
- font
- isConnected
- italic
- nextElementSibling
- nextSibling ✅
- nextVisibleSibling
- offsetHeight ✅
- offsetLeft
- offsetTop
- offsetWidth ✅
- parentNode ✅
- previousElementSibling
- previousSibling ✅
- previousVisibleSibling
- style
✅ 展开
type: string
文本内容,只读。
// data
var {firstChild} = Parser.parse("a");
assert.strictEqual(firstChild.data, "a");展开
type: number
文本长度。
// length (main)
var {firstChild} = Parser.parse("ab");
assert.strictEqual(firstChild.length, 2);
firstChild.length = 1;
assert.equal(firstChild, "a");✅ 展开
type: 'text'
只读。反过来 type: 'text' 也一定对应纯文本节点。
// type
var {firstChild} = Parser.parse("a");
assert.strictEqual(firstChild.type, "text");从 AstNode 继承的方法
- addEventListener
- after
- before
- compareDocumentPosition
- destroy
- dispatchEvent
- getAbsoluteIndex ✅
- getAncestors
- getLine
- getLines ✅
- getRelativeIndex ✅
- getRootNode ✅
- indexFromPos ✅
- is ✅
- isEqualNode
- listEventListeners
- posFromIndex ✅
- remove
- removeAllEventListeners
- removeEventListener
- replaceWith
展开
param: string 添加的字符串
在后方添加字符串。
// appendData (main)
var {firstChild} = Parser.parse("a");
firstChild.appendData("b");
assert.equal(firstChild, "ab");展开
returns: this
拷贝节点。
// cloneNode (main)
var {firstChild} = Parser.parse("a");
assert.deepStrictEqual(firstChild.cloneNode(), firstChild);展开
param: number 起始位置
param: number 删减字符数
删减字符串。
// deleteData (main)
var {firstChild} = Parser.parse("abcd");
firstChild.deleteData(-2, 1);
assert.equal(firstChild, "abd");
firstChild.deleteData(2);
assert.equal(firstChild, "ab");
firstChild.deleteData(-1, 2);
assert.equal(firstChild, "a");展开
加入的版本:1.1.4
转义 =。
// escape
var root = Parser.parse("a=b=|");
root.firstChild.escape();
assert.deepStrictEqual(
root.childNodes.map(String),
["a", "{{=}}", "b", "{{=}}", "{{!}}"],
);展开
param: number 插入位置
param: string 待插入的字符串
插入字符串。
// insertData (main)
var {firstChild} = Parser.parse("ab");
firstChild.insertData(-1, "c");
assert.equal(firstChild, "acb");
firstChild.insertData(0, "d");
assert.equal(firstChild, "dacb");✅ 展开
returns: LintError[]
报告潜在语法错误。
// lint
var root, lastChild, linkText, attrValue;
assert.deepStrictEqual(Parser.parse("<a>\n0<c<b </ i").firstChild.lint(), [
{
rule: "tag-like",
severity: "warning",
message: 'lonely "<"',
startLine: 0,
startCol: 0,
startIndex: 0,
endLine: 0,
endCol: 2,
endIndex: 2,
suggestions: [
{
desc: "escape",
range: [0, 1],
text: "<",
},
],
},
{
rule: "tag-like",
severity: "error",
message: 'lonely "<"',
startLine: 1,
startCol: 3,
startIndex: 7,
endLine: 1,
endCol: 5,
endIndex: 9,
suggestions: [
{
desc: "escape",
range: [7, 8],
text: "<",
},
],
},
{
rule: "tag-like",
severity: "warning",
message: 'lonely "<"',
startLine: 1,
startCol: 6,
startIndex: 10,
endLine: 1,
endCol: 10,
endIndex: 14,
suggestions: [
{
desc: "escape",
range: [10, 11],
text: "<",
},
],
},
]);
assert.deepStrictEqual(Parser.parse("-{").firstChild.lint(), [
{
rule: "lonely-bracket",
severity: "warning",
message: 'lonely "-{"',
startLine: 0,
startCol: 0,
startIndex: 0,
endLine: 0,
endCol: 2,
endIndex: 2,
},
]);
assert.deepStrictEqual(Parser.parse("}-").firstChild.lint(), [
{
rule: "lonely-bracket",
severity: "warning",
message: 'lonely "}-"',
startLine: 0,
startCol: 0,
startIndex: 0,
endLine: 0,
endCol: 2,
endIndex: 2,
},
]);
assert.deepStrictEqual(Parser.parse("}{}{{}}").firstChild.lint(), [
{
rule: "lonely-bracket",
severity: "warning",
message: 'lonely "}"',
startLine: 0,
startCol: 0,
startIndex: 0,
endLine: 0,
endCol: 1,
endIndex: 1,
},
{
rule: "lonely-bracket",
severity: "warning",
message: 'lonely "{"',
startLine: 0,
startCol: 3,
startIndex: 3,
endLine: 0,
endCol: 5,
endIndex: 5,
},
{
rule: "lonely-bracket",
severity: "warning",
message: 'lonely "}"',
startLine: 0,
startCol: 5,
startIndex: 5,
endLine: 0,
endCol: 7,
endIndex: 7,
},
]);
assert.deepStrictEqual(Parser.parse("{{a}}}").lastChild.lint(), [
{
rule: "lonely-bracket",
severity: "warning",
message: 'lonely "}"',
startLine: 0,
startCol: 5,
startIndex: 5,
endLine: 0,
endCol: 6,
endIndex: 6,
},
]);
assert.deepStrictEqual(Parser.parse("][][[]]").firstChild.lint(), [
{
rule: "lonely-bracket",
severity: "warning",
message: 'lonely "]"',
startLine: 0,
startCol: 0,
startIndex: 0,
endLine: 0,
endCol: 1,
endIndex: 1,
},
{
rule: "lonely-bracket",
severity: "warning",
message: 'lonely "["',
startLine: 0,
startCol: 3,
startIndex: 3,
endLine: 0,
endCol: 5,
endIndex: 5,
},
{
rule: "lonely-bracket",
severity: "warning",
message: 'lonely "]"',
startLine: 0,
startCol: 5,
startIndex: 5,
endLine: 0,
endCol: 7,
endIndex: 7,
},
]);
root = Parser.parse("[//a []]");
linkText = root.querySelector("ext-link-text");
({lastChild} = root);
assert.equal(linkText, "[");
assert.equal(lastChild, "]");
assert.deepStrictEqual(linkText.firstChild.lint(), [
{
rule: "lonely-bracket",
severity: "warning",
message: 'lonely "["',
startLine: 0,
startCol: 5,
startIndex: 5,
endLine: 0,
endCol: 6,
endIndex: 6,
suggestions: [
{
desc: "escape",
range: [6, 7],
text: "]",
},
],
},
]);
assert.deepStrictEqual(lastChild.lint(), [
{
rule: "lonely-bracket",
severity: "warning",
message: 'lonely "]"',
startLine: 0,
startCol: 7,
startIndex: 7,
endLine: 0,
endCol: 8,
endIndex: 8,
},
]);
assert.deepStrictEqual(Parser.parse("[ftp://a").firstChild.lint(), [
{
rule: "lonely-bracket",
severity: "error",
message: 'lonely "["',
startLine: 0,
startCol: 0,
startIndex: 0,
endLine: 0,
endCol: 1,
endIndex: 1,
},
]);
assert.deepStrictEqual(Parser.parse("ftp://a]").lastChild.lint(), [
{
rule: "lonely-bracket",
severity: "error",
message: 'lonely "]"',
startLine: 0,
startCol: 7,
startIndex: 7,
endLine: 0,
endCol: 8,
endIndex: 8,
suggestions: [
{
range: [0, 0],
text: "[",
desc: "opening bracket",
},
],
},
]);
assert.deepStrictEqual(Parser.parse("中HTTP://a").firstChild.lint(), [
{
rule: "lonely-http",
severity: "warning",
message: 'lonely "http://"',
startLine: 0,
startCol: 1,
startIndex: 1,
endLine: 0,
endCol: 8,
endIndex: 8,
suggestions: [
{
desc: "whitespace",
range: [1, 1],
text: " ",
},
],
},
]);
assert.deepStrictEqual(
Parser.parse("pmid: 1\nRFC:1\nisbn 123456").firstChild.lint(),
[
{
rule: "lonely-http",
severity: "warning",
message: 'lonely "PMID"',
startLine: 0,
startCol: 0,
startIndex: 0,
endLine: 0,
endCol: 4,
endIndex: 4,
suggestions: [
{
desc: "uppercase",
range: [0, 4],
text: "PMID",
},
{
desc: "whitespace",
range: [4, 5],
text: " ",
},
],
},
{
rule: "lonely-http",
severity: "warning",
message: 'lonely "RFC"',
startLine: 1,
startCol: 0,
startIndex: 8,
endLine: 1,
endCol: 3,
endIndex: 11,
suggestions: [
{
desc: "whitespace",
range: [11, 12],
text: " ",
},
],
},
{
rule: "lonely-http",
severity: "warning",
message: 'lonely "ISBN"',
startLine: 2,
startCol: 0,
startIndex: 14,
endLine: 2,
endCol: 4,
endIndex: 18,
suggestions: [
{
desc: "uppercase",
range: [14, 18],
text: "ISBN",
},
],
},
],
);
root = Parser.parse('<p title="[[A]]http://">');
attrValue = root.querySelector("attr-value");
assert.equal(attrValue, "[[A]]http://");
assert.deepStrictEqual(attrValue.firstChild.lint(), [
{
rule: "lonely-bracket",
severity: "warning",
message: 'lonely "["',
startLine: 0,
startCol: 10,
startIndex: 10,
endLine: 0,
endCol: 12,
endIndex: 12,
},
{
rule: "lonely-bracket",
severity: "warning",
message: 'lonely "]"',
startLine: 0,
startCol: 13,
startIndex: 13,
endLine: 0,
endCol: 15,
endIndex: 15,
},
]);
assert.deepStrictEqual(Parser.parse("Foo\n==Bar==.\n").firstChild.lint(), [
{
rule: "syntax-like",
severity: "error",
message: "header-like syntax in plain text",
startLine: 1,
startCol: 0,
startIndex: 4,
endLine: 1,
endCol: 8,
endIndex: 12,
},
]);
root = Parser.parse('<references group="[[a]]" />');
attrValue = root.querySelector("attr-value");
assert.equal(attrValue, "[[a]]");
assert.deepStrictEqual(attrValue.firstChild.lint(), []);🌐 展开
以 HTML 格式打印。
// print (print)
var {firstChild} = Parser.parse("&<>");
assert.equal(firstChild.print(), "&<>");✅ 展开
param: string 替换的字符串
替换字符串。
// replaceData
var {firstChild} = Parser.parse("a");
firstChild.replaceData("b");
assert.equal(firstChild, "b");展开
param: number 分裂位置
将文本子节点分裂为两部分。
// splitText
var {firstChild} = Parser.parse("ab");
firstChild.splitText(1);
assert.equal(firstChild, "a");
assert.equal(firstChild.nextSibling, "b");展开
param: number 起始位置
param: number 字符数
returns: string
提取子串。
// substringData (main)
var {firstChild} = Parser.parse("abc");
assert.strictEqual(firstChild.substringData(-2, 1), "b");
assert.strictEqual(firstChild.substringData(1), "bc");展开
加入的版本:1.10.0
param: boolean 是否禁用换行
returns: string
转换为 HTML。
// toHtml (main)
var {firstChild} = Parser.parse("<\n>");
assert.strictEqual(firstChild.toHtml(), "<\n>");
assert.strictEqual(firstChild.toHtml(true), "< >");