Skip to content
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
ralyodio opened this issue Sep 12, 2013 · 8 comments
Closed

finds a url inside a base64 encoded image string #117

ralyodio opened this issue Sep 12, 2013 · 8 comments

Comments

@ralyodio
Copy link

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
Copy link
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.

@ralyodio
Copy link
Author

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
Copy link
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?

@ralyodio
Copy link
Author

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
Copy link
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 /<[^<]+=("')?/

@ralyodio
Copy link
Author

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
Copy link
Member

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
…d allow filtering URLs by optional RegExp as well as ignore URLs in HTML - closing #117, #131
@rodneyrehm
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants