# cascading-properties

> Define virtual properties for HTML elements using CSS selectors.

Latest version **0.1.0** (published 2015-04-28) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install cascading-properties
pnpm add cascading-properties
yarn add cascading-properties
bun add cascading-properties
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2015-04-28 |
| First published | 2015-04-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Raphael Saunier |
| Maintainers | raphaelsaunier |
| Keywords | cascading, properties, css, selectors, matchesselector |

## Links

- npm: https://www.npmjs.com/package/cascading-properties
- Repository: https://github.com/raphaelsaunier/cascading-properties
- Issues: https://github.com/raphaelsaunier/cascading-properties/issues
- npm.io page: https://npm.io/package/cascading-properties

## Dependencies (3)

- [specimen](https://npm.io/package/specimen.md) ^0.2.0
- [component-closest](https://npm.io/package/component-closest.md) ^0.1.4
- [component-matches-selector](https://npm.io/package/component-matches-selector.md) ^0.1.5

## Alternatives

- [style-dictionary](https://npm.io/package/style-dictionary.md) — 2.0M weekly downloads
- [postcss-merge-idents](https://npm.io/package/postcss-merge-idents.md) — 1.7M weekly downloads
- [@fontsource/noto-sans](https://npm.io/package/@fontsource/noto-sans.md) — 93.0K weekly downloads
- [uglifycss](https://npm.io/package/uglifycss.md) — 71.6K weekly downloads
- [mat4-interpolate](https://npm.io/package/mat4-interpolate.md) — 23.3K weekly downloads

## Recent versions

- 0.1.0 (latest) — 2015-04-28
- 0.0.1 — 2015-04-20

## README

# Cascading Properties [![Build Status](https://travis-ci.org/raphaelsaunier/cascading-properties.svg)](https://travis-ci.org/raphaelsaunier/cascading-properties)

Define virtual properties for HTML elements using CSS selectors.

## Why?

We needed a way to attach properties to HTML elements in arbitrary documents, without altering the original markup and polluting it with `data-*` attributes. CSS selectors emerged as a natural fit for this.

However, [CSS Variables](http://dev.w3.org/csswg/css-variables/) aren’t available in all browsers yet, and proposals like [Cascading Attribute Sheets](https://lists.w3.org/Archives/Public/public-webapps/2012JulSep/0508.html) and its [implementations](https://github.com/search?utf8=%E2%9C%93&q=cascading+attribute+sheets) are primarily intended for HTML attributes and therefore mutate the DOM. So we had to come up with our own solution.

## How?

The `CascadingPropertySet` instance simply expects some property definitions along with a set of rules:

```js
var properties = new CascadingPropertySet();

// Define the properties with default values or those which are inherited
// (any properties with no default value or inheritance do not need to be explicitly defined)
properties.addProperties({
  someProperty: {
    defaultValue: 0
  },
  someOtherProperty: {
    inherited: true
  }
});

// Define the rules using a SASS-like syntax:
properties.addRules({
  'ul':{
    someOtherProperty: true,
    yetAnotherProperty: 2,
    'li': {
      someProperty: 1,
    }
  },
  'li:last-child': {
    someProperty: 3
  }
});
```

We can then retrieve the values by passing the reference to the element and the name of the property
to the `getValue` method. The property value with the highest selector specificity will be returned:

```js
// Turn a test HTML string into DOM elements using https://github.com/component/domify
var ul = domify('<ul><li>Hello</li><li>World</li></ul>');

properties.getValue(ul,             'someProperty')       // => 0
properties.getValue(ul.firstChild,  'someProperty')       // => 1
properties.getValue(ul,             'yetAnotherProperty') // => 2
properties.getValue(ul.lastChild,   'someProperty')       // => 3
properties.getValue(ul.firstChild,  'someOtherProperty')  // => true
properties.getValue(ul,             'undefinedProperty')  // => null
```

---
_Source: https://npm.io/package/cascading-properties · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
