Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attributes are duplicated multiple nodes #23

Closed
soukicz opened this issue Apr 20, 2015 · 3 comments · Fixed by #24
Closed

Attributes are duplicated multiple nodes #23

soukicz opened this issue Apr 20, 2015 · 3 comments · Fixed by #24

Comments

@soukicz
Copy link
Contributor

soukicz commented Apr 20, 2015

There is very specific case when attributes from two nodes are merged together. Interesting is that if you remove div with class xxx, it will start working again.

My code:

$twigLoader = new Goetas\Twital\TwitalLoader(new \Twig_Loader_String()); //new \TwitalLoader(new \Twig_Loader_String());
$twigLoader->addSourceAdapter('/.*/', new Goetas\Twital\SourceAdapter\HTML5Adapter());
$twig = new Twig_Environment($twigLoader, ['cache' => false, 'autoescape' => true]);

$source = <<<EOT
                   <t:omit> <ol style="opc">
            <li t:attr="true ? style='display:none'"></li>
        </ol>
        <div class="xxx"> <span class="abc">yyy</span></div>

        <div class='qqq'>
            <input type='checkbox' t:attr="true ? checked='checked'">

        </div>
        </t:omit>
EOT;

print_r($twig->loadTemplate($source)->render([]));

Expected result:

<ol style="opc">
            <li style="display:none"></li>
        </ol>
        <div class="xxx"> <span class="abc">yyy</span></div>

        <div class="qqq">
            <input type="checkbox" checked="checked">

        </div>

Actual result:

<ol style="opc">
            <li style="display:none"></li>
        </ol>
        <div class="xxx"> <span class="abc">yyy</span></div>

        <div class="qqq">
            <input type="checkbox" style="display:none" checked="checked">

        </div>

Both LI and INPUT have display:none but it was defined only for LI.

I looked into php cache of template file a there is the same key used for both nodes (__a9000000006d37f06100007f887925bc9c). My guess is that problem is in spl_object_hash which is used for keys:
When an object is destroyed, its hash may be reused for other objects.
https://php.net/spl_object_hash

@soukicz
Copy link
Contributor Author

soukicz commented Apr 21, 2015

I have updated issue with actual code to reproduce problem. This is the minimal code I managed to get - originaly it was 1000 lines and these are the minimum which are still producing wrong output.

@soukicz
Copy link
Contributor Author

soukicz commented Apr 22, 2015

I am using this fix but I am not sure if it is the best aproach:

// Goetas\Twital\Attribute\AttrAttribute.php
public static function getVarname(\DOMNode $node) {
     if(!isset($node->uniqueId)) {
         $node->uniqueId = '__a9' . md5(uniqid('', true) . spl_object_hash($node));
     }
     return $node->uniqueId;
}

@soukicz soukicz changed the title Attribute hash is not unique Attributes are duplicated multiple nodes Apr 22, 2015
@goetas goetas closed this as completed in 7f34b8a Apr 22, 2015
@goetas goetas reopened this Apr 25, 2015
@goetas
Copy link
Owner

goetas commented Apr 25, 2015

Reopening the issue because of 7f34b8a#commitcomment-10900689

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

Successfully merging a pull request may close this issue.

2 participants