Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

addAttributes method is confusing in Widget class #7095

Closed
Serhii-DV opened this issue Jun 12, 2014 · 14 comments
Closed

addAttributes method is confusing in Widget class #7095

Serhii-DV opened this issue Jun 12, 2014 · 14 comments
Labels
Milestone

Comments

@Serhii-DV
Copy link
Contributor

Widget class has two methods for adding attributes: addAttribute and addAttributes.

addAttribute method is working fine and it allows to add custom attributes to widget.

$widget->addAttribute('data-var1', 1);
$widget->addAttribute('data-var2', 2);

and it will generate widget with this attributes:

<select ... data-var1="1" data-var2="2">

But, addAttributes doesn't work as expected.

$widget->addAttributes(array(
   'data-var1' => 1,
   'data-var2' => 2
));

In this case no attributes will be generated

<select ...>

The problem is in that addAttributes methods invokes __set magick method and add unknowns attributes to $arrConfiguration value. addAttribute method directly adds attributes to protected $arrAttributes value. And getAttributes method takes attributes from $arrAttributes value not $arrConfiguration.

@leofeyer
Copy link
Member

What would you suggest?

@Serhii-DV
Copy link
Contributor Author

I think we could safely use addAttribute method for each array element in addAttributes and don't think about backward campability. Until now no one has pointed on this problem, so no one have used addAttributes method.

@leofeyer
Copy link
Member

and don't think about backward campability

This can never be the solution!

@Serhii-DV
Copy link
Contributor Author

Another solution. Add new method addConfigurations (in the same way as addAttributes) and use it in Widget::__constructor and Widget::parse methods instead of addAttributes.

And in addAttributes method check Contao version, something like that:

    public function addConfigurations($arrConfigurations)
    {
        if (!is_array($arrConfigurations))
        {
            return;
        }

        foreach ($arrConfigurations as $k=>$v)
        {
            $this->$k = $v;
        }
    }


    public function addAttributes($arrAttributes)
    {
        if (!is_array($arrAttributes))
        {
            return;
        }

        if (version_compare(VERSION, '3.3', '<') && version_compare(BUILD, '3', '<'))
        {
            $this->addConfigurations($arrAttributes);
        }
        else
        {
            foreach ($arrAttributes as $k=>$v)
            {
                $this->addAttribute($k, $v);
            }
        }

    }

@leofeyer
Copy link
Member

The best solution IMHO is to adjust the addAttributes() method so it correctly identifies the attributes. This is already done by whitelisting all known attributes in the setter method, so we should just add a routine which adds everything starting with data- as attribute instead of as configuration value there.

@leofeyer leofeyer added this to the 3.2.12 milestone Jun 13, 2014
@Serhii-DV
Copy link
Contributor Author

But HTML 5 specification allows to add attribute with any name not only with data- prefix. I can, for example, add myvar="12321" attribute and it will be HTML 5 compatible.

@Serhii-DV
Copy link
Contributor Author

For example, AngularJS uses custom HTML attributes like ng-model, ng-submit and etc.

@leofeyer leofeyer removed this from the 3.2.12 milestone Jun 13, 2014
@leofeyer
Copy link
Member

Then we need to discuss this. @contao/developers

@ausi
Copy link
Member

ausi commented Jun 13, 2014

<select myvar="12321"></select> is not valid HTML5. Custom attributes have to start with data-, see: w3.org/html/wg/drafts/html/master/dom.html.

@fjacobi
Copy link

fjacobi commented Jun 13, 2014

Yep, AngularJS is not valid by default - you have to use a directive to validate it.

@psi-4ward
Copy link
Contributor

Whats the problem with invalid attributes? Imho are there no side effects and are heavily used by various JS widgets like Angular or Datepickers. For me we sould accept every attribute a developer pushes in!

@fjacobi
Copy link

fjacobi commented Jun 17, 2014

I don't have any problems with invalid attributes.
I think every developer using this function should know if he wants validity or not - whitelisting is not a real option imho.

@leofeyer
Copy link
Member

Please read the ticket – you are missing the point.

@leofeyer
Copy link
Member

leofeyer commented Sep 5, 2014

Changed in b6f5d08. I have added support for data- attributes as well as ng- attributes (although the latter is not valid HTML5).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants