Skip to content

Commit fe00cef

Browse files
authored
[Java.Interop.Tools.JavaSource] Support html tags with attributes (#1286)
Context: dotnet/android#9647 dotnet/android#9647 attempted to import API docs for API 35, and produced the following warning: The following issues were found, review the build log for more details: > ## Unable to translate remarks for android/app/admin/DevicePolicyManager: > JavadocImport-Error (31:39): Syntax error, expected: </p>, </P>, #PCDATA, <tt>, <TT>, <i>, <I>, <a attr=, <code>, {@code, {@docroot}, {@inheritdoc}, {@link, {@linkplain, {@literal, {@see, {@value}, {@value, IgnorableDeclaration, {@param, UnknownHtmlElementStart, <p>, <P>, <pre , @author, @apiSince, @deprecated, @deprecatedSince, @exception, @inheritdoc, @hide, @param, @return, @see, @Serialdata, @serialField, @SInCE, @throws, @[unknown], @Version <li>A <i id="deviceowner">Device Owner</i>, which only ever exists on the ^ Parsing logic fails here because the `<i>` tag has an `id` attribute _and_ is present in an open `<p>` tag. Turns Out™ that HTML allows attributes on nearly *everything*; e.g. from [§3.2.3 Global attributes][0]: > The following attributes are common to and may be specified on all > [HTML elements](https://dev.w3.org/html5/spec-LC/infrastructure.html#html-elements) > (even those not defined in this specification): > * … > * `id` Given this, it doesn't make sense for `CreateStartElement()` to not allow any attributes. Update `CreateStartElement()` so that *all* elements *ignore* any specified attributes (by default), which allows `<i id="deviceowner">Device Owner</i>` to work. The regex used has also been improved to include word boundaries around the tag name to make sure that it does not match unexpected elements. [0]: https://dev.w3.org/html5/spec-LC/elements.html#global-attributes
1 parent 2c06b3c commit fe00cef

File tree

2 files changed

+9
-18
lines changed

2 files changed

+9
-18
lines changed

src/Java.Interop.Tools.JavaSource/Java.Interop.Tools.JavaSource/SourceJavadocToXmldocGrammar.HtmlBnfTerms.cs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ internal void CreateRules (SourceJavadocToXmldocGrammar grammar)
6666
var fontstyle_i = CreateHtmlToCrefElement (grammar, "i", "i", InlineDeclarations, optionalEnd: true);
6767

6868
var preText = new PreBlockDeclarationBodyTerminal ();
69-
PreBlockDeclaration.Rule = CreateStartElementIgnoreAttribute ("pre") + preText + CreateEndElement ("pre", grammar, optional: true);
69+
PreBlockDeclaration.Rule = CreateStartElement ("pre") + preText + CreateEndElement ("pre", grammar, optional: true);
7070
PreBlockDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
7171
if (!grammar.ShouldImport (ImportJavadoc.Remarks)) {
7272
parseNode.AstNode = "";
@@ -82,7 +82,7 @@ internal void CreateRules (SourceJavadocToXmldocGrammar grammar)
8282
FontStyleDeclaration.Rule = fontstyle_tt | fontstyle_i;
8383

8484
PBlockDeclaration.Rule =
85-
CreateStartElement ("p", grammar) + InlineDeclarations + CreateEndElement ("p", grammar, optional:true)
85+
CreateStartElement ("p") + InlineDeclarations + CreateEndElement ("p", grammar, optional:true)
8686
;
8787
PBlockDeclaration.AstConfig.NodeCreator = (context, parseNode) => {
8888
var remarks = FinishParse (context, parseNode).Remarks;
@@ -260,7 +260,7 @@ static string GetChildNodesAsString (ParseTreeNode parseNode)
260260

261261
static NonTerminal CreateHtmlToCrefElement (Grammar grammar, string htmlElement, string crefElement, BnfTerm body, bool optionalEnd = false)
262262
{
263-
var start = CreateStartElement (htmlElement, grammar);
263+
var start = CreateStartElement (htmlElement);
264264
var end = CreateEndElement (htmlElement, grammar, optionalEnd);
265265
var nonTerminal = new NonTerminal ("<" + htmlElement + ">", ConcatChildNodes) {
266266
Rule = start + body + end,
@@ -275,28 +275,15 @@ static NonTerminal CreateHtmlToCrefElement (Grammar grammar, string htmlElement,
275275
return nonTerminal;
276276
}
277277

278-
static NonTerminal CreateStartElement (string startElement, Grammar grammar)
278+
static RegexBasedTerminal CreateStartElement (string startElement, string attribute = "")
279279
{
280-
var start = new NonTerminal ("<" + startElement + ">", nodeCreator: (context, parseNode) => parseNode.AstNode = "") {
281-
Rule = grammar.ToTerm ("<" + startElement + ">") | "<" + startElement.ToUpperInvariant () + ">",
282-
};
283-
return start;
284-
}
285-
286-
static RegexBasedTerminal CreateStartElementIgnoreAttribute (string startElement, string attribute)
287-
{
288-
return new RegexBasedTerminal ($"<{startElement} {attribute}", $@"(?i)<{startElement}\s*{attribute}[^>]*>") {
280+
return new RegexBasedTerminal ($"<{startElement} {attribute}>", $@"(?i)<\b{startElement}\b\s*{attribute}[^>]*>") {
289281
AstConfig = new AstNodeConfig {
290282
NodeCreator = (context, parseNode) => parseNode.AstNode = "",
291283
},
292284
};
293285
}
294286

295-
static RegexBasedTerminal CreateStartElementIgnoreAttribute (string startElement)
296-
{
297-
return CreateStartElementIgnoreAttribute (startElement, "");
298-
}
299-
300287
static NonTerminal CreateEndElement (string endElement, Grammar grammar, bool optional = false)
301288
{
302289
var end = new NonTerminal (endElement, nodeCreator: (context, parseNode) => parseNode.AstNode = "") {

tests/Java.Interop.Tools.JavaSource-Tests/SourceJavadocToXmldocGrammar.HtmlBnfTermsTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public void PBlockDeclaration ()
3434
r = p.Parse("<p>r= <em>unknown</em> text");
3535
Assert.IsFalse (r.HasErrors (), DumpMessages (r, p));
3636
Assert.AreEqual ("<para>r= &lt;em&gt;unknown&lt;/em&gt; text</para>", r.Root.AstNode.ToString ());
37+
38+
r = p.Parse ("<p>For <li>A <i id=\"deviceowner\">Device Owner</i>");
39+
Assert.IsFalse (r.HasErrors (), DumpMessages (r, p));
40+
Assert.AreEqual ("<para>For &lt;li&gt;A <i>Device Owner</i></para>", r.Root.AstNode.ToString ());
3741
}
3842

3943
[Test]

0 commit comments

Comments
 (0)