Giter Club home page Giter Club logo

Comments (8)

tautologistics avatar tautologistics commented on July 26, 2024

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.

vpulim avatar vpulim commented on July 26, 2024

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.

tluyben avatar tluyben commented on July 26, 2024

When will this be fixed? A lot of sites have this issue...

from node-htmlparser.

tluyben avatar tluyben commented on July 26, 2024

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.

vpulim avatar vpulim commented on July 26, 2024

@tluyben: this seems to work well with my test cases as well. thanks for the patch!

from node-htmlparser.

tluyben avatar tluyben commented on July 26, 2024

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.

scriby avatar scriby commented on July 26, 2024

Any chance on getting this fix in? It would be a great help for a project I'm working on.

from node-htmlparser.

petrbela avatar petrbela commented on July 26, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.