Skip to content

Conversation

naitoh
Copy link
Contributor

@naitoh naitoh commented Jul 21, 2024

Why?

XML with content at the start of the document is invalid.

https://www.w3.org/TR/2006/REC-xml11-20060816/#document

[1]   document   ::=   ( prolog element Misc* ) - ( Char* RestrictedChar Char* )

https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-prolog

[22]   	prolog	   ::=   	XMLDecl Misc* (doctypedecl Misc*)?

https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-XMLDecl

[23]   	XMLDecl	   ::=   	'<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'

https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-Misc

[27]   	Misc	   ::=   	Comment | PI | S

https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-PI

[16]   	PI	   ::=   	'<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'

https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-PITarget

[17]   	PITarget	   ::=   	Name - (('X' | 'x') ('M' | 'm') ('L' | 'l'))

https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-doctypedecl

[28]   	doctypedecl	   ::=   	'<!DOCTYPE' S Name (S ExternalID)? S? ('[' intSubset ']' S?)? '>'

See: #164 (comment)

@naitoh naitoh marked this pull request as ready for review July 21, 2024 05:46
end
end

def test_garbage_text
Copy link
Member

Choose a reason for hiding this comment

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

Could you improve test name? This is not a garbage text test now.
test_comment?

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 see.

Comment on lines 47 to 51
doc = parse(<<~XML)
<?x y
<!--?><?x -->?>
<r/>
XML
Copy link
Member

Choose a reason for hiding this comment

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

If you want to use <<~, could you indent content?

Suggested change
doc = parse(<<~XML)
<?x y
<!--?><?x -->?>
<r/>
XML
doc = parse(<<~XML)
<?x y
<!--?><?x -->?>
<r/>
XML

BTW, I prefer <<- to <<~ for XML content because it's not a program code.
I think that indentation is needless for not a program code in program.

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 see.

Comment on lines 52 to 53
assert_equal(["x", "y\n<!--"], [ doc.children[0].target, doc.children[0].content])
assert_equal(["x", "-->"], [ doc.children[1].target, doc.children[1].content])
Copy link
Member

Choose a reason for hiding this comment

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

Could you unify these assertions to one assert for easy to debug?

Suggested change
assert_equal(["x", "y\n<!--"], [ doc.children[0].target, doc.children[0].content])
assert_equal(["x", "-->"], [ doc.children[1].target, doc.children[1].content])
assert_equal([["x", "y\n<!--"],
["x", "-->"]],
[[doc.children[0].target, doc.children[0].content],
[doc.children[1].target, doc.children[1].content]])

See also: https://www.clear-code.com/blog/2011/2/28.html

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 see.
It looks good.

@naitoh naitoh force-pushed the fix_content_at_the_start_of_the_document branch from 1722700 to 0f7f84d Compare July 22, 2024 14:09
@naitoh naitoh requested a review from kou July 22, 2024 14:15
## Why?

XML with content at the start of the document is invalid.

https://www.w3.org/TR/2006/REC-xml11-20060816/#document

```
[1]   document   ::=   ( prolog element Misc* ) - ( Char* RestrictedChar Char* )
```

https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-prolog

```
[22]   	prolog	   ::=   	XMLDecl Misc* (doctypedecl Misc*)?
```

https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-XMLDecl
```
[23]   	XMLDecl	   ::=   	'<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'
```

https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-Misc

```
[27]   	Misc	   ::=   	Comment | PI | S
```

https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-PI

```
[16]   	PI	   ::=   	'<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
```

https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-PITarget

```
[17]   	PITarget	   ::=   	Name - (('X' | 'x') ('M' | 'm') ('L' | 'l'))
```

https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-doctypedecl
```
[28]   	doctypedecl	   ::=   	'<!DOCTYPE' S Name (S ExternalID)? S? ('[' intSubset ']' S?)? '>'
```
@naitoh naitoh force-pushed the fix_content_at_the_start_of_the_document branch from 0f7f84d to 5ebd293 Compare July 22, 2024 14:30
@kou kou changed the title fix: Content at the start of the document Add support for detecting invalid XML that has unsupported content before root element Jul 22, 2024
@kou kou merged commit 2bca7bd into ruby:master Jul 22, 2024
@kou
Copy link
Member

kou commented Jul 22, 2024

Thanks.

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.

2 participants