-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[part of RFC] Implement Document::${body,head,title} #13791
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
Conversation
b442f9d
to
6fcfba6
Compare
6fcfba6
to
e64c035
Compare
e64c035
to
2171955
Compare
Hello @nielsdos , Was reading your rfc for this. But have a question about it. There you have the following signature for Document;
When reading the spec you linked to it says Is it correct the title is string on empty or missing the nullable? |
Hi @mkroeders ! Indeed the title element will be NULL when there is no such element. The
So the title property is the empty string when there is no title element in the document. Furthermore, it is defined as non-nullable in the IDL for document: https://html.spec.whatwg.org/#the-document-object:document.title |
e15f946
to
8b5f288
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall makes sense, just a couple of remarks/questions
ext/dom/php_dom.stub.php
Outdated
/** @readonly */ | ||
public ?Element $head; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$head
doesn't seem to be defined in this commit yet?
/* 1. If the title element is null and the head element is null, then return. */ | ||
xmlNodePtr title = dom_get_title_element(docp); | ||
xmlNodePtr head = dom_html_document_element_read_raw(docp, dom_accept_head_name); | ||
if (title == NULL && head == NULL) { | ||
return SUCCESS; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a choice from the DOM spec. But okay sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel free to merge as one big commit or as individual commits, also UPGRADING entries :)
8b5f288
to
190ef42
Compare
@xabbuh According to https://wiki.php.net/rfc/class-naming-acronyms it follows the following rule (emphasis mine): Diverging from this policy is allowed to keep internal consistency within a single extension, if the name follows an established, language-agnostic standard, or for other reasons, if those reasons are properly justified and voted on as part of the RFC process. In this case, the DOM spec defines the name of the class. |
@nielsdos Thank you for the explanation. 👍 |
I'm running into an issue with attributes. The & character gets encoded which is preventing me from encoding < and > within attributes.
When I call
Having a flag that lets me declare that I'll be handling the encoding for specific attributes would be nice. Here is the code I'm using: function allowTags(string|\Stringable $html, array|string $allowedTags = [], ?string $selector = null, int $flags = LIBXML_NOERROR | LIBXML_HTML_NOIMPLIED) : string
{
if(is_string($allowedTags)) {
$tags = explode(',', $allowedTags);
$allowedTags = [];
foreach ($tags as $tag) {
[$tag, $attributes] = explode(':', $tag, 2);
if ($attributes) {
$attributes = explode('|', $attributes);
}
$allowedTags[$tag] = $attributes ?? [];
}
}
$dom = \Dom\HTMLDocument::createFromString((string) $html, $flags);
$allowedTagsList = array_keys($allowedTags);
foreach ($dom->querySelectorAll($selector ?? '*') as $node) {
if (!in_array($node->localName, $allowedTagsList)) {
$node->parentNode->removeChild($node);
continue;
}
foreach (iterator_to_array($node->attributes) as $attribute) {
if (!$attribute->localName || !in_array($attribute->localName, $allowedTags[$node->localName] ?? [])) {
$node->removeAttribute($attribute->localName);
continue;
}
$attribute->value = htmlspecialchars($attribute->value, ENT_QUOTES);
}
}
return $dom->saveHTML();
} Calling it: allowTags(
"<script>alert('test');</script><a href=\"<script></script>\">Link</a>",
['a' => ['href']]
);
|
@kevindees Simply remove the htmlspecialchars call. |
Part of a larger RFC: https://wiki.php.net/rfc/dom_additions_84