Skip to content

Commit

Permalink
Closes #18 (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
gucong3000 committed Dec 13, 2017
1 parent da33e34 commit 21fe309
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 14 deletions.
30 changes: 23 additions & 7 deletions lib/html-parser.js
Expand Up @@ -4,12 +4,28 @@ const htmlparser = require("htmlparser2");

function iterateCode (source, onStyleTag, onStyleAttribute) {
let currentStyle = null;
let isStructureHTML = false;
let level = 0;
let isFirstTag = true;

function onFirstTag () {
// Found a tag, the structure is now confirmed as HTML
if (isFirstTag) {
if (parser.startIndex <= 0 || !source.slice(0, parser.startIndex).trim()) {
level = 2;
}
isFirstTag = false;
}
}

const parser = new htmlparser.Parser({
oncomment: onFirstTag,
onprocessinginstruction: onFirstTag,
onopentag (name, attribute) {
// Found a tag, the structure is now confirmed as HTML
isStructureHTML = true;
onFirstTag();

if (!level) {
level = 1;
}

// Test if current tag is a valid <style> tag.
if (name !== "style") {
Expand Down Expand Up @@ -40,7 +56,7 @@ function iterateCode (source, onStyleTag, onStyleAttribute) {

parser.parseComplete(source);

return isStructureHTML;
return level;
}

function getSubString (str, regexp) {
Expand All @@ -54,7 +70,7 @@ function getLang (attribute) {
return getSubString(attribute.type, /^\w+\/(?:x-)?(\w+)$/i) || getSubString(attribute.lang, /^(\w+)(?:\?.+)?$/) || "css";
}

function htmlParser (source) {
function htmlParser (source, opts) {
const styles = [];
function onStyleTag (style) {
const firstNewLine = /^[ \t]*\r?\n/.exec(style.content);
Expand All @@ -75,9 +91,9 @@ function htmlParser (source) {
isHTMLAttribute: true,
});
}
const isStructureHTML = iterateCode(source, onStyleTag, onStyleAttribute);
const level = iterateCode(source, onStyleTag, onStyleAttribute);

if (!isStructureHTML) {
if (opts.from ? !level : level < 2) {
return;
}

Expand Down
8 changes: 7 additions & 1 deletion lib/markdown-parser.js
Expand Up @@ -3,7 +3,11 @@
const remark = require("remark");
const findAllAfter = require("unist-util-find-all-after");

module.exports = function (source) {
module.exports = function (source, opts) {
// Skip known non-markdown files.
if (opts.from && !/\.(?:md|markdown)(?:\?.+)?$/i.test(opts.from)) {
return;
}
let isStructureMD = false;
const ast = remark().parse(source);
const blocks = findAllAfter(ast, 0, (node) => {
Expand All @@ -12,9 +16,11 @@ module.exports = function (source) {
return /^(?:(?:[ps]?c)|le|wx|sa?|sugar)ss$/i.test(node.lang);
}
});

if (!isStructureMD) {
return;
}

return blocks.map((block) => {
const startIndex = source.indexOf(block.value, block.position.start.offset);
const content = source.slice(startIndex, block.position.end.offset).replace(/[ \t]*`*$/, "");
Expand Down
8 changes: 4 additions & 4 deletions lib/parser.js
Expand Up @@ -7,14 +7,14 @@ const mdParser = require("./markdown-parser");
const BlockFixer = require("./block-fixer");

function parser (source, opts) {
// Skip known style sheet files
if (opts.from && /\.(?:css|(?:wx|le|sa|sc|s)ss|styl(?:us)?)(?:\?.+)?$/i.test(opts.from)) {
// Skip known style sheet files.
if (opts.from && /\.(?:(?:p(?:ost)?)?css|(?:wx|le|sa|sc|s)ss|styl(?:us)?)(?:\?.+)?$/i.test(opts.from)) {
return;
}

source = source && source.toString();
const styleTag = htmlParser(source);
const stylesMd = mdParser(source);
const styleTag = htmlParser(source, opts);
const stylesMd = mdParser(source, opts);
if (!styleTag && !stylesMd) {
return;
}
Expand Down
64 changes: 64 additions & 0 deletions test/index.js
Expand Up @@ -117,4 +117,68 @@ describe("API", () => {
});
}).to.throw("custom parse error");
});

it("Not parse HTML in comments without filename", () => {
return postcss([

]).process(
[
"\t/*",
"\tWrite this in a comment:",
"\t<div style=\"display: flex; flex-direction: row\">",
"\t*/",
].join("\n"), {
syntax: syntax,
}
).then(result => {
expect(result.root.nodes).to.have.lengthOf(1);
expect(result.root.nodes[0]).to.have.property("type").to.equal("comment");
});
});

[
"css",
"pcss",
"postcss",
].forEach(extName => {
it("Not parse HTML in comments for `*." + extName + "`", () => {
return postcss([

]).process(
[
"/*",
" * <div style=\"display: flex; flex-direction: row\">",
" */",
].join("\n"), {
syntax: syntax,
from: "comment." + extName,
}
).then(result => {
expect(result.root.nodes).to.have.lengthOf(1);
expect(result.root.nodes[0]).to.have.property("type").to.equal("comment");
});
});
});

[
"scss",
"less",
"sss",
].forEach(extName => {
it("Not parse HTML in comments for `*." + extName + "`", () => {
return postcss([

]).process(
[
"// <div style=\"display: flex; flex-direction: row\">",
].join("\n"), {
syntax: syntax,
from: "comment." + extName,
}
).then(result => {
expect(result.root.nodes).to.have.lengthOf(1);
expect(result.root.nodes[0]).to.have.property("type").to.equal("comment");
});
});
});
});
4 changes: 2 additions & 2 deletions test/markdown.js
Expand Up @@ -39,7 +39,7 @@ describe("markdown tests", () => {
},
]).process(md, {
syntax,
from: "lang.vue",
from: "lang.md",
}).then(result => {
expect(result.content).to.equal(md);
});
Expand Down Expand Up @@ -72,7 +72,7 @@ describe("markdown tests", () => {
stylefmt,
]).process(source, {
syntax,
from: "lang.vue",
from: "lang.md",
}).then(result => {
expect(result.content).to.equal(code);
});
Expand Down

0 comments on commit 21fe309

Please sign in to comment.