Skip to content

Commit

Permalink
Merge pull request #12 from posthtml/feat/esm
Browse files Browse the repository at this point in the history
  • Loading branch information
cossssmin authored Feb 28, 2024
2 parents 505afa1 + f89832b commit 03fb958
Show file tree
Hide file tree
Showing 16 changed files with 12,064 additions and 2,828 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
charset = utf-8
indent_size = 2
indent_style = space
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
23 changes: 23 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2023
},
"plugins": ["@typescript-eslint"],
"rules": {
"indent": [2, 2, {"SwitchCase": 1}],
"quotes": [2, "double"],
"linebreak-style": [2, "unix"],
"camelcase": [2, {"properties": "always"}],
"brace-style": [2, "1tbs", {"allowSingleLine": true}]
},
"env": {
"es6": true,
"node": true,
"browser": false
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
]
}
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: npm
directory: "/"
schedule:
interval: weekly
28 changes: 28 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: build

on:
push:
branches: [master]
pull_request:

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18, 20]

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm test
env:
CI: true
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
coverage
*.log
2 changes: 0 additions & 2 deletions .npmignore

This file was deleted.

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License (MIT)

Copyright (c) Rasmus Fløe

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
84 changes: 84 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<div align="center">
<img width="150" height="150" title="PostHTML" src="https://posthtml.github.io/posthtml/logo.svg">
<h1>posthtml-match-helper</h1>

Expand CSS selectors into PostHTML matcher objects

[![Version][npm-version-shield]][npm]
[![Build][github-ci-shield]][github-ci]
[![License][license-shield]][license]
[![Downloads][npm-stats-shield]][npm-stats]
</div>

## Introduction

This PostHTML plugin can turn simple CSS selectors into [matcher objects](https://github.com/posthtml/posthtml/blob/master/README.md#match).

Supported features:

* Tags: `"div"` returns `{tag: "div"}`.
* Ids: `"#bar"` returns `{attrs: {id: "bar"}}`.
* Classes: `.foo` returns `{attrs: { class: /(?:^|\s)foo(?:\\s|$)/ }}`. Any number of classnames supported.
* Attribute selectors: any number of standard [attribute selectors](https://developer.mozilla.org/en/docs/Web/CSS/Attribute_selectors) can be used<sup><a href="#attribute_selectors_footnote">1</a></sup> including the following non-standard:
* `[attr!=value]`: matches attributes with values that do not contain `value`.
* Multiple node selectors: `"div, span"` returns `[{tag: "div"}, {tag: "span"}]`.

**<sup><a name="attribute_selectors_footnote">1</a></sup>** Multiple attribute selectors for the same attribute are not supported (this includes mixing classnames and attribute selectors matching `class`).

The basic template for selectors (and order of features) looks like this:

```js
"tag#id.class.name[attr*=value][otherattr^='start']"
```

## Basic usage

```js
import matchHelper from "posthtml-match-helper";

tree.match(matchHelper("div.class"), function (node) {
// do stuff with matched node...
});
```

## Advanced usage

```js
import matchHelper from "posthtml-match-helper";

tree.match(matchHelper("input.my-control[type!='radio'][checked], input[value^='foo'][checked]"), function (node) {
// do stuff with node that matched either of the selectors...
});
```

## Classnames with escaped characters

If you need to match nodes with classnames that use escaped characters, like those in Tailwind CSS utilities with arbitrary values, use the following syntax:

```js
import matchHelper from "posthtml-match-helper";

tree.match(matchHelper("input.\\[display:none\\]"), function (node) {
// do stuff with node that matched either of the selectors...
});
```


## The helper function

#### Arguments

* `matcher` (string) - A CSS selector that describes the node you want to match in PostHTML.

#### Returns

A matcher object or an array of matcher objects.

[npm]: https://www.npmjs.com/package/posthtml-match-helper
[npm-version-shield]: https://img.shields.io/npm/v/posthtml-match-helper.svg
[npm-stats]: http://npm-stat.com/charts.html?package=posthtml-match-helper
[npm-stats-shield]: https://img.shields.io/npm/dt/posthtml-match-helper.svg
[github-ci]: https://github.com/posthtml/posthtml-match-helper/actions/workflows/nodejs.yml
[github-ci-shield]: https://github.com/posthtml/posthtml-match-helper/actions/workflows/nodejs.yml/badge.svg
[license]: ./LICENSE
[license-shield]: https://img.shields.io/npm/l/posthtml-match-helper.svg
156 changes: 0 additions & 156 deletions index.js

This file was deleted.

File renamed without changes.
Loading

0 comments on commit 03fb958

Please sign in to comment.