Skip to content

Commit 694eef9

Browse files
authored
Remove non-HTML support (#43)
1 parent 576772c commit 694eef9

File tree

12 files changed

+65
-580
lines changed

12 files changed

+65
-580
lines changed

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ PostCSS HTML Syntax
1010
title="Philosopher’s stone, logo of PostCSS"
1111
src="http://postcss.github.io/postcss/logo.svg">
1212

13-
[PostCSS](https://github.com/postcss/postcss) Syntax for parsing:
14-
- HTML (and HTML-like)
15-
- [PHP](http://php.net)
16-
- [Vue component](https://vue-loader.vuejs.org/)
17-
- [Quick App](https://doc.quickapp.cn/framework/source-file.html)
18-
- [styled components](https://www.styled-components.com)
19-
- [Markdown](https://daringfireball.net/projects/markdown/syntax)
13+
[PostCSS](https://github.com/postcss/postcss) Syntax for parsing HTML (and HTML-like)
14+
- [PHP](http://php.net)
15+
- [Vue component](https://vue-loader.vuejs.org/)
16+
- [Quick App](https://doc.quickapp.cn/framework/source-file.html)
2017

2118
## Getting Started
2219

@@ -36,7 +33,12 @@ If you want support SCSS/SASS/LESS/SugarSS syntax, you need to install the corre
3633
## Use Cases
3734

3835
```js
39-
var syntax = require('postcss-html');
36+
var syntax = require('postcss-html')({
37+
sass: require('postcss-sass'),
38+
scss: require('postcss-scss'),
39+
less: require('postcss-less'),
40+
sugarss: require('sugarss'),
41+
});
4042
postcss(plugins).process(source, { syntax: syntax }).then(function (result) {
4143
// An alias for the result.css property. Use it with syntaxes that generate non-CSS output.
4244
result.content
@@ -45,7 +47,7 @@ postcss(plugins).process(source, { syntax: syntax }).then(function (result) {
4547

4648
### Style Transformations
4749

48-
The main use case of this plugin is to apply PostCSS transformations to HTML / [Markdown](https://daringfireball.net/projects/markdown/syntax) / [Vue component](https://vue-loader.vuejs.org/). For example, if you need to lint SCSS in `*.vue` with [Stylelint](http://stylelint.io/); or you need add vendor prefixes to CSS in `*.html` with [Autoprefixer](https://github.com/postcss/autoprefixer).
50+
The main use case of this plugin is to apply PostCSS transformations to HTML (and HTML-like). For example, if you need to lint SCSS in `*.vue` with [Stylelint](http://stylelint.io/); or you need add vendor prefixes to CSS in `*.html` with [Autoprefixer](https://github.com/postcss/autoprefixer).
4951

5052
### Syntax Infer for Stylesheet Files
5153

lib/html-parser.js

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
"use strict";
22

33
const htmlparser = require("htmlparser2");
4-
const requireParser = require("./require-parser");
5-
const jsParser = requireParser("js");
64

75
function iterateCode (source, onStyleTag, onStyleAttribute) {
86
const currentTag = {};
@@ -30,7 +28,7 @@ function iterateCode (source, onStyleTag, onStyleAttribute) {
3028
}
3129

3230
// Test if current tag is a valid <script> or <style> tag.
33-
if (!/^(?:script|style)$/i.test(name) || attribute.src) {
31+
if (!/^style$/i.test(name) || attribute.src) {
3432
return;
3533
}
3634

@@ -78,26 +76,18 @@ function getLang (attribute) {
7876
function htmlParser (source, opts) {
7977
const styles = [];
8078

81-
const onTag = {
82-
style: function onStyleTag (style) {
83-
const firstNewLine = /^[ \t]*\r?\n/.exec(style.content);
84-
style.lang = getLang(style.attribute);
85-
if (firstNewLine) {
86-
const offset = firstNewLine[0].length;
87-
style.startIndex += offset;
88-
style.content = style.content.slice(offset);
89-
}
90-
style.content = style.content.replace(/[ \t]*$/, "");
91-
style.isHTMLTag = true;
92-
styles.push(style);
93-
},
94-
script: function onScriptTag (script) {
95-
jsParser(script.content, opts, script.attribute).forEach(style => {
96-
style.startIndex += script.startIndex;
97-
styles.push(style);
98-
});
99-
},
100-
};
79+
function onStyleTag (style) {
80+
const firstNewLine = /^[ \t]*\r?\n/.exec(style.content);
81+
style.lang = getLang(style.attribute);
82+
if (firstNewLine) {
83+
const offset = firstNewLine[0].length;
84+
style.startIndex += offset;
85+
style.content = style.content.slice(offset);
86+
}
87+
style.content = style.content.replace(/[ \t]*$/, "");
88+
style.isHTMLTag = true;
89+
styles.push(style);
90+
}
10191
function onStyleAttribute (content, endIndex) {
10292
const startIndex = endIndex - content.length;
10393
if (source[startIndex - 1] === source[endIndex] && /\S/.test(source[endIndex])) {
@@ -108,7 +98,7 @@ function htmlParser (source, opts) {
10898
});
10999
}
110100
}
111-
const level = iterateCode(source, tag => onTag[tag.tagName](tag), onStyleAttribute);
101+
const level = iterateCode(source, onStyleTag, onStyleAttribute);
112102

113103
if (opts.from ? !level : level < 2) {
114104
return;

lib/js-parser.js

Lines changed: 0 additions & 125 deletions
This file was deleted.

lib/md-parser.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

lib/parse-style.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,13 @@ class LocalFixer {
5555
map: false,
5656
}));
5757
} catch (error) {
58-
if (style.isStyled) {
59-
return;
60-
}
6158
throw this.error(error);
6259
}
6360
this.root(root);
6461
Object.assign(root.source, {
6562
isHTMLAttribute: !!style.isHTMLAttribute,
6663
isMarkdown: !!style.isMarkdown,
6764
isHTMLTag: !!style.isHTMLTag,
68-
isStyled: !!style.isStyled,
6965
lang: style.lang || "css",
7066
syntax,
7167
});

lib/parser.js

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
const Input = require("postcss/lib/input");
44
const Document = require("./document");
55
const docFixer = require("./parse-style");
6-
const requireParser = require("./require-parser");
7-
const jsParser = requireParser("js");
8-
const mdParser = requireParser("md");
9-
const htmlParser = requireParser("html");
6+
const htmlParser = require("./html-parser");
107

118
function parser (source, opts) {
129
function filenameMatch (reg) {
@@ -18,44 +15,21 @@ function parser (source, opts) {
1815
}
1916

2017
source = source && source.toString();
21-
let styles;
22-
23-
if (filenameMatch(/\.(?:m?[jt]sx?|es\d*|pac)(?:\?.*)?$/i)) {
24-
styles = jsParser(source, opts);
25-
} else {
26-
if (filenameMatch(/\.(?:md|markdown)(?:\?.*)?$/i)) {
27-
styles = mdParser(source, opts);
28-
}
29-
const styleHtm = htmlParser(source, opts);
30-
if (styleHtm) {
31-
if (styles) {
32-
styles.push.apply(styles, styleHtm);
33-
} else {
34-
styles = styleHtm;
35-
}
36-
} else if (!styles && filenameMatch(/\.(?:[sx]?html?|[sx]ht|vue|ux|php)(?:\?.*)?$/i)) {
37-
styles = [];
38-
}
39-
}
40-
41-
if (!styles) {
42-
return;
43-
}
4418

19+
const styleHtm = htmlParser(source, opts);
4520
const document = new Document();
4621
const parseStyle = docFixer(source, opts);
4722
let index = 0;
48-
styles.sort((a, b) => {
49-
return a.startIndex - b.startIndex;
50-
}).forEach(style => {
51-
const root = parseStyle(style);
52-
if (!root) {
53-
return;
54-
}
55-
root.raws.beforeStart = source.slice(index, style.startIndex);
56-
index = style.startIndex + style.content.length;
57-
document.nodes.push(root);
58-
});
23+
if (styleHtm) {
24+
styleHtm.sort((a, b) => {
25+
return a.startIndex - b.startIndex;
26+
}).forEach(style => {
27+
const root = parseStyle(style);
28+
root.raws.beforeStart = source.slice(index, style.startIndex);
29+
index = style.startIndex + style.content.length;
30+
document.nodes.push(root);
31+
});
32+
}
5933
document.raws.afterEnd = index ? source.slice(index) : source;
6034
document.source = {
6135
input: new Input(source, opts),

lib/require-parser.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

package.json

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "postcss-html",
3-
"version": "0.19.0",
4-
"description": "PostCSS Syntax for parsing HTML / Markdown / Vue Component",
3+
"version": "0.20.0",
4+
"description": "PostCSS Syntax for parsing HTML (and HTML-like)",
55
"main": "lib/index.js",
66
"scripts": {
77
"report-coverage": "codecov",
@@ -45,9 +45,7 @@
4545
},
4646
"homepage": "https://github.com/gucong3000/postcss-html#readme",
4747
"dependencies": {
48-
"htmlparser2": "^3.9.2",
49-
"remark": "^9.0.0",
50-
"unist-util-find-all-after": "^1.0.1"
48+
"htmlparser2": "^3.9.2"
5149
},
5250
"peerDependencies": {
5351
"postcss": ">=5.0.0",
@@ -58,13 +56,6 @@
5856
"sugarss": ">=1.0.0"
5957
},
6058
"devDependencies": {
61-
"@types/autoprefixer": "^6.7.3",
62-
"@types/babel-core": "^6.25.3",
63-
"@types/babel-traverse": "^6.25.3",
64-
"@types/babylon": "^6.16.2",
65-
"@types/chai": "^4.1.2",
66-
"@types/mocha": "^5.0.0",
67-
"@types/stylelint": "^7.11.0",
6859
"autoprefixer": "^8.2.0",
6960
"chai": "^4.1.2",
7061
"codecov": "^3.0.0",

0 commit comments

Comments
 (0)