Adding comment support for quoted attributes

This commit is contained in:
Sergey
2017-04-12 09:42:35 -07:00
parent 5d1d4031e6
commit d18b3a0bf8
4 changed files with 105 additions and 18 deletions

View File

@ -82,6 +82,8 @@ PARSER_END(VfParser)
| <#TEXT_IN_EL: (~["}", "'", "\""])+ >
| <#CLOSEBRACE: ("}")>
| <#DOT: "." >
| <#COMMNT_START: "/*" >
| <#COMMNT_END: "*/" >
}
@ -161,12 +163,14 @@ PARSER_END(VfParser)
<ElAttribTagStateSQ> TOKEN :
{
<END_OF_EL_ATTRIB_SQ: (<WHITESPACES>)? <CLOSEBRACE> > : AttrValueBetweenSingleQuotesState
<COMMENT_OPEN_SQ: <COMMNT_START> > : InlineCommentStateSQ
| <END_OF_EL_ATTRIB_SQ: (<WHITESPACES>)? <CLOSEBRACE> > : AttrValueBetweenSingleQuotesState
}
<ElAttribTagStateDQ> TOKEN :
{
<END_OF_EL_ATTRIB_DQ: (<WHITESPACES>)? <CLOSEBRACE> > : AttrValueBetweenDoubleQuotesState
<COMMENT_OPEN_DQ: <COMMNT_START> > : InlineCommentStateDQ
| <END_OF_EL_ATTRIB_DQ: (<WHITESPACES>)? <CLOSEBRACE> > : AttrValueBetweenDoubleQuotesState
}
<ElAttribTagStateNQ> TOKEN :
@ -176,7 +180,7 @@ PARSER_END(VfParser)
<ElInScriptState> TOKEN :
{
<COMMENT_OPEN_SCRIPT: "/*" > : InlineCommentStateScript
<COMMENT_OPEN_SCRIPT: <COMMNT_START> > : InlineCommentStateScript
| <END_OF_EL_SCRIPT: (<WHITESPACES>)? <CLOSEBRACE> > : HtmlScriptContentState
}
@ -260,10 +264,23 @@ PARSER_END(VfParser)
<InlineCommentStateScript> TOKEN :
{
< COMMENT_CLOSE_SCRIPT: ("*/" ) > : ElInScriptState
< COMMENT_CLOSE_SCRIPT: (<COMMNT_END>) > : ElInScriptState
| < COMMENT_INNER_TEXT_SCRIPT: (~[]) >
}
<InlineCommentStateSQ> TOKEN :
{
< COMMENT_CLOSE_SQ: (<COMMNT_END>) > : ElAttribTagStateSQ
| < COMMENT_INNER_TEXT_SQ: (~[]) >
}
<InlineCommentStateDQ> TOKEN :
{
< COMMENT_CLOSE_DQ: (<COMMNT_END>) > : ElAttribTagStateDQ
| < COMMENT_INNER_TEXT_DQ: (~[]) >
}
<HtmlScriptContentState> TOKEN :
{
<HTML_SCRIPT_END_TAG : "</script>" > : AfterTagState
@ -409,7 +426,10 @@ void Expression() :
{}
{
ConditionalExpression() [ AssignmentOperator() Expression() ]
| InlineCommentExpression() ( ConditionalExpression() | InlineCommentExpression() )*
| CommentExpression() ( ConditionalExpression() | CommentExpression() )*
| ELDQCommentExpression() ( ConditionalExpression() | ELDQCommentExpression() )*
| ELSQCommentExpression() ( ConditionalExpression() | ELSQCommentExpression() )*
}
void AssignmentOperator() #void :
@ -492,7 +512,25 @@ void PrimaryExpression() #void :
PrimaryPrefix() ( LOOKAHEAD(2) PrimarySuffix() )*
}
void InlineCommentExpression() :
void ELSQCommentExpression() #CommentExpression :
{
StringBuffer content = new StringBuffer();
Token t;
}
{
<COMMENT_OPEN_SQ> ( t = <COMMENT_INNER_TEXT_SQ> { content.append(t.image); })* <COMMENT_CLOSE_SQ> { jjtThis.setImage(content.toString()); }
}
void ELDQCommentExpression() #CommentExpression :
{
StringBuffer content = new StringBuffer();
Token t;
}
{
<COMMENT_OPEN_DQ> ( t = <COMMENT_INNER_TEXT_DQ> { content.append(t.image); })* <COMMENT_CLOSE_DQ> { jjtThis.setImage(content.toString()); }
}
void CommentExpression() :
{
StringBuffer content = new StringBuffer();
Token t;

View File

@ -90,7 +90,7 @@ public class VfParserVisitorAdapter implements VfParserVisitor {
}
@Override
public Object visit(ASTInlineCommentExpression node, Object data) {
public Object visit(ASTCommentExpression node, Object data) {
return visit((VfNode) node, data);
}
}

View File

@ -28,7 +28,7 @@ import net.sourceforge.pmd.lang.vf.ast.ASTElement;
import net.sourceforge.pmd.lang.vf.ast.ASTExpression;
import net.sourceforge.pmd.lang.vf.ast.ASTHtmlScript;
import net.sourceforge.pmd.lang.vf.ast.ASTIdentifier;
import net.sourceforge.pmd.lang.vf.ast.ASTInlineCommentExpression;
import net.sourceforge.pmd.lang.vf.ast.ASTCommentExpression;
import net.sourceforge.pmd.lang.vf.ast.ASTLiteral;
import net.sourceforge.pmd.lang.vf.ast.ASTText;
import net.sourceforge.pmd.lang.vf.ast.VfNode;
@ -133,7 +133,7 @@ public abstract class AbstractVfRule extends AbstractRule implements VfParserVis
return visit((VfNode) node, data);
}
public Object visit(ASTInlineCommentExpression node, Object data) {
public Object visit(ASTCommentExpression node, Object data) {
return visit((VfNode) node, data);
}
}

View File

@ -152,6 +152,53 @@ public class VfDocStyleTest extends AbstractVfNodesTest {
assertEquals("Correct script content expected!", "Script!", text.getImage());
}
/**
* Test parsing of EL in attribute of an element.
*/
@Test
public void testELInTagValue() {
Set<ASTElement> elememts = getNodes(ASTElement.class, TEST_EL_IN_TAG_ATTRIBUTE);
assertEquals("One element expected!", 1, elememts.size());
ASTElement element = elememts.iterator().next();
ASTAttributeValue attribute = element.getFirstDescendantOfType(ASTAttributeValue.class);
ASTIdentifier id = attribute.getFirstDescendantOfType(ASTIdentifier.class);
assertEquals("Correct identifier expected", "foo", id.getImage());
}
/**
* Test parsing of EL in attribute of an element that also has a comment.
*/
@Test
public void testELInTagValueWithCommentDQ() {
Set<ASTElement> elememts = getNodes(ASTElement.class, TEST_EL_IN_TAG_ATTRIBUTE_WITH_COMMENT);
assertEquals("One element expected!", 1, elememts.size());
ASTElement element = elememts.iterator().next();
ASTElExpression elExpr = element.getFirstDescendantOfType(ASTElExpression.class);
ASTIdentifier id = elExpr.getFirstDescendantOfType(ASTIdentifier.class);
assertEquals("Correct identifier expected", "init", id.getImage());
ASTCommentExpression comment = elExpr.getFirstDescendantOfType(ASTCommentExpression.class);
assertEquals("Correct comment expected!", "comment here", comment.getImage());
}
/**
* Test parsing of EL in attribute of an element that also has a comment.
*/
@Test
public void testELInTagValueWithCommentSQ() {
Set<ASTElement> elememts = getNodes(ASTElement.class, TEST_EL_IN_TAG_ATTRIBUTE_WITH_COMMENT_SQ);
assertEquals("One element expected!", 1, elememts.size());
ASTElement element = elememts.iterator().next();
ASTElExpression elExpr = element.getFirstDescendantOfType(ASTElExpression.class);
ASTIdentifier id = elExpr.getFirstDescendantOfType(ASTIdentifier.class);
assertEquals("Correct identifier expected", "init", id.getImage());
ASTCommentExpression comment = elExpr.getFirstDescendantOfType(ASTCommentExpression.class);
assertEquals("Correct comment expected!", "comment here", comment.getImage());
}
/**
* Test parsing of EL in HTML &lt;script&gt; element.
@ -168,7 +215,6 @@ public class VfDocStyleTest extends AbstractVfNodesTest {
assertEquals("Correct EL content expected!", "elInScript", id.getImage());
}
/**
* Test parsing of inline comment in EL.
*/
@ -180,13 +226,12 @@ public class VfDocStyleTest extends AbstractVfNodesTest {
ASTText text = script.getFirstChildOfType(ASTText.class);
assertEquals("Correct script content expected!", "vartext=", text.getImage());
ASTElExpression el = script.getFirstChildOfType(ASTElExpression.class);
List<ASTInlineCommentExpression> comments = el.findDescendantsOfType(ASTInlineCommentExpression.class);
List<ASTCommentExpression> comments = el.findDescendantsOfType(ASTCommentExpression.class);
assertEquals("Correct comment size expected!", 2, comments.size());
ASTIdentifier id = el.getFirstDescendantOfType(ASTIdentifier.class);
assertEquals("Correct EL content expected!", "elInScript", id.getImage());
}
/**
* Test parsing of quoted EL in HTML &lt;script&gt; element.
*/
@ -673,6 +718,10 @@ public class VfDocStyleTest extends AbstractVfNodesTest {
private static final String TEST_HTML_SCRIPT = "<html><head><script>Script!</script></head></html>";
private static final String TEST_EL_IN_TAG_ATTRIBUTE = "<apex:page action=\"{!foo}\">text</apex:page>";
private static final String TEST_EL_IN_TAG_ATTRIBUTE_WITH_COMMENT = "<apex:page action=\"{!/*comment here*/init}\">text</apex:page>";
private static final String TEST_EL_IN_TAG_ATTRIBUTE_WITH_COMMENT_SQ = "<apex:page action='{!/*comment here*/init}'>text</apex:page>";
private static final String TEST_EL_IN_HTML_SCRIPT = "<html><head><script>var text={!elInScript};</script></head></html>";
private static final String TEST_EL_IN_HTML_SCRIPT_WITH_COMMENT = "<html><head><script>var text={!/*junk1*/elInScript/*junk2*/};</script></head></html>";