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

feat(common): add utility functions for xmlns attributes #173

Merged
merged 3 commits into from
May 17, 2020
Merged

Conversation

tal-sapan
Copy link
Contributor

No description provided.

@tal-sapan tal-sapan requested a review from bd82 May 13, 2020 11:12
@CLAassistant
Copy link

CLAassistant commented May 13, 2020

CLA assistant check
All committers have signed the CLA.

packages/common/api.d.ts Outdated Show resolved Hide resolved
if (matchArr === null) {
return false;
}
if (includeEmptyPrefix === true) {
Copy link
Member

Choose a reason for hiding this comment

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

if (includeEmptyPrefix === true) { correctness depends on running after:
if (matchArr === null) { return false; }

should an else if be used to explicitly mark this relationship?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I prefer not to, would it be enough to add a comment?

return true;
}
/* istanbul ignore next - defensive coding */
const groups = matchArr.groups || {};
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need the || {} in JavaScript.

Because we know that in Node 10.0+ named capture groups are supported and that
the match succeed if we reached this line.

In TypeScript we used || {} to calm to compiler as the .groups property is defined as optional. so its not really defensive coding, we can prove it can't reach this branch so we don't actually need the branch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, I'll remove it

/* istanbul ignore next - defensive coding */
const groups = matchArr.groups || {};
// In the case where key === "xmlns:", prefix will be empty and prefixWithColon will not be empty
return !(groups.prefixWithColon && !groups.prefix);
Copy link
Member

@bd82 bd82 May 13, 2020

Choose a reason for hiding this comment

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

Would it be simpler to have more distinct groups in the regExp? (no overlap)

  1. xmlns
  2. colon
  3. prefix

and then ask if 1 &2 exists (empty prefix case) or 1 & 2 & 3 exists (proper xmlns)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed the condition a bit to make it clearer, let me know if you think I should still change this

* @param key - the attribute key
* @param includeEmptyPrefix - should true be returned when there is no prefix (key === "xmlns:")
*/
export function isXMLNamespaceKey(
Copy link
Member

Choose a reason for hiding this comment

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

should the term prefix be part of this name somehow?
I think that is the term used in the XML specs, perhaps instead of the key which is related to the attribute but we are dealing with a string here. (more like a tokenizer)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The function is only meaningful when related to xml attribute keys, a regular string isn't a namespace key, I wanted to make it clear

// xmlns attributes explicitly can't contain ":" after the "xmlns:" part.
const namespaceRegex = /^xmlns(?<prefixWithColon>:(?<prefix>[^:]*))?$/;

function isXMLNamespaceKey(key, includeEmptyPrefix) {
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The link doesn't work

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added

Copy link
Member

@bd82 bd82 left a comment

Choose a reason for hiding this comment

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

See inline comments.

@bd82 bd82 merged commit 20d6c09 into master May 17, 2020
@bd82 bd82 deleted the xmlns-utils branch May 17, 2020 11:35
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.

3 participants