Comments (8)
The way parsing is done right now, that isn't easily possible. The example isn't valid HTML because the "<" and ">" should be escaped. I'm working on a 2.0 that should handle your example via the setting of an option.
from node-htmlparser.
i know its not valid HTML but unfortunately there is HTML out in the wild that doesn't escape brackets properly and most browsers do seem to ignore brackets when they are part of an attribute value. but thanks for attempting to handle it properly in version 2.0. your parser is extremely fast and useful even in its current state. great work!
from node-htmlparser.
When will this be fixed? A lot of sites have this issue...
from node-htmlparser.
I really needed a solution asap, so here we go with this small hack which works for all my cases; in the htmlparser.js just find the parseTags and add this bit of code;
Parser.prototype.parseTags = function Parser$parseTags () {
var bufferEnd = this._buffer.length - 1;
while (Parser._reTags.test(this._buffer)) {
this._next = Parser._reTags.lastIndex - 1;
var tagSep;// = this._buffer.charAt(this._next); //The currently found tag marker
var rawData;// = this._buffer.substring(this._current, this._next); //The next chunk of data to parse
var a1 = 0, a2 = 0;
for (var i=this._current; i<this._buffer.length; i++) {
if (this._buffer.charAt(i) == '"') {
if (a2 == 0) {
if (a1 == 0) a1 = i; else a1 = 0;
}
}
if (this._buffer.charAt(i) == "'") {
if (a1 == 0) {
if (a2 == 0) a2 = i; else a2 = 0;
}
}
if (a1 == 0 && a2 == 0 && (this._buffer.charAt(i) == '>' || this._buffer.charAt(i) == '<')) {
this._next = i;
tagSep = this._buffer.charAt(this._next);
rawData = this._buffer.substring(this._current, this._next);
break;
}
}
After adding this my jquery experiments are the same as they are in Firebug which saves me tons of work.
from node-htmlparser.
@tluyben: this seems to work well with my test cases as well. thanks for the patch!
from node-htmlparser.
Ofcourse you can remove the regex call altogether to save some speed;
//while (Parser._reTags.test(this._buffer)) {
while (true) {
this._next = -1; //Parser._reTags.lastIndex - 1;
var tagSep;// = this._buffer.charAt(this._next); //The currently found tag marker
var rawData;// = this._buffer.substring(this._current, this._next); //The next chunk of data to parse
var a1 = 0, a2 = 0;
for (var i=this._current; i<this._buffer.length; i++) {
if (this._buffer.charAt(i) == '"') {
if (a2 == 0) {
if (a1 == 0) a1 = i; else a1 = 0;
}
}
if (this._buffer.charAt(i) == "'") {
if (a1 == 0) {
if (a2 == 0) a2 = i; else a2 = 0;
}
}
if (a1 == 0 && a2 == 0 && (this._buffer.charAt(i) == '>' || this._buffer.charAt(i) == '<')) {
this._next = i;
tagSep = this._buffer.charAt(this._next);
rawData = this._buffer.substring(this._current, this._next);
break;
}
}
// are we done?
if (this._next < 0) {
break;
}
And you need a1,a2 only as 0/1 (boolean); not as the index; it's an index because while typing this I was going to follow another strategy which was harder which did use those indexes.
from node-htmlparser.
Any chance on getting this fix in? It would be a great help for a project I'm working on.
from node-htmlparser.
@scriby You can use my patched version (simply change dependency from 1.x
to git://github.com/petrbela/node-htmlparser.git
)... however, I think you'll be better off with v2.
from node-htmlparser.
Related Issues (20)
- does not support attribute names without values HOT 1
- <source> tags are not parsed properly HOT 1
- maybe you should update your documents~ HOT 3
- No DefaultHandler in master branch? HOT 2
- Can you give me a quick rundown on how to use this?
- act like a real stream HOT 1
- Parser hangs on some input
- erron on parse uncolsed tags
- Is this project dead? If so is there an alternative? HOT 5
- why can't I npm install v2.0.0
- Suspect code via npm (mal/adware???) HOT 1
- Bug in parser
- Problem when a <script> tag is not closed.
- "<" symbol not parsed as text when common browsers do it HOT 2
- Streaming To Parser in Node - Example Request HOT 1
- form detection in js
- '>' char used inside attribute value is matched by parser incorrectly as closing whole tag section HOT 1
- Vulnerable Regular Expression
- TypeError: Tautologistics.NodeHtmlParser.DefaultHandler is not a constructor HOT 3
- Convert JSON back to HTML HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-htmlparser.