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

Removed empty attribute #28

Closed
soukicz opened this issue May 7, 2015 · 3 comments
Closed

Removed empty attribute #28

soukicz opened this issue May 7, 2015 · 3 comments

Comments

@soukicz
Copy link
Contributor

soukicz commented May 7, 2015

Twital is removing empty attributes which is fine for most elements except OPTION. IE9 will use option text as value if there is missing value attribute.

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

$source = <<<EOT
<select name="my">
<option value="">XXX</option>
<option value="1">first</option>
</select>
EOT;

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

Expected output:

<select name="my">
<option value="">XXX</option>
<option value="1">first</option>
</select>
<!--  IE9 will send my=  (empty value) -->

Actual output:

<select name="my">
<option value>XXX</option>
<option value="1">first</option>
</select>
<!--  IE9 will send my=XXX -->
@soukicz
Copy link
Contributor Author

soukicz commented May 7, 2015

I have created this quick patch but i don't like very much that placeholder:

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Goetas\Twital\EventDispatcher\SourceEvent;
use Goetas\Twital\EventDispatcher\TemplateEvent;

class CompatibilitySubscriber implements EventSubscriberInterface {
    const PLACEHOLDER = 'placeholderXXX###===';

    public static function getSubscribedEvents() {
        return [
            'compiler.pre_dump' => [
                ['valueAddPlaceholder']
            ],
            'compiler.post_dump' => [
                ['valueRemovePlaceholder']
            ],
        ];
    }

    public function valueAddPlaceholder(TemplateEvent $event) {
        $doc = $event->getTemplate()->getDocument();

        $xp = new \DOMXPath($doc);
        $xp->registerNamespace('xh', 'http://www.w3.org/1999/xhtml');

        /**
         * @var $option \DOMElement
         */
        foreach ($xp->query("//xh:option[@value='']") as $option) {
            $option->setAttribute('value', self::PLACEHOLDER);
        }
    }

    public function valueRemovePlaceholder(SourceEvent $event) {
        $event->setTemplate(str_replace(
                ' value="' . self::PLACEHOLDER . '"',
                ' value=""',
                $event->getTemplate())
        );
    }
}

@goetas
Copy link
Owner

goetas commented May 7, 2015

It should be an https://github.com/Masterminds/html5-php issue.. i will check it

@goetas
Copy link
Owner

goetas commented May 9, 2015

See Masterminds/html5-php#84, it will be fixed soon

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

No branches or pull requests

2 participants