Use tagName rather than nodeName when we know the node is an element

`tagName` is only defined on Element, and it returns its tag name.

`nodeName` is defined on Node, and it returns the tag name for Elements,
and a string like '#text' or '#document-fragment' for other types.

We were using both, which made it harder to reason about what types
we're dealing with.

Change-Id: I8e621e5872bdf78c84ec553cfbfcdbf0192f0589
This commit is contained in:
Bartosz Dziewoński 2022-02-21 19:54:15 +01:00
parent 063174e71c
commit 584f6a020c
5 changed files with 8 additions and 8 deletions

View file

@ -291,7 +291,7 @@ class CommentModifier {
$hasElements = false;
foreach ( $nodes as $node ) {
if ( $node instanceof Element ) {
if ( strtolower( $node->nodeName ) !== strtolower( $type ) ) {
if ( strtolower( $node->tagName ) !== strtolower( $type ) ) {
return false;
}
$hasElements = true;

View file

@ -598,7 +598,7 @@ class CommentParser {
// if the timestamp node is the only child of a link node, use the link node instead
//
// Handle links nested in formatting elements.
if ( $event === 'leave' && $node instanceof Element && strtolower( $node->nodeName ) === 'a' ) {
if ( $event === 'leave' && $node instanceof Element && strtolower( $node->tagName ) === 'a' ) {
$username = $this->getUsernameFromLink( $node );
if ( $username ) {
// Accept the first link to the user namespace, then only accept links to that user

View file

@ -148,9 +148,9 @@ class CommentUtils {
public static function isCommentSeparator( Node $node ): bool {
return $node instanceof Element && (
// Empty paragraphs (`<p><br></p>`) between indented comments mess up indentation detection
strtolower( $node->nodeName ) === 'br' ||
strtolower( $node->tagName ) === 'br' ||
// Horizontal line
strtolower( $node->nodeName ) === 'hr' ||
strtolower( $node->tagName ) === 'hr' ||
// {{outdent}} templates
DOMCompat::getClassList( $node )->contains( 'outdent-template' )
);
@ -213,7 +213,7 @@ class CommentUtils {
do {
if (
$node instanceof Element &&
in_array( strtolower( $node->nodeName ), $tagNames )
in_array( strtolower( $node->tagName ), $tagNames )
) {
return $node;
}

View file

@ -40,7 +40,7 @@ function ReplyWidget( commentController, commentDetails, config ) {
// don't need to worry about transcluded replies.
this.pageExists = mw.config.get( 'wgRelevantArticleId', 0 ) !== 0;
var contextNode = utils.closestElement( threadItem.range.endContainer, [ 'dl', 'ul', 'ol' ] );
this.context = contextNode ? contextNode.nodeName.toLowerCase() : 'dl';
this.context = contextNode ? contextNode.tagName.toLowerCase() : 'dl';
// TODO: Should storagePrefix include pageName?
this.storagePrefix = 'reply/' + threadItem.id;
this.storage = controller.storage;

View file

@ -113,9 +113,9 @@ function cantHaveElementChildren( node ) {
function isCommentSeparator( node ) {
return node.nodeType === Node.ELEMENT_NODE && (
// Empty paragraphs (`<p><br></p>`) between indented comments mess up indentation detection
node.nodeName.toLowerCase() === 'br' ||
node.tagName.toLowerCase() === 'br' ||
// Horizontal line
node.nodeName.toLowerCase() === 'hr' ||
node.tagName.toLowerCase() === 'hr' ||
// {{outdent}} templates
node.classList.contains( 'outdent-template' )
);