postcss-html
Advanced tools
Comparing version 1.3.1 to 1.4.0
@@ -6,88 +6,94 @@ "use strict"; | ||
const buildTemplateSyntax = require("../template/syntax"); | ||
const SvelteTokenizer = require("./svelte-tokenizer"); | ||
function iterateCode(source, onStyleTag, onStyleAttribute) { | ||
function iterateCode(source, { onStyleTag, onStyleAttribute, svelte }) { | ||
const openTag = {}; | ||
let disable, ignore, style; | ||
const parser = new htmlparser.Parser({ | ||
oncomment: (data) => { | ||
ignore = false; | ||
const match = /(?:^|\s+)postcss-(\w+)(?:\s+|$)/i.exec(data); | ||
if (!match) { | ||
return; | ||
} | ||
const directive = match[1].toLowerCase(); | ||
if (directive === "enable") { | ||
disable = false; | ||
} else if (directive === "disable") { | ||
disable = true; | ||
} else if (directive === "ignore") { | ||
ignore = true; | ||
} | ||
}, | ||
onopentag(name, attribute) { | ||
openTag[name] = true; | ||
const parser = new htmlparser.Parser( | ||
{ | ||
oncomment: (data) => { | ||
ignore = false; | ||
const match = /(?:^|\s+)postcss-(\w+)(?:\s+|$)/i.exec(data); | ||
if (!match) { | ||
return; | ||
} | ||
const directive = match[1].toLowerCase(); | ||
if (directive === "enable") { | ||
disable = false; | ||
} else if (directive === "disable") { | ||
disable = true; | ||
} else if (directive === "ignore") { | ||
ignore = true; | ||
} | ||
}, | ||
onopentag(name, attribute) { | ||
openTag[name] = true; | ||
const currIgnore = ignore; | ||
ignore = false; | ||
if (currIgnore) { | ||
// ignore | ||
return; | ||
} | ||
// Test if current tag is a valid <style> tag. | ||
if (!/^style$/i.test(name)) { | ||
return; | ||
} | ||
const currIgnore = ignore; | ||
ignore = false; | ||
if (currIgnore) { | ||
// ignore | ||
return; | ||
} | ||
// Test if current tag is a valid <style> tag. | ||
if (!/^style$/i.test(name)) { | ||
return; | ||
} | ||
style = { | ||
inXsls: openTag["xsl:stylesheet"], | ||
inXslt: openTag["xsl:template"], | ||
inHtml: openTag.html, | ||
tagName: name, | ||
attribute, | ||
startIndex: parser.endIndex + 1, | ||
}; | ||
}, | ||
style = { | ||
inXsls: openTag["xsl:stylesheet"], | ||
inXslt: openTag["xsl:template"], | ||
inHtml: openTag.html, | ||
tagName: name, | ||
attribute, | ||
startIndex: parser.endIndex + 1, | ||
}; | ||
}, | ||
onclosetag(name) { | ||
openTag[name] = false; | ||
ignore = false; | ||
if (disable || !style || name !== style.tagName) { | ||
return; | ||
} | ||
onclosetag(name) { | ||
openTag[name] = false; | ||
ignore = false; | ||
if (disable || !style || name !== style.tagName) { | ||
return; | ||
} | ||
let content = source.slice(style.startIndex, parser.startIndex); | ||
let content = source.slice(style.startIndex, parser.startIndex); | ||
const firstNewLine = /^[\t ]*\r?\n/.exec(content); | ||
if (firstNewLine) { | ||
const offset = firstNewLine[0].length; | ||
style.startIndex += offset; | ||
content = content.slice(offset); | ||
} | ||
style.content = content.replace(/[\t ]*$/, ""); | ||
const firstNewLine = /^[\t ]*\r?\n/.exec(content); | ||
if (firstNewLine) { | ||
const offset = firstNewLine[0].length; | ||
style.startIndex += offset; | ||
content = content.slice(offset); | ||
} | ||
style.content = content.replace(/[\t ]*$/, ""); | ||
onStyleTag(style); | ||
style = null; | ||
}, | ||
onStyleTag(style); | ||
style = null; | ||
}, | ||
onattribute(name, content) { | ||
if (disable || ignore || name !== "style") { | ||
return; | ||
} | ||
const endIndex = parser.tokenizer._index; | ||
const startIndex = endIndex - content.length; | ||
if ( | ||
source[startIndex - 1] !== source[endIndex] || | ||
!/\S/.test(source[endIndex]) | ||
) { | ||
return; | ||
} | ||
onStyleAttribute({ | ||
content, | ||
startIndex, | ||
inline: true, | ||
inTemplate: openTag.template, | ||
}); | ||
onattribute(name, content) { | ||
if (disable || ignore || name !== "style") { | ||
return; | ||
} | ||
const endIndex = parser.tokenizer._index; | ||
const startIndex = endIndex - content.length; | ||
if ( | ||
source[startIndex - 1] !== source[endIndex] || | ||
!/\S/.test(source[endIndex]) | ||
) { | ||
return; | ||
} | ||
onStyleAttribute({ | ||
content, | ||
startIndex, | ||
inline: true, | ||
inTemplate: openTag.template, | ||
}); | ||
}, | ||
}, | ||
}); | ||
{ | ||
Tokenizer: svelte ? SvelteTokenizer : undefined, | ||
} | ||
); | ||
@@ -149,3 +155,7 @@ parser.parseComplete(source); | ||
iterateCode(source, onStyleTag, onStyleAttribute); | ||
iterateCode(source, { | ||
onStyleTag, | ||
onStyleAttribute, | ||
svelte: opts.from && /\.svelte$/i.test(opts.from), | ||
}); | ||
@@ -152,0 +162,0 @@ return styles; |
@@ -6,2 +6,3 @@ "use strict"; | ||
const { cssSyntax, cssSafeSyntax } = require("./syntaxes"); | ||
const { loadModule, isModuleNotFoundError } = require("../shared/load-module"); | ||
@@ -116,15 +117,8 @@ const defaultRules = [ | ||
try { | ||
const m = require("module"); | ||
const cwd = process.cwd(); | ||
const relativeTo = path.join(cwd, "__placeholder__.js"); | ||
// eslint-disable-next-line node/no-unsupported-features/node-builtins -- ignore | ||
return m.createRequire(relativeTo)(syntax); | ||
} catch (error) { | ||
if (!isModuleNotFoundError(error)) { | ||
throw error; | ||
} | ||
// ignore | ||
const loadedModule = loadModule(syntax); | ||
if (loadedModule) { | ||
return loadedModule; | ||
} | ||
/* istanbul ignore if */ | ||
if (standardModuleResolvers[syntax]) { | ||
@@ -143,7 +137,1 @@ try { | ||
} | ||
function isModuleNotFoundError(error) { | ||
return ( | ||
error && typeof error === "object" && error.code === "MODULE_NOT_FOUND" | ||
); | ||
} |
{ | ||
"name": "postcss-html", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"publishConfig": { | ||
@@ -5,0 +5,0 @@ "access": "public" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26577
17
714