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

Parse Polymer 2 template insertion style #6909

Merged
merged 3 commits into from
Nov 19, 2019
Merged

Conversation

denis-anisimov
Copy link
Contributor

@denis-anisimov denis-anisimov commented Nov 11, 2019

Fixes #6116


This change is Reviewable

@ujoni ujoni added the +0.0.1 label Nov 12, 2019
Copy link
Contributor

@ujoni ujoni left a comment

Choose a reason for hiding this comment

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

Reviewed 4 of 6 files at r1.
Reviewable status: 6 unresolved discussions, 0 of 1 LGTMs obtained (waiting on @denis-anisimov)


flow-server/src/main/java/com/vaadin/flow/component/polymertemplate/BundleParser.java, line 88 at r1 (raw file):

([\\`|\\'|\\\"])

Should the contents be wrapped in character class ([...])? It seems like there is an or pattern, but that should not work inside of a character class. Same seems to apply to TEMPLATE_PATTERN.


flow-server/src/main/java/com/vaadin/flow/component/polymertemplate/BundleParser.java, line 201 at r1 (raw file):

                    continue;
                }
                return templates.get(0);

If the last bits with the continues were replaced with something like

            Optional<Element> template = domModule.map(module -> {
                JsoupUtils.removeCommentsRecursively(domModule.get());
                Elements templates = domModule.get()
                        .getElementsByTag(TEMPLATE_TAG_NAME);
                return !templates.isEmpty() ? templates.get(0) : null;
            });

            if (template.isPresent()) {
                return template.get();
            }

this could be a normal else if branch without the while and double continues


flow-server/src/main/java/com/vaadin/flow/component/polymertemplate/JsoupUtils.java, line 73 at r1 (raw file):

    }

}

new line missing


flow-server/src/test/resources/META-INF/VAADIN/config/no-html-template.json, line 13 at r1 (raw file):

	],
	"hash": "foo"
}

Missing new line.

Some of the indents are formatted with tabs while others are spaces.

Copy link
Contributor Author

@denis-anisimov denis-anisimov left a comment

Choose a reason for hiding this comment

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

Reviewable status: 6 unresolved discussions, 0 of 1 LGTMs obtained (waiting on @denis-anisimov and @ujoni)


flow-server/src/main/java/com/vaadin/flow/component/polymertemplate/BundleParser.java, line 88 at r1 (raw file):

Previously, ujoni (Joni) wrote…
([\\`|\\'|\\\"])

Should the contents be wrapped in character class ([...])? It seems like there is an or pattern, but that should not work inside of a character class. Same seems to apply to TEMPLATE_PATTERN.

I'm not sure what you mean......
([\\|\'|\"])` has

  • parenthesis ()
  • [] which makes or between back-tick, ' , ".

So for me everything looks fine.
What do you mean by character class ?

Copy link
Contributor

@ujoni ujoni left a comment

Choose a reason for hiding this comment

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

Reviewable status: 6 unresolved discussions, 0 of 1 LGTMs obtained (waiting on @denis-anisimov)


flow-server/src/main/java/com/vaadin/flow/component/polymertemplate/BundleParser.java, line 88 at r1 (raw file):

Previously, denis-anisimov (Denis) wrote…

I'm not sure what you mean......
([\\|\'|\"])` has

  • parenthesis ()
  • [] which makes or between back-tick, ' , ".

So for me everything looks fine.
What do you mean by character class ?

Shouldn't it just read (\\`|\\'|\\\") or [`'\\"] instead of ([\\`|\\'|\\\"]). The [ and ] marks a character group (and could be thought to have an implicit | between the characters) . I am not actually sure why the quotes (other than ") need to be escaped at all. They aren't special characters, right?

Copy link
Contributor Author

@denis-anisimov denis-anisimov left a comment

Choose a reason for hiding this comment

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

Reviewable status: 6 unresolved discussions, 0 of 1 LGTMs obtained (waiting on @denis-anisimov and @ujoni)


flow-server/src/main/java/com/vaadin/flow/component/polymertemplate/BundleParser.java, line 88 at r1 (raw file):

Previously, ujoni (Joni) wrote…

Shouldn't it just read (\\`|\\'|\\\") or [`'\\"] instead of ([\\`|\\'|\\\"]). The [ and ] marks a character group (and could be thought to have an implicit | between the characters) . I am not actually sure why the quotes (other than ") need to be escaped at all. They aren't special characters, right?

Hmm.....

I don't think so.

( is just a marker for group to reuse it later and reference as \\n (it's referenced later on in regexp as \\1).

[] is used for or.

So ([\\|\'|\"])looks correct for me : it a group which consist of one character ( or , or). back tick quote is used for multiline strings ( I suppose). If you have a short string which is one line then ( I think ) you may use"or'` .
But I don't actually know exactly. This is just a modification of the existing pattern/logic.
At least it works....

Copy link
Contributor

@ujoni ujoni left a comment

Choose a reason for hiding this comment

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

Reviewable status: 6 unresolved discussions, 0 of 1 LGTMs obtained (waiting on @denis-anisimov)


flow-server/src/main/java/com/vaadin/flow/component/polymertemplate/BundleParser.java, line 88 at r1 (raw file):

Previously, denis-anisimov (Denis) wrote…

Hmm.....

I don't think so.

( is just a marker for group to reuse it later and reference as \\n (it's referenced later on in regexp as \\1).

[] is used for or.

So ([\\|\'|\"])looks correct for me : it a group which consist of one character ( or , or). back tick quote is used for multiline strings ( I suppose). If you have a short string which is one line then ( I think ) you may use"or'` .
But I don't actually know exactly. This is just a modification of the existing pattern/logic.
At least it works....

Ah right, we use the () result. So no removing that. But the [\\`|\'|\"] will match exactly one `, ', ", or | since the | does not have special meaning inside of [ and ].

So I guess someone could break stuff with

return magicTemplateVariable || html`custom template here`;

Copy link
Contributor Author

@denis-anisimov denis-anisimov left a comment

Choose a reason for hiding this comment

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

Reviewable status: 6 unresolved discussions, 0 of 1 LGTMs obtained (waiting on @denis-anisimov and @ujoni)


flow-server/src/main/java/com/vaadin/flow/component/polymertemplate/BundleParser.java, line 88 at r1 (raw file):

Previously, ujoni (Joni) wrote…

Ah right, we use the () result. So no removing that. But the [\\`|\'|\"] will match exactly one `, ', ", or | since the | does not have special meaning inside of [ and ].

So I guess someone could break stuff with

return magicTemplateVariable || html`custom template here`;

Aaaaah.
Alright....
May be you are right.
I have to check it.

Copy link
Contributor

@ujoni ujoni left a comment

Choose a reason for hiding this comment

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

Reviewable status: 6 unresolved discussions, 0 of 1 LGTMs obtained (waiting on @denis-anisimov)


flow-server/src/main/java/com/vaadin/flow/component/polymertemplate/BundleParser.java, line 88 at r1 (raw file):

Previously, denis-anisimov (Denis) wrote…

Aaaaah.
Alright....
May be you are right.
I have to check it.

Though, the above would not actually work in any case since the regex only accepts white space between return and html.
Well, anyways, I think the | is not needed inside the square brackets.

Copy link
Contributor Author

@denis-anisimov denis-anisimov left a comment

Choose a reason for hiding this comment

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

Dismissed @vaadin-bot from 2 discussions.
Reviewable status: 3 unresolved discussions, 0 of 1 LGTMs obtained (waiting on @ujoni)


flow-server/src/main/java/com/vaadin/flow/component/polymertemplate/BundleParser.java, line 88 at r1 (raw file):

Previously, ujoni (Joni) wrote…

Though, the above would not actually work in any case since the regex only accepts white space between return and html.
Well, anyways, I think the | is not needed inside the square brackets.

Done.


flow-server/src/main/java/com/vaadin/flow/component/polymertemplate/BundleParser.java, line 201 at r1 (raw file):

Previously, ujoni (Joni) wrote…

If the last bits with the continues were replaced with something like

            Optional<Element> template = domModule.map(module -> {
                JsoupUtils.removeCommentsRecursively(domModule.get());
                Elements templates = domModule.get()
                        .getElementsByTag(TEMPLATE_TAG_NAME);
                return !templates.isEmpty() ? templates.get(0) : null;
            });

            if (template.isPresent()) {
                return template.get();
            }

this could be a normal else if branch without the while and double continues

I've extracted the code into a separate method.
May be it can be simplified a bit but I don't see how I can get rid of while without making another assumption.

There is a test which contain two dom-modules assignments.
One dom-module is empty ( kind of invalid , doesn't contain any template) and another
is a valid one.
It's not possible to choose the correct dom-module without while .


flow-server/src/main/java/com/vaadin/flow/component/polymertemplate/JsoupUtils.java, line 73 at r1 (raw file):

Previously, ujoni (Joni) wrote…

new line missing

Done.


flow-server/src/test/resources/META-INF/VAADIN/config/no-html-template.json, line 13 at r1 (raw file):

Previously, ujoni (Joni) wrote…

Missing new line.

Some of the indents are formatted with tabs while others are spaces.

Done.

Copy link
Contributor

@ujoni ujoni left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 1 of 3 files at r2.
Reviewable status: :shipit: complete! all discussions resolved, 1 of 1 LGTMs obtained (waiting on @ujoni)

"Found Polymer 2 style insertion as a Polymer 3 template content {}",
group);

templateDocument = Jsoup.parse(group);
Copy link
Collaborator

Choose a reason for hiding this comment

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

MINOR Introduce a new variable instead of reusing the parameter "templateDocument". rule

.forEach(template::appendChild);

return template;
}

private static Element tryParsePolymer2(Document templateDocument,
Matcher noTemplateMatcher) {
while (noTemplateMatcher.find()
Copy link
Collaborator

Choose a reason for hiding this comment

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

MAJOR Reduce the total number of break and continue statements in this loop to use at most one. rule

@vaadin-bot
Copy link
Collaborator

SonarQube analysis reported 2 issues

  • MAJOR 1 major
  • MINOR 1 minor

Watch the comments in this conversation to review them.

@ujoni ujoni added +1.0.0 and removed +0.0.1 labels Nov 19, 2019
Copy link
Contributor Author

@denis-anisimov denis-anisimov left a comment

Choose a reason for hiding this comment

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

Dismissed @vaadin-bot from 2 discussions.
Reviewable status: :shipit: complete! all discussions resolved, 1 of 1 LGTMs obtained (waiting on @ujoni)

@denis-anisimov denis-anisimov merged commit 433206f into master Nov 19, 2019
@denis-anisimov denis-anisimov deleted the 6116-npm-parser branch November 19, 2019 07:33
@ujoni ujoni added +0.0.1 and removed +1.0.0 labels Nov 19, 2019
@denis-anisimov denis-anisimov added this to the 2.2.0.alpha5 milestone Nov 21, 2019
mehdi-vaadin pushed a commit that referenced this pull request Nov 25, 2019
mehdi-vaadin pushed a commit that referenced this pull request Nov 25, 2019
@mehdi-vaadin
Copy link
Contributor

Cherry-picked to 2.0.

mehdi-vaadin pushed a commit that referenced this pull request Dec 4, 2019
mehdi-vaadin pushed a commit that referenced this pull request Dec 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NPM parser should be able to parse any correct P3 format.
4 participants