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

Add support for pseudo class :has #1

Open
wants to merge 1 commit into
base: pseudo-class-is
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions dist/dropcss.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ function nth(a, b, pos) {
return pos <= b && pos % a === bMod;
}

const pseudoClasses = /not|is/;
const pseudoClasses = /not|is|has/;

// assumes stripPseudos(sel); has already been called
function parse(sel) {
Expand Down Expand Up @@ -712,6 +712,11 @@ function find(m, ctx) {
case 'is':
res = find(val, {node: ctx.node, idx: val.length - 1});
break;
case 'has':
res = ctx.node.childNodes.some(
(node) => find(val, { node, idx: val.length - 1})
);
break;
case 'first-child':
res = tidx == 0;
break;
Expand Down Expand Up @@ -965,7 +970,7 @@ function postProc(out, shouldDrop, log, START) {

const ATTRIBUTES = /\[([\w-]+)(?:(.?=)"?([^\]]*?)"?)?\]/i;

const pseudoAssertable = /:(?:first|last|nth|only|not|is)\b/;
const pseudoAssertable = /:(?:first|last|nth|only|not|is|has)\b/;

const pseudoNonAssertableParenth = /:(?:lang)\([^)]*\)/g;

Expand Down
9 changes: 7 additions & 2 deletions dist/dropcss.iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ var dropcss = (function () {
return pos <= b && pos % a === bMod;
}

const pseudoClasses = /not|is/;
const pseudoClasses = /not|is|has/;

// assumes stripPseudos(sel); has already been called
function parse(sel) {
Expand Down Expand Up @@ -713,6 +713,11 @@ var dropcss = (function () {
case 'is':
res = find(val, {node: ctx.node, idx: val.length - 1});
break;
case 'has':
res = ctx.node.childNodes.some(
(node) => find(val, { node, idx: val.length - 1})
);
break;
case 'first-child':
res = tidx == 0;
break;
Expand Down Expand Up @@ -966,7 +971,7 @@ var dropcss = (function () {

const ATTRIBUTES = /\[([\w-]+)(?:(.?=)"?([^\]]*?)"?)?\]/i;

const pseudoAssertable = /:(?:first|last|nth|only|not|is)\b/;
const pseudoAssertable = /:(?:first|last|nth|only|not|is|has)\b/;

const pseudoNonAssertableParenth = /:(?:lang)\([^)]*\)/g;

Expand Down
2 changes: 1 addition & 1 deletion dist/dropcss.iife.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/dropcss.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { LOGGING } from './env';

const ATTRIBUTES = /\[([\w-]+)(?:(.?=)"?([^\]]*?)"?)?\]/i;

const pseudoAssertable = /:(?:first|last|nth|only|not|is)\b/;
const pseudoAssertable = /:(?:first|last|nth|only|not|is|has)\b/;

const pseudoNonAssertableParenth = /:(?:lang)\([^)]*\)/g;

Expand Down
5 changes: 5 additions & 0 deletions src/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ function find(m, ctx) {
case 'is':
res = find(val, {node: ctx.node, idx: val.length - 1});
break;
case 'has':
res = ctx.node.childNodes.some(
(node) => find(val, { node, idx: val.length - 1})
);
break;
case 'first-child':
res = tidx == 0;
break;
Expand Down
2 changes: 1 addition & 1 deletion src/sel.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { takeUntilMatchedClosing } from './css';
import { parseErr } from './err';

const pseudoClasses = /not|is/
const pseudoClasses = /not|is|has/

// assumes stripPseudos(sel); has already been called
export function parse(sel) {
Expand Down
147 changes: 146 additions & 1 deletion test/src/2-context-aware-unary-sel.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,149 @@ describe('Context-aware, unary selector', () => {
assert.equal(out, '');
});
});
});

describe(':has(<tag>)', () => {
it('should retain present', function() {
let {css: out} = dropcss({
html: '<div><span></span></div>',
css: ':has(span) {a:b;}',
});
assert.equal(out, ':has(span){a:b;}')
});

it('should drop absent', function() {
let {css: out} = dropcss({
html: '<div></div>',
css: ':has(span) {a:b;}',
});
assert.equal(out, '');;
});
});

describe(':has(#id)', () => {
it('should retain present', function() {
let {css: out} = dropcss({
html: '<div><div id="a"></div></div>',
css: ':has(#a) {a:b;}',
});
assert.equal(out, ':has(#a){a:b;}');
});

it('should drop absent', function() {
let {css: out} = dropcss({
html: '<div><div id="b"></div></div>',
css: ':has(#a) {a:b;}',
});
assert.equal(out, '');
});
});

describe(':has(.class)', () => {
it('should retain present', function() {
let {css: out} = dropcss({
html: '<div><div class="a"></div></div>',
css: ':has(.a) {a:b;}',
});
assert.equal(out, ':has(.a){a:b;}');
});

it('should drop absent', function() {
let {css: out} = dropcss({
html: '<div><div class="b"></div></div>',
css: ':has(.a) {a:b;}',
});
assert.equal(out, '');
});
});

describe(':has([attr])', () => {
it('should retain present', function() {
let {css: out} = dropcss({
html: '<div><div foo></div></div>',
css: ':has([foo]) {a:b;}',
});
assert.equal(out, ':has([foo]){a:b;}');
});

it('should drop absent', function() {
let {css: out} = dropcss({
html: '<div><div bar></div></div>',
css: ':has([foo]) {a:b;}',
});
assert.equal(out, '');
});
});

// todo: test [foo="val"], [foo='val']
describe(':has([attr=value])', () => {
it('should retain present', function() {
let {css: out} = dropcss({
html: '<div><div foo="bar"></div></div>',
css: ':has([foo=bar]) {a:b;}',
});
assert.equal(out, ':has([foo=bar]){a:b;}');
});

it('should drop absent', function() {
let {css: out} = dropcss({
html: '<div><div foo="cow"></div></div>',
css: ':has([foo=bar]) {a:b;}',
});
assert.equal(out, '');
});
});

describe(':has([attr*=value])', () => {
it('should retain present', function() {
let {css: out} = dropcss({
html: '<div><div foo="bar"></div></div>',
css: ':has([foo*=a]) {a:b;}',
});
assert.equal(out, ':has([foo*=a]){a:b;}');
});

it('should drop absent', function() {
let {css: out} = dropcss({
html: '<div><div foo="bar"></div></div>',
css: ':has([foo*=c]) {a:b;}',
});
assert.equal(out, '');
});
});

describe(':has([attr^=value])', () => {
it('should retain present', function() {
let {css: out} = dropcss({
html: '<div><div foo="bar"></div></div>',
css: ':has([foo^=b]) {a:b;}',
});
assert.equal(out, ':has([foo^=b]){a:b;}');
});

it('should drop absent', function() {
let {css: out} = dropcss({
html: '<div><div foo="bar"></div></div>',
css: ':has([foo^=c]) {a:b;}',
});
assert.equal(out, '');
});
});

describe(':has([attr$=value])', () => {
it('should retain present', function() {
let {css: out} = dropcss({
html: '<div><div foo="bar"></div></div>',
css: ':has([foo$=r]) {a:b;}',
});
assert.equal(out, ':has([foo$=r]){a:b;}');
});

it('should drop absent', function() {
let {css: out} = dropcss({
html: '<div><div foo="bar"></div></div>',
css: ':has([foo$=z]) {a:b;}',
});
assert.equal(out, '');
});
});
});