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

[Feature Request] More :data[condition] selectors? #197

Open
cxj opened this issue May 15, 2018 · 6 comments
Open

[Feature Request] More :data[condition] selectors? #197

cxj opened this issue May 15, 2018 · 6 comments

Comments

@cxj
Copy link
Contributor

cxj commented May 15, 2018

Would it be possible and desirable to add a few more conditional operators to the :data[condition] selectors, possibly modeling the syntax on other CSS attribute selectors? For example, I would find the following operations very helpful (chosen syntax is completely arbitrary and repurposed from CSS):

  • Check whether data array is an empty array:
div:data[myArray*=""] {display: none;}
  • Check whether data array contains an element named key1:
div:data[myArray^="key1"] {display: none;}
@TRPB
Copy link
Member

TRPB commented May 15, 2018

You can use div:data["x" in myArray] which does in_array('x', $myArray) or div:data[myArray!=""] {display: none;} to determine whether and array is empty.

@cxj
Copy link
Contributor Author

cxj commented May 15, 2018

Is there some way to check for a key in the array? in_array() works on values only.

@TRPB
Copy link
Member

TRPB commented May 15, 2018

div:data[myArray.key1] should work, although I haven't tested it.

edit: Though that will be the same as !empty($array['key1']) rather than array_key_exists.

@cxj
Copy link
Contributor Author

cxj commented May 16, 2018

This check for an empty array doesn't seem to work:

<?php
require "vendor/autoload.php";

$xml = '<div>This is the foo message.</div>';
$tss = 'div:data[foo!=""] { content: "condition true"; }';

$tpl = new Transphporm\Builder($xml, $tss);

$data['foo'] = [];

echo $tpl->output($data)->body . PHP_EOL;

I expected the output to be <div>This is the foo message.</div> because array foo is empty, i.e. foo="". But my output is <div>condition true</div>.

@garrettw
Copy link
Contributor

garrettw commented May 16, 2018

But it is true that empty array !== empty string.

@TRPB
Copy link
Member

TRPB commented May 16, 2018

Transphporm uses != internally instead of !==

@cxj cxj changed the title More :data[condition] selectors? (enhancement request) [Feature Request] More :data[condition] selectors? Sep 20, 2018
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

3 participants