當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript parse5.SAXParser類代碼示例

本文整理匯總了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 = "";
                }
            }
        });
    }
開發者ID:gitter-badger,項目名稱:template-lint,代碼行數:48,代碼來源:parse-state.ts

示例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);
                    }                       
                }
            });
        });
    }
開發者ID:Thanood,項目名稱:aurelia-template-lint,代碼行數:30,代碼來源:repeatfor.ts

示例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;
        });
    }
開發者ID:atsu85,項目名稱:template-lint,代碼行數:58,代碼來源:linter.ts

示例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;
}
開發者ID:krizzdewizz,項目名稱:vscode-tag-rename,代碼行數:43,代碼來源:rename.ts

示例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;
        });
    }
開發者ID:gitter-badger,項目名稱:template-lint,代碼行數:13,代碼來源:parse-state.ts

示例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;
            }
        }); 
    }  
開發者ID:Thanood,項目名稱:aurelia-template-lint,代碼行數:51,代碼來源:template.ts

示例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);
                        }
                    }
                }
            });
        });
    }
開發者ID:atsu85,項目名稱:template-lint,代碼行數:50,代碼來源:attribute-value.ts

示例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);
         }
     });
 }
開發者ID:atsu85,項目名稱:template-lint,代碼行數:15,代碼來源:obsolete-tag.ts

示例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});
            }
        })
    }
開發者ID:Thanood,項目名稱:aurelia-template-lint,代碼行數:17,代碼來源:slot.ts

示例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);
        });
    }
開發者ID:atsu85,項目名稱:template-lint,代碼行數:44,代碼來源:unique-id.ts


注:本文中的parse5.SAXParser類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。