New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

finds a url inside a base64 encoded image string #117

Closed
chovy opened this Issue Sep 12, 2013 · 8 comments

Comments

Projects
None yet
2 participants
@chovy

chovy commented Sep 12, 2013

I don't know if this is intended or not, but its causing problems for me.

a base64 encoded image data url is being recognized as a url, so there ends up being a n anchor tag wrapped inside the src attribute of the image when using

If my text contains something like this:

<img src="data:image/png;base64,iVBORw0K...">

Using this method

         text = URI.withinString(text, function(url) {
                return '<a href="'+url+'" class="ext">'+url+'</a>
         });

I end up with this:

<img src="<a href="data:image/png;base64,iVBORw0K...">data:image/png;base64,iVBORw0K...</a>">

...which obviously is invalid html.

@rodneyrehm

This comment has been minimized.

Show comment
Hide comment
@rodneyrehm

rodneyrehm Sep 12, 2013

Member

.withinString() can't handle HTML. It never could, it never will. If you want to convert URLs to links on HTML content or the DOM, have a look at this example. Note that the example is using TreeWalker which is not available/fully supported in older browsers.

Member

rodneyrehm commented Sep 12, 2013

.withinString() can't handle HTML. It never could, it never will. If you want to convert URLs to links on HTML content or the DOM, have a look at this example. Note that the example is using TreeWalker which is not available/fully supported in older browsers.

@chovy

This comment has been minimized.

Show comment
Hide comment
@chovy

chovy Sep 12, 2013

its actually a string with HTML embedded in it. Before it gets added to the dom.

I wonder if I can add it to the dom first, then filter on text nodes, and apy withinString to those nodes instead of the html string.

chovy commented Sep 12, 2013

its actually a string with HTML embedded in it. Before it gets added to the dom.

I wonder if I can add it to the dom first, then filter on text nodes, and apy withinString to those nodes instead of the html string.

@rodneyrehm

This comment has been minimized.

Show comment
Hide comment
@rodneyrehm

rodneyrehm Sep 12, 2013

Member

that's exactly what the jsbin demo is supposed to show ;)

let me know what you made of it. maybe we should add this method to URI.js' jQuery plugin?

Member

rodneyrehm commented Sep 12, 2013

that's exactly what the jsbin demo is supposed to show ;)

let me know what you made of it. maybe we should add this method to URI.js' jQuery plugin?

@chovy

This comment has been minimized.

Show comment
Hide comment
@chovy

chovy Sep 12, 2013

this is how i'm doing it now:

https://gist.github.com/chovy/1e3f3df674e1557c230e

pretty easy to filter by textnodes first.

but my $message contains text which has <img src="data"> string, not a dom node...because its escaped...I think I need to unescape it first, then filter. but then I'm not sure how do add the non-text nodes back into the dom.

chovy commented Sep 12, 2013

this is how i'm doing it now:

https://gist.github.com/chovy/1e3f3df674e1557c230e

pretty easy to filter by textnodes first.

but my $message contains text which has <img src="data"> string, not a dom node...because its escaped...I think I need to unescape it first, then filter. but then I'm not sure how do add the non-text nodes back into the dom.

@rodneyrehm

This comment has been minimized.

Show comment
Hide comment
@rodneyrehm

rodneyrehm Sep 12, 2013

Member

well, you can establish context within the callback function like so:

var text = "hello {{hello}} ((hello)) {{hello}} world";
text = text.replace(/(hello)/g, function(url) { 
    var original = arguments[arguments.length -1];
    var start = arguments[arguments.length -2];
    var length = url.length;

    if (original.substring(start -2, start) !== '{{') {
        return url;
    }

    if (original.substring(start + length, start + length + 2) !== '}}') {
        return url;
    }

    return "within double curly braces";
});

so this could be used to see if your match is preceded by /<[^<]+=("')?/

Member

rodneyrehm commented Sep 12, 2013

well, you can establish context within the callback function like so:

var text = "hello {{hello}} ((hello)) {{hello}} world";
text = text.replace(/(hello)/g, function(url) { 
    var original = arguments[arguments.length -1];
    var start = arguments[arguments.length -2];
    var length = url.length;

    if (original.substring(start -2, start) !== '{{') {
        return url;
    }

    if (original.substring(start + length, start + length + 2) !== '}}') {
        return url;
    }

    return "within double curly braces";
});

so this could be used to see if your match is preceded by /<[^<]+=("')?/

@chovy

This comment has been minimized.

Show comment
Hide comment
@chovy

chovy Sep 13, 2013

is there some kind of limit to the length of the url passed to the callback function for URI.withinString?

I noticed that on larger files, they all seem to be truncated. around 65k characters.

chovy commented Sep 13, 2013

is there some kind of limit to the length of the url passed to the callback function for URI.withinString?

I noticed that on larger files, they all seem to be truncated. around 65k characters.

@rodneyrehm

This comment has been minimized.

Show comment
Hide comment
@rodneyrehm

rodneyrehm Sep 13, 2013

Member

URI.js does not impose any limits. if there is something fishy, it may be RegExp itself.

Member

rodneyrehm commented Sep 13, 2013

URI.js does not impose any limits. if there is something fishy, it may be RegExp itself.

rodneyrehm added a commit that referenced this issue Jan 23, 2014

fixing URI.withinString to not use URI.find_uri_expression anymore an…
…d allow filtering URLs by optional RegExp as well as ignore URLs in HTML - closing #117, #131
@rodneyrehm

This comment has been minimized.

Show comment
Hide comment
@rodneyrehm

rodneyrehm Jan 23, 2014

Member

The issue is resolved in v1.12.0 see the docs for an enhanced API.

Member

rodneyrehm commented Jan 23, 2014

The issue is resolved in v1.12.0 see the docs for an enhanced API.

@rodneyrehm rodneyrehm closed this Jan 23, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment