GithubHelp home page GithubHelp logo

Comments (3)

oscarotero avatar oscarotero commented on June 2, 2024

The problem is undefined is a valid value for the href attribute, this is an issue in the website side.

A solution would be assume the "undefined" value as an empty value, but this opens the door to other values that must be considered empty like "null", "nil", etc. I don't know if make such assumptions is a good idea.

from embed.

grandeljay avatar grandeljay commented on June 2, 2024

Hmmm... Are the values null, nil etc also valid? My assumption is that there was some issue with JavaScript and the value has undefined which surely isn't intentional. I mean, why would anybody want undefined, etc here? I think it would be best if you handle it.

In this case, this small change should fix the issue.

From:

if (!$language || !$href) {

To:

if (!$language || !$href || 'undefined' === $href) {

Full code:

<?php

declare(strict_types=1);

namespace Embed\Detectors;

class Languages extends Detector
{
    /**
     * @return \Psr\Http\Message\UriInterface[]
     */
    public function detect(): array
    {
        $document  = $this->extractor->getDocument();
        $languages = array();

        foreach ($document->select('.//link[@hreflang]')->nodes() as $node) {
            $language = $node->getAttribute('hreflang');
            $href     = $node->getAttribute('href');

            if (!$language || !$href || 'undefined' === $href) {
                continue;
            }

            $languages[$language] = $this->extractor->resolveUri($href);
        }

        return $languages;
    }
}

Alternatively is there something I can do to handle this case? I really don't know what the best solution would be here.

from embed.

oscarotero avatar oscarotero commented on June 2, 2024

In this case, I'd create a helper, so it can be used in other places. Something like:

function isEmpty($value) {
    return empty($value) || $value === "undefined";
}

If you want to work on a PR, I'll review and merge it.

from embed.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.