Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

#11801 - Restricted Font Size Input To Only Valid Entries #13123

Merged
merged 14 commits into from
Mar 3, 2017
Merged

#11801 - Restricted Font Size Input To Only Valid Entries #13123

merged 14 commits into from
Mar 3, 2017

Conversation

nyteksf
Copy link
Contributor

@nyteksf nyteksf commented Feb 24, 2017

Related to #11801.

Text was able to be inputted into Font Size within Themes Settings (e.g. "View > Themes...") even when it included non-ASCII characters or was in a generally unusable format.

Corrected this issue by ensuring all font sizes are to begin with a number, and are completed by either "px" or "em" only. 'Done' button remains disabled until said format is adhered to correctly--at which time the functionality of said button becomes restored allowing input to be saved.

@zaggino

Copy link
Contributor

@zaggino zaggino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a first quick look without testing, let me know when you'll look at the comments

src/config.json Outdated
@@ -20,7 +20,7 @@
"extension_url": "https://s3.amazonaws.com/extend.brackets/{0}/{0}-{1}.zip",
"linting.enabled_by_default": true,
"build_timestamp": "",
"healthDataServerURL": "https://healthdev.brackets.io/healthDataLog"
"healthDataServerURL": "https://health.brackets.io/healthDataLog"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to revert this change (it applies automatically after doing npm install, but we don't want it in PRs)

var $target = $(this);
var $input = $target.val();
var validInput = /^[\d\.]+(p|px|e|em){0,1}$/g;
var btn = document.getElementById("done");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

</div>
</div>
</form>
</div>
<div class="modal-footer">
<button class="dialog-button btn" data-button-id="cancel">{{Strings.CANCEL}}</button>
<button class="dialog-button btn primary" data-button-id="save">{{Strings.DONE}}</button>
<button class="dialog-button btn primary" id="done" data-button-id="save">{{Strings.DONE}}</button>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id done is not very good, you should be more specific when introducing id's, something like theme-settings-done-btn would be more appropriate

</div>
</div>

<div class="control-group">
<label class="control-label">{{Strings.FONT_FAMILY}}:</label>
<div class="controls">
<input type="text" data-target="fontFamily" value="{{settings.fontFamily}}">
<input type="text" id="font-family-input" data-target="fontFamily" value="{{settings.fontFamily}}">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't need to introduce id's here, you can use .on("input", "[data-target=fontFamily]", function () {
https://www.w3schools.com/jquery/sel_attribute_equal_value.asp

.on("input", "[data-target]:text", function () {
.on("input", "#font-size-input", function () {
var $target = $(this);
var $input = $target.val();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ prefix for variables indicates that it's a jquery (or a DOM) element
in this case $input is a string and should really be named input or even better targetValue

// Make sure that the font size is expressed in terms we can handle (px or em). If not, 'done' button disabled until input corrected.

if ($input.search(validInput) === -1) {
($input.length === 0) ? btn.disabled=false : btn.disabled=true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btn.disabled = $input.length !== 0 is better

if ($input.search(validInput) === -1) {
($input.length === 0) ? btn.disabled=false : btn.disabled=true;
return false;
}else{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}else{ after return doesn't make sense, just close the if with } and continue normally

@zaggino
Copy link
Contributor

zaggino commented Feb 24, 2017

also make sure command grunt eslint in your branch doesn't throw any errors

.eslintrc.json Outdated
@@ -57,6 +57,7 @@
"$": false,

"window": false,
"document": false,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for mixed signals, but please revert this.
document is a generic word and in Brackets usually refer to the Document object.
I think you can go with jquery here, can't you? $("#theme-settings-done-btn")

Copy link
Contributor Author

@nyteksf nyteksf Feb 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay--I've been busy with moving, although wanted to finish and reply. That said,, yes, I can use jQuery to avoid going here. Good point. And thanks for clarifying. This is my first pull request; let alone being my first open source patch. I wasn't sure if I should use jQuery as little as possible as noobish as that sounds. This turned out to be a simple fix. :)

@ficristo
Copy link
Collaborator

Out of curiosity, any reason to not use the pattern attribute?

@zaggino
Copy link
Contributor

zaggino commented Feb 26, 2017

Yeah, using pattern ( https://www.w3schools.com/tags/att_input_pattern.asp ) directly on the input would probably be better.

@mohanad1980
Copy link

hello can you contact me [email protected]

@nyteksf
Copy link
Contributor Author

nyteksf commented Feb 27, 2017

Thanks, guys, for your speedy replies. And the link. To reply now to @ficristo: that HTML5 pattern attribute was actually wholly new to me. I've learned and relearned a bunch of neat stuff here in just a few reviewer comments. And I have to agree with you both that "pattern" is much better in usage. Thanks again! Please feel free to let me know if anything else would be problematic.

@zaggino
Copy link
Contributor

zaggino commented Feb 27, 2017

fine with me, @ficristo ?

@ficristo
Copy link
Collaborator

It seems fine for me.
Ideally we could expose this regex:

validFont = /^[\d\.]+(px|em)$/;

and use it on the html.

@zaggino
Copy link
Contributor

zaggino commented Feb 27, 2017

@nyteksf to do what @ficristo wrote, you'd have to edit this file https://github.com/nyteksf/brackets/blob/9a6f12cf259276ebd163e70266030377df67eb2b/src/view/ViewCommandHandlers.js and make this

validFont = /^[\d\.]+(px|em)$/;
an exported variable.

Then here https://github.com/nyteksf/brackets/blob/9a6f12cf259276ebd163e70266030377df67eb2b/src/view/ThemeSettings.js#L82 you'd pass the ViewCommandHandlers.validFontRegex (or however you call it) to Mustache and render it here https://github.com/nyteksf/brackets/blob/9a6f12cf259276ebd163e70266030377df67eb2b/src/htmlContent/themes-settings.html#L33 like {{ fontPattern }} or something

Hope that's clear enough, @ficristo correct me if I'm wrong

@petetnt
Copy link
Collaborator

petetnt commented Feb 27, 2017

LGTM, but we should accept at least rem values and percentages too, no?

@zaggino
Copy link
Contributor

zaggino commented Feb 27, 2017

Current regexp which is used deeper (

validFont = /^[\d\.]+(px|em)$/;
) seems to only allow px|em ... do we want to change this?

@nyteksf
Copy link
Contributor Author

nyteksf commented Mar 3, 2017

Thank you, guys. I have had to fiddle around with the RegExp a little to make the prescribed changes work. It seems backslashes need to be escaped to be exported successfully with mustache--having tried the various remedies in the docs that seemed to apply--and this causes the RegExp constructor used for the non-exported segment to be passed something unintended in that case (e.g., \\ and d instead of \d). Likewise, to use RegExp literally, it seems would require .slice()ing off the foreslashes to be made compatible with the HTML5 Pattern Attribute for export thereunto.

That said, rather than attempting to escape any characters, I have switched the RegExp in question to "[0-9.]..." instead of "[\d.]". But everything else should be as asked for to my awareness. Please let me know what you think in any case. Thanks very much. :)

@zaggino @ficristo

Copy link
Contributor

@zaggino zaggino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a few small changes that are of stylistic nature and I believe we're good to merge this

@@ -30,7 +30,7 @@ <h1 class="dialog-title">{{Strings.THEMES_SETTINGS}}</h1>
<div class="control-group">
<label class="control-label">{{Strings.FONT_SIZE}}:</label>
<div class="controls">
<input type="text" data-target="fontSize" value="{{settings.fontSize}}">
<input type="text" pattern="{{ settings.validFontSizeRegExp}}" data-target="fontSize" value="{{settings.fontSize}}">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing space before }}

return result;
}

var validFontSizeRegExp = ViewCommandHandlers.validFontSizeRegExp;
Copy link
Contributor

@zaggino zaggino Mar 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constant definitions should always be on top of the file after imports, or you can use ViewCommandHandlers.validFontSizeRegExp directly

@@ -81,7 +82,7 @@ define(function (require, exports, module) {
var template = $("<div>").append($settings).html();
var $template = $(Mustache.render(template, {"settings": currentSettings, "themes": themes, "Strings": Strings}));

// Select the correct theme.
// Select the correct theme.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

invalid whitespace change


// Make sure that the font size is expressed in terms we can handle (px or em). If not, simply bail.
if (fsStyle.search(validFont) === -1) {
var validFontSizeRegExpStr = "^[0-9.]+(px|em)$"; // Need RegExp string to be exported for compatibility with HTML5 pattern attribute.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constant definitions should always be on top (after import declarations) and not between functions, also needs a comment, something like:

/*
 * Font sizes should be validated by this regexp
 */

@nyteksf
Copy link
Contributor Author

nyteksf commented Mar 3, 2017

Whoops! Well, okay: there you go. Thanks again for a speedy reply!

@zaggino

@zaggino
Copy link
Contributor

zaggino commented Mar 3, 2017

Testing this, it allows input like:
12.00.00px
can we please change regexp to be maybe:
"^[0-9]+(\\.[0-9]+)?(px|em)$"

@zaggino
Copy link
Contributor

zaggino commented Mar 3, 2017

Done with testing, just change this one and I'm merging this (promise 😄 )

@nyteksf
Copy link
Contributor Author

nyteksf commented Mar 3, 2017

Okay. I'm going out on a limb here: so of course please do correct me if my suggestion happens to be wrong. I added your change to the original deeper RegExp. Then in doing my own testing, I had found that by comparison to the original it worked almost perfectly. That RegExp in question, however, didn't handle cases like ".5em", without the user first adding a zero to make it like "0.5em". That isn't a problem, I'm sure. My change seems like a small change indeed with that being said--but I figured I could change it still for sake of ease of use. And now it should handle cases like "0.5em" as well as ".5em". Please let me know if it works as it seems to on my end. And thanks again for all your help. :)))

@zaggino

@nyteksf
Copy link
Contributor Author

nyteksf commented Mar 3, 2017

Nevermind. I found a flaw with my last change. I am re-changing the RegExp to address cases like "14.0.0px", which still weren't prevented. My bad.

@nyteksf
Copy link
Contributor Author

nyteksf commented Mar 3, 2017

Okay, @zaggino. My fingers are crossed this time. I'm sending my final changes over your way if you'll please take another look now. :)

@zaggino
Copy link
Contributor

zaggino commented Mar 3, 2017

Oh, github new features are superb, did a small change to your regexp through the web - when inside a string you need to escape \. like this \\. - otherwise an expression 12a5px would be valid, I've fixed that

@zaggino
Copy link
Contributor

zaggino commented Mar 3, 2017

Looks good to me now. I think all comments above have been considered so I'm merging this.
Thanks a lot for the PR a for sticking with it to the end, hope we'll meet again on some other one.
Cheers!

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

Successfully merging this pull request may close these issues.

5 participants