/** * NULL record values should be pulled as `none`. */ public function testNullRecordValues() { $exhibit = $this->_exhibit(); $exhibit->styles = "\n .all {\n point-image: url;\n }\n "; $record = new NeatlineRecord($exhibit); $record->point_image = null; $exhibit->pullStyles($record); $this->assertEquals(array('all' => array('point_image' => 'none')), nl_readCSS($exhibit->styles)); }
/** * When data is saved to an existing record, rules from the `all` selector * should _not_ be pulled to the record before it is used to update the * exhibit CSS. Otherwise, existing values on the exhibit stylesheet would * always clobber new values from the form, making it impossible to change * any of the fields controlled by `all`. */ public function testNotPullAllTagWhenSaved() { $exhibit = $this->_exhibit(); $exhibit->styles = "\n .all {\n fill-color: 1;\n }\n "; $exhibit->save(); // Save data to existing record. $record = $this->_record($exhibit); $record->saveForm(array('fill_color' => '2')); $exhibit = $this->_reload($exhibit); // Should not pull CSS value. $this->assertEquals('2', $record->fill_color); // CSS should be changed. $this->assertEquals(array('all' => array('fill_color' => '2')), nl_readCSS($exhibit->styles)); }
/** * Selectors with underscores should be extracted. * @regression */ public function testParseSelectorsWithUnderscores() { $this->assertEquals(array('tag_with_underscores' => array('prop' => 'val')), nl_readCSS("\n .tag_with_underscores {\n prop: val;\n }\n ")); }
/** * Update record styles to match exhibit CSS. For example, if `styles` on * the parent exhibit is: * * .tag1 { * fill-color: #111111; * } * .tag2 { * stroke-color: #222222; * } * * And `array('tag1', 'tag2')` is passed, `fill_color` should be set to * '#111111' and `stroke_color` to '#222222'. * * @param array $tags An array of tags to pull. */ public function pullStyles($tags) { // If the record is new, pull the `all` tag. if (!$this->exists()) { $tags = array_merge($tags, array('all')); } // Parse the stylesheet. $css = nl_readCSS($this->getExhibit()->styles); // Gather style columns. $valid = nl_getStyles(); // Walk CSS rules. foreach ($css as $selector => $rules) { // If the tag is being pulled. if (in_array($selector, $tags)) { // Walk valid CSS rules. foreach ($rules as $prop => $val) { // Is the property valid? if (in_array($prop, $valid)) { // Set value if not `auto` or `none`. if ($val != 'auto' && $val != 'none') { $this->{$prop} = $val; } else { if ($val == 'none') { $this->{$prop} = null; } } } } } } }
/** * Update the exhibit stylesheet with values from a specific record. For * example, if `styles` is: * * .tag { * fill-color: #111111; * stroke-color: #222222; * } * * And the record is tagged with `tag` has a `fill_color` of `#333333` and * a `stroke_color` of `#444444`, the stylesheet would be updated to: * * .tag { * fill-color: #333333; * stroke-color: #444444; * } * * @param NeatlineRecord $record The record to update from. */ public function pullStyles($record) { // Parse the stylesheet. $css = nl_readCSS($this->styles); // Explode record tags. $tags = nl_explode($record->tags); // Gather style columns. $valid = nl_getStyles(); foreach ($css as $selector => $rules) { // Is the record tagged with the selector? if (in_array($selector, $tags) || $selector == 'all') { // Scan valid rule definitions. foreach ($rules as $prop => $val) { // Is the property valid? if (in_array($prop, $valid)) { // Get the record value. $value = !is_null($record->{$prop}) ? $record->{$prop} : 'none'; // Update the CSS. $css[$selector][$prop] = $value; } } } } // Recompile the stylesheet. $this->styles = nl_writeCSS($css); }