This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
- Fix for Issue #391 #1509
Merged
Merged
- Fix for Issue #391 #1509
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,7 +73,9 @@ define(function (require, exports, module) { | |
var selectorStartChar = -1, selectorStartLine = -1; | ||
var selectorGroupStartLine = -1, selectorGroupStartChar = -1; | ||
var declListStartLine = -1, declListStartChar = -1; | ||
|
||
var escapePattern = /\\[^\\]+/g; | ||
var validationPattern = /\\([a-fA-F0-9]{6}|[a-fA-F0-9]{4}(\s|\\|$)|[a-fA-F0-9]{2}(\s|\\|$)|.)/; | ||
|
||
// implement _firstToken()/_nextToken() methods to | ||
// provide a single stream of tokens | ||
|
||
|
@@ -183,7 +185,19 @@ define(function (require, exports, module) { | |
break; | ||
} | ||
} | ||
|
||
|
||
// Is faster regexp.test() than a replace with no hits? | ||
//if(/\\/.test(currentSelector)) | ||
//{ | ||
// Double replace in case of pattern overlapping (regex improvement?) | ||
currentSelector = currentSelector.replace(escapePattern,function(escapedToken){ | ||
return escapedToken.replace(validationPattern,function(unicodeChar){ | ||
unicodeChar = unicodeChar.substr(1); | ||
if (unicodeChar.length==1) return unicodeChar; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Always use === operator in Brackets. |
||
else return parseInt(unicodeChar,16)<0x10FFFF?String.fromCharCode(parseInt(unicodeChar,16)):"�"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try to keep code within 80 columns. |
||
}); | ||
}); | ||
//} | ||
currentSelector = currentSelector.trim(); | ||
if (currentSelector !== "") { | ||
selectors.push({selector: currentSelector, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* Test ".si\mple" (simple) */ | ||
.si\mple {} | ||
|
||
/* Test ".not\\so\|simple\?" (not\so|simple?) */ | ||
.not\\so\|simple\? {} | ||
|
||
/* Test ".\74 wodigi\74 s" (.twodigits)*/ | ||
.\74 wodigi\74 s {} | ||
|
||
/* Test ".fourdigi\0074 s" (.fourdigits)*/ | ||
.fourdigi\0074 s {} | ||
|
||
/* Test ".sixdigi\000074s" (.sixdigits) */ | ||
.sixdigi\000074s {} | ||
|
||
/* Test ".two-digit-endspac\65 " (.two-digit-endspace) */ | ||
.two-digit-endspac\65 {} | ||
|
||
/* Test ".four-digit-endspac\0065 " (.four-digit-endspace) */ | ||
.four-digit-endspac\0065 {} | ||
|
||
/* Test ".six-digit-endspace\000065" (.six-digit-endspace) */ | ||
.six-digit-endspac\000065 {} | ||
|
||
/* Test ".mi\78 in\002D it\2D a\00006C\006C" (.mixin-it-all) */ | ||
.mi\78 in\002D it\2D a\00006C\006C{} | ||
|
||
/* Test ".\74 wo-wi\74out-space" (.two-wi74out-space) */ | ||
.\74 wo-wi\74out-space {} | ||
|
||
/* Test ".four-n\0085-space" (.four-n0085-space) */ | ||
.four-n\0085-space {} | ||
|
||
/* Test "" Out of range unicode char, uses replace instead */ | ||
.\110000\0075\74\cc6699frange {} | ||
|
||
.escape\|random\|char { | ||
color: red; | ||
} | ||
|
||
.mixin\!tUp { | ||
font-weight: bold; | ||
} | ||
|
||
.\34 04 { | ||
background: red; | ||
} | ||
|
||
.\34 04 strong { | ||
color: #ff00ff; | ||
font-weight: bold; | ||
} | ||
|
||
.trailingTest\+ { | ||
color: red; | ||
} | ||
|
||
/* This hideous test of hideousness checks for the selector "blockquote" with various permutations of hex escapes */ | ||
\62\6c\6f \63 \6B \0071 \000075o\74 e { | ||
color: silver; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a basic performance testing timer builtin to Brackets called PerfUtils that you can use to time both cases. Use it something like this:
Then use Debug > Show Performance Data to see min/max/avg times measured.
I measured this both ways and saw a max timing of 1ms, so this code does not seem have a significant impact. Here's a way you can try to get sub ms timing info (but I haven't tried it, so not sure if it works):
http://updates.html5rocks.com/2012/08/When-milliseconds-are-not-enough-performance-now
I would think that regexp.test() would be faster than a replace with no hits, so please uncomment these lines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did a test run with jsperf and it seems that regexp.test() is around 30% faster.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent usage of jsperf! So, you're randomly generating selectors from a random length of alpha-numeric characters, right? I don't think that's a representative set of data for your code since there are no escaped chars. Maybe you could build the array of selectors with a hard-coded list of mostly normal selectors, then mix in the ones with escaped chars from your unit tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The goal of the previous test was just to find out if a simple regex test was faster than a replace when the pattern in the replace yields no hits. It was focused on the subset of data where no unicode chars are found in the selectors. That's why I didn't add any escaped chars.
In any case, and to be more thorough, I've created a second test. In this one, some characters are being randomly substituted with their escaped equivalents. This ensures that the replace code is executed. As expected, the version with the
if(regex.test())
is slightly slower as it's executing the same piece of code plus the additionalif
operator.To complete a study on this, I think we'd need some usage statistics of escaped characters in css so that we could model how often this happens (I guess not that often anyway :D). With that data, we could compare the overall impact of this fix and the difference in performance between using or not the first if clause. In any case, I assume using escaped characters in css is pretty rare so I'm confident in using the test() solution based on the results of the first test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed.