Работа над XmlParser
This commit is contained in:
@@ -131,6 +131,18 @@ class NodeObject extends NodeBase {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
set value(v) {
|
||||||
|
switch (this.type) {
|
||||||
|
case NODE:
|
||||||
|
this.raw[3] = v;
|
||||||
|
break;
|
||||||
|
case TEXT:
|
||||||
|
case CDATA:
|
||||||
|
case COMMENT:
|
||||||
|
this.raw[1] = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
add(node, after = '*') {
|
add(node, after = '*') {
|
||||||
if (this.type !== NODE)
|
if (this.type !== NODE)
|
||||||
return;
|
return;
|
||||||
@@ -338,20 +350,28 @@ class XmlParser extends NodeBase {
|
|||||||
return new XmlParser(newRawNodes);
|
return new XmlParser(newRawNodes);
|
||||||
}
|
}
|
||||||
|
|
||||||
s(selector, self) {
|
$$(selector, self) {
|
||||||
return this.select(selector, self);
|
return this.select(selector, self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$$self(selector) {
|
||||||
|
return this.$$(selector, true);
|
||||||
|
}
|
||||||
|
|
||||||
selectFirst(selector, self) {
|
selectFirst(selector, self) {
|
||||||
const result = this.select(selector, self);
|
const result = this.select(selector, self);
|
||||||
const node = (result.count ? result.rawNodes[0] : null);
|
const node = (result.count ? result.rawNodes[0] : null);
|
||||||
return this.toObject(node);
|
return this.toObject(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
sf(selector, self) {
|
$(selector, self) {
|
||||||
return this.selectFirst(selector, self);
|
return this.selectFirst(selector, self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$self(selector) {
|
||||||
|
return this.$(selector, true);
|
||||||
|
}
|
||||||
|
|
||||||
toJson(options = {}) {
|
toJson(options = {}) {
|
||||||
const {format = false} = options;
|
const {format = false} = options;
|
||||||
|
|
||||||
@@ -383,7 +403,6 @@ class XmlParser extends NodeBase {
|
|||||||
|
|
||||||
for (const n of nodes) {
|
for (const n of nodes) {
|
||||||
const node = new NodeObject(n);
|
const node = new NodeObject(n);
|
||||||
lastType = node.type;
|
|
||||||
|
|
||||||
let open = '';
|
let open = '';
|
||||||
let body = '';
|
let body = '';
|
||||||
@@ -412,8 +431,8 @@ class XmlParser extends NodeBase {
|
|||||||
close = `</${node.name}>`;
|
close = `</${node.name}>`;
|
||||||
|
|
||||||
if (format) {
|
if (format) {
|
||||||
open = '\n' + ' '.repeat(depth) + open;
|
open = (lastType !== TEXT ? '\n' + ' '.repeat(depth) : '') + open;
|
||||||
close = (deepType === NODE ? ' '.repeat(depth) : '') + close + '\n';
|
close = (deepType === NODE ? '\n' + ' '.repeat(depth) : '') + close;
|
||||||
}
|
}
|
||||||
} else if (node.type === TEXT) {
|
} else if (node.type === TEXT) {
|
||||||
body = node.value || '';
|
body = node.value || '';
|
||||||
@@ -424,6 +443,7 @@ class XmlParser extends NodeBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
result += `${open}${body}${close}`;
|
result += `${open}${body}${close}`;
|
||||||
|
lastType = node.type;
|
||||||
}
|
}
|
||||||
|
|
||||||
deepType = lastType;
|
deepType = lastType;
|
||||||
@@ -437,7 +457,7 @@ class XmlParser extends NodeBase {
|
|||||||
|
|
||||||
fromString(xmlString, options = {}, pickNode = () => true) {
|
fromString(xmlString, options = {}, pickNode = () => true) {
|
||||||
const parsed = [];
|
const parsed = [];
|
||||||
const root = this.createNode(parsed);
|
const root = this.createNode('root', null, parsed);//fake node
|
||||||
let node = root;
|
let node = root;
|
||||||
|
|
||||||
let route = '';
|
let route = '';
|
||||||
@@ -460,7 +480,7 @@ class XmlParser extends NodeBase {
|
|||||||
if (ignoreNode)
|
if (ignoreNode)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (tail) {
|
if (tail && tail.trim() !== '') {
|
||||||
const parsedAttrs = sax.getAttrsSync(tail, lowerCase);
|
const parsedAttrs = sax.getAttrsSync(tail, lowerCase);
|
||||||
const attrs = new Map();
|
const attrs = new Map();
|
||||||
for (const attr of parsedAttrs.values()) {
|
for (const attr of parsedAttrs.values()) {
|
||||||
|
|||||||
@@ -288,9 +288,11 @@ function getAttrsSync(tail, lowerCase = true) {
|
|||||||
let inName = false;
|
let inName = false;
|
||||||
let inValue = false;
|
let inValue = false;
|
||||||
let waitValue = false;
|
let waitValue = false;
|
||||||
let waitEq = false;
|
let waitEq = true;
|
||||||
|
|
||||||
const pushResult = () => {
|
const pushResult = () => {
|
||||||
|
if (waitEq)
|
||||||
|
value = true;
|
||||||
if (lowerCase)
|
if (lowerCase)
|
||||||
name = name.toLowerCase();
|
name = name.toLowerCase();
|
||||||
if (name != '') {
|
if (name != '') {
|
||||||
@@ -308,7 +310,7 @@ function getAttrsSync(tail, lowerCase = true) {
|
|||||||
inName = false;
|
inName = false;
|
||||||
inValue = false;
|
inValue = false;
|
||||||
waitValue = false;
|
waitValue = false;
|
||||||
waitEq = false;
|
waitEq = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
tail = tail.replace(/[\t\n\r]/g, ' ');
|
tail = tail.replace(/[\t\n\r]/g, ' ');
|
||||||
@@ -321,7 +323,6 @@ function getAttrsSync(tail, lowerCase = true) {
|
|||||||
else
|
else
|
||||||
pushResult();
|
pushResult();
|
||||||
} else if (inName) {
|
} else if (inName) {
|
||||||
waitEq = true;
|
|
||||||
inName = false;
|
inName = false;
|
||||||
}
|
}
|
||||||
} else if (!inValue && c == '=') {
|
} else if (!inValue && c == '=') {
|
||||||
|
|||||||
Reference in New Issue
Block a user