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

Implement lit template parser for limited template formats which extends LitTemplate element #8979

Merged
merged 12 commits into from
Sep 11, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,8 @@ public static Element parseLitTemplateElement(String fileName,
// template contents.
if (templateMatcher.find() && templateMatcher.groupCount() == 2) {
String group = templateMatcher.group(2);
// in case greedy match let's truncate the second group (remove
// everything after delimiter)
int index = group.indexOf(templateMatcher.group(1));
if (index >= 0) {
group = group.substring(0, index);
}
group = getNonGreadyTemplateContent(group,
templateMatcher.group(1));
LOGGER.trace("Found regular Lit template content was {}", group);

templateDocument = Jsoup.parse(group);
Expand All @@ -273,6 +269,37 @@ public static Element parseLitTemplateElement(String fileName,
return null;
}

private static String getNonGreadyTemplateContent(String greedyContent,
String delimiter) {
String templateContent = greedyContent;
// in case greedy match let's truncate the second group (remove
// everything after delimiter)
int index = -1;
while (true) {
index = greedyContent.indexOf(delimiter, index + 1);
if (index < 0) {
break;
}
templateContent = greedyContent.substring(0, index);
if (!endsWithEscaped(templateContent)) {
break;
}
}
return templateContent;
}

private static boolean endsWithEscaped(String value) {
int slashCount = 0;
for (int i = value.length() - 1; i >= 0; i--) {
if (value.charAt(i) == '\\') {
slashCount++;
} else {
break;
}
}
return slashCount % 2 == 1;
}

denis-anisimov marked this conversation as resolved.
Show resolved Hide resolved
private static Element tryParsePolymer2(Document templateDocument,
Matcher noTemplateMatcher) {
while (noTemplateMatcher.find()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
,
{
"name": "./frontend/MyGreedyLitElement.js",
"source": "// Import an element\nimport { LitElement, html } from 'lit-element';\n\n// Define an element class\n export class MyGreedyLitElement extends LitElement {\n\n // Define the element's template\n render() {\n return html`\n <style>\n :host{ \n margin: 5px; \n }\n \n .response { margin-top: 10px; } \n </style>\n <div>Tag name doesn't match the JS module name<div>inner</div></div> <div id='test' class='response'>greedy</div>\n `;\n static get styles() { return css`:host { background-color: pink } <span>incorrect content</span>`; } }\n}\n\n// Register the element with the browser\ncustomElements.define('my-greedy-element', MyGreedyLitElement);"
"source": "// Import an element\nimport { LitElement, html } from 'lit-element';\n\n// Define an element class\n export class MyGreedyLitElement extends LitElement {\n\n // Define the element's template\n render() {\n return html`\n <style>\n :host{ \n margin: 5px; \n }\n \n .response { margin-top: 10px; } \n </style>\n <div>\\`Tag name doesn't match the JS module name<div>inner</div></div> <div id='test' class='response'>greedy</div>\n `;\n static get styles() { return css`:host { background-color: pink } <span>incorrect content</span>`; } }\n}\n\n// Register the element with the browser\ncustomElements.define('my-greedy-element', MyGreedyLitElement);"
joheriks marked this conversation as resolved.
Show resolved Hide resolved
}
]
}
Expand Down