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

Attributes with single quotes not working in form #49

Open
siolfyr opened this issue Mar 14, 2019 · 9 comments
Open

Attributes with single quotes not working in form #49

siolfyr opened this issue Mar 14, 2019 · 9 comments

Comments

@siolfyr
Copy link

siolfyr commented Mar 14, 2019

Any maintainers still checking this repo?

It seems like currently any attributes with single quotes turn into the attribute name, i.e. class='page-wrapper' will turn into class="class".

Single quotes are valid in html for attributes, and, more importantly for me, in our project we have data-json='{"json":"values"}' which will become data-json="data-json".

@Gregwar
Copy link
Owner

Gregwar commented Mar 14, 2019

Hello,
I think escaping the quotes (") using HTML Entities is by far a better approach than using simple quotes, here is a snippet demostrating how it works using htmlentities() in php:

<script type="text/javascript">
function display() {
    element = document.getElementById('something');
    dataJson = JSON.parse(element.attributes['data-json'].value);
    alert(dataJson['json']);
}
</script>
<body onload="display()">
<div id="something" data-json="<?php echo htmlentities(json_encode(["json" => "values"])); ?>">
<!-- Will result in: <div id="something" data-json="{&quot;json&quot;:&quot;values&quot;}"> !-->
</div>
</body>

@Gregwar
Copy link
Owner

Gregwar commented Mar 14, 2019

Else, how would you process JSONs containing both " and ' ?

{"message": "Sorry I'm late"}

@Gregwar
Copy link
Owner

Gregwar commented Mar 14, 2019

(And note that this is natural since there is no need to unescape)

@siolfyr
Copy link
Author

siolfyr commented Mar 15, 2019

Else, how would you process JSONs containing both " and ' ?

{"message": "Sorry I'm late"}

I would do it the same way that browsers do it, by parsing the HTML based on which type of quote is used.

Sorry, but do you really think it's a far better solution to work around it rather than to have Formidable parse valid HTML? Otherwise it's nothing more than a subset of HTML. From the HTML specification:

The HTML specification says: Attributes are placed inside the start tag, and consist of a name and a value, separated by an = character. The attribute value can remain unquoted if it doesn't contain spaces or any of " ' ` = < or > . Otherwise, it has to be quoted using either single or double quotes

The json is not generated by PHP. It would be less work to change Formidable than to change all of the JSON. Would you accept a pull request for it?

@Gregwar
Copy link
Owner

Gregwar commented Mar 15, 2019

Yes of course, simple quotes are OK with HTML specifications anyway, so I agree with supporting it
However I'm just pointing out that it might still be an issue if you have json containing both " and '

@Gregwar
Copy link
Owner

Gregwar commented Mar 21, 2019

I just committed a fix in master that should handle it

@siolfyr
Copy link
Author

siolfyr commented Mar 25, 2019

Awesome! But, it looks like if the value contains double quotes, it looks like it currently causes the string to terminate early.

Could you check out my pull-request? I think something like that would work in all attribute situations other than the special cases (type, name, etc).

@Gregwar
Copy link
Owner

Gregwar commented Mar 26, 2019

There was indeed an issue since the attribute data was not escaped
Does 3025618 solves your problem ? (Added unit test)

@Gregwar
Copy link
Owner

Gregwar commented May 24, 2019

Hello
Is your issue solved?

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