Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Add feature detection of unicode properties support #292

Merged
merged 6 commits into from
Jul 23, 2019

Conversation

msamsel
Copy link
Contributor

@msamsel msamsel commented Jul 3, 2019

Suggested merge commit message (convention)

Feature: Add feature detection of unicode properties support.


Additional information

@coveralls
Copy link

Coverage Status

Coverage increased (+0.0004%) to 99.826% when pulling ef16b13 on t/ckeditor5-word-count/5 into bacc764 on master.

* @protected
* @namespace
*/
export default {
Copy link
Contributor

Choose a reason for hiding this comment

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

I was wondering whether it should be palced in https://github.com/ckeditor/ckeditor5-utils/blob/cf06347de71b235b564a13016a260af7c191a34d/src/unicode.js - but at the end the linked file is related to unicode handling, while this check is focused on RegExp in context of a given unicode goup handling.

However I'd give it better namespacing, I'm thinking more like: utils.featuredetection.regexp.isXyzSupported().

Copy link
Contributor

Choose a reason for hiding this comment

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

@mlewand to clarify:

import featureDetection from '@ckeditor5-utils/src/utils/featuredetection';

console.log(
    'Regex group is supported:',
    featureDetection.regex.isUnicodePropertySupported()
);

ps.: Shouldn't it be isUnicodePropertyEscapesSupported()? 😆

Copy link
Contributor

Choose a reason for hiding this comment

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

@msamsel please create also a PR in mention repo to use this util instaed own feature detection.

Copy link
Contributor

Choose a reason for hiding this comment

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

@jodator I was thinking more about:

import regExpFeatureDetection from '@ckeditor5-utils/src/utils/featuredetection/regexp';

console.log(
    'RegExp group is supported:',
    regExpFeatureDetection.isUnicodePropertySupported()
);

This would allow us to nicely organize stuff in future.

And it's important for us to use RegExp with p consistently, as this is how the type is named in JS.

Copy link
Contributor

Choose a reason for hiding this comment

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

RegExp with p consistently, a

typo, sorry :P

Copy link
Contributor

@jodator jodator left a comment

Choose a reason for hiding this comment

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

@mlewand one thing to clarify.
@msamsel use this util in mention also.

* @protected
* @namespace
*/
export default {
Copy link
Contributor

Choose a reason for hiding this comment

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

@mlewand to clarify:

import featureDetection from '@ckeditor5-utils/src/utils/featuredetection';

console.log(
    'Regex group is supported:',
    featureDetection.regex.isUnicodePropertySupported()
);

ps.: Shouldn't it be isUnicodePropertyEscapesSupported()? 😆

* @protected
* @namespace
*/
export default {
Copy link
Contributor

Choose a reason for hiding this comment

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

@msamsel please create also a PR in mention repo to use this util instaed own feature detection.

export default {
/**
* Indicates whether the current browser supports ES2018 Unicode properties like `\p{P}` or `\p{L}`.
*
Copy link
Contributor

Choose a reason for hiding this comment

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

Since I'm always searching for this. Please add a link to:

https://www.unicode.org/reports/tr44/#GC_Values_Table

so we get a link to those categories :)

@msamsel
Copy link
Contributor Author

msamsel commented Jul 17, 2019

ps.: Shouldn't it be isUnicodePropertyEscapesSupported()? 😆

@jodator usually in the documentation you can find the name "Unicode property" or just "property". The word "escape" seems to be strictly related to the existence of \ inside a specific regular expression. I think, in this case, the word "escape" should be skipped, as we don't talk about usage inside regular expression syntax, but rather accessibility of the browser's feature.

@mlewand
Copy link
Contributor

mlewand commented Jul 17, 2019

Also let's try to keep the names simple if possible 🙂

@jodator
Copy link
Contributor

jodator commented Jul 17, 2019

OMG next time I'll use <joke> ... </joke> 😄

@msamsel msamsel requested review from jodator and mlewand July 17, 2019 12:46
@msamsel
Copy link
Contributor Author

msamsel commented Jul 17, 2019

OMG next time I'll use <joke> ... </joke> 😄

Those could be really useful, as it is really hard to notice the sarcasm in the written text, where you expect the feedback ;)

// See https://github.com/ckeditor/ckeditor5-mention/issues/44#issuecomment-487002174.

try {
isSupported = 'ć'.search( new RegExp( '[\\p{L}]', 'u' ) ) === 0;
Copy link
Member

Choose a reason for hiding this comment

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

Why new RegExp and not a literal?

Copy link
Contributor

Choose a reason for hiding this comment

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

@Reinmar I'm investigating this - ESLint is complaining about this.

Copy link
Contributor Author

@msamsel msamsel Jul 18, 2019

Choose a reason for hiding this comment

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

Yeah, definitely there should be comments as this not clear in such cases :(

Tl;dr; ESLint doesn't recognize it.

Simillar discussion is here:
ckeditor/ckeditor5-word-count#10 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean that we have to bumpt the ESLint rules used in CKEditor to be able to use /\p{L}/u AFAICS.

Copy link
Contributor

Choose a reason for hiding this comment

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

OH we've already published the changes and after updating dependencies it works :) So our ESLint config works with this now 🎉

Copy link
Contributor Author

@msamsel msamsel Jul 18, 2019

Choose a reason for hiding this comment

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

Ok so I tried it, and it's not working with our environment :(
Even when ESLint does not return an error, there is run acorn somewhere under the hood which throws a bunch of errors with those regexp literals:
Screenshot 2019-07-18 at 11 21 22

Copy link
Contributor

Choose a reason for hiding this comment

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

There's an upcoming fix for that. Right now we cannot use them :(

Copy link
Member

Choose a reason for hiding this comment

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

OK, fair enough. Please leave a comment in the code with an explanation.

*/

/**
* @module utils/featuredetection/regexp
Copy link
Member

Choose a reason for hiding this comment

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

Does it have to be entire namespace for regexp? Do we expect to have many of those?

Copy link
Member

Choose a reason for hiding this comment

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

I think that moving this to env.js will be cleaner.

Copy link
Contributor

Choose a reason for hiding this comment

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

Something like this?

const env = {
	isMac: isMac( userAgent ),
	// ...
	features: {
		regexp: {
			isUnicodePropertySupported: isUnicodePropertySupported()
		}
	}
	// or just
	features: {
		isRegExpUnicodePropertySupported: isRegExpUnicodePropertySupported()
	}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Does it have to be entire namespace for regexp? Do we expect to have many of those?

@Reinmar There are several features that are about to come (not widely supported) into JS RegExp:

  • s (a.k.a. dotAll) flag
  • named groups
  • Unicode property escapes (that's in this PR)

Also ES2018 finally added lookahead/lookbehind, luckily both seems to be implemented by all major browser vendors. 🎉

Note that future ES revisions might also expose more RegExp features to further reduce the gap.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are several features that are about to come (not widely supported) into JS RegExp: (..)

I wonder how many of them are we going to use and will have to detect.

For me having feature detection inside env.js seems to be a good solution. The first example is more appealing to me.

Copy link
Member

Choose a reason for hiding this comment

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

@mlewand What would be the problem if we had like 5 regexp-oriented properties in env?

We can create new namespaces for every new distinctive thing that may become "plural" with time. But many of them don't become plural and majority doesn't exceed 3 options. If you don't see technical problems for keeping that in env, I'd keep that in env. They will be easier to find there.

Copy link
Member

Choose a reason for hiding this comment

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

@jodator

       // or just
	features: {
		isRegExpUnicodePropertySupported: isRegExpUnicodePropertySupported()
	}

sounds best to me – it's the most flexible option.

Copy link
Member

@Reinmar Reinmar left a comment

Choose a reason for hiding this comment

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

See comments.

@msamsel msamsel requested a review from Reinmar July 18, 2019 10:32
@jodator
Copy link
Contributor

jodator commented Jul 19, 2019

@msamsel The one thing remains - move the feature detection to the env as

features: {
		isRegExpUnicodePropertySupported: isRegExpUnicodePropertySupported()
	}

I'd keep that in env. They will be easier to find there.

@msamsel
Copy link
Contributor Author

msamsel commented Jul 22, 2019

@jodator I've moved feature detection to env.

@ma2ciek
Copy link
Contributor

ma2ciek commented Jul 22, 2019

features: {
isRegExpUnicodePropertySupported: isRegExpUnicodePropertySupported()
}

This looks a little bit inconsistent for me, sorry for bumping late into the established discussion.

Shouldn't it be like the following? AFAIK we don't prefix boolean props with is, has, etc.

features: {
	regExpUnicodePropertySupported: isRegExpUnicodePropertySupported()
}

@jodator
Copy link
Contributor

jodator commented Jul 23, 2019

Shouldn't it be like the following? AFAIK we don't prefix boolean props with is, has, etc.

@ma2ciek dunno, it looks like we do :P

isMac: isMac( userAgent ),

isEdge: isEdge( userAgent ),

isGecko: isGecko( userAgent ),

from other files:
https://github.com/ckeditor/ckeditor5-engine/blob/9a40550a7425670264974edc9252f3dcf888ce2f/src/model/element.js#L87

https://github.com/ckeditor/ckeditor5-engine/blob/a58265d1c76c1e2345772861b05770280af13297/src/model/documentselection.js#L680

So we actually prefix boolean values with is/has/are :)

@ma2ciek
Copy link
Contributor

ma2ciek commented Jul 23, 2019

Shouldn't it be like the following? AFAIK we don't prefix boolean props with is, has, etc.

@ma2ciek dunno, it looks like we do :P

Wow. I was pretty sure we do the opposite. So let's be consistent.

PS OTOH I don't like this convention because you end up not knowing if the property has a call signature or not.

@jodator
Copy link
Contributor

jodator commented Jul 23, 2019

I'm merging this one as other PRs might be closed later on and this will be already used by Mention feature.

@jodator jodator merged commit 21c0f6b into master Jul 23, 2019
@jodator jodator deleted the t/ckeditor5-word-count/5 branch July 23, 2019 09:05
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.

6 participants