Skip to content

Commit

Permalink
Add explanation why lang takes precendence over xml:lang
Browse files Browse the repository at this point in the history
  • Loading branch information
xemlock committed Aug 19, 2020
1 parent e1acc1c commit dbe79de
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 7 additions & 3 deletions library/HTMLPurifier/AttrTransform/HTML5/Lang.php
Expand Up @@ -24,13 +24,17 @@ public function transform($attr, $config, $context)
$attr['lang'] = $lang = $xml_lang;
}

// If both lang and xml:lang are present, ensure they're in sync,
// lang having priority over xml:lang
// If lang and xml:lang are both present, ensure that their values are
// equal, with lang having priority over xml:lang, as according to the
// spec, lang attribute in the XML namespace is not allowed on HTML
// elements, and non-namespaced attribute with the literal localname
// 'xml:lang' has no effect on language processing.
// https://html.spec.whatwg.org/#the-lang-and-xml:lang-attributes
if ($lang !== false && $xml_lang !== false && $xml_lang !== $lang) {
$attr['xml:lang'] = $lang;
}

// xml:lang will be stripped out unless %HTML.XHTML is true
// xml:lang will be stripped out later unless %HTML.XHTML is true

return $attr;
}
Expand Down
Expand Up @@ -21,7 +21,7 @@ public function testXmlLang()
// When the attribute xml:lang in no namespace is specified, the element must also have the attribute lang present with the same value.
$this->assertPurification('<p xml:lang="en">text</p>', '<p xml:lang="en" lang="en">text</p>');

// When the attribute xml:lang in no namespace is specified, the element must also have the attribute lang present with the same value.
// When there is a conflict between xml:lang and lang values, lang takes precedence
$this->assertPurification('<p lang="en" xml:lang="pl">text</p>', '<p lang="en" xml:lang="en">text</p>');

$this->assertPurification('<hr xml:lang="en">', '<hr xml:lang="en" lang="en" />');
Expand Down

0 comments on commit dbe79de

Please sign in to comment.