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

Wrong text mode for title element? #40

Closed
KitaitiMakoto opened this issue May 18, 2014 · 8 comments · Fixed by #41
Closed

Wrong text mode for title element? #40

KitaitiMakoto opened this issue May 18, 2014 · 8 comments · Fixed by #41

Comments

@KitaitiMakoto
Copy link
Contributor

Hello,

Thanks for the nice library. I installed and tried this HTML5 lib.

And founded that handling of entity references in title element is wrong like this:

<?php
// entityref-in-title.php
require_once 'vendor/autoload.php';

$html = <<<EOH
<!doctype html>
<title>&#x27;</title>
<p>&#x27;</p>
EOH;

echo \HTML5::loadHTML($html)->saveHTML();
$ php -v
PHP 5.6.0beta1 (cli) (built: Apr 17 2014 15:46:38)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.6.0-dev, Copyright (c) 1998-2014 Zend Technologies
$ php ./entityref-in-title.php
<!DOCTYPE html>
<html><title>&amp;#x27;</title>
<p>'</p></html>

In example above, &#x27; should be decoded as '(quotation) but actually doesn't.

If I set text mode for title element to 81, the entity ref is decoded properly:

<?php
require_once 'vendor/autoload.php';

$html = <<<EOH
<!doctype html>
<title>&#x27;</title>
<p>&#x27;</p>
EOH;

\HTML5\Elements::$html5['title'] = 81;
echo \HTML5::loadHTML($html)->saveHTML();
$ php ./entityref-in-title.php
<!DOCTYPE html>
<html><title>'</title>
<p>'</p></html>

I've intended to send a pull request but I couldn't because I didn't know why ¥HTML5¥Elements::$html5['title'] was set to 5.

Could you consider about this?

@mattfarina
Copy link
Member

I'm starting to do a little reading on the spec to get some background.

For reference, the spec section on title is at http://www.w3.org/TR/html51/document-metadata.html#the-title-element. In the section of the spec for parsing the head it says title should be parsed as RCDATA. Following along, RCDATA should handle consuming character references properly.

TEXT_RCDATA should handle character references making 5 work.

@KitaitiMakoto did you track if this is happening in the parser or writer/serializer?

@mattfarina
Copy link
Member

Did a little digging and the issue is in the parser.

@technosophos
Copy link
Member

Does it make a difference if the HTML is well-formed? With malformed HTML, it might have gotten kicked into QuirksMode.

@mattfarina
Copy link
Member

With a composer file that includes html5-php and querypath try the following code:

<?php
require_once 'vendor/autoload.php';

$html = <<<EOH
<!doctype html>
<html>
<head>
<title>&#x27;</title>
</head>
<body>
<p>&#x27;</p>
</body>
</html>
EOH;

$dom = \HTML5::loadHTML($html);
echo \HTML5::saveHTML($dom);

$qp = htmlqp($dom);
print_r($qp->find('title')->get(0));

You'll get the output:

<!DOCTYPE html>
<html><head>
<title>&amp;#x27;</title>
</head>
<body>
<p>'</p>
</body>
</html>
DOMElement Object
(
    [tagName] => title
    [schemaTypeInfo] => 
    [nodeName] => title
    [nodeValue] => &#x27;
    [nodeType] => 1
    [parentNode] => (object value omitted)
    [childNodes] => (object value omitted)
    [firstChild] => (object value omitted)
    [lastChild] => (object value omitted)
    [previousSibling] => (object value omitted)
    [nextSibling] => (object value omitted)
    [attributes] => (object value omitted)
    [ownerDocument] => (object value omitted)
    [namespaceURI] => 
    [prefix] => 
    [localName] => title
    [baseURI] => 
    [textContent] => &#x27;
)

Validation says the html is valid html5.

I've not had a chance to dig further than this yet.

@technosophos
Copy link
Member

Here's the reason:
https://github.com/Masterminds/html5-php/blob/master/src/HTML5/Elements.php#L163

TEXT_RCDATA means that the text inside the tag should be parsed as "raw character data" with no entity decoding.

I haven't tested, but if we change that from 5 to 1, title should decode entities. Wanna give that a try @KitaitiMakoto ? Please let us know if it works.

@technosophos
Copy link
Member

@mattfarina just showed me that I'm wrong. There's a bug in the parser where it does not handle entities in RCDATA fields correctly.

http://www.w3.org/TR/html51/syntax.html#rcdata-state

So I need to fix that in the tokenizer.

@KitaitiMakoto
Copy link
Contributor Author

Sorry for my late reply. But I'm still busy now. Will try and reply on the weekend.

@KitaitiMakoto
Copy link
Contributor Author

Thank you for digging code and pointing specification.
I wrote patch to fix this issue and send a pull request #41.
Could you review and merge the patch?

technosophos added a commit that referenced this issue May 30, 2014
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 a pull request may close this issue.

3 participants