Skip to content

Commit

Permalink
Merge pull request #39 from WebFiori/dev
Browse files Browse the repository at this point in the history
Added Support for Adding `http-equiv` Meta
  • Loading branch information
usernane authored Nov 28, 2023
2 parents ec2c836 + c8b8a52 commit c8d4db1
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 26 deletions.
21 changes: 21 additions & 0 deletions tests/webfiori/test/ui/HeadNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,27 @@ public function testAddMeta03() {
$this->assertEquals('World', $node->getMeta('hello')->getAttribute('content'));
$this->assertEquals('Page Description.', $node->getMeta('description')->getAttribute('content'));
}
/**
* @test
*/
public function testAddMeta04() {
$node = new HeadNode();
$this->assertFalse($node->hasMeta('content-security-policy'));
$node->addMetaHttpEquiv('content-security-pOlicy', "script-src 'self'");
$this->assertNull($node->getMeta('Content-Security-policy'));
$this->assertEquals("script-src 'self'", $node->getMeta('Content-Security-policy', true)->getAttribute('content'));
$this->assertEquals(3,$node->childrenCount());
$this->assertTrue($node->hasMeta('content-security-policy'));
$node->addMetaHttpEquiv('content-security-policy', 'Hello');
$this->assertEquals("script-src 'self'", $node->getMeta('Content-Security-policy', true)->getAttribute('content'));
$node->addMetaHttpEquiv('content-security-policy', 'Hello', true);
$this->assertEquals(3,$node->childrenCount());
$this->assertEquals("Hello", $node->getMeta('Content-Security-policy', true)->getAttribute('content'));
$node->addMetaHttpEquiv('refresh', 30);
$this->assertEquals(4,$node->childrenCount());
$this->assertTrue($node->hasMeta('refresh'));

}
/**
* @test
*/
Expand Down
82 changes: 56 additions & 26 deletions webfiori/ui/HeadNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,43 @@ public function addLink(string $rel, string $href, array $otherAttrs = []) : Hea

return $this;
}
public function addMetaHttpEquiv(string $name, string $content, bool $override = false) : HeadNode {
$trimmedName = trim(strtolower($name));

if (strlen($trimmedName) != 0) {
$meta = $this->getMeta($trimmedName, true);

if ($meta !== null && $override === true) {
$meta->setAttribute('content', $content);

return $this;
} else if ($meta === null) {
$meta = new HTMLNode('meta');
$meta->setAttribute('http-equiv', $trimmedName);
$meta->setAttribute('content', $content);
$this->insertMetaInCorrectOrder($meta);
}
}

return $this;
}
private function insertMetaInCorrectOrder(HTMLNode $newMeta) {
$insertPosition = -1;

for ($x = 0 ; $x < $this->childrenCount() ; $x++) {
$chNode = $this->getChild($x);

if ($chNode->getNodeName() == 'meta') {
$insertPosition = $x;
}
}

if ($insertPosition != -1) {
$this->insert($newMeta, $insertPosition + 1);
} else {
$this->addChild($newMeta);
}
}
/**
* Adds new meta tag.
*
Expand Down Expand Up @@ -482,21 +519,7 @@ public function addMeta(string $name, string $content, bool $override = false) :
$meta = new HTMLNode('meta');
$meta->setAttribute('name', $trimmedName);
$meta->setAttribute('content', $content);
$insertPosition = -1;

for ($x = 0 ; $x < $this->childrenCount() ; $x++) {
$chNode = $this->getChild($x);

if ($chNode->getNodeName() == 'meta') {
$insertPosition = $x;
}
}

if ($insertPosition != -1) {
$this->insert($meta,$insertPosition + 1);
} else {
$this->addChild($meta);
}
$this->insertMetaInCorrectOrder($meta);
}
}

Expand Down Expand Up @@ -530,7 +553,7 @@ public function getAlternates() : LinkedList {
$child = $children->get($x);
$childName = $child->getNodeName();

if ($childName == 'link' && $child->hasAttribute('rel') && $child->getAttributeValue('rel') == 'alternate') {
if ($childName == 'link' && $child->hasAttribute('rel') && $child->getAttribute('rel') == 'alternate') {
$list->add($child);
}
}
Expand All @@ -557,7 +580,7 @@ public function getBaseNode() : HTMLNode {
* @since 1.1.3
*/
public function getBaseURL() {
return $this->baseNode->getAttributeValue('href');
return $this->baseNode->getAttribute('href');
}
/**
* Returns the canonical URL if set.
Expand All @@ -568,7 +591,7 @@ public function getBaseURL() {
* @since 1.0
*/
public function getCanonical() {
return $this->canonical->getAttributeValue('href');
return $this->canonical->getAttribute('href');
}
/**
* Returns an object of type HTMLNode that represents the canonical URL.
Expand All @@ -590,7 +613,7 @@ public function getCanonicalNode() : HTMLNode {
* @since 1.1.4
*/
public function getCharSet() {
return $this->metaCharset->getAttributeValue('charset');
return $this->metaCharset->getAttribute('charset');
}
/**
* Returns an object of type HTMLNode that represents the meta tag which
Expand Down Expand Up @@ -623,7 +646,7 @@ public function getCSSNodes() : LinkedList {
$child = $children->get($x);
$childName = $child->getNodeName();

if ($childName == 'link' && $child->hasAttribute('rel') && $child->getAttributeValue('rel') == 'stylesheet') {
if ($childName == 'link' && $child->hasAttribute('rel') && $child->getAttribute('rel') == 'stylesheet') {
$list->add($child);
}
}
Expand All @@ -647,7 +670,7 @@ public function getJSNodes() : LinkedList {
$child = $children->get($x);
$childName = $child->getNodeName();

if ($childName == 'script' && $child->hasAttribute('type') && $child->getAttributeValue('type') == 'text/javascript') {
if ($childName == 'script' && $child->hasAttribute('type') && $child->getAttribute('type') == 'text/javascript') {
$list->add($child);
}
}
Expand All @@ -672,22 +695,27 @@ public function getLinkNodes() : LinkedList {
* tag. Note that if the meta node that you would like to get is
* the tag which has the attribute 'charset', then the passed attribute
* must have the value 'charset'.
* Note that this value is case insensitive.
*
* @param bool $httpEquvi If set to true, the search will be based on the
* value of the attribute 'http-equiv' instead of 'name'.
*
* @return HTMLNode|null If a meta tag which has the given name was found,
* It will be returned. If no meta node was found, null is returned.
*
* @since 1.1.2
*/
public function getMeta(string $name) {
public function getMeta(string $name, bool $httpEquvi = false) {
$lName = strtolower(trim($name));

$attribute = $httpEquvi ? 'http-equiv' : 'name';

if ($lName == 'charset') {
return $this->getCharsetNode();
} else {
for ($x = 0 ; $x < $this->childrenCount() ; $x++) {
$node = $this->children()->get($x);

if ($node->getNodeName() == 'meta' && $node->getAttributeValue('name') == $name) {
if ($node->getNodeName() == 'meta' && $node->getAttribute($attribute) == $lName) {
return $node;
}
}
Expand Down Expand Up @@ -822,6 +850,8 @@ public function hasJs(string $src) : bool {
* @param string $name The value of the attribute 'name' of the meta
* tag. If the developer would like to check for the existence of the
* node which has the attribute 'charset', he can pass the value 'charset'.
* Also, this can be the value of the attribute 'http-equiv' of the meta tag.
* Note that this value is case insensitive.
*
* @return bool If a meta tag which has the given name was found,
* true is returned. false otherwise.
Expand All @@ -837,7 +867,7 @@ public function hasMeta(string $name) : bool {
for ($x = 0 ; $x < $this->childrenCount() ; $x++) {
$node = $this->children()->get($x);

if ($node->getNodeName() == 'meta' && $node->getAttributeValue('name') == $name) {
if ($node->getNodeName() == 'meta' && ($node->getAttribute('name') == $lName || $node->getAttribute('http-equiv') == $lName)) {
return true;
}
}
Expand Down Expand Up @@ -1028,7 +1058,7 @@ private function addChildHelper(HTMLNode $node) {
}
}

if (!$this->hasMeta($node->getAttributeValue('name'))) {
if (!$this->hasMeta($node->getAttribute('name'))) {
parent::addChild($node);
}
} else {
Expand Down

0 comments on commit c8d4db1

Please sign in to comment.