mirror of
https://github.com/danbulant/node-html-parser
synced 2026-06-19 22:51:29 +00:00
fix: [issure10](https://github.com/ashi009/node-fast-html-parser/issues/10)
This commit is contained in:
parent
9d16798f46
commit
2504afe8e1
1 changed files with 20 additions and 6 deletions
26
src/index.ts
26
src/index.ts
|
|
@ -482,15 +482,29 @@ export class Matcher {
|
||||||
const parts = matcher.split('.');
|
const parts = matcher.split('.');
|
||||||
const tagName = parts[0];
|
const tagName = parts[0];
|
||||||
const classes = parts.slice(1).sort();
|
const classes = parts.slice(1).sort();
|
||||||
let source = '';
|
let source = '"use strict";';
|
||||||
if (tagName && tagName != '*') {
|
if (tagName && tagName != '*') {
|
||||||
if (tagName[0] == '#')
|
let matcher: RegExpMatchArray;
|
||||||
|
if (tagName[0] == '#') {
|
||||||
source += 'if (el.id != ' + JSON.stringify(tagName.substr(1)) + ') return false;';
|
source += 'if (el.id != ' + JSON.stringify(tagName.substr(1)) + ') return false;';
|
||||||
else
|
} else if (matcher = tagName.match(/^\[\s*(\S+)\s*(=|!=)\s*((((["'])([^\6]*)\6))|(\S*?))\]\s*/)) {
|
||||||
|
const attr_key = matcher[1];
|
||||||
|
let method = matcher[2];
|
||||||
|
if (method !== '=' && method !== '!=') {
|
||||||
|
throw new Error('Selector not supported, Expect [key${op}value].op must be =,!=');
|
||||||
|
}
|
||||||
|
if (method === '=') {
|
||||||
|
method = '==';
|
||||||
|
}
|
||||||
|
const value = matcher[7] || matcher[8];
|
||||||
|
source += `const attrs = el.attributes;for (const key in attrs){const val = attrs[key]; if (key == "${attr_key}" && val ${method} "${value}"){return true;}} return false;`;
|
||||||
|
} else {
|
||||||
source += 'if (el.tagName != ' + JSON.stringify(tagName) + ') return false;';
|
source += 'if (el.tagName != ' + JSON.stringify(tagName) + ') return false;';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (classes.length > 0)
|
if (classes.length > 0) {
|
||||||
source += 'for (var cls = ' + JSON.stringify(classes) + ', i = 0; i < cls.length; i++) if (el.classNames.indexOf(cls[i]) === -1) return false;';
|
source += 'for (var cls = ' + JSON.stringify(classes) + ', i = 0; i < cls.length; i++) if (el.classNames.indexOf(cls[i]) === -1) return false;';
|
||||||
|
}
|
||||||
source += 'return true;';
|
source += 'return true;';
|
||||||
return pMatchFunctionCache[matcher] = new Function('el', source) as MatherFunction;
|
return pMatchFunctionCache[matcher] = new Function('el', source) as MatherFunction;
|
||||||
});
|
});
|
||||||
|
|
@ -537,7 +551,7 @@ export class Matcher {
|
||||||
}
|
}
|
||||||
|
|
||||||
const kMarkupPattern = /<!--[^]*?(?=-->)-->|<(\/?)([a-z][a-z0-9]*)\s*([^>]*?)(\/?)>/ig;
|
const kMarkupPattern = /<!--[^]*?(?=-->)-->|<(\/?)([a-z][a-z0-9]*)\s*([^>]*?)(\/?)>/ig;
|
||||||
const kAttributePattern = /\b(id|class)\s*=\s*("([^"]+)"|'([^']+)'|(\S+))/ig;
|
const kAttributePattern = /(^|\s)(id|class)\s*=\s*("([^"]+)"|'([^']+)'|(\S+))/ig;
|
||||||
const kSelfClosingElements = {
|
const kSelfClosingElements = {
|
||||||
meta: true,
|
meta: true,
|
||||||
img: true,
|
img: true,
|
||||||
|
|
@ -604,7 +618,7 @@ export function parse(data: string, options?: {
|
||||||
// not </ tags
|
// not </ tags
|
||||||
var attrs = {};
|
var attrs = {};
|
||||||
for (var attMatch; attMatch = kAttributePattern.exec(match[3]);)
|
for (var attMatch; attMatch = kAttributePattern.exec(match[3]);)
|
||||||
attrs[attMatch[1]] = attMatch[3] || attMatch[4] || attMatch[5];
|
attrs[attMatch[2]] = attMatch[4] || attMatch[5] || attMatch[6];
|
||||||
// console.log(attrs);
|
// console.log(attrs);
|
||||||
if (!match[4] && kElementsClosedByOpening[currentParent.tagName]) {
|
if (!match[4] && kElementsClosedByOpening[currentParent.tagName]) {
|
||||||
if (kElementsClosedByOpening[currentParent.tagName][match[2]]) {
|
if (kElementsClosedByOpening[currentParent.tagName][match[2]]) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue