本文整理汇总了TypeScript中parse5.SAXParser类的典型用法代码示例。如果您正苦于以下问题:TypeScript SAXParser类的具体用法?TypeScript SAXParser怎么用?TypeScript SAXParser使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SAXParser类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: initPreRules
initPreRules(parser: SAXParser) {
this.stack = [];
this.issues = [];
var self = this;
var stack = this.stack;
parser.on("startTag", (name, attrs, selfClosing, location) => {
self.nextScope = null;
self.nextStack = null;
if (!selfClosing && !self.isVoid(name)) {
let currentScope = self.scope;
let nextScope = ""
if (stack.length > 0)
nextScope = stack[stack.length - 1].scope;
if (self.isScope(name))
nextScope = name;
self.nextScope = nextScope;
self.nextStack = new ParseNode(currentScope, name, location);
}
});
parser.on("endTag", (name, location) => {
if (stack.length <= 0 || stack[stack.length - 1].name != name) {
let issue = new Issue({
message: "mismatched close tag",
line: location.line,
column: location.col,
severity: IssueSeverity.Error
});
self.issues.push(issue);
}
else {
stack.pop();
if (stack.length > 0) {
self.scope = stack[stack.length - 1].scope;
}
else {
self.scope = "";
}
}
});
}
示例2: init
init(parser: SAXParser, parseState: ParseState) {
super.init(parser, parseState);
var syntax:RegExp = /(.+)( +of +)(.+)/
parser.on("startTag", (tag, attrs, selfClosing, loc) => {
var self = this;
attrs.forEach(attr => {
if (attr.name == "repeat") {
let error = new RuleError("did you miss `.for` on repeat?", loc.line, loc.col);
self.reportError(error);
return;
}
if (attr.name == "repeat.for") {
var script = attr.value.trim();
var matches = script.match(syntax);
var error = null;
if(matches == null || matches.length == 0){
let error = new RuleError("repeat syntax should be of form `* of *`", loc.line, loc.col);
self.reportError(error);
}
}
});
});
}
示例3: lint
lint(html: string|Stream): Promise<Issue[]> {
var parser: SAXParser = new SAXParser({ locationInfo: true });
var parseState: ParseState = new ParseState(this.scopes, this.voids);
parseState.initPreRules(parser);
let rules = this.rules;
rules.forEach((rule) => {
rule.init(parser, parseState);
});
parseState.initPostRules(parser);
var work:SAXParser;
if(typeof(html) === 'string')
{
var stream: Readable = new Readable();
stream.push(html);
stream.push(null);
work = stream.pipe(parser);
}else if(this.isStream(html))
{
work = html.pipe(parser);
}
else{
throw new Error("html isn't pipeable");
}
var completed = new Promise<void>(function (resolve, reject) {
work.on("end", () => {
parseState.finalise();
resolve();
});
});
var ruleTasks = [];
rules.forEach((rule) => {
let task = completed.then(() => {
return rule.finalise();
});
ruleTasks.push(task);
});
return Promise.all(ruleTasks).then(results => {
var all = new Array<Issue>();
results.forEach(parts => {
all = all.concat(parts);
});
return all;
});
}
示例4: findMatchingEnd
export function findMatchingEnd(text: string, pos: number, hasVoidElements: boolean): Match {
const starts: StartPositions = {};
let depth = 0;
let startFound: { name: string, depth: number, position: number };
let startMatch: Match;
let endMatch: Match;
const toId = (name: string) => name + depth;
const isVoid: (name: string) => boolean = hasVoidElements ? isVoidElement : () => false;
const parser = new parse.SAXParser({ locationInfo: true });
parser.on('startTag', (name: string, attrs, selfClosing, location: parse.LocationInfo) => {
const voidd = selfClosing || isVoid(name);
starts[toId(name)] = location;
if (inRange(pos, location.startOffset, name.length)) {
if (voidd) {
startMatch = { length: name.length, start: location.startOffset + START_LEN };
parser.stop();
} else {
startFound = { name, depth, position: location.startOffset };
}
}
if (!voidd) {
depth++;
}
});
parser.on('endTag', (name: string, location: parse.LocationInfo) => {
depth--;
if (startFound && startFound.name === name && startFound.depth === depth) {
endMatch = { length: name.length, start: startFound.position + START_LEN, end: location.startOffset + END_LEN };
parser.stop();
} else if (inRange(pos, location.startOffset + 1, name.length)) {
startMatch = { length: name.length, start: starts[toId(name)].startOffset + START_LEN, end: location.startOffset + END_LEN };
parser.stop();
}
});
parser.end(text);
return endMatch || startMatch;
}
示例5: initPostRules
initPostRules(parser: SAXParser) {
var self = this;
parser.on("startTag", () => {
if (self.nextScope !== null)
self.scope = self.nextScope;
self.nextScope = null;
if (self.nextStack != null)
self.stack.push(self.nextStack)
self.nextStack = null;
});
}
示例6: init
init(parser: SAXParser, parseState: ParseState) {
this.disable = false;
this.first = true;
this.count = 0;
parser.on('startTag', (name, attrs, selfClosing, location) => {
// Ignore Full HTML Documents
if (this.disable || name == 'html') {
this.disable = true;
return;
}
if (this.first) {
if (name != 'template') {
let error = new RuleError("root element is not template", location.line, location.col);
this.reportError(error);
return;
}
this.count++;
this.first = false;
return;
}
if (name == 'template') {
if (this.count > 0) {
let stack = parseState.stack;
let stackCount = stack.length;
if (stackCount > 0) {
this.containers.forEach(containerName => {
if(stack[stackCount-1].name == containerName)
{
let error = new RuleError(`template as child of <${containerName}> not allowed`, location.line, location.col);
this.reportError(error);
}
});
}
else {
let error = new RuleError("more than one template in file", location.line, location.col);
this.reportError(error);
}
}
this.count += 1;
}
});
}
示例7: init
init(parser: SAXParser, parseState: ParseState) {
parser.on("startTag", (tag, attrs, selfClosing, loc) => {
attrs.forEach(attr => {
var pattern = this.patterns.find(x => {
if(x.tag && x.tag != tag)
return false;
return matches != attr.name.match(x.attr);
});
if (pattern) {
var matches;
if (pattern.is != null) {
matches = attr.value.match(pattern.is);
if (matches == null || matches[0] != attr.value) {
let issue = new Issue({
message: pattern.msg || `attribute value doesn't match expected pattern`,
severity: IssueSeverity.Error,
line: loc.line,
column: loc.col,
});
this.reportIssue(issue);
}
} else if (pattern.not != null) {
matches = attr.value.match(pattern.not);
if (matches != null) {
let issue = new Issue({
message: pattern.msg || `attribute value matched a disallowed pattern`,
severity: IssueSeverity.Error,
line: loc.line,
column: loc.col
});
this.reportIssue(issue);
}
} else /* no value expected */ {
if (attr.value != "") {
let issue = new Issue({
message: pattern.msg || `attribute should not have a value`,
severity: IssueSeverity.Error,
line: loc.line,
column: loc.col
});
this.reportIssue(issue);
}
}
}
});
});
}
示例8: init
init(parser: SAXParser, parseState: ParseState) {
parser.on("startTag", (tag, attrs, selfClosing, loc) => {
var obsolete = this.obsoletes.find(x => x.tag == tag);
if (obsolete) {
let issue = new Issue({
message: `<${tag}> is obsolete`,
severity: IssueSeverity.Error,
line: loc.line,
column: loc.col,
detail: obsolete.msg || "",
});
this.reportIssue(issue);
}
});
}
示例9: init
init(parser: SAXParser, parseState: ParseState) {
var self = this;
var stack = parseState.stack;
parser.on("startTag", (tag, attrs, sc, loc) => {
if (tag == 'slot') {
var name = "";
var nameIndex = attrs.findIndex((a)=>a.name=="name");
if(nameIndex >= 0)
name = attrs[nameIndex].value;
self.slots.push({name:name, loc:loc});
}
})
}
示例10: init
init(parser: SAXParser, parseState: ParseState) {
this.ids = [];
parser.on('startTag', (name, attrs, selfClosing, loc) => {
let idAttr = attrs.find(x => x.name == "id");
if (!idAttr)
return;
var id = idAttr.value;
if (id === "") {
let issue = new Issue({
message: "id cannot be empty",
severity: IssueSeverity.Warning,
line: loc.line,
column: loc.col
});
this.reportIssue(issue);
}
else if (id.match(/^[^a-z]+|[^\w:.-]+/) != null){
let issue = new Issue({
message: "id contains illegal characters",
severity: IssueSeverity.Error,
line: loc.line,
column: loc.col
});
this.reportIssue(issue);
}
else if (this.ids.indexOf(id) != -1) {
let issue = new Issue({
message: `duplicated id: ${id}`,
severity: IssueSeverity.Error,
line: loc.line,
column: loc.col
});
this.reportIssue(issue);
}
this.ids.push(id);
});
}