Skip to content
Closed
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
22 changes: 1 addition & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,10 @@
"prepare": "npm run build",
"release": "npx npmpub"
},
"dependencies": {
"cssesc": "^3.0.0",
"util-deprecate": "^1.0.2"
},
"dependencies": {},
"license": "MIT",
"engines": {
"node": ">=4"
"node": ">=6"
},
"homepage": "https://github.com/postcss/postcss-selector-parser",
"contributors": [
Expand Down
20 changes: 18 additions & 2 deletions src/selectors/attribute.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
import cssesc from "cssesc";
import cssesc from "../util/cssesc";
import unesc from "../util/unesc";
import Namespace from "./namespace";
import { ATTRIBUTE } from "./types";

const deprecate = require("util-deprecate");
function deprecate(fn, msg) {
let warned = false;
return function () {
if (!warned) {
warned = true;
// Match util-deprecate: emit a process warning on Node (so --no-deprecation,
// --throw-deprecation and process.on("warning") keep working), and fall back
// to console.warn in browsers.
if (typeof process !== "undefined" && process.emitWarning) {
process.emitWarning(msg, "DeprecationWarning");
} else if (typeof console !== "undefined" && console.warn) {
console.warn(msg);
}
}
return fn.apply(this, arguments);
};
}

const WRAPPED_IN_QUOTES = /^('|")([^]*)\1$/;

Expand Down
2 changes: 1 addition & 1 deletion src/selectors/className.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cssesc from "cssesc";
import cssesc from "../util/cssesc";
import { ensureObject } from "../util";
import Node from "./node";
import { CLASS } from "./types";
Expand Down
62 changes: 13 additions & 49 deletions src/selectors/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,76 +223,40 @@ export default class Container extends Node {
});
}

walkAttributes(callback) {
_walkType(type, callback) {
return this.walk((selector) => {
if (selector.type === types.ATTRIBUTE) {
if (selector.type === type) {
return callback.call(this, selector);
}
});
}

walkAttributes(callback) {
return this._walkType(types.ATTRIBUTE, callback);
}
walkClasses(callback) {
return this.walk((selector) => {
if (selector.type === types.CLASS) {
return callback.call(this, selector);
}
});
return this._walkType(types.CLASS, callback);
}

walkCombinators(callback) {
return this.walk((selector) => {
if (selector.type === types.COMBINATOR) {
return callback.call(this, selector);
}
});
return this._walkType(types.COMBINATOR, callback);
}

walkComments(callback) {
return this.walk((selector) => {
if (selector.type === types.COMMENT) {
return callback.call(this, selector);
}
});
return this._walkType(types.COMMENT, callback);
}

walkIds(callback) {
return this.walk((selector) => {
if (selector.type === types.ID) {
return callback.call(this, selector);
}
});
return this._walkType(types.ID, callback);
}

walkNesting(callback) {
return this.walk((selector) => {
if (selector.type === types.NESTING) {
return callback.call(this, selector);
}
});
return this._walkType(types.NESTING, callback);
}

walkPseudos(callback) {
return this.walk((selector) => {
if (selector.type === types.PSEUDO) {
return callback.call(this, selector);
}
});
return this._walkType(types.PSEUDO, callback);
}

walkTags(callback) {
return this.walk((selector) => {
if (selector.type === types.TAG) {
return callback.call(this, selector);
}
});
return this._walkType(types.TAG, callback);
}

walkUniversals(callback) {
return this.walk((selector) => {
if (selector.type === types.UNIVERSAL) {
return callback.call(this, selector);
}
});
return this._walkType(types.UNIVERSAL, callback);
}

split(callback) {
Expand Down
2 changes: 1 addition & 1 deletion src/selectors/namespace.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cssesc from "cssesc";
import cssesc from "../util/cssesc";
import { ensureObject } from "../util";
import Node from "./node";

Expand Down
64 changes: 64 additions & 0 deletions src/util/cssesc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*! https://mths.be/cssesc v3.0.0 by @mathias | MIT License */
// Inlined and trimmed to the subset of options postcss-selector-parser uses
// (`isIdentifier`, `quotes: "single" | "double"`, `wrap`). Output is identical
// to cssesc@3 for those options.
const regexSingleEscape = /[ -,./:-@[\]^`{-~]/;
const regexExcessiveSpaces = /(^|\\+)?(\\[A-F0-9]{1,6})\x20(?![a-fA-F0-9\x20])/g;

export default function cssesc(string, options = {}) {
let quotes = options.quotes === "double" ? '"' : "'";
let isIdentifier = options.isIdentifier;
let firstChar = string.charAt(0);
let output = "";
let counter = 0;
let length = string.length;

while (counter < length) {
let character = string.charAt(counter++);
let codePoint = character.charCodeAt(0);
let value;

if (codePoint < 0x20 || codePoint > 0x7e) {
if (codePoint >= 0xd800 && codePoint <= 0xdbff && counter < length) {
let extra = string.charCodeAt(counter++);
if ((extra & 0xfc00) === 0xdc00) {
codePoint = ((codePoint & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000;
} else {
counter--;
}
}
value = "\\" + codePoint.toString(16).toUpperCase() + " ";
} else {
if (/[\t\n\f\r\v]/.test(character)) {
value = "\\" + codePoint.toString(16).toUpperCase() + " ";
} else if (
character === "\\" ||
(!isIdentifier && character === quotes) ||
(isIdentifier && regexSingleEscape.test(character))
) {
value = "\\" + character;
} else {
value = character;
}
}
output += value;
}

if (isIdentifier) {
if (/^-[-\d]/.test(output)) {
output = "\\-" + output.slice(1);
} else if (/\d/.test(firstChar)) {
output = "\\3" + firstChar + " " + output.slice(1);
}
}

output = output.replace(regexExcessiveSpaces, ($0, $1, $2) => {
if ($1 && $1.length % 2) return $0;
return ($1 || "") + $2;
});

if (!isIdentifier && options.wrap) {
return quotes + output + quotes;
}
return output;
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"target": "es5",
"target": "es2015",
"lib": ["es2015"],
"downlevelIteration": true,
"importHelpers": false,
Expand Down
Loading