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

Feature: Allow for additional attributes #22

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

Feature: Allow for additional attributes #22

wants to merge 8 commits into from

Conversation

terence1990
Copy link

I needed a data attribute on the link in my project so thought this would be helpful for others

@ljosa
Copy link
Owner

ljosa commented May 13, 2017

What quoting is necessary here? I know we don't have any quoting for target, but that one has a much more limited use.

Can you please add unit tests? See test_urlize.js.

The last line should end with a newline.

@terence1990
Copy link
Author

@ljosa i'll do this on Friday, what do you mean by quoting? - do you mean code commenting?

@ljosa
Copy link
Owner

ljosa commented Aug 17, 2017

Perhaps I should have written "escaping" instead of "quoting." The code builds HTML. As you know, many characters have special meaning in HTML. If these characters occur in the value of your data attribute, then the result can be invalid HTML (or even a security problem, such as an XSS vulnerability, depending on where the value comes from). I believe that &, <, >, `, and " need to be escaped (but please give that some thought and make sure it's correct).

Perhaps it would be a good idea to validate the names of the data attributes also, since controls, U+0020 SPACE, U+0022 ("), U+0027 ('), U+003E (>), U+002F (/), U+003D (=), and noncharacters are not allowed in HTML attribute names (source).

@terence1990
Copy link
Author

Hey @ljosa - have escaped values and removed illegal characters from keys and added tests - all asserting true

@ljosa
Copy link
Owner

ljosa commented Aug 19, 2017

Sorry, that's not right kind of escaping. If you have an HTML document with

<a id="foo" href="http://www.example.com/" position="left" open-in-app="%22%2Ctrue">http://www.example.com/</a>

then
document.getElementById('foo').getAttribute('open-in-app')
returns "%22%2Ctrue",
which is not what you want. You'll have to replace the illegal characters with HTML entities. For instance:

<a id="foo" href="http://www.example.com/" position="left" open-in-app="&quot;,true">http://www.example.com/</a>

Then, document.getElementById('foo').getAttribute('open-in-app') returns "\",true" as it should.

Update the HTMLEntities Escape through extending String and Regex
prototypes

Updated tests to expect HTMLEntities

Neatened up indenting via JSFiddle
@terence1990
Copy link
Author

terence1990 commented Aug 19, 2017

@ljosa just made another commit fixes this to ensure HTMLEntities are returned - tests updated and asserting true

@ljosa
Copy link
Owner

ljosa commented Aug 20, 2017

A few problems still:

  • In the value, ampersand (&) needs to be escaped as &amp;.
  • In the value, backtick (`) needs to be escaped as &#96;.
  • The correct escape for single quote (') is &#39;, not &#8216;.

Question: why are you escaping space, slash, and equal sign in the values? I don't think that is necessary—do you have reason to believe otherwise?

I believe the unit test for the values should be:

	it('add additional attributes with illegal values', function () {
	    equal(urlize('http://www.example.com/', {attrs: {position: 'left', 'open-in-app': '&<>`"\n\'/true'}}),
	          '<a href="http://www.example.com/" position="left" open-in-app="&amp;&lt;&gt;&#96;&quot;&#13;&#39;/true">http://www.example.com/</a>');
	});

Do you agree?

Finally, can you please document the new functionality in the README.md?

Thanks!

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

Successfully merging this pull request may close these issues.

2 participants