postcss
新しい Processor
インスタンスを作成し、plugins
を CSS プロセッサとして適用します。
let postcss = require('postcss')
postcss(plugins).process(css, { from, to }).then(result => {
console.log(result.css)
})
引数 | 型 | 説明 |
---|---|---|
plugins | AcceptedPlugin[] | PostCSS プラグイン。 |
引数 | 型 | 説明 |
---|---|---|
plugins… | AcceptedPlugin[] | PostCSS プラグイン。 |
Processor
を返します。
postcss.AcceptedPlugin
型: Object | OldPlugin<any> | Plugin | PluginCreator<any> | Processor | TransformCallback。
postcss.Builder
引数 | 型 |
---|---|
part | string |
node | AnyNode |
type | "end" | "start" |
postcss.Helpers
型: Object & Postcss。
postcss.JSONHydrator
引数 | 型 |
---|---|
data | object |
引数 | 型 |
---|---|
data | object[] |
Node
を返します。
postcss.OldPlugin
引数 | 型 |
---|---|
opts | T |
引数 | 型 |
---|---|
root | Root |
result | Result<Document_ | Root> |
Transformer
を返します。
postcss.Parser
引数 | 型 |
---|---|
css | string | Object |
opts | Pick<ProcessOptions<Document_ | Root>, "map" | "from"> |
RootNode
を返します。
postcss.PluginCreator
引数 | 型 |
---|---|
opts | PluginOptions |
postcss.Postcss
型: typeof postcss。
postcss.SourceMap
型: SourceMapGenerator & Object。
postcss.Stringifier
引数 | 型 |
---|---|
node | AnyNode |
builder | Builder |
postcss.TransformCallback
引数 | 型 |
---|---|
root | Root |
result | Result<Document_ | Root> |
void | Promise<void>
を返します。
postcss.Transformer
引数 | 型 |
---|---|
root | Root |
result | Result<Document_ | Root> |
void | Promise<void>
を返します。
postcss.atRule
新しい AtRule
ノードを作成します。
引数 | 型 | 説明 |
---|---|---|
defaults | AtRuleProps | 新しいノードのプロパティ。 |
AtRule_
を返します。
postcss.comment
新しい Comment
ノードを作成します。
引数 | 型 | 説明 |
---|---|---|
defaults | CommentProps | 新しいノードのプロパティ。 |
Comment_
を返します。
postcss.decl
新しい Declaration
ノードを作成します。
引数 | 型 | 説明 |
---|---|---|
defaults | DeclarationProps | 新しいノードのプロパティ。 |
Declaration_
を返します。
postcss.document
新しい Document
ノードを作成します。
引数 | 型 | 説明 |
---|---|---|
defaults | DocumentProps | 新しいノードのプロパティ。 |
Document_
を返します。
postcss.fromJSON
JSON AST (Node#toJSON
から) を AST クラスに再ハイドレートします。
const json = root.toJSON()
// save to file, send by network, etc
const root2 = postcss.fromJSON(json)
引数 | 型 |
---|---|
data | object |
引数 | 型 |
---|---|
data | object[] |
Node
を返します。
postcss.list
型: List。
postcss.parse
ソース CSS を解析し、ソース CSS ノードを含む新しい Root
または Document
ノードを返します。
// Simple CSS concatenation with source map support
const root1 = postcss.parse(css1, { from: file1 })
const root2 = postcss.parse(css2, { from: file2 })
root1.append(root2).toResult().css
引数 | 型 |
---|---|
css | string | Object |
opts | Pick<ProcessOptions<Document_ | Root>, "map" | "from"> |
Root
を返します。
postcss.root
新しい Root
ノードを作成します。
引数 | 型 | 説明 |
---|---|---|
defaults | RootProps | 新しいノードのプロパティ。 |
Root
を返します。
postcss.rule
新しい Rule
ノードを作成します。
引数 | 型 |
---|---|
defaults | RuleProps |
Rule
を返します。
postcss.stringify
ノードツリーを CSS 文字列に変換するためのデフォルト関数。
引数 | 型 |
---|---|
node | AnyNode |
builder | Builder |
AtRule
at-rule を表します。
Once (root, { AtRule }) {
let media = new AtRule({ name: 'media', params: 'print' })
media.append(…)
root.append(media)
}
CSS で {}
ブロックが後に続く場合、このノードは子を表す nodes プロパティを持ちます。
const root = postcss.parse('@charset "UTF-8"; @media print {}')
const charset = root.first
charset.type //=> 'atrule'
charset.nodes //=> undefined
const media = root.last
media.nodes //=> []
AtRule#after()
現在のノードの親に、現在のノードの後に新しいノードを挿入します。
node.parent.insertAfter(node, add)
のエイリアス。
decl.after('color: black')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
AtRule
を返します。
AtRule#append()
コンテナの末尾に新しいノードを挿入します。
const decl1 = new Declaration({ prop: 'color', value: 'black' })
const decl2 = new Declaration({ prop: 'background-color', value: 'white' })
rule.append(decl1, decl2)
root.append({ name: 'charset', params: '"UTF-8"' }) // at-rule
root.append({ selector: 'a' }) // rule
rule.append({ prop: 'color', value: 'black' }) // declaration
rule.append({ text: 'Comment' }) // comment
root.append('a {}')
root.first.append('color: black; z-index: 1')
引数 | 型 | 説明 |
---|---|---|
nodes… | (string | string[] | Node | ChildProps | ChildProps[] | Node[])[] | 新しいノード。 |
AtRule
を返します。
AtRule#assign()
既存のノードインスタンスにプロパティを割り当てます。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引数 | 型 | 説明 |
---|---|---|
overrides | object | AtRuleProps | ノードをオーバーライドするための新しいプロパティ。 |
AtRule
を返します。
AtRule#before()
現在のノードの親に、現在のノードの前に新しいノードを挿入します。
node.parent.insertBefore(node, add)
のエイリアス。
decl.before('content: ""')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
AtRule
を返します。
AtRule#cleanRaws()
ノードとその子に対するコードスタイルのプロパティをクリアします。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引数 | 型 | 説明 |
---|---|---|
keepBetween | boolean | raws.between シンボルを保持します。 |
AtRule#clone()
既存のノードのクローンを作成します。これには、すべてのプロパティとその値が含まれます。これには raws
が含まれますが、type
は含まれません。
decl.raws.before //=> "\n "
const cloned = decl.clone({ prop: '-moz-' + decl.prop })
cloned.raws.before //=> "\n "
cloned.toString() //=> -moz-transform: scale(0)
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<AtRuleProps> | クローンでオーバーライドする新しいプロパティ。 |
AtRule
を返します。
AtRule#cloneAfter()
ノードをクローンし、結果のクローンされたノードを現在のノードの後に挿入するためのショートカット。
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<AtRuleProps> | クローンでオーバーライドする新しいプロパティ。 |
AtRule
を返します。
AtRule#cloneBefore()
ノードをクローンし、結果のクローンされたノードを現在のノードの前に挿入するためのショートカット。
decl.cloneBefore({ prop: '-moz-' + decl.prop })
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<AtRuleProps> | クローンでオーバーライドする新しいプロパティ。 |
AtRule
を返します。
AtRule#each()
コンテナの直接の子を反復処理し、子ごとに callback
を呼び出します。
コールバックで false
を返すと、反復処理が中断されます。
このメソッドは、コンテナの直接の子のみを反復処理します。コンテナのすべての子孫ノードを再帰的に反復処理する必要がある場合は、Container#walk
を使用します。
for {}
サイクルまたは Array#forEach
とは異なり、このイテレータは、反復処理中に子ノードの配列をミューテートする場合でも安全です。PostCSS は、ミューテーションに合わせて現在のインデックスを調整します。
const root = postcss.parse('a { color: black; z-index: 1 }')
const rule = root.first
for (const decl of rule.nodes) {
decl.cloneBefore({ prop: '-webkit-' + decl.prop })
// Cycle will be infinite, because cloneBefore moves the current node
// to the next index
}
rule.each(decl => {
decl.cloneBefore({ prop: '-webkit-' + decl.prop })
// Will be executed only for color and z-index
})
引数 | 型 | 説明 |
---|---|---|
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
AtRule#error()
クラス CssSyntaxError
のインスタンスを作成し、このメソッドに渡されたパラメータがエラーインスタンスに割り当てられます。
エラーインスタンスには、エラーの説明、ソース内のノードの元の位置、行番号と列番号を示す説明が含まれます。
前のマップが存在する場合は、ソースの元の位置を取得するために使用されます。
ここでの前のマップは、以前のコンパイルで生成されたソースマップを指します。例: Less、Stylus、Sass。
このメソッドは、エラーインスタンスをスローするのではなく返します。
if (!variables[name]) {
throw decl.error(`Unknown variable ${name}`, { word: name })
// CssSyntaxError: postcss-vars:a.sass:4:3: Unknown variable $black
// color: $black
// a
// ^
// background: white
}
引数 | 型 | 説明 |
---|---|---|
message | string | エラーインスタンスの説明。 |
options | NodeErrorOptions | エラーインスタンスのオプション。 |
CssSyntaxError_
を返します。
AtRule#every()
コールバックがコンテナのすべての子に対して true
を返す場合は true
を返します。
const noPrefixes = rule.every(i => i.prop[0] !== '-')
引数 | 型 | 説明 |
---|---|---|
condition | Function | イテレータは true または false を返します。 |
boolean
を返します。
AtRule#index()
Container#nodes
配列内の child
のインデックスを返します。
rule.index( rule.nodes[2] ) //=> 2
引数 | 型 | 説明 |
---|---|---|
child | number | ChildNode | 現在のコンテナの子。 |
number
を返します。
AtRule#insertAfter()
コンテナ内の古いノードの後に新しいノードを挿入します。
引数 | 型 | 説明 |
---|---|---|
oldNode | number | ChildNode | 子または子のインデックス。 |
newNode | string | string[] | ChildNode | ChildProps | ChildNode[] | ChildProps[] | 新しいノード。 |
AtRule
を返します。
AtRule#insertBefore()
コンテナ内の古いノードの前に新しいノードを挿入します。
rule.insertBefore(decl, decl.clone({ prop: '-webkit-' + decl.prop }))
引数 | 型 | 説明 |
---|---|---|
oldNode | number | ChildNode | 子または子のインデックス。 |
newNode | string | string[] | ChildNode | ChildProps | ChildNode[] | ChildProps[] | 新しいノード。 |
AtRule
を返します。
AtRule#name
at-rule の名前は、@
の直後に続きます。
const root = postcss.parse('@media print {}')
media.name //=> 'media'
const media = root.first
型: string。
AtRule#next()
ノードの親の次の子を返します。現在のノードが最後の子の場合は undefined
を返します。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
ChildNode
を返します。
AtRule#nodes
コンテナの子を含む配列。
const root = postcss.parse('a { color: black }')
root.nodes.length //=> 1
root.nodes[0].selector //=> 'a'
root.nodes[0].nodes[0].prop //=> 'color'
型: ChildNode[]。
AtRule#params
at-rule のパラメータ。at-rule の名前の後に続き、{}
ブロックの前にある値です。
const root = postcss.parse('@media print, screen {}')
const media = root.first
media.params //=> 'print, screen'
型: string。
AtRule#parent
現在のノードの親を表します。
root.nodes[0].parent === root //=> true
型: Container_<ChildNode>。
AtRule#positionBy()
ノード内の単語またはインデックスの位置を取得します。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word"> | オプション。 |
Position
を返します。
AtRule#positionInside()
文字列のインデックスを行/列に変換します。
引数 | 型 | 説明 |
---|---|---|
index | number | ノードの文字列内の記号の番号。 |
Position
を返します。
AtRule#prepend()
コンテナの先頭に新しいノードを挿入します。
const decl1 = new Declaration({ prop: 'color', value: 'black' })
const decl2 = new Declaration({ prop: 'background-color', value: 'white' })
rule.prepend(decl1, decl2)
root.append({ name: 'charset', params: '"UTF-8"' }) // at-rule
root.append({ selector: 'a' }) // rule
rule.append({ prop: 'color', value: 'black' }) // declaration
rule.append({ text: 'Comment' }) // comment
root.append('a {}')
root.first.append('color: black; z-index: 1')
引数 | 型 | 説明 |
---|---|---|
nodes… | (string | string[] | Node | ChildProps | ChildProps[] | Node[])[] | 新しいノード。 |
AtRule
を返します。
AtRule#prev()
ノードの親の前の子を返します。現在のノードが最初の子である場合は、undefined
を返します。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
ChildNode
を返します。
AtRule#push()
ノードの末尾に子を追加します。
rule.push(new Declaration({ prop: 'color', value: 'black' }))
引数 | 型 | 説明 |
---|---|---|
child | ChildNode | 新しいノード。 |
AtRule
を返します。
AtRule#rangeBy()
ノード内の単語または開始インデックスと終了インデックスの範囲を取得します。開始インデックスは包括的で、終了インデックスは排他的です。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word" | "endIndex"> | オプション。 |
Range
を返します。
AtRule#raw()
raws
値を返します。ノードにコードスタイルプロパティがない場合(ノードが手動で作成または複製されたため)、PostCSSはツリー内の他のノードを調べてコードスタイルプロパティを自動検出しようとします。
const root = postcss.parse('a { background: white }')
root.nodes[0].append({ prop: 'color', value: 'black' })
root.nodes[0].nodes[1].raws.before //=> undefined
root.nodes[0].nodes[1].raw('before') //=> ' '
引数 | 型 | 説明 |
---|---|---|
prop | string | コードスタイルプロパティの名前。 |
defaultType | string | デフォルト値の名前。値が prop と同じ場合は省略できます。 |
string
を返します。
AtRule#raws
CSSソースコードに存在する不要な空白と文字を表します。
元の入力と同じように、バイト単位で等しいノード文字列を生成するための情報。
raws オブジェクトのプロパティはパーサーによって決定されます。デフォルトのパーサーは次のプロパティを使用します
before
: ノードの前の空白記号。宣言の前の*
および_
記号も保存します(IEハック)。after
: ノードの最後の子からノードの末尾までの空白記号。between
: 宣言の場合はプロパティと値の間、ルールの場合はセレクタと{
の間、または at-rule の場合は最後のパラメータと{
の間の記号。semicolon
: 最後の子が(オプションの)セミコロンを持っている場合は true が含まれます。afterName
: at-rule 名とそのパラメータの間の空白。left
:/*
とコメントのテキストの間の空白記号。right
: コメントのテキストと */ の間の空白記号。
important
: important ステートメントの内容。単に!important
ではない場合。
PostCSSはセレクタ、宣言値、および at-rule パラメータ内のコメントをフィルタリングしますが、raws に元のコンテンツを保存します。
const root = postcss.parse('a {\n color:black\n}')
root.first.first.raws //=> { before: '\n ', between: ':' }
型: AtRuleRaws。
AtRule#remove()
ノードをその親から削除し、親プロパティを削除します。
if (decl.prop.match(/^-webkit-/)) {
decl.remove()
}
AtRule
を返します。
AtRule#removeAll()
コンテナからすべての子を削除し、それらの親プロパティをクリアします。
rule.removeAll()
rule.nodes.length //=> 0
AtRule
を返します。
AtRule#removeChild()
ノードをコンテナから削除し、ノードとその子から親プロパティをクリアします。
rule.nodes.length //=> 5
rule.removeChild(decl)
rule.nodes.length //=> 4
decl.parent //=> undefined
引数 | 型 | 説明 |
---|---|---|
child | number | ChildNode | 子または子のインデックス。 |
AtRule
を返します。
AtRule#replaceValues()
引数 | 型 |
---|---|
pattern | string | RegExp |
replaced | string | Function |
引数 | 型 | 説明 |
---|---|---|
pattern | string | RegExp | パターンを置換します。 |
options | ValueOptions | |
replaced | string | Function |
AtRule
を返します。
AtRule#replaceWith()
現在のノードの前にノードを挿入し、現在のノードを削除します。
AtRule: {
mixin: atrule => {
atrule.replaceWith(mixinRules[atrule.params])
}
}
引数 | 型 | 説明 |
---|---|---|
nodes… | (ChildNode | ChildProps | ChildNode[] | ChildProps[])[] | 現在のノードを置き換えるモード。 |
AtRule
を返します。
AtRule#root()
ノードのツリーの Root インスタンスを検索します。
root.nodes[0].nodes[0].root() === root
Root
を返します。
AtRule#some()
コンテナの子の少なくとも1つに対してコールバックが true
を返す場合は、true
を返します。
const hasPrefix = rule.some(i => i.prop[0] === '-')
引数 | 型 | 説明 |
---|---|---|
condition | Function | イテレータは true または false を返します。 |
boolean
を返します。
AtRule#source
ノードの起源に関する情報を表し、ソースマップの生成に必要です。
PostCSSが提供するパブリックAPIを使用して手動で作成されたノードは、source
が未定義になり、ソースマップに表示されません。
このため、プラグイン開発者は、複製されたノードはデフォルトで元のノードと同じソースを持つか、手動で作成されたノードにソースを割り当てるため、ノードを複製することを検討する必要があります。
decl.source.input.from //=> '/home/ai/source.css'
decl.source.start //=> { line: 10, column: 2 }
decl.source.end //=> { line: 10, column: 12 }
// Incorrect method, source not specified!
const prefixed = postcss.decl({
prop: '-moz-' + decl.prop,
value: decl.value
})
// Correct method, source is inherited when duplicating.
const prefixed = decl.clone({
prop: '-moz-' + decl.prop
})
if (atrule.name === 'add-link') {
const rule = postcss.rule({
selector: 'a',
source: atrule.source
})
atrule.parent.insertBefore(atrule, rule)
}
型: Source。
AtRule#toJSON()
JSON.stringify()
の循環リンクを修正します。
object
を返します。
AtRule#toString()
ノードの型に応じて、ノードをブラウザで読み取り可能なカスケーディングスタイルシート文字列にコンパイルします。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引数 | 型 | 説明 |
---|---|---|
stringifier | Stringifier | Syntax<Document_ | Root> | 文字列生成で使用する構文。 |
string
を返します。
AtRule#type
抽象構文木におけるノードの型を表します。
ノードの型は、ノードの識別と型に基づいた操作の実行に役立ちます。
const declaration = new Declaration({
prop: 'color',
value: 'black'
})
declaration.type //=> 'decl'
型: "atrule"。
AtRule#walk()
コンテナの子孫ノードをトラバースし、各ノードに対してコールバックを呼び出します。
container.each() と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
コンテナの直下の子のみを反復する必要がある場合は、Container#each
を使用します。
root.walk(node => {
// Traverses all descendant nodes.
})
引数 | 型 | 説明 |
---|---|---|
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
AtRule#walkAtRules()
コンテナの子孫ノードをトラバースし、各 at-rule ノードに対してコールバックを呼び出します。
フィルタを渡すと、名前が一致する at-rule のみに対して反復が行われます。
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
root.walkAtRules(rule => {
if (isOld(rule.name)) rule.remove()
})
let first = false
root.walkAtRules('charset', rule => {
if (!first) {
first = true
} else {
rule.remove()
}
})
引数 | 型 | 説明 |
---|---|---|
nameFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
AtRule#walkComments()
引数 | 型 |
---|---|
callback | Function |
void | false
を返します。
AtRule#walkDecls()
コンテナの子孫ノードをトラバースし、各宣言ノードに対してコールバックを呼び出します。
フィルタを渡すと、プロパティが一致する宣言のみに対して反復が行われます。
root.walkDecls(decl => {
checkPropertySupport(decl.prop)
})
root.walkDecls('border-radius', decl => {
decl.remove()
})
root.walkDecls(/^background/, decl => {
decl.value = takeFirstColorFromGradient(decl.value)
})
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
引数 | 型 | 説明 |
---|---|---|
propFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
AtRule#walkRules()
コンテナの子孫ノードをトラバースし、各ルールノードに対してコールバックを呼び出します。
フィルタを渡すと、セレクタが一致するルールのみに対して反復が行われます。
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
const selectors = []
root.walkRules(rule => {
selectors.push(rule.selector)
})
console.log(`Your CSS uses ${ selectors.length } selectors`)
引数 | 型 | 説明 |
---|---|---|
selectorFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
AtRule#warn()
Result#warn のラッパーであり、警告を生成する便利な方法を提供します。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引数 | 型 | 説明 |
---|---|---|
result | Result<Document_ | Root> | 警告を受け取る Result インスタンス。 |
message | string | 警告の説明。 |
options | WarningOptions | 警告のオプション。 |
Warning
を返します。
Comment
CSSコメント を処理するクラスを表します
Once (root, { Comment }) {
const note = new Comment({ text: 'Note: …' })
root.append(note)
}
セレクタ、at-rule パラメータ、または宣言値内の CSS コメントは、上記で説明した raws
プロパティに保存されることに注意してください。
Comment#after()
現在のノードの親に、現在のノードの後に新しいノードを挿入します。
node.parent.insertAfter(node, add)
のエイリアス。
decl.after('color: black')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Comment
を返します。
Comment#assign()
既存のノードインスタンスにプロパティを割り当てます。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引数 | 型 | 説明 |
---|---|---|
overrides | object | CommentProps | ノードをオーバーライドするための新しいプロパティ。 |
Comment
を返します。
Comment#before()
現在のノードの親に、現在のノードの前に新しいノードを挿入します。
node.parent.insertBefore(node, add)
のエイリアス。
decl.before('content: ""')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Comment
を返します。
Comment#cleanRaws()
ノードとその子に対するコードスタイルのプロパティをクリアします。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引数 | 型 | 説明 |
---|---|---|
keepBetween | boolean | raws.between シンボルを保持します。 |
Comment#clone()
既存のノードのクローンを作成します。これには、すべてのプロパティとその値が含まれます。これには raws
が含まれますが、type
は含まれません。
decl.raws.before //=> "\n "
const cloned = decl.clone({ prop: '-moz-' + decl.prop })
cloned.raws.before //=> "\n "
cloned.toString() //=> -moz-transform: scale(0)
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<CommentProps> | クローンでオーバーライドする新しいプロパティ。 |
Comment
を返します。
Comment#cloneAfter()
ノードをクローンし、結果のクローンされたノードを現在のノードの後に挿入するためのショートカット。
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<CommentProps> | クローンでオーバーライドする新しいプロパティ。 |
Comment
を返します。
Comment#cloneBefore()
ノードをクローンし、結果のクローンされたノードを現在のノードの前に挿入するためのショートカット。
decl.cloneBefore({ prop: '-moz-' + decl.prop })
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<CommentProps> | クローンでオーバーライドする新しいプロパティ。 |
Comment
を返します。
Comment#error()
クラス CssSyntaxError
のインスタンスを作成し、このメソッドに渡されたパラメータがエラーインスタンスに割り当てられます。
エラーインスタンスには、エラーの説明、ソース内のノードの元の位置、行番号と列番号を示す説明が含まれます。
前のマップが存在する場合は、ソースの元の位置を取得するために使用されます。
ここでの前のマップは、以前のコンパイルで生成されたソースマップを指します。例: Less、Stylus、Sass。
このメソッドは、エラーインスタンスをスローするのではなく返します。
if (!variables[name]) {
throw decl.error(`Unknown variable ${name}`, { word: name })
// CssSyntaxError: postcss-vars:a.sass:4:3: Unknown variable $black
// color: $black
// a
// ^
// background: white
}
引数 | 型 | 説明 |
---|---|---|
message | string | エラーインスタンスの説明。 |
options | NodeErrorOptions | エラーインスタンスのオプション。 |
CssSyntaxError_
を返します。
Comment#next()
ノードの親の次の子を返します。現在のノードが最後の子の場合は undefined
を返します。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
ChildNode
を返します。
Comment#parent
現在のノードの親を表します。
root.nodes[0].parent === root //=> true
型: Container_<ChildNode>。
Comment#positionBy()
ノード内の単語またはインデックスの位置を取得します。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word"> | オプション。 |
Position
を返します。
Comment#positionInside()
文字列のインデックスを行/列に変換します。
引数 | 型 | 説明 |
---|---|---|
index | number | ノードの文字列内の記号の番号。 |
Position
を返します。
Comment#prev()
ノードの親の前の子を返します。現在のノードが最初の子である場合は、undefined
を返します。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
ChildNode
を返します。
Comment#rangeBy()
ノード内の単語または開始インデックスと終了インデックスの範囲を取得します。開始インデックスは包括的で、終了インデックスは排他的です。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word" | "endIndex"> | オプション。 |
Range
を返します。
Comment#raw()
raws
値を返します。ノードにコードスタイルプロパティがない場合(ノードが手動で作成または複製されたため)、PostCSSはツリー内の他のノードを調べてコードスタイルプロパティを自動検出しようとします。
const root = postcss.parse('a { background: white }')
root.nodes[0].append({ prop: 'color', value: 'black' })
root.nodes[0].nodes[1].raws.before //=> undefined
root.nodes[0].nodes[1].raw('before') //=> ' '
引数 | 型 | 説明 |
---|---|---|
prop | string | コードスタイルプロパティの名前。 |
defaultType | string | デフォルト値の名前。値が prop と同じ場合は省略できます。 |
string
を返します。
Comment#raws
CSSソースコードに存在する不要な空白と文字を表します。
元の入力と同じように、バイト単位で等しいノード文字列を生成するための情報。
raws オブジェクトのプロパティはパーサーによって決定されます。デフォルトのパーサーは次のプロパティを使用します
before
: ノードの前の空白記号。宣言の前の*
および_
記号も保存します(IEハック)。after
: ノードの最後の子からノードの末尾までの空白記号。between
: 宣言の場合はプロパティと値の間、ルールの場合はセレクタと{
の間、または at-rule の場合は最後のパラメータと{
の間の記号。semicolon
: 最後の子が(オプションの)セミコロンを持っている場合は true が含まれます。afterName
: at-rule 名とそのパラメータの間の空白。left
:/*
とコメントのテキストの間の空白記号。right
: コメントのテキストと */ の間の空白記号。
important
: important ステートメントの内容。単に!important
ではない場合。
PostCSSはセレクタ、宣言値、および at-rule パラメータ内のコメントをフィルタリングしますが、raws に元のコンテンツを保存します。
const root = postcss.parse('a {\n color:black\n}')
root.first.first.raws //=> { before: '\n ', between: ':' }
型: CommentRaws。
Comment#remove()
ノードをその親から削除し、親プロパティを削除します。
if (decl.prop.match(/^-webkit-/)) {
decl.remove()
}
Comment
を返します。
Comment#replaceWith()
現在のノードの前にノードを挿入し、現在のノードを削除します。
AtRule: {
mixin: atrule => {
atrule.replaceWith(mixinRules[atrule.params])
}
}
引数 | 型 | 説明 |
---|---|---|
nodes… | (ChildNode | ChildProps | ChildNode[] | ChildProps[])[] | 現在のノードを置き換えるモード。 |
Comment
を返します。
Comment#root()
ノードのツリーの Root インスタンスを検索します。
root.nodes[0].nodes[0].root() === root
Root
を返します。
Comment#source
ノードの起源に関する情報を表し、ソースマップの生成に必要です。
PostCSSが提供するパブリックAPIを使用して手動で作成されたノードは、source
が未定義になり、ソースマップに表示されません。
このため、プラグイン開発者は、複製されたノードはデフォルトで元のノードと同じソースを持つか、手動で作成されたノードにソースを割り当てるため、ノードを複製することを検討する必要があります。
decl.source.input.from //=> '/home/ai/source.css'
decl.source.start //=> { line: 10, column: 2 }
decl.source.end //=> { line: 10, column: 12 }
// Incorrect method, source not specified!
const prefixed = postcss.decl({
prop: '-moz-' + decl.prop,
value: decl.value
})
// Correct method, source is inherited when duplicating.
const prefixed = decl.clone({
prop: '-moz-' + decl.prop
})
if (atrule.name === 'add-link') {
const rule = postcss.rule({
selector: 'a',
source: atrule.source
})
atrule.parent.insertBefore(atrule, rule)
}
型: Source。
Comment#text
コメントのテキスト。
型: string。
Comment#toJSON()
JSON.stringify()
の循環リンクを修正します。
object
を返します。
Comment#toString()
ノードの型に応じて、ノードをブラウザで読み取り可能なカスケーディングスタイルシート文字列にコンパイルします。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引数 | 型 | 説明 |
---|---|---|
stringifier | Stringifier | Syntax<Document_ | Root> | 文字列生成で使用する構文。 |
string
を返します。
Comment#type
抽象構文木におけるノードの型を表します。
ノードの型は、ノードの識別と型に基づいた操作の実行に役立ちます。
const declaration = new Declaration({
prop: 'color',
value: 'black'
})
declaration.type //=> 'decl'
型: "comment"。
Comment#warn()
Result#warn のラッパーであり、警告を生成する便利な方法を提供します。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引数 | 型 | 説明 |
---|---|---|
result | Result<Document_ | Root> | 警告を受け取る Result インスタンス。 |
message | string | 警告の説明。 |
options | WarningOptions | 警告のオプション。 |
Warning
を返します。
Container
Root
、AtRule
、および Rule
コンテナノードは、子を操作するのに役立ついくつかの共通メソッドを継承します。
すべてのコンテナは任意のコンテンツを格納できることに注意してください。ルール内にルールを記述すると、PostCSS がそれを解析します。
Container#after()
現在のノードの親に、現在のノードの後に新しいノードを挿入します。
node.parent.insertAfter(node, add)
のエイリアス。
decl.after('color: black')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Container<Child>
を返します。
Container#append()
コンテナの末尾に新しいノードを挿入します。
const decl1 = new Declaration({ prop: 'color', value: 'black' })
const decl2 = new Declaration({ prop: 'background-color', value: 'white' })
rule.append(decl1, decl2)
root.append({ name: 'charset', params: '"UTF-8"' }) // at-rule
root.append({ selector: 'a' }) // rule
rule.append({ prop: 'color', value: 'black' }) // declaration
rule.append({ text: 'Comment' }) // comment
root.append('a {}')
root.first.append('color: black; z-index: 1')
引数 | 型 | 説明 |
---|---|---|
nodes… | (string | string[] | Node | ChildProps | ChildProps[] | Node[])[] | 新しいノード。 |
Container<Child>
を返します。
Container#assign()
既存のノードインスタンスにプロパティを割り当てます。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引数 | 型 | 説明 |
---|---|---|
overrides | object | ContainerProps | ノードをオーバーライドするための新しいプロパティ。 |
Container<Child>
を返します。
Container#before()
現在のノードの親に、現在のノードの前に新しいノードを挿入します。
node.parent.insertBefore(node, add)
のエイリアス。
decl.before('content: ""')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Container<Child>
を返します。
Container#cleanRaws()
ノードとその子に対するコードスタイルのプロパティをクリアします。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引数 | 型 | 説明 |
---|---|---|
keepBetween | boolean | raws.between シンボルを保持します。 |
Container#clone()
既存のノードのクローンを作成します。これには、すべてのプロパティとその値が含まれます。これには raws
が含まれますが、type
は含まれません。
decl.raws.before //=> "\n "
const cloned = decl.clone({ prop: '-moz-' + decl.prop })
cloned.raws.before //=> "\n "
cloned.toString() //=> -moz-transform: scale(0)
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<ContainerProps> | クローンでオーバーライドする新しいプロパティ。 |
Container<Child>
を返します。
Container#cloneAfter()
ノードをクローンし、結果のクローンされたノードを現在のノードの後に挿入するためのショートカット。
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<ContainerProps> | クローンでオーバーライドする新しいプロパティ。 |
Container<Child>
を返します。
Container#cloneBefore()
ノードをクローンし、結果のクローンされたノードを現在のノードの前に挿入するためのショートカット。
decl.cloneBefore({ prop: '-moz-' + decl.prop })
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<ContainerProps> | クローンでオーバーライドする新しいプロパティ。 |
Container<Child>
を返します。
Container#each()
コンテナの直接の子を反復処理し、子ごとに callback
を呼び出します。
コールバックで false
を返すと、反復処理が中断されます。
このメソッドは、コンテナの直接の子のみを反復処理します。コンテナのすべての子孫ノードを再帰的に反復処理する必要がある場合は、Container#walk
を使用します。
for {}
サイクルまたは Array#forEach
とは異なり、このイテレータは、反復処理中に子ノードの配列をミューテートする場合でも安全です。PostCSS は、ミューテーションに合わせて現在のインデックスを調整します。
const root = postcss.parse('a { color: black; z-index: 1 }')
const rule = root.first
for (const decl of rule.nodes) {
decl.cloneBefore({ prop: '-webkit-' + decl.prop })
// Cycle will be infinite, because cloneBefore moves the current node
// to the next index
}
rule.each(decl => {
decl.cloneBefore({ prop: '-webkit-' + decl.prop })
// Will be executed only for color and z-index
})
引数 | 型 | 説明 |
---|---|---|
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Container#error()
クラス CssSyntaxError
のインスタンスを作成し、このメソッドに渡されたパラメータがエラーインスタンスに割り当てられます。
エラーインスタンスには、エラーの説明、ソース内のノードの元の位置、行番号と列番号を示す説明が含まれます。
前のマップが存在する場合は、ソースの元の位置を取得するために使用されます。
ここでの前のマップは、以前のコンパイルで生成されたソースマップを指します。例: Less、Stylus、Sass。
このメソッドは、エラーインスタンスをスローするのではなく返します。
if (!variables[name]) {
throw decl.error(`Unknown variable ${name}`, { word: name })
// CssSyntaxError: postcss-vars:a.sass:4:3: Unknown variable $black
// color: $black
// a
// ^
// background: white
}
引数 | 型 | 説明 |
---|---|---|
message | string | エラーインスタンスの説明。 |
options | NodeErrorOptions | エラーインスタンスのオプション。 |
CssSyntaxError_
を返します。
Container#every()
コールバックがコンテナのすべての子に対して true
を返す場合は true
を返します。
const noPrefixes = rule.every(i => i.prop[0] !== '-')
引数 | 型 | 説明 |
---|---|---|
condition | Function | イテレータは true または false を返します。 |
boolean
を返します。
Container#index()
Container#nodes
配列内の child
のインデックスを返します。
rule.index( rule.nodes[2] ) //=> 2
引数 | 型 | 説明 |
---|---|---|
child | number | Child | 現在のコンテナの子。 |
number
を返します。
Container#insertAfter()
コンテナ内の古いノードの後に新しいノードを挿入します。
引数 | 型 | 説明 |
---|---|---|
oldNode | number | Child | 子または子のインデックス。 |
newNode | string | string[] | Child | ChildProps | ChildProps[] | Child[] | 新しいノード。 |
Container<Child>
を返します。
Container#insertBefore()
コンテナ内の古いノードの前に新しいノードを挿入します。
rule.insertBefore(decl, decl.clone({ prop: '-webkit-' + decl.prop }))
引数 | 型 | 説明 |
---|---|---|
oldNode | number | Child | 子または子のインデックス。 |
newNode | string | string[] | Child | ChildProps | ChildProps[] | Child[] | 新しいノード。 |
Container<Child>
を返します。
Container#next()
ノードの親の次の子を返します。現在のノードが最後の子の場合は undefined
を返します。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
ChildNode
を返します。
Container#nodes
コンテナの子を含む配列。
const root = postcss.parse('a { color: black }')
root.nodes.length //=> 1
root.nodes[0].selector //=> 'a'
root.nodes[0].nodes[0].prop //=> 'color'
型: Child[]。
Container#parent
現在のノードの親を表します。
root.nodes[0].parent === root //=> true
型: Container_<ChildNode> | Document_。
Container#positionBy()
ノード内の単語またはインデックスの位置を取得します。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word"> | オプション。 |
Position
を返します。
Container#positionInside()
文字列のインデックスを行/列に変換します。
引数 | 型 | 説明 |
---|---|---|
index | number | ノードの文字列内の記号の番号。 |
Position
を返します。
Container#prepend()
コンテナの先頭に新しいノードを挿入します。
const decl1 = new Declaration({ prop: 'color', value: 'black' })
const decl2 = new Declaration({ prop: 'background-color', value: 'white' })
rule.prepend(decl1, decl2)
root.append({ name: 'charset', params: '"UTF-8"' }) // at-rule
root.append({ selector: 'a' }) // rule
rule.append({ prop: 'color', value: 'black' }) // declaration
rule.append({ text: 'Comment' }) // comment
root.append('a {}')
root.first.append('color: black; z-index: 1')
引数 | 型 | 説明 |
---|---|---|
nodes… | (string | string[] | Node | ChildProps | ChildProps[] | Node[])[] | 新しいノード。 |
Container<Child>
を返します。
Container#prev()
ノードの親の前の子を返します。現在のノードが最初の子である場合は、undefined
を返します。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
ChildNode
を返します。
Container#push()
ノードの末尾に子を追加します。
rule.push(new Declaration({ prop: 'color', value: 'black' }))
引数 | 型 | 説明 |
---|---|---|
child | Child | 新しいノード。 |
Container<Child>
を返します。
Container#rangeBy()
ノード内の単語または開始インデックスと終了インデックスの範囲を取得します。開始インデックスは包括的で、終了インデックスは排他的です。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word" | "endIndex"> | オプション。 |
Range
を返します。
Container#raw()
raws
値を返します。ノードにコードスタイルプロパティがない場合(ノードが手動で作成または複製されたため)、PostCSSはツリー内の他のノードを調べてコードスタイルプロパティを自動検出しようとします。
const root = postcss.parse('a { background: white }')
root.nodes[0].append({ prop: 'color', value: 'black' })
root.nodes[0].nodes[1].raws.before //=> undefined
root.nodes[0].nodes[1].raw('before') //=> ' '
引数 | 型 | 説明 |
---|---|---|
prop | string | コードスタイルプロパティの名前。 |
defaultType | string | デフォルト値の名前。値が prop と同じ場合は省略できます。 |
string
を返します。
Container#raws
CSSソースコードに存在する不要な空白と文字を表します。
元の入力と同じように、バイト単位で等しいノード文字列を生成するための情報。
raws オブジェクトのプロパティはパーサーによって決定されます。デフォルトのパーサーは次のプロパティを使用します
before
: ノードの前の空白記号。宣言の前の*
および_
記号も保存します(IEハック)。after
: ノードの最後の子からノードの末尾までの空白記号。between
: 宣言の場合はプロパティと値の間、ルールの場合はセレクタと{
の間、または at-rule の場合は最後のパラメータと{
の間の記号。semicolon
: 最後の子が(オプションの)セミコロンを持っている場合は true が含まれます。afterName
: at-rule 名とそのパラメータの間の空白。left
:/*
とコメントのテキストの間の空白記号。right
: コメントのテキストと */ の間の空白記号。
important
: important ステートメントの内容。単に!important
ではない場合。
PostCSSはセレクタ、宣言値、および at-rule パラメータ内のコメントをフィルタリングしますが、raws に元のコンテンツを保存します。
const root = postcss.parse('a {\n color:black\n}')
root.first.first.raws //=> { before: '\n ', between: ':' }
型: any。
Container#remove()
ノードをその親から削除し、親プロパティを削除します。
if (decl.prop.match(/^-webkit-/)) {
decl.remove()
}
Container<Child>
を返します。
Container#removeAll()
コンテナからすべての子を削除し、それらの親プロパティをクリアします。
rule.removeAll()
rule.nodes.length //=> 0
Container<Child>
を返します。
Container#removeChild()
ノードをコンテナから削除し、ノードとその子から親プロパティをクリアします。
rule.nodes.length //=> 5
rule.removeChild(decl)
rule.nodes.length //=> 4
decl.parent //=> undefined
引数 | 型 | 説明 |
---|---|---|
child | number | Child | 子または子のインデックス。 |
Container<Child>
を返します。
Container#replaceValues()
引数 | 型 |
---|---|
pattern | string | RegExp |
replaced | string | Function |
引数 | 型 | 説明 |
---|---|---|
pattern | string | RegExp | パターンを置換します。 |
options | ValueOptions | |
replaced | string | Function |
Container<Child>
を返します。
Container#replaceWith()
現在のノードの前にノードを挿入し、現在のノードを削除します。
AtRule: {
mixin: atrule => {
atrule.replaceWith(mixinRules[atrule.params])
}
}
引数 | 型 | 説明 |
---|---|---|
nodes… | (ChildNode | ChildProps | ChildNode[] | ChildProps[])[] | 現在のノードを置き換えるモード。 |
Container<Child>
を返します。
Container#root()
ノードのツリーの Root インスタンスを検索します。
root.nodes[0].nodes[0].root() === root
Root
を返します。
Container#some()
コンテナの子の少なくとも1つに対してコールバックが true
を返す場合は、true
を返します。
const hasPrefix = rule.some(i => i.prop[0] === '-')
引数 | 型 | 説明 |
---|---|---|
condition | Function | イテレータは true または false を返します。 |
boolean
を返します。
Container#source
ノードの起源に関する情報を表し、ソースマップの生成に必要です。
PostCSSが提供するパブリックAPIを使用して手動で作成されたノードは、source
が未定義になり、ソースマップに表示されません。
このため、プラグイン開発者は、複製されたノードはデフォルトで元のノードと同じソースを持つか、手動で作成されたノードにソースを割り当てるため、ノードを複製することを検討する必要があります。
decl.source.input.from //=> '/home/ai/source.css'
decl.source.start //=> { line: 10, column: 2 }
decl.source.end //=> { line: 10, column: 12 }
// Incorrect method, source not specified!
const prefixed = postcss.decl({
prop: '-moz-' + decl.prop,
value: decl.value
})
// Correct method, source is inherited when duplicating.
const prefixed = decl.clone({
prop: '-moz-' + decl.prop
})
if (atrule.name === 'add-link') {
const rule = postcss.rule({
selector: 'a',
source: atrule.source
})
atrule.parent.insertBefore(atrule, rule)
}
型: Source。
Container#toJSON()
JSON.stringify()
の循環リンクを修正します。
object
を返します。
Container#toString()
ノードの型に応じて、ノードをブラウザで読み取り可能なカスケーディングスタイルシート文字列にコンパイルします。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引数 | 型 | 説明 |
---|---|---|
stringifier | Stringifier | Syntax<Document_ | Root> | 文字列生成で使用する構文。 |
string
を返します。
Container#type
抽象構文木におけるノードの型を表します。
ノードの型は、ノードの識別と型に基づいた操作の実行に役立ちます。
const declaration = new Declaration({
prop: 'color',
value: 'black'
})
declaration.type //=> 'decl'
型: string。
Container#walk()
コンテナの子孫ノードをトラバースし、各ノードに対してコールバックを呼び出します。
container.each() と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
コンテナの直下の子のみを反復する必要がある場合は、Container#each
を使用します。
root.walk(node => {
// Traverses all descendant nodes.
})
引数 | 型 | 説明 |
---|---|---|
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Container#walkAtRules()
コンテナの子孫ノードをトラバースし、各 at-rule ノードに対してコールバックを呼び出します。
フィルタを渡すと、名前が一致する at-rule のみに対して反復が行われます。
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
root.walkAtRules(rule => {
if (isOld(rule.name)) rule.remove()
})
let first = false
root.walkAtRules('charset', rule => {
if (!first) {
first = true
} else {
rule.remove()
}
})
引数 | 型 | 説明 |
---|---|---|
nameFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Container#walkComments()
引数 | 型 |
---|---|
callback | Function |
void | false
を返します。
Container#walkDecls()
コンテナの子孫ノードをトラバースし、各宣言ノードに対してコールバックを呼び出します。
フィルタを渡すと、プロパティが一致する宣言のみに対して反復が行われます。
root.walkDecls(decl => {
checkPropertySupport(decl.prop)
})
root.walkDecls('border-radius', decl => {
decl.remove()
})
root.walkDecls(/^background/, decl => {
decl.value = takeFirstColorFromGradient(decl.value)
})
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
引数 | 型 | 説明 |
---|---|---|
propFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Container#walkRules()
コンテナの子孫ノードをトラバースし、各ルールノードに対してコールバックを呼び出します。
フィルタを渡すと、セレクタが一致するルールのみに対して反復が行われます。
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
const selectors = []
root.walkRules(rule => {
selectors.push(rule.selector)
})
console.log(`Your CSS uses ${ selectors.length } selectors`)
引数 | 型 | 説明 |
---|---|---|
selectorFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Container#warn()
Result#warn のラッパーであり、警告を生成する便利な方法を提供します。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引数 | 型 | 説明 |
---|---|---|
result | Result<Document_ | Root> | 警告を受け取る Result インスタンス。 |
message | string | 警告の説明。 |
options | WarningOptions | 警告のオプション。 |
Warning
を返します。
CssSyntaxError
CSSパーサーは、壊れたCSSに対してこのエラーをスローします。
カスタムパーサーは、Node#error
メソッドを使用して、壊れたカスタム構文に対してこのエラーをスローできます。
PostCSS は、入力ソースマップを使用して、エラーの元の場所を検出します。Sass ファイルを作成し、それを CSS にコンパイルしてから PostCSS で解析した場合、PostCSS は Sass ファイル内の元の位置を表示します。
PostCSS 入力内の位置が必要な場合 (例: 前のコンパイラをデバッグする場合) は、error.input.file
を使用します。
// Raising error from plugin
throw node.error('Unknown variable', { plugin: 'postcss-vars' })
// Catching and checking syntax error
try {
postcss.parse('a{')
} catch (error) {
if (error.name === 'CssSyntaxError') {
error //=> CssSyntaxError
}
}
CssSyntaxError#column
エラーのソース列。
error.column //=> 1
error.input.column //=> 4
PostCSS は、入力ソースマップを使用して元の場所を検出します。PostCSS 入力内の位置が必要な場合は、error.input.column
を使用します。
型: number。
CssSyntaxError#endColumn
エラーの終了位置のソース列 (排他的)。エラーが範囲に関連する場合に提供されます。
error.endColumn //=> 1
error.input.endColumn //=> 4
PostCSS は、入力ソースマップを使用して元の場所を検出します。PostCSS 入力内の位置が必要な場合は、error.input.endColumn
を使用します。
型: number。
CssSyntaxError#endLine
エラーの終了位置のソース行 (排他的)。エラーが範囲に関連する場合に提供されます。
error.endLine //=> 3
error.input.endLine //=> 4
PostCSS は、入力ソースマップを使用して元の場所を検出します。PostCSS 入力内の位置が必要な場合は、error.input.endLine
を使用します。
型: number。
CssSyntaxError#file
破損したファイルへの絶対パス。
error.file //=> 'a.sass'
error.input.file //=> 'a.css'
PostCSS は、入力ソースマップを使用して元の場所を検出します。PostCSS 入力内の位置が必要な場合は、error.input.file
を使用します。
型: string。
CssSyntaxError#input
入力ファイルに関する PostCSS 内部情報を含む入力オブジェクト。入力に前のツールからのソースマップがある場合、PostCSS は元の (たとえば、Sass) ソースを使用します。このオブジェクトを使用して、PostCSS 入力ソースを取得できます。
error.input.file //=> 'a.css'
error.file //=> 'a.sass'
型: FilePosition。
CssSyntaxError#line
エラーのソース行。
error.line //=> 2
error.input.line //=> 4
PostCSS は、入力ソースマップを使用して元の場所を検出します。PostCSS 入力内の位置が必要な場合は、error.input.line
を使用します。
型: number。
CssSyntaxError#message
プラグイン、ファイル、行、および列を含む GNU エラー形式の完全なエラーテキスト。
error.message //=> 'a.css:1:1: Unclosed block'
型: string。
CssSyntaxError#name
常に 'CssSyntaxError'
に等しくなります。npm に複数の PostCSS バージョンが存在する可能性があるため、常に error instanceof CssSyntaxError
ではなく、error.name === 'CssSyntaxError'
でエラー型を確認する必要があります。
if (error.name === 'CssSyntaxError') {
error //=> CssSyntaxError
}
型: "CssSyntaxError"。
CssSyntaxError#plugin
エラーがプラグインから発生した場合のプラグイン名。
error.plugin //=> 'postcss-vars'
型: string。
CssSyntaxError#reason
エラーメッセージ。
error.message //=> 'Unclosed block'
型: string。
CssSyntaxError#showSourceCode()
エラーの原因となった CSS ソースの数行を返します。
CSS に sourceContent
を含まない入力ソースマップがある場合、このメソッドは空の文字列を返します。
error.showSourceCode() //=> " 4 | }
// 5 | a {
// > 6 | bad
// | ^
// 7 | }
// 8 | b {"
引数 | 型 | 説明 |
---|---|---|
color | boolean | 矢印がターミナルのカラーコードで赤く色付けされるかどうか。デフォルトでは、PostCSS は process.stdout.isTTY と process.env.NODE_DISABLE_COLORS によってカラーサポートを検出します。 |
string
を返します。
CssSyntaxError#source
破損したファイルのソースコード。
error.source //=> 'a { b {} }'
error.input.source //=> 'a b { }'
型: string。
CssSyntaxError#stack
型: string。
CssSyntaxError#toString()
エラー位置、メッセージ、および破損部分のソースコードを返します。
error.toString() //=> "CssSyntaxError: app.css:1:1: Unclosed block
// > 1 | a {
// | ^"
string
を返します。
Declaration
CSS 宣言を処理するクラスを表します。
Once (root, { Declaration }) {
const color = new Declaration({ prop: 'color', value: 'black' })
root.append(color)
}
const root = postcss.parse('a { color: black }')
const decl = root.first?.first
decl.type //=> 'decl'
decl.toString() //=> ' color: black'
Declaration#after()
現在のノードの親に、現在のノードの後に新しいノードを挿入します。
node.parent.insertAfter(node, add)
のエイリアス。
decl.after('color: black')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Declaration
を返します。
Declaration#assign()
既存のノードインスタンスにプロパティを割り当てます。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引数 | 型 | 説明 |
---|---|---|
overrides | object | DeclarationProps | ノードをオーバーライドするための新しいプロパティ。 |
Declaration
を返します。
Declaration#before()
現在のノードの親に、現在のノードの前に新しいノードを挿入します。
node.parent.insertBefore(node, add)
のエイリアス。
decl.before('content: ""')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Declaration
を返します。
Declaration#cleanRaws()
ノードとその子に対するコードスタイルのプロパティをクリアします。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引数 | 型 | 説明 |
---|---|---|
keepBetween | boolean | raws.between シンボルを保持します。 |
Declaration#clone()
既存のノードのクローンを作成します。これには、すべてのプロパティとその値が含まれます。これには raws
が含まれますが、type
は含まれません。
decl.raws.before //=> "\n "
const cloned = decl.clone({ prop: '-moz-' + decl.prop })
cloned.raws.before //=> "\n "
cloned.toString() //=> -moz-transform: scale(0)
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<DeclarationProps> | クローンでオーバーライドする新しいプロパティ。 |
Declaration
を返します。
Declaration#cloneAfter()
ノードをクローンし、結果のクローンされたノードを現在のノードの後に挿入するためのショートカット。
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<DeclarationProps> | クローンでオーバーライドする新しいプロパティ。 |
Declaration
を返します。
Declaration#cloneBefore()
ノードをクローンし、結果のクローンされたノードを現在のノードの前に挿入するためのショートカット。
decl.cloneBefore({ prop: '-moz-' + decl.prop })
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<DeclarationProps> | クローンでオーバーライドする新しいプロパティ。 |
Declaration
を返します。
Declaration#error()
クラス CssSyntaxError
のインスタンスを作成し、このメソッドに渡されたパラメータがエラーインスタンスに割り当てられます。
エラーインスタンスには、エラーの説明、ソース内のノードの元の位置、行番号と列番号を示す説明が含まれます。
前のマップが存在する場合は、ソースの元の位置を取得するために使用されます。
ここでの前のマップは、以前のコンパイルで生成されたソースマップを指します。例: Less、Stylus、Sass。
このメソッドは、エラーインスタンスをスローするのではなく返します。
if (!variables[name]) {
throw decl.error(`Unknown variable ${name}`, { word: name })
// CssSyntaxError: postcss-vars:a.sass:4:3: Unknown variable $black
// color: $black
// a
// ^
// background: white
}
引数 | 型 | 説明 |
---|---|---|
message | string | エラーインスタンスの説明。 |
options | NodeErrorOptions | エラーインスタンスのオプション。 |
CssSyntaxError_
を返します。
Declaration#important
宣言の優先度を表します。
true の場合、CSS 宣言には important 指定子が付きます。
const root = postcss.parse('a { color: black !important; color: red }')
root.first.first.important //=> true
root.first.last.important //=> undefined
型: boolean。
Declaration#next()
ノードの親の次の子を返します。現在のノードが最後の子の場合は undefined
を返します。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
ChildNode
を返します。
Declaration#parent
現在のノードの親を表します。
root.nodes[0].parent === root //=> true
型: Container_<ChildNode>。
Declaration#positionBy()
ノード内の単語またはインデックスの位置を取得します。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word"> | オプション。 |
Position
を返します。
Declaration#positionInside()
文字列のインデックスを行/列に変換します。
引数 | 型 | 説明 |
---|---|---|
index | number | ノードの文字列内の記号の番号。 |
Position
を返します。
Declaration#prev()
ノードの親の前の子を返します。現在のノードが最初の子である場合は、undefined
を返します。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
ChildNode
を返します。
Declaration#prop
CSS 宣言のプロパティ名。
const root = postcss.parse('a { color: black }')
const decl = root.first.first
decl.prop //=> 'color'
型: string。
Declaration#rangeBy()
ノード内の単語または開始インデックスと終了インデックスの範囲を取得します。開始インデックスは包括的で、終了インデックスは排他的です。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word" | "endIndex"> | オプション。 |
Range
を返します。
Declaration#raw()
raws
値を返します。ノードにコードスタイルプロパティがない場合(ノードが手動で作成または複製されたため)、PostCSSはツリー内の他のノードを調べてコードスタイルプロパティを自動検出しようとします。
const root = postcss.parse('a { background: white }')
root.nodes[0].append({ prop: 'color', value: 'black' })
root.nodes[0].nodes[1].raws.before //=> undefined
root.nodes[0].nodes[1].raw('before') //=> ' '
引数 | 型 | 説明 |
---|---|---|
prop | string | コードスタイルプロパティの名前。 |
defaultType | string | デフォルト値の名前。値が prop と同じ場合は省略できます。 |
string
を返します。
Declaration#raws
CSSソースコードに存在する不要な空白と文字を表します。
元の入力と同じように、バイト単位で等しいノード文字列を生成するための情報。
raws オブジェクトのプロパティはパーサーによって決定されます。デフォルトのパーサーは次のプロパティを使用します
before
: ノードの前の空白記号。宣言の前の*
および_
記号も保存します(IEハック)。after
: ノードの最後の子からノードの末尾までの空白記号。between
: 宣言の場合はプロパティと値の間、ルールの場合はセレクタと{
の間、または at-rule の場合は最後のパラメータと{
の間の記号。semicolon
: 最後の子が(オプションの)セミコロンを持っている場合は true が含まれます。afterName
: at-rule 名とそのパラメータの間の空白。left
:/*
とコメントのテキストの間の空白記号。right
: コメントのテキストと */ の間の空白記号。
important
: important ステートメントの内容。単に!important
ではない場合。
PostCSSはセレクタ、宣言値、および at-rule パラメータ内のコメントをフィルタリングしますが、raws に元のコンテンツを保存します。
const root = postcss.parse('a {\n color:black\n}')
root.first.first.raws //=> { before: '\n ', between: ':' }
型: DeclarationRaws。
Declaration#remove()
ノードをその親から削除し、親プロパティを削除します。
if (decl.prop.match(/^-webkit-/)) {
decl.remove()
}
Declaration
を返します。
Declaration#replaceWith()
現在のノードの前にノードを挿入し、現在のノードを削除します。
AtRule: {
mixin: atrule => {
atrule.replaceWith(mixinRules[atrule.params])
}
}
引数 | 型 | 説明 |
---|---|---|
nodes… | (ChildNode | ChildProps | ChildNode[] | ChildProps[])[] | 現在のノードを置き換えるモード。 |
Declaration
を返します。
Declaration#root()
ノードのツリーの Root インスタンスを検索します。
root.nodes[0].nodes[0].root() === root
Root
を返します。
Declaration#source
ノードの起源に関する情報を表し、ソースマップの生成に必要です。
PostCSSが提供するパブリックAPIを使用して手動で作成されたノードは、source
が未定義になり、ソースマップに表示されません。
このため、プラグイン開発者は、複製されたノードはデフォルトで元のノードと同じソースを持つか、手動で作成されたノードにソースを割り当てるため、ノードを複製することを検討する必要があります。
decl.source.input.from //=> '/home/ai/source.css'
decl.source.start //=> { line: 10, column: 2 }
decl.source.end //=> { line: 10, column: 12 }
// Incorrect method, source not specified!
const prefixed = postcss.decl({
prop: '-moz-' + decl.prop,
value: decl.value
})
// Correct method, source is inherited when duplicating.
const prefixed = decl.clone({
prop: '-moz-' + decl.prop
})
if (atrule.name === 'add-link') {
const rule = postcss.rule({
selector: 'a',
source: atrule.source
})
atrule.parent.insertBefore(atrule, rule)
}
型: Source。
Declaration#toJSON()
JSON.stringify()
の循環リンクを修正します。
object
を返します。
Declaration#toString()
ノードの型に応じて、ノードをブラウザで読み取り可能なカスケーディングスタイルシート文字列にコンパイルします。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引数 | 型 | 説明 |
---|---|---|
stringifier | Stringifier | Syntax<Document_ | Root> | 文字列生成で使用する構文。 |
string
を返します。
Declaration#type
抽象構文木におけるノードの型を表します。
ノードの型は、ノードの識別と型に基づいた操作の実行に役立ちます。
const declaration = new Declaration({
prop: 'color',
value: 'black'
})
declaration.type //=> 'decl'
型: "decl"。
Declaration#value
CSS 宣言のプロパティ値。
値文字列内の CSS コメントはすべてフィルタリングされます。ソース値に存在する CSS コメントは、raws
プロパティで利用できます。
新しい value
を割り当てると、ノードを文字列にコンパイルする際に raws
プロパティのコメントは無視されます。
const root = postcss.parse('a { color: black }')
const decl = root.first.first
decl.value //=> 'black'
型: string。
Declaration#variable
CSS および SASS/SCSS で変数を宣言するために使用される、宣言が --
または $
で始まる場合に true
を返すゲッターを表します。
const root = postcss.parse(':root { --one: 1 }')
const one = root.first.first
one.variable //=> true
const root = postcss.parse('$one: 1')
const one = root.first
one.variable //=> true
型: boolean。
Declaration#warn()
Result#warn のラッパーであり、警告を生成する便利な方法を提供します。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引数 | 型 | 説明 |
---|---|---|
result | Result<Document_ | Root> | 警告を受け取る Result インスタンス。 |
message | string | 警告の説明。 |
options | WarningOptions | 警告のオプション。 |
Warning
を返します。
Document
ファイルを表現し、解析されたすべてのノードが含まれています。
実験的: このノードの一部の側面は、マイナーまたはパッチバージョンのリリースで変更される可能性があります。
const document = htmlParser(
'<html><style>a{color:black}</style><style>b{z-index:2}</style>'
)
document.type //=> 'document'
document.nodes.length //=> 2
Document#after()
現在のノードの親に、現在のノードの後に新しいノードを挿入します。
node.parent.insertAfter(node, add)
のエイリアス。
decl.after('color: black')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Document
を返します。
Document#append()
コンテナの末尾に新しいノードを挿入します。
const decl1 = new Declaration({ prop: 'color', value: 'black' })
const decl2 = new Declaration({ prop: 'background-color', value: 'white' })
rule.append(decl1, decl2)
root.append({ name: 'charset', params: '"UTF-8"' }) // at-rule
root.append({ selector: 'a' }) // rule
rule.append({ prop: 'color', value: 'black' }) // declaration
rule.append({ text: 'Comment' }) // comment
root.append('a {}')
root.first.append('color: black; z-index: 1')
引数 | 型 | 説明 |
---|---|---|
nodes… | (string | string[] | Node | ChildProps | ChildProps[] | Node[])[] | 新しいノード。 |
Document
を返します。
Document#assign()
既存のノードインスタンスにプロパティを割り当てます。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引数 | 型 | 説明 |
---|---|---|
overrides | object | DocumentProps | ノードをオーバーライドするための新しいプロパティ。 |
Document
を返します。
Document#before()
現在のノードの親に、現在のノードの前に新しいノードを挿入します。
node.parent.insertBefore(node, add)
のエイリアス。
decl.before('content: ""')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Document
を返します。
Document#cleanRaws()
ノードとその子に対するコードスタイルのプロパティをクリアします。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引数 | 型 | 説明 |
---|---|---|
keepBetween | boolean | raws.between シンボルを保持します。 |
Document#clone()
既存のノードのクローンを作成します。これには、すべてのプロパティとその値が含まれます。これには raws
が含まれますが、type
は含まれません。
decl.raws.before //=> "\n "
const cloned = decl.clone({ prop: '-moz-' + decl.prop })
cloned.raws.before //=> "\n "
cloned.toString() //=> -moz-transform: scale(0)
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<DocumentProps> | クローンでオーバーライドする新しいプロパティ。 |
Document
を返します。
Document#cloneAfter()
ノードをクローンし、結果のクローンされたノードを現在のノードの後に挿入するためのショートカット。
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<DocumentProps> | クローンでオーバーライドする新しいプロパティ。 |
Document
を返します。
Document#cloneBefore()
ノードをクローンし、結果のクローンされたノードを現在のノードの前に挿入するためのショートカット。
decl.cloneBefore({ prop: '-moz-' + decl.prop })
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<DocumentProps> | クローンでオーバーライドする新しいプロパティ。 |
Document
を返します。
Document#each()
コンテナの直接の子を反復処理し、子ごとに callback
を呼び出します。
コールバックで false
を返すと、反復処理が中断されます。
このメソッドは、コンテナの直接の子のみを反復処理します。コンテナのすべての子孫ノードを再帰的に反復処理する必要がある場合は、Container#walk
を使用します。
for {}
サイクルまたは Array#forEach
とは異なり、このイテレータは、反復処理中に子ノードの配列をミューテートする場合でも安全です。PostCSS は、ミューテーションに合わせて現在のインデックスを調整します。
const root = postcss.parse('a { color: black; z-index: 1 }')
const rule = root.first
for (const decl of rule.nodes) {
decl.cloneBefore({ prop: '-webkit-' + decl.prop })
// Cycle will be infinite, because cloneBefore moves the current node
// to the next index
}
rule.each(decl => {
decl.cloneBefore({ prop: '-webkit-' + decl.prop })
// Will be executed only for color and z-index
})
引数 | 型 | 説明 |
---|---|---|
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Document#error()
クラス CssSyntaxError
のインスタンスを作成し、このメソッドに渡されたパラメータがエラーインスタンスに割り当てられます。
エラーインスタンスには、エラーの説明、ソース内のノードの元の位置、行番号と列番号を示す説明が含まれます。
前のマップが存在する場合は、ソースの元の位置を取得するために使用されます。
ここでの前のマップは、以前のコンパイルで生成されたソースマップを指します。例: Less、Stylus、Sass。
このメソッドは、エラーインスタンスをスローするのではなく返します。
if (!variables[name]) {
throw decl.error(`Unknown variable ${name}`, { word: name })
// CssSyntaxError: postcss-vars:a.sass:4:3: Unknown variable $black
// color: $black
// a
// ^
// background: white
}
引数 | 型 | 説明 |
---|---|---|
message | string | エラーインスタンスの説明。 |
options | NodeErrorOptions | エラーインスタンスのオプション。 |
CssSyntaxError_
を返します。
Document#every()
コールバックがコンテナのすべての子に対して true
を返す場合は true
を返します。
const noPrefixes = rule.every(i => i.prop[0] !== '-')
引数 | 型 | 説明 |
---|---|---|
condition | Function | イテレータは true または false を返します。 |
boolean
を返します。
Document#index()
Container#nodes
配列内の child
のインデックスを返します。
rule.index( rule.nodes[2] ) //=> 2
引数 | 型 | 説明 |
---|---|---|
child | number | Root | 現在のコンテナの子。 |
number
を返します。
Document#insertAfter()
コンテナ内の古いノードの後に新しいノードを挿入します。
引数 | 型 | 説明 |
---|---|---|
oldNode | number | Root | 子または子のインデックス。 |
newNode | string | string[] | ChildProps | ChildProps[] | Root | Root[] | 新しいノード。 |
Document
を返します。
Document#insertBefore()
コンテナ内の古いノードの前に新しいノードを挿入します。
rule.insertBefore(decl, decl.clone({ prop: '-webkit-' + decl.prop }))
引数 | 型 | 説明 |
---|---|---|
oldNode | number | Root | 子または子のインデックス。 |
newNode | string | string[] | ChildProps | ChildProps[] | Root | Root[] | 新しいノード。 |
Document
を返します。
Document#next()
ノードの親の次の子を返します。現在のノードが最後の子の場合は undefined
を返します。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
ChildNode
を返します。
Document#nodes
コンテナの子を含む配列。
const root = postcss.parse('a { color: black }')
root.nodes.length //=> 1
root.nodes[0].selector //=> 'a'
root.nodes[0].nodes[0].prop //=> 'color'
型: Root[]。
Document#parent
現在のノードの親を表します。
root.nodes[0].parent === root //=> true
型: undefined。
Document#positionBy()
ノード内の単語またはインデックスの位置を取得します。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word"> | オプション。 |
Position
を返します。
Document#positionInside()
文字列のインデックスを行/列に変換します。
引数 | 型 | 説明 |
---|---|---|
index | number | ノードの文字列内の記号の番号。 |
Position
を返します。
Document#prepend()
コンテナの先頭に新しいノードを挿入します。
const decl1 = new Declaration({ prop: 'color', value: 'black' })
const decl2 = new Declaration({ prop: 'background-color', value: 'white' })
rule.prepend(decl1, decl2)
root.append({ name: 'charset', params: '"UTF-8"' }) // at-rule
root.append({ selector: 'a' }) // rule
rule.append({ prop: 'color', value: 'black' }) // declaration
rule.append({ text: 'Comment' }) // comment
root.append('a {}')
root.first.append('color: black; z-index: 1')
引数 | 型 | 説明 |
---|---|---|
nodes… | (string | string[] | Node | ChildProps | ChildProps[] | Node[])[] | 新しいノード。 |
Document
を返します。
Document#prev()
ノードの親の前の子を返します。現在のノードが最初の子である場合は、undefined
を返します。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
ChildNode
を返します。
Document#push()
ノードの末尾に子を追加します。
rule.push(new Declaration({ prop: 'color', value: 'black' }))
引数 | 型 | 説明 |
---|---|---|
child | Root | 新しいノード。 |
Document
を返します。
Document#rangeBy()
ノード内の単語または開始インデックスと終了インデックスの範囲を取得します。開始インデックスは包括的で、終了インデックスは排他的です。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word" | "endIndex"> | オプション。 |
Range
を返します。
Document#raw()
raws
値を返します。ノードにコードスタイルプロパティがない場合(ノードが手動で作成または複製されたため)、PostCSSはツリー内の他のノードを調べてコードスタイルプロパティを自動検出しようとします。
const root = postcss.parse('a { background: white }')
root.nodes[0].append({ prop: 'color', value: 'black' })
root.nodes[0].nodes[1].raws.before //=> undefined
root.nodes[0].nodes[1].raw('before') //=> ' '
引数 | 型 | 説明 |
---|---|---|
prop | string | コードスタイルプロパティの名前。 |
defaultType | string | デフォルト値の名前。値が prop と同じ場合は省略できます。 |
string
を返します。
Document#raws
CSSソースコードに存在する不要な空白と文字を表します。
元の入力と同じように、バイト単位で等しいノード文字列を生成するための情報。
raws オブジェクトのプロパティはパーサーによって決定されます。デフォルトのパーサーは次のプロパティを使用します
before
: ノードの前の空白記号。宣言の前の*
および_
記号も保存します(IEハック)。after
: ノードの最後の子からノードの末尾までの空白記号。between
: 宣言の場合はプロパティと値の間、ルールの場合はセレクタと{
の間、または at-rule の場合は最後のパラメータと{
の間の記号。semicolon
: 最後の子が(オプションの)セミコロンを持っている場合は true が含まれます。afterName
: at-rule 名とそのパラメータの間の空白。left
:/*
とコメントのテキストの間の空白記号。right
: コメントのテキストと */ の間の空白記号。
important
: important ステートメントの内容。単に!important
ではない場合。
PostCSSはセレクタ、宣言値、および at-rule パラメータ内のコメントをフィルタリングしますが、raws に元のコンテンツを保存します。
const root = postcss.parse('a {\n color:black\n}')
root.first.first.raws //=> { before: '\n ', between: ':' }
型: any。
Document#remove()
ノードをその親から削除し、親プロパティを削除します。
if (decl.prop.match(/^-webkit-/)) {
decl.remove()
}
Document
を返します。
Document#removeAll()
コンテナからすべての子を削除し、それらの親プロパティをクリアします。
rule.removeAll()
rule.nodes.length //=> 0
Document
を返します。
Document#removeChild()
ノードをコンテナから削除し、ノードとその子から親プロパティをクリアします。
rule.nodes.length //=> 5
rule.removeChild(decl)
rule.nodes.length //=> 4
decl.parent //=> undefined
引数 | 型 | 説明 |
---|---|---|
child | number | Root | 子または子のインデックス。 |
Document
を返します。
Document#replaceValues()
引数 | 型 |
---|---|
pattern | string | RegExp |
replaced | string | Function |
引数 | 型 | 説明 |
---|---|---|
pattern | string | RegExp | パターンを置換します。 |
options | ValueOptions | |
replaced | string | Function |
Document
を返します。
Document#replaceWith()
現在のノードの前にノードを挿入し、現在のノードを削除します。
AtRule: {
mixin: atrule => {
atrule.replaceWith(mixinRules[atrule.params])
}
}
引数 | 型 | 説明 |
---|---|---|
nodes… | (ChildNode | ChildProps | ChildNode[] | ChildProps[])[] | 現在のノードを置き換えるモード。 |
Document
を返します。
Document#root()
ノードのツリーの Root インスタンスを検索します。
root.nodes[0].nodes[0].root() === root
Root
を返します。
Document#some()
コンテナの子の少なくとも1つに対してコールバックが true
を返す場合は、true
を返します。
const hasPrefix = rule.some(i => i.prop[0] === '-')
引数 | 型 | 説明 |
---|---|---|
condition | Function | イテレータは true または false を返します。 |
boolean
を返します。
Document#source
ノードの起源に関する情報を表し、ソースマップの生成に必要です。
PostCSSが提供するパブリックAPIを使用して手動で作成されたノードは、source
が未定義になり、ソースマップに表示されません。
このため、プラグイン開発者は、複製されたノードはデフォルトで元のノードと同じソースを持つか、手動で作成されたノードにソースを割り当てるため、ノードを複製することを検討する必要があります。
decl.source.input.from //=> '/home/ai/source.css'
decl.source.start //=> { line: 10, column: 2 }
decl.source.end //=> { line: 10, column: 12 }
// Incorrect method, source not specified!
const prefixed = postcss.decl({
prop: '-moz-' + decl.prop,
value: decl.value
})
// Correct method, source is inherited when duplicating.
const prefixed = decl.clone({
prop: '-moz-' + decl.prop
})
if (atrule.name === 'add-link') {
const rule = postcss.rule({
selector: 'a',
source: atrule.source
})
atrule.parent.insertBefore(atrule, rule)
}
型: Source。
Document#toJSON()
JSON.stringify()
の循環リンクを修正します。
object
を返します。
Document#toResult()
ドキュメントの CSS ルートを表す Result
インスタンスを返します。
const root1 = postcss.parse(css1, { from: 'a.css' })
const root2 = postcss.parse(css2, { from: 'b.css' })
const document = postcss.document()
document.append(root1)
document.append(root2)
const result = document.toResult({ to: 'all.css', map: true })
引数 | 型 |
---|---|
options | ProcessOptions<Document_ | Root> |
Result<Document_ | Root>
を返します。
Document#toString()
ノードの型に応じて、ノードをブラウザで読み取り可能なカスケーディングスタイルシート文字列にコンパイルします。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引数 | 型 | 説明 |
---|---|---|
stringifier | Stringifier | Syntax<Document_ | Root> | 文字列生成で使用する構文。 |
string
を返します。
Document#type
抽象構文木におけるノードの型を表します。
ノードの型は、ノードの識別と型に基づいた操作の実行に役立ちます。
const declaration = new Declaration({
prop: 'color',
value: 'black'
})
declaration.type //=> 'decl'
型: "document"。
Document#walk()
コンテナの子孫ノードをトラバースし、各ノードに対してコールバックを呼び出します。
container.each() と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
コンテナの直下の子のみを反復する必要がある場合は、Container#each
を使用します。
root.walk(node => {
// Traverses all descendant nodes.
})
引数 | 型 | 説明 |
---|---|---|
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Document#walkAtRules()
コンテナの子孫ノードをトラバースし、各 at-rule ノードに対してコールバックを呼び出します。
フィルタを渡すと、名前が一致する at-rule のみに対して反復が行われます。
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
root.walkAtRules(rule => {
if (isOld(rule.name)) rule.remove()
})
let first = false
root.walkAtRules('charset', rule => {
if (!first) {
first = true
} else {
rule.remove()
}
})
引数 | 型 | 説明 |
---|---|---|
nameFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Document#walkComments()
引数 | 型 |
---|---|
callback | Function |
void | false
を返します。
Document#walkDecls()
コンテナの子孫ノードをトラバースし、各宣言ノードに対してコールバックを呼び出します。
フィルタを渡すと、プロパティが一致する宣言のみに対して反復が行われます。
root.walkDecls(decl => {
checkPropertySupport(decl.prop)
})
root.walkDecls('border-radius', decl => {
decl.remove()
})
root.walkDecls(/^background/, decl => {
decl.value = takeFirstColorFromGradient(decl.value)
})
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
引数 | 型 | 説明 |
---|---|---|
propFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Document#walkRules()
コンテナの子孫ノードをトラバースし、各ルールノードに対してコールバックを呼び出します。
フィルタを渡すと、セレクタが一致するルールのみに対して反復が行われます。
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
const selectors = []
root.walkRules(rule => {
selectors.push(rule.selector)
})
console.log(`Your CSS uses ${ selectors.length } selectors`)
引数 | 型 | 説明 |
---|---|---|
selectorFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Document#warn()
Result#warn のラッパーであり、警告を生成する便利な方法を提供します。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引数 | 型 | 説明 |
---|---|---|
result | Result<Document_ | Root> | 警告を受け取る Result インスタンス。 |
message | string | 警告の説明。 |
options | WarningOptions | 警告のオプション。 |
Warning
を返します。
Input
ソース CSS を表します。
const root = postcss.parse(css, { from: file })
const input = root.source.input
Input#css
入力 CSS ソース。
const input = postcss.parse('a{}', { from: file }).input
input.css //=> "a{}"
型: string。
Input#error()
引数 | 型 |
---|---|
message | string |
start | Object | Object |
end | Object | Object |
opts | Object |
引数 | 型 |
---|---|
message | string |
line | number |
column | number |
opts | Object |
引数 | 型 |
---|---|
message | string |
offset | number |
opts | Object |
CssSyntaxError_
を返します。
Input#file
from
オプションで定義された CSS ソースファイルへの絶対パス。
const root = postcss.parse(css, { from: 'a.css' })
root.source.input.file //=> '/home/ai/a.css'
型: string。
Input#fromOffset()
ソースオフセットを行と列に変換します。
引数 | 型 | 説明 |
---|---|---|
offset | number | ソースオフセット。 |
Object
を返します。
Input#hasBOM
ソースコードに Unicode BOM があるかどうかを示すフラグ。
型: boolean。
Input#id
CSS ソースの一意の ID。from
オプションが指定されていない場合に作成されます (PostCSS はファイルパスを知らないため)。
const root = postcss.parse(css)
root.source.input.file //=> undefined
root.source.input.id //=> "<input css 8LZeVF>"
型: string。
Input#map
PostCSS の前のコンパイルステップ (たとえば、Sass コンパイラから) から渡された入力ソースマップ。
root.source.input.map.consumer().sources //=> ['a.sass']
型: PreviousMap_。
Input#origin()
入力ソースマップを読み取り、入力ソース (たとえば、PostCSS に渡される前に CSS にコンパイルされた Sass ファイル) 内のシンボル位置を返します。オプションで、排他的な終了位置を指定できます。
root.source.input.origin(1, 1) //=> { file: 'a.css', line: 3, column: 1 }
root.source.input.origin(1, 1, 1, 4)
//=> { file: 'a.css', line: 3, column: 1, endLine: 3, endColumn: 4 }
引数 | 型 | 説明 |
---|---|---|
line | number | 入力 CSS の包含開始位置の行。 |
column | number | 入力 CSS の包含開始位置の列。 |
endLine | number | 入力 CSS の排他的終了位置の行。 |
endColumn | number | 入力 CSS の排他的終了位置の列。 |
false | FilePosition
を返します。
LazyResult
PostCSS 変換の結果に対するプロミスプロキシ。
LazyResult
インスタンスは、Processor#process
によって返されます。
const lazy = postcss([autoprefixer]).process(css)
LazyResult#async()
プラグインを非同期的に実行し、Result
を返します。
Promise<Result<RootNode>>
を返します。
LazyResult#catch
型: Function。
LazyResult#finally
型: Function。
LazyResult#sync()
プラグインを同期的に実行し、Result
を返します。
Result<RootNode>
を返します。
LazyResult#then
型: Function。
LazyResult#toString()
LazyResult#css
プロパティのエイリアス。
lazy + '' === lazy.css
string
を返します。
LazyResult#warnings()
同期プラグインを通じて入力 CSS を処理し、Result#warnings
を呼び出します。
Warning[]
を返します。
NoWorkResult
PostCSS 変換の結果に対するプロミスプロキシ。このレイジーリザルトインスタンスは、NoWorkResult#root
または Result#root
にアクセスしない限り、css を解析しません。詳細については、以下の例を参照してください。プラグインが定義されていない場合 *のみ* 、Processor#process
によって NoWork
インスタンスが返されます。
const noWorkResult = postcss().process(css) // No plugins are defined.
// CSS is not parsed
let root = noWorkResult.root // now css is parsed because we accessed the root
NoWorkResult#async()
プラグインを非同期的に実行し、Result
を返します。
NoWorkResult#catch
型: Function。
NoWorkResult#finally
型: Function。
NoWorkResult#sync()
プラグインを同期的に実行し、Result
を返します。
NoWorkResult#then
型: Function。
NoWorkResult#toString()
LazyResult#css
プロパティのエイリアス。
lazy + '' === lazy.css
string
を返します。
NoWorkResult#warnings()
同期プラグインを通じて入力 CSS を処理し、Result#warnings
を呼び出します。
Warning[]
を返します。
Node
Node#after()
現在のノードの親に、現在のノードの後に新しいノードを挿入します。
node.parent.insertAfter(node, add)
のエイリアス。
decl.after('color: black')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Node
を返します。
Node#assign()
既存のノードインスタンスにプロパティを割り当てます。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引数 | 型 | 説明 |
---|---|---|
overrides | object | ノードをオーバーライドするための新しいプロパティ。 |
Node
を返します。
Node#before()
現在のノードの親に、現在のノードの前に新しいノードを挿入します。
node.parent.insertBefore(node, add)
のエイリアス。
decl.before('content: ""')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Node
を返します。
Node#cleanRaws()
ノードとその子に対するコードスタイルのプロパティをクリアします。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引数 | 型 | 説明 |
---|---|---|
keepBetween | boolean | raws.between シンボルを保持します。 |
Node#clone()
既存のノードのクローンを作成します。これには、すべてのプロパティとその値が含まれます。これには raws
が含まれますが、type
は含まれません。
decl.raws.before //=> "\n "
const cloned = decl.clone({ prop: '-moz-' + decl.prop })
cloned.raws.before //=> "\n "
cloned.toString() //=> -moz-transform: scale(0)
引数 | 型 | 説明 |
---|---|---|
overrides | object | クローンでオーバーライドする新しいプロパティ。 |
Node
を返します。
Node#cloneAfter()
ノードをクローンし、結果のクローンされたノードを現在のノードの後に挿入するためのショートカット。
引数 | 型 | 説明 |
---|---|---|
overrides | object | クローンでオーバーライドする新しいプロパティ。 |
Node
を返します。
Node#cloneBefore()
ノードをクローンし、結果のクローンされたノードを現在のノードの前に挿入するためのショートカット。
decl.cloneBefore({ prop: '-moz-' + decl.prop })
引数 | 型 | 説明 |
---|---|---|
overrides | object | クローンでオーバーライドする新しいプロパティ。 |
Node
を返します。
Node#error()
クラス CssSyntaxError
のインスタンスを作成し、このメソッドに渡されたパラメータがエラーインスタンスに割り当てられます。
エラーインスタンスには、エラーの説明、ソース内のノードの元の位置、行番号と列番号を示す説明が含まれます。
前のマップが存在する場合は、ソースの元の位置を取得するために使用されます。
ここでの前のマップは、以前のコンパイルで生成されたソースマップを指します。例: Less、Stylus、Sass。
このメソッドは、エラーインスタンスをスローするのではなく返します。
if (!variables[name]) {
throw decl.error(`Unknown variable ${name}`, { word: name })
// CssSyntaxError: postcss-vars:a.sass:4:3: Unknown variable $black
// color: $black
// a
// ^
// background: white
}
引数 | 型 | 説明 |
---|---|---|
message | string | エラーインスタンスの説明。 |
options | NodeErrorOptions | エラーインスタンスのオプション。 |
CssSyntaxError_
を返します。
Node#next()
ノードの親の次の子を返します。現在のノードが最後の子の場合は undefined
を返します。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
ChildNode
を返します。
Node#parent
現在のノードの親を表します。
root.nodes[0].parent === root //=> true
型: Container_<ChildNode> | Document_。
Node#positionBy()
ノード内の単語またはインデックスの位置を取得します。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word"> | オプション。 |
Position
を返します。
Node#positionInside()
文字列のインデックスを行/列に変換します。
引数 | 型 | 説明 |
---|---|---|
index | number | ノードの文字列内の記号の番号。 |
Position
を返します。
Node#prev()
ノードの親の前の子を返します。現在のノードが最初の子である場合は、undefined
を返します。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
ChildNode
を返します。
Node#rangeBy()
ノード内の単語または開始インデックスと終了インデックスの範囲を取得します。開始インデックスは包括的で、終了インデックスは排他的です。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word" | "endIndex"> | オプション。 |
Range
を返します。
Node#raw()
raws
値を返します。ノードにコードスタイルプロパティがない場合(ノードが手動で作成または複製されたため)、PostCSSはツリー内の他のノードを調べてコードスタイルプロパティを自動検出しようとします。
const root = postcss.parse('a { background: white }')
root.nodes[0].append({ prop: 'color', value: 'black' })
root.nodes[0].nodes[1].raws.before //=> undefined
root.nodes[0].nodes[1].raw('before') //=> ' '
引数 | 型 | 説明 |
---|---|---|
prop | string | コードスタイルプロパティの名前。 |
defaultType | string | デフォルト値の名前。値が prop と同じ場合は省略できます。 |
string
を返します。
Node#raws
CSSソースコードに存在する不要な空白と文字を表します。
元の入力と同じように、バイト単位で等しいノード文字列を生成するための情報。
raws オブジェクトのプロパティはパーサーによって決定されます。デフォルトのパーサーは次のプロパティを使用します
before
: ノードの前の空白記号。宣言の前の*
および_
記号も保存します(IEハック)。after
: ノードの最後の子からノードの末尾までの空白記号。between
: 宣言の場合はプロパティと値の間、ルールの場合はセレクタと{
の間、または at-rule の場合は最後のパラメータと{
の間の記号。semicolon
: 最後の子が(オプションの)セミコロンを持っている場合は true が含まれます。afterName
: at-rule 名とそのパラメータの間の空白。left
:/*
とコメントのテキストの間の空白記号。right
: コメントのテキストと */ の間の空白記号。
important
: important ステートメントの内容。単に!important
ではない場合。
PostCSSはセレクタ、宣言値、および at-rule パラメータ内のコメントをフィルタリングしますが、raws に元のコンテンツを保存します。
const root = postcss.parse('a {\n color:black\n}')
root.first.first.raws //=> { before: '\n ', between: ':' }
型: any。
Node#remove()
ノードをその親から削除し、親プロパティを削除します。
if (decl.prop.match(/^-webkit-/)) {
decl.remove()
}
Node
を返します。
Node#replaceWith()
現在のノードの前にノードを挿入し、現在のノードを削除します。
AtRule: {
mixin: atrule => {
atrule.replaceWith(mixinRules[atrule.params])
}
}
引数 | 型 | 説明 |
---|---|---|
nodes… | (ChildNode | ChildProps | ChildNode[] | ChildProps[])[] | 現在のノードを置き換えるモード。 |
Node
を返します。
Node#root()
ノードのツリーの Root インスタンスを検索します。
root.nodes[0].nodes[0].root() === root
Root
を返します。
Node#source
ノードの起源に関する情報を表し、ソースマップの生成に必要です。
PostCSSが提供するパブリックAPIを使用して手動で作成されたノードは、source
が未定義になり、ソースマップに表示されません。
このため、プラグイン開発者は、複製されたノードはデフォルトで元のノードと同じソースを持つか、手動で作成されたノードにソースを割り当てるため、ノードを複製することを検討する必要があります。
decl.source.input.from //=> '/home/ai/source.css'
decl.source.start //=> { line: 10, column: 2 }
decl.source.end //=> { line: 10, column: 12 }
// Incorrect method, source not specified!
const prefixed = postcss.decl({
prop: '-moz-' + decl.prop,
value: decl.value
})
// Correct method, source is inherited when duplicating.
const prefixed = decl.clone({
prop: '-moz-' + decl.prop
})
if (atrule.name === 'add-link') {
const rule = postcss.rule({
selector: 'a',
source: atrule.source
})
atrule.parent.insertBefore(atrule, rule)
}
型: Source。
Node#toJSON()
JSON.stringify()
の循環リンクを修正します。
object
を返します。
Node#toString()
ノードの型に応じて、ノードをブラウザで読み取り可能なカスケーディングスタイルシート文字列にコンパイルします。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引数 | 型 | 説明 |
---|---|---|
stringifier | Stringifier | Syntax<Document_ | Root> | 文字列生成で使用する構文。 |
string
を返します。
Node#type
抽象構文木におけるノードの型を表します。
ノードの型は、ノードの識別と型に基づいた操作の実行に役立ちます。
const declaration = new Declaration({
prop: 'color',
value: 'black'
})
declaration.type //=> 'decl'
型: string。
Node#warn()
Result#warn のラッパーであり、警告を生成する便利な方法を提供します。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引数 | 型 | 説明 |
---|---|---|
result | Result<Document_ | Root> | 警告を受け取る Result インスタンス。 |
message | string | 警告の説明。 |
options | WarningOptions | 警告のオプション。 |
Warning
を返します。
PreviousMap
入力 CSS からのソースマップ情報。たとえば、Sass コンパイラー後のソースマップ。
このクラスは、入力 CSS 内、または入力ファイルに近いファイルシステム (from
オプションに従って) にソースマップを自動的に検索します。
const root = parse(css, { from: 'a.sass.css' })
root.input.map //=> PreviousMap
PreviousMap#annotation
sourceMappingURL
の内容。
型: string。
PreviousMap#consumer()
source-map
ライブラリからSourceMapGenerator
クラスのインスタンスを作成し、ソースマップ情報を処理します。
これは遅延メソッドであるため、最初に呼び出されたときにのみオブジェクトを作成し、その後はキャッシュを使用します。
SourceMapConsumer
を返します。
PreviousMap#file
CSSソースの識別子です。ユーザーがfrom
オプションを設定した場合はInput#file
が含まれ、設定しなかった場合はInput#id
が含まれます。
型: string。
PreviousMap#inline
ソースマップがdata-uriによって入力CSSにインライン化されたかどうかを示します。
型: boolean。
PreviousMap#mapFile
ソースマップファイルへのパスです。
型: string。
PreviousMap#root
ソースマップが別のファイルにある場合の、ソースマップファイルのディレクトリです。
型: string。
PreviousMap#text
ソースマップファイルの内容です。
型: string。
PreviousMap#withContent()
ソースマップに、入力ソーステキストを含むsourcesContent
が含まれているかどうかを示します。
boolean
を返します。
Processor
CSSを処理するためのプラグインが含まれます。Processor
インスタンスを1つ作成し、そのプラグインを初期化してから、多数のCSSファイルに対してそのインスタンスを使用します。
const processor = postcss([autoprefixer, postcssNested])
processor.process(css1).then(result => console.log(result.css))
processor.process(css2).then(result => console.log(result.css))
Processor#plugins
このプロセッサーに追加されたプラグインです。
const processor = postcss([autoprefixer, postcssNested])
processor.plugins.length //=> 2
型: (Plugin | TransformCallback | Transformer)[]。
Processor#process()
ソースCSSを解析し、LazyResult
Promiseプロキシを返します。一部のプラグインは非同期である可能性があるため、変換は実行されません。変換はLazyResult
メソッドで適用されます。
processor.process(css, { from: 'a.css', to: 'a.out.css' })
.then(result => {
console.log(result.css)
})
引数 | 型 | 説明 |
---|---|---|
css | string | Root | Result<Document_ | Root> | Object | LazyResult_<Document_ | Root> | 入力CSSを含む文字列、またはBufferのようにtoString() メソッドを持つ任意のオブジェクト。オプションで、Result インスタンスを送信すると、プロセッサーはそこからRoot を取得します。 |
引数 | 型 | 説明 |
---|---|---|
css | string | Root | Result<Document_ | Root> | LazyResult_<Document_ | Root> | Object | 入力CSSを含む文字列、またはBufferのようにtoString() メソッドを持つ任意のオブジェクト。オプションで、Result インスタンスを送信すると、プロセッサーはそこからRoot を取得します。 |
options | ProcessOptions<RootNode> |
NoWorkResult_ | LazyResult_<Document_ | Root>
を返します。
Processor#use()
CSSプロセッサーとして使用するプラグインを追加します。
PostCSSプラグインは4つの形式で指定できます。
Plugin
形式のプラグイン。pluginCreator.postcss = true
を持つプラグインクリエーター関数。PostCSSはこの関数を引数なしで呼び出し、プラグインを取得します。- 関数。PostCSSは、最初の引数としてRoot、2番目の引数として現在の
Result
インスタンスを関数に渡します。 - 別の
Processor
インスタンス。PostCSSはそのインスタンスからこのインスタンスにプラグインをコピーします。
プラグインは、postcss
インスタンスを作成するときに引数として渡すことでも追加できます([postcss(plugins)
]を参照)。
非同期プラグインは、Promise
インスタンスを返す必要があります。
const processor = postcss()
.use(autoprefixer)
.use(postcssNested)
引数 | 型 | 説明 |
---|---|---|
plugin | AcceptedPlugin | PostCSSプラグインまたはプラグインを持つProcessor 。 |
Processor
を返します。
Processor#version
現在のPostCSSバージョン。
if (result.processor.version.split('.')[0] !== '6') {
throw new Error('This plugin works only with PostCSS 6')
}
型: string。
Result
PostCSS変換の結果を提供します。
Resultインスタンスは、LazyResult#then
またはRoot#toResult
メソッドによって返されます。
postcss([autoprefixer]).process(css).then(result => {
console.log(result.css)
})
const result2 = postcss.parse(css).toResult()
Result#css
Result#root
を表すCSS文字列。
postcss.parse('a{}').toResult().css //=> "a{}"
型: string。
Result#lastPlugin
最後に実行されたPostCSSプラグイン。
型: Plugin | TransformCallback。
Result#map
source-map
ライブラリからのSourceMapGenerator
クラスのインスタンスで、Result#root
インスタンスへの変更を表します。
result.map.toJSON() //=> { version: 3, file: 'a.css', … }
if (result.map) {
fs.writeFileSync(result.opts.to + '.map', result.map.toString())
}
型: SourceMap。
Result#messages
プラグインからのメッセージ(警告やカスタムメッセージなど)が含まれます。各メッセージには、typeとpluginプロパティが必要です。
AtRule: {
import: (atRule, { result }) {
const importedFile = parseImport(atRule)
result.messages.push({
type: 'dependency',
plugin: 'postcss-import',
file: importedFile,
parent: result.opts.from
})
}
}
型: Message[]。
Result#opts
このResultインスタンスを生成したProcessor#process
またはRoot#toResult
呼び出しからのオプションです。]
root.toResult(opts).opts === opts
型: ResultOptions。
Result#processor
この変換に使用されたProcessorインスタンス。
for (const plugin of result.processor.plugins) {
if (plugin.postcssPlugin === 'postcss-bad') {
throw 'postcss-good is incompatible with postcss-bad'
}
})
型: Processor。
Result#root
すべての変換後のRootノード。
root.toResult().root === root
型: RootNode。
Result#toString()
Result#css
の内容を返します。
result + '' === result.css
string
を返します。
Result#warn()
Warning
のインスタンスを作成し、Result#messages
に追加します。
if (decl.important) {
result.warn('Avoid !important', { node: decl, word: '!important' })
}
引数 | 型 |
---|---|
message | string |
options | WarningOptions |
Warning
を返します。
Result#warnings()
プラグインからの警告を返します。Result#messages
からWarning
インスタンスをフィルタリングします。
result.warnings().forEach(warn => {
console.warn(warn.toString())
})
Warning[]
を返します。
Root
CSSファイルを表し、解析されたすべてのノードが含まれます。
const root = postcss.parse('a{color:black} b{z-index:2}')
root.type //=> 'root'
root.nodes.length //=> 2
Root#after()
現在のノードの親に、現在のノードの後に新しいノードを挿入します。
node.parent.insertAfter(node, add)
のエイリアス。
decl.after('color: black')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Root
を返します。
Root#append()
コンテナの末尾に新しいノードを挿入します。
const decl1 = new Declaration({ prop: 'color', value: 'black' })
const decl2 = new Declaration({ prop: 'background-color', value: 'white' })
rule.append(decl1, decl2)
root.append({ name: 'charset', params: '"UTF-8"' }) // at-rule
root.append({ selector: 'a' }) // rule
rule.append({ prop: 'color', value: 'black' }) // declaration
rule.append({ text: 'Comment' }) // comment
root.append('a {}')
root.first.append('color: black; z-index: 1')
引数 | 型 | 説明 |
---|---|---|
nodes… | (string | string[] | Node | ChildProps | ChildProps[] | Node[])[] | 新しいノード。 |
Root
を返します。
Root#assign()
既存のノードインスタンスにプロパティを割り当てます。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引数 | 型 | 説明 |
---|---|---|
overrides | object | RootProps | ノードをオーバーライドするための新しいプロパティ。 |
Root
を返します。
Root#before()
現在のノードの親に、現在のノードの前に新しいノードを挿入します。
node.parent.insertBefore(node, add)
のエイリアス。
decl.before('content: ""')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Root
を返します。
Root#cleanRaws()
ノードとその子に対するコードスタイルのプロパティをクリアします。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引数 | 型 | 説明 |
---|---|---|
keepBetween | boolean | raws.between シンボルを保持します。 |
Root#clone()
既存のノードのクローンを作成します。これには、すべてのプロパティとその値が含まれます。これには raws
が含まれますが、type
は含まれません。
decl.raws.before //=> "\n "
const cloned = decl.clone({ prop: '-moz-' + decl.prop })
cloned.raws.before //=> "\n "
cloned.toString() //=> -moz-transform: scale(0)
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<RootProps> | クローンでオーバーライドする新しいプロパティ。 |
Root
を返します。
Root#cloneAfter()
ノードをクローンし、結果のクローンされたノードを現在のノードの後に挿入するためのショートカット。
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<RootProps> | クローンでオーバーライドする新しいプロパティ。 |
Root
を返します。
Root#cloneBefore()
ノードをクローンし、結果のクローンされたノードを現在のノードの前に挿入するためのショートカット。
decl.cloneBefore({ prop: '-moz-' + decl.prop })
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<RootProps> | クローンでオーバーライドする新しいプロパティ。 |
Root
を返します。
Root#each()
コンテナの直接の子を反復処理し、子ごとに callback
を呼び出します。
コールバックで false
を返すと、反復処理が中断されます。
このメソッドは、コンテナの直接の子のみを反復処理します。コンテナのすべての子孫ノードを再帰的に反復処理する必要がある場合は、Container#walk
を使用します。
for {}
サイクルまたは Array#forEach
とは異なり、このイテレータは、反復処理中に子ノードの配列をミューテートする場合でも安全です。PostCSS は、ミューテーションに合わせて現在のインデックスを調整します。
const root = postcss.parse('a { color: black; z-index: 1 }')
const rule = root.first
for (const decl of rule.nodes) {
decl.cloneBefore({ prop: '-webkit-' + decl.prop })
// Cycle will be infinite, because cloneBefore moves the current node
// to the next index
}
rule.each(decl => {
decl.cloneBefore({ prop: '-webkit-' + decl.prop })
// Will be executed only for color and z-index
})
引数 | 型 | 説明 |
---|---|---|
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Root#error()
クラス CssSyntaxError
のインスタンスを作成し、このメソッドに渡されたパラメータがエラーインスタンスに割り当てられます。
エラーインスタンスには、エラーの説明、ソース内のノードの元の位置、行番号と列番号を示す説明が含まれます。
前のマップが存在する場合は、ソースの元の位置を取得するために使用されます。
ここでの前のマップは、以前のコンパイルで生成されたソースマップを指します。例: Less、Stylus、Sass。
このメソッドは、エラーインスタンスをスローするのではなく返します。
if (!variables[name]) {
throw decl.error(`Unknown variable ${name}`, { word: name })
// CssSyntaxError: postcss-vars:a.sass:4:3: Unknown variable $black
// color: $black
// a
// ^
// background: white
}
引数 | 型 | 説明 |
---|---|---|
message | string | エラーインスタンスの説明。 |
options | NodeErrorOptions | エラーインスタンスのオプション。 |
CssSyntaxError_
を返します。
Root#every()
コールバックがコンテナのすべての子に対して true
を返す場合は true
を返します。
const noPrefixes = rule.every(i => i.prop[0] !== '-')
引数 | 型 | 説明 |
---|---|---|
condition | Function | イテレータは true または false を返します。 |
boolean
を返します。
Root#index()
Container#nodes
配列内の child
のインデックスを返します。
rule.index( rule.nodes[2] ) //=> 2
引数 | 型 | 説明 |
---|---|---|
child | number | ChildNode | 現在のコンテナの子。 |
number
を返します。
Root#insertAfter()
コンテナ内の古いノードの後に新しいノードを挿入します。
引数 | 型 | 説明 |
---|---|---|
oldNode | number | ChildNode | 子または子のインデックス。 |
newNode | string | string[] | ChildNode | ChildProps | ChildNode[] | ChildProps[] | 新しいノード。 |
Root
を返します。
Root#insertBefore()
コンテナ内の古いノードの前に新しいノードを挿入します。
rule.insertBefore(decl, decl.clone({ prop: '-webkit-' + decl.prop }))
引数 | 型 | 説明 |
---|---|---|
oldNode | number | ChildNode | 子または子のインデックス。 |
newNode | string | string[] | ChildNode | ChildProps | ChildNode[] | ChildProps[] | 新しいノード。 |
Root
を返します。
Root#next()
ノードの親の次の子を返します。現在のノードが最後の子の場合は undefined
を返します。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
ChildNode
を返します。
Root#nodes
コンテナの子を含む配列。
const root = postcss.parse('a { color: black }')
root.nodes.length //=> 1
root.nodes[0].selector //=> 'a'
root.nodes[0].nodes[0].prop //=> 'color'
型: ChildNode[]。
Root#parent
現在のノードの親を表します。
root.nodes[0].parent === root //=> true
型: Document_。
Root#positionBy()
ノード内の単語またはインデックスの位置を取得します。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word"> | オプション。 |
Position
を返します。
Root#positionInside()
文字列のインデックスを行/列に変換します。
引数 | 型 | 説明 |
---|---|---|
index | number | ノードの文字列内の記号の番号。 |
Position
を返します。
Root#prepend()
コンテナの先頭に新しいノードを挿入します。
const decl1 = new Declaration({ prop: 'color', value: 'black' })
const decl2 = new Declaration({ prop: 'background-color', value: 'white' })
rule.prepend(decl1, decl2)
root.append({ name: 'charset', params: '"UTF-8"' }) // at-rule
root.append({ selector: 'a' }) // rule
rule.append({ prop: 'color', value: 'black' }) // declaration
rule.append({ text: 'Comment' }) // comment
root.append('a {}')
root.first.append('color: black; z-index: 1')
引数 | 型 | 説明 |
---|---|---|
nodes… | (string | string[] | Node | ChildProps | ChildProps[] | Node[])[] | 新しいノード。 |
Root
を返します。
Root#prev()
ノードの親の前の子を返します。現在のノードが最初の子である場合は、undefined
を返します。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
ChildNode
を返します。
Root#push()
ノードの末尾に子を追加します。
rule.push(new Declaration({ prop: 'color', value: 'black' }))
引数 | 型 | 説明 |
---|---|---|
child | ChildNode | 新しいノード。 |
Root
を返します。
Root#rangeBy()
ノード内の単語または開始インデックスと終了インデックスの範囲を取得します。開始インデックスは包括的で、終了インデックスは排他的です。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word" | "endIndex"> | オプション。 |
Range
を返します。
Root#raw()
raws
値を返します。ノードにコードスタイルプロパティがない場合(ノードが手動で作成または複製されたため)、PostCSSはツリー内の他のノードを調べてコードスタイルプロパティを自動検出しようとします。
const root = postcss.parse('a { background: white }')
root.nodes[0].append({ prop: 'color', value: 'black' })
root.nodes[0].nodes[1].raws.before //=> undefined
root.nodes[0].nodes[1].raw('before') //=> ' '
引数 | 型 | 説明 |
---|---|---|
prop | string | コードスタイルプロパティの名前。 |
defaultType | string | デフォルト値の名前。値が prop と同じ場合は省略できます。 |
string
を返します。
Root#raws
CSSソースコードに存在する不要な空白と文字を表します。
元の入力と同じように、バイト単位で等しいノード文字列を生成するための情報。
raws オブジェクトのプロパティはパーサーによって決定されます。デフォルトのパーサーは次のプロパティを使用します
before
: ノードの前の空白記号。宣言の前の*
および_
記号も保存します(IEハック)。after
: ノードの最後の子からノードの末尾までの空白記号。between
: 宣言の場合はプロパティと値の間、ルールの場合はセレクタと{
の間、または at-rule の場合は最後のパラメータと{
の間の記号。semicolon
: 最後の子が(オプションの)セミコロンを持っている場合は true が含まれます。afterName
: at-rule 名とそのパラメータの間の空白。left
:/*
とコメントのテキストの間の空白記号。right
: コメントのテキストと */ の間の空白記号。
important
: important ステートメントの内容。単に!important
ではない場合。
PostCSSはセレクタ、宣言値、および at-rule パラメータ内のコメントをフィルタリングしますが、raws に元のコンテンツを保存します。
const root = postcss.parse('a {\n color:black\n}')
root.first.first.raws //=> { before: '\n ', between: ':' }
型: RootRaws。
Root#remove()
ノードをその親から削除し、親プロパティを削除します。
if (decl.prop.match(/^-webkit-/)) {
decl.remove()
}
Root
を返します。
Root#removeAll()
コンテナからすべての子を削除し、それらの親プロパティをクリアします。
rule.removeAll()
rule.nodes.length //=> 0
Root
を返します。
Root#removeChild()
ノードをコンテナから削除し、ノードとその子から親プロパティをクリアします。
rule.nodes.length //=> 5
rule.removeChild(decl)
rule.nodes.length //=> 4
decl.parent //=> undefined
引数 | 型 | 説明 |
---|---|---|
child | number | ChildNode | 子または子のインデックス。 |
Root
を返します。
Root#replaceValues()
引数 | 型 |
---|---|
pattern | string | RegExp |
replaced | string | Function |
引数 | 型 | 説明 |
---|---|---|
pattern | string | RegExp | パターンを置換します。 |
options | ValueOptions | |
replaced | string | Function |
Root
を返します。
Root#replaceWith()
現在のノードの前にノードを挿入し、現在のノードを削除します。
AtRule: {
mixin: atrule => {
atrule.replaceWith(mixinRules[atrule.params])
}
}
引数 | 型 | 説明 |
---|---|---|
nodes… | (ChildNode | ChildProps | ChildNode[] | ChildProps[])[] | 現在のノードを置き換えるモード。 |
Root
を返します。
Root#root()
ノードのツリーの Root インスタンスを検索します。
root.nodes[0].nodes[0].root() === root
Root
を返します。
Root#some()
コンテナの子の少なくとも1つに対してコールバックが true
を返す場合は、true
を返します。
const hasPrefix = rule.some(i => i.prop[0] === '-')
引数 | 型 | 説明 |
---|---|---|
condition | Function | イテレータは true または false を返します。 |
boolean
を返します。
Root#source
ノードの起源に関する情報を表し、ソースマップの生成に必要です。
PostCSSが提供するパブリックAPIを使用して手動で作成されたノードは、source
が未定義になり、ソースマップに表示されません。
このため、プラグイン開発者は、複製されたノードはデフォルトで元のノードと同じソースを持つか、手動で作成されたノードにソースを割り当てるため、ノードを複製することを検討する必要があります。
decl.source.input.from //=> '/home/ai/source.css'
decl.source.start //=> { line: 10, column: 2 }
decl.source.end //=> { line: 10, column: 12 }
// Incorrect method, source not specified!
const prefixed = postcss.decl({
prop: '-moz-' + decl.prop,
value: decl.value
})
// Correct method, source is inherited when duplicating.
const prefixed = decl.clone({
prop: '-moz-' + decl.prop
})
if (atrule.name === 'add-link') {
const rule = postcss.rule({
selector: 'a',
source: atrule.source
})
atrule.parent.insertBefore(atrule, rule)
}
型: Source。
Root#toJSON()
JSON.stringify()
の循環リンクを修正します。
object
を返します。
Root#toResult()
ルートのCSSを表すResult
インスタンスを返します。
const root1 = postcss.parse(css1, { from: 'a.css' })
const root2 = postcss.parse(css2, { from: 'b.css' })
root1.append(root2)
const result = root1.toResult({ to: 'all.css', map: true })
引数 | 型 |
---|---|
options | ProcessOptions<Document_ | Root> |
Result<Document_ | Root>
を返します。
Root#toString()
ノードの型に応じて、ノードをブラウザで読み取り可能なカスケーディングスタイルシート文字列にコンパイルします。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引数 | 型 | 説明 |
---|---|---|
stringifier | Stringifier | Syntax<Document_ | Root> | 文字列生成で使用する構文。 |
string
を返します。
Root#type
抽象構文木におけるノードの型を表します。
ノードの型は、ノードの識別と型に基づいた操作の実行に役立ちます。
const declaration = new Declaration({
prop: 'color',
value: 'black'
})
declaration.type //=> 'decl'
型: "root"。
Root#walk()
コンテナの子孫ノードをトラバースし、各ノードに対してコールバックを呼び出します。
container.each() と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
コンテナの直下の子のみを反復する必要がある場合は、Container#each
を使用します。
root.walk(node => {
// Traverses all descendant nodes.
})
引数 | 型 | 説明 |
---|---|---|
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Root#walkAtRules()
コンテナの子孫ノードをトラバースし、各 at-rule ノードに対してコールバックを呼び出します。
フィルタを渡すと、名前が一致する at-rule のみに対して反復が行われます。
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
root.walkAtRules(rule => {
if (isOld(rule.name)) rule.remove()
})
let first = false
root.walkAtRules('charset', rule => {
if (!first) {
first = true
} else {
rule.remove()
}
})
引数 | 型 | 説明 |
---|---|---|
nameFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Root#walkComments()
引数 | 型 |
---|---|
callback | Function |
void | false
を返します。
Root#walkDecls()
コンテナの子孫ノードをトラバースし、各宣言ノードに対してコールバックを呼び出します。
フィルタを渡すと、プロパティが一致する宣言のみに対して反復が行われます。
root.walkDecls(decl => {
checkPropertySupport(decl.prop)
})
root.walkDecls('border-radius', decl => {
decl.remove()
})
root.walkDecls(/^background/, decl => {
decl.value = takeFirstColorFromGradient(decl.value)
})
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
引数 | 型 | 説明 |
---|---|---|
propFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Root#walkRules()
コンテナの子孫ノードをトラバースし、各ルールノードに対してコールバックを呼び出します。
フィルタを渡すと、セレクタが一致するルールのみに対して反復が行われます。
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
const selectors = []
root.walkRules(rule => {
selectors.push(rule.selector)
})
console.log(`Your CSS uses ${ selectors.length } selectors`)
引数 | 型 | 説明 |
---|---|---|
selectorFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Root#warn()
Result#warn のラッパーであり、警告を生成する便利な方法を提供します。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引数 | 型 | 説明 |
---|---|---|
result | Result<Document_ | Root> | 警告を受け取る Result インスタンス。 |
message | string | 警告の説明。 |
options | WarningOptions | 警告のオプション。 |
Warning
を返します。
Rule
CSSルールを表します。セレクターとそれに続く宣言ブロックです。
Once (root, { Rule }) {
let a = new Rule({ selector: 'a' })
a.append(…)
root.append(a)
}
const root = postcss.parse('a{}')
const rule = root.first
rule.type //=> 'rule'
rule.toString() //=> 'a{}'
Rule#after()
現在のノードの親に、現在のノードの後に新しいノードを挿入します。
node.parent.insertAfter(node, add)
のエイリアス。
decl.after('color: black')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Rule
を返します。
Rule#append()
コンテナの末尾に新しいノードを挿入します。
const decl1 = new Declaration({ prop: 'color', value: 'black' })
const decl2 = new Declaration({ prop: 'background-color', value: 'white' })
rule.append(decl1, decl2)
root.append({ name: 'charset', params: '"UTF-8"' }) // at-rule
root.append({ selector: 'a' }) // rule
rule.append({ prop: 'color', value: 'black' }) // declaration
rule.append({ text: 'Comment' }) // comment
root.append('a {}')
root.first.append('color: black; z-index: 1')
引数 | 型 | 説明 |
---|---|---|
nodes… | (string | string[] | Node | ChildProps | ChildProps[] | Node[])[] | 新しいノード。 |
Rule
を返します。
Rule#assign()
既存のノードインスタンスにプロパティを割り当てます。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引数 | 型 | 説明 |
---|---|---|
overrides | object | RuleProps | ノードをオーバーライドするための新しいプロパティ。 |
Rule
を返します。
Rule#before()
現在のノードの親に、現在のノードの前に新しいノードを挿入します。
node.parent.insertBefore(node, add)
のエイリアス。
decl.before('content: ""')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Rule
を返します。
Rule#cleanRaws()
ノードとその子に対するコードスタイルのプロパティをクリアします。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引数 | 型 | 説明 |
---|---|---|
keepBetween | boolean | raws.between シンボルを保持します。 |
Rule#clone()
既存のノードのクローンを作成します。これには、すべてのプロパティとその値が含まれます。これには raws
が含まれますが、type
は含まれません。
decl.raws.before //=> "\n "
const cloned = decl.clone({ prop: '-moz-' + decl.prop })
cloned.raws.before //=> "\n "
cloned.toString() //=> -moz-transform: scale(0)
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<RuleProps> | クローンでオーバーライドする新しいプロパティ。 |
Rule
を返します。
Rule#cloneAfter()
ノードをクローンし、結果のクローンされたノードを現在のノードの後に挿入するためのショートカット。
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<RuleProps> | クローンでオーバーライドする新しいプロパティ。 |
Rule
を返します。
Rule#cloneBefore()
ノードをクローンし、結果のクローンされたノードを現在のノードの前に挿入するためのショートカット。
decl.cloneBefore({ prop: '-moz-' + decl.prop })
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<RuleProps> | クローンでオーバーライドする新しいプロパティ。 |
Rule
を返します。
Rule#each()
コンテナの直接の子を反復処理し、子ごとに callback
を呼び出します。
コールバックで false
を返すと、反復処理が中断されます。
このメソッドは、コンテナの直接の子のみを反復処理します。コンテナのすべての子孫ノードを再帰的に反復処理する必要がある場合は、Container#walk
を使用します。
for {}
サイクルまたは Array#forEach
とは異なり、このイテレータは、反復処理中に子ノードの配列をミューテートする場合でも安全です。PostCSS は、ミューテーションに合わせて現在のインデックスを調整します。
const root = postcss.parse('a { color: black; z-index: 1 }')
const rule = root.first
for (const decl of rule.nodes) {
decl.cloneBefore({ prop: '-webkit-' + decl.prop })
// Cycle will be infinite, because cloneBefore moves the current node
// to the next index
}
rule.each(decl => {
decl.cloneBefore({ prop: '-webkit-' + decl.prop })
// Will be executed only for color and z-index
})
引数 | 型 | 説明 |
---|---|---|
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Rule#error()
クラス CssSyntaxError
のインスタンスを作成し、このメソッドに渡されたパラメータがエラーインスタンスに割り当てられます。
エラーインスタンスには、エラーの説明、ソース内のノードの元の位置、行番号と列番号を示す説明が含まれます。
前のマップが存在する場合は、ソースの元の位置を取得するために使用されます。
ここでの前のマップは、以前のコンパイルで生成されたソースマップを指します。例: Less、Stylus、Sass。
このメソッドは、エラーインスタンスをスローするのではなく返します。
if (!variables[name]) {
throw decl.error(`Unknown variable ${name}`, { word: name })
// CssSyntaxError: postcss-vars:a.sass:4:3: Unknown variable $black
// color: $black
// a
// ^
// background: white
}
引数 | 型 | 説明 |
---|---|---|
message | string | エラーインスタンスの説明。 |
options | NodeErrorOptions | エラーインスタンスのオプション。 |
CssSyntaxError_
を返します。
Rule#every()
コールバックがコンテナのすべての子に対して true
を返す場合は true
を返します。
const noPrefixes = rule.every(i => i.prop[0] !== '-')
引数 | 型 | 説明 |
---|---|---|
condition | Function | イテレータは true または false を返します。 |
boolean
を返します。
Rule#index()
Container#nodes
配列内の child
のインデックスを返します。
rule.index( rule.nodes[2] ) //=> 2
引数 | 型 | 説明 |
---|---|---|
child | number | ChildNode | 現在のコンテナの子。 |
number
を返します。
Rule#insertAfter()
コンテナ内の古いノードの後に新しいノードを挿入します。
引数 | 型 | 説明 |
---|---|---|
oldNode | number | ChildNode | 子または子のインデックス。 |
newNode | string | string[] | ChildNode | ChildProps | ChildNode[] | ChildProps[] | 新しいノード。 |
Rule
を返します。
Rule#insertBefore()
コンテナ内の古いノードの前に新しいノードを挿入します。
rule.insertBefore(decl, decl.clone({ prop: '-webkit-' + decl.prop }))
引数 | 型 | 説明 |
---|---|---|
oldNode | number | ChildNode | 子または子のインデックス。 |
newNode | string | string[] | ChildNode | ChildProps | ChildNode[] | ChildProps[] | 新しいノード。 |
Rule
を返します。
Rule#next()
ノードの親の次の子を返します。現在のノードが最後の子の場合は undefined
を返します。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
ChildNode
を返します。
Rule#nodes
コンテナの子を含む配列。
const root = postcss.parse('a { color: black }')
root.nodes.length //=> 1
root.nodes[0].selector //=> 'a'
root.nodes[0].nodes[0].prop //=> 'color'
型: ChildNode[]。
Rule#parent
現在のノードの親を表します。
root.nodes[0].parent === root //=> true
型: Container_<ChildNode>。
Rule#positionBy()
ノード内の単語またはインデックスの位置を取得します。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word"> | オプション。 |
Position
を返します。
Rule#positionInside()
文字列のインデックスを行/列に変換します。
引数 | 型 | 説明 |
---|---|---|
index | number | ノードの文字列内の記号の番号。 |
Position
を返します。
Rule#prepend()
コンテナの先頭に新しいノードを挿入します。
const decl1 = new Declaration({ prop: 'color', value: 'black' })
const decl2 = new Declaration({ prop: 'background-color', value: 'white' })
rule.prepend(decl1, decl2)
root.append({ name: 'charset', params: '"UTF-8"' }) // at-rule
root.append({ selector: 'a' }) // rule
rule.append({ prop: 'color', value: 'black' }) // declaration
rule.append({ text: 'Comment' }) // comment
root.append('a {}')
root.first.append('color: black; z-index: 1')
引数 | 型 | 説明 |
---|---|---|
nodes… | (string | string[] | Node | ChildProps | ChildProps[] | Node[])[] | 新しいノード。 |
Rule
を返します。
Rule#prev()
ノードの親の前の子を返します。現在のノードが最初の子である場合は、undefined
を返します。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
ChildNode
を返します。
Rule#push()
ノードの末尾に子を追加します。
rule.push(new Declaration({ prop: 'color', value: 'black' }))
引数 | 型 | 説明 |
---|---|---|
child | ChildNode | 新しいノード。 |
Rule
を返します。
Rule#rangeBy()
ノード内の単語または開始インデックスと終了インデックスの範囲を取得します。開始インデックスは包括的で、終了インデックスは排他的です。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word" | "endIndex"> | オプション。 |
Range
を返します。
Rule#raw()
raws
値を返します。ノードにコードスタイルプロパティがない場合(ノードが手動で作成または複製されたため)、PostCSSはツリー内の他のノードを調べてコードスタイルプロパティを自動検出しようとします。
const root = postcss.parse('a { background: white }')
root.nodes[0].append({ prop: 'color', value: 'black' })
root.nodes[0].nodes[1].raws.before //=> undefined
root.nodes[0].nodes[1].raw('before') //=> ' '
引数 | 型 | 説明 |
---|---|---|
prop | string | コードスタイルプロパティの名前。 |
defaultType | string | デフォルト値の名前。値が prop と同じ場合は省略できます。 |
string
を返します。
Rule#raws
CSSソースコードに存在する不要な空白と文字を表します。
元の入力と同じように、バイト単位で等しいノード文字列を生成するための情報。
raws オブジェクトのプロパティはパーサーによって決定されます。デフォルトのパーサーは次のプロパティを使用します
before
: ノードの前の空白記号。宣言の前の*
および_
記号も保存します(IEハック)。after
: ノードの最後の子からノードの末尾までの空白記号。between
: 宣言の場合はプロパティと値の間、ルールの場合はセレクタと{
の間、または at-rule の場合は最後のパラメータと{
の間の記号。semicolon
: 最後の子が(オプションの)セミコロンを持っている場合は true が含まれます。afterName
: at-rule 名とそのパラメータの間の空白。left
:/*
とコメントのテキストの間の空白記号。right
: コメントのテキストと */ の間の空白記号。
important
: important ステートメントの内容。単に!important
ではない場合。
PostCSSはセレクタ、宣言値、および at-rule パラメータ内のコメントをフィルタリングしますが、raws に元のコンテンツを保存します。
const root = postcss.parse('a {\n color:black\n}')
root.first.first.raws //=> { before: '\n ', between: ':' }
型: RuleRaws。
Rule#remove()
ノードをその親から削除し、親プロパティを削除します。
if (decl.prop.match(/^-webkit-/)) {
decl.remove()
}
Rule
を返します。
Rule#removeAll()
コンテナからすべての子を削除し、それらの親プロパティをクリアします。
rule.removeAll()
rule.nodes.length //=> 0
Rule
を返します。
Rule#removeChild()
ノードをコンテナから削除し、ノードとその子から親プロパティをクリアします。
rule.nodes.length //=> 5
rule.removeChild(decl)
rule.nodes.length //=> 4
decl.parent //=> undefined
引数 | 型 | 説明 |
---|---|---|
child | number | ChildNode | 子または子のインデックス。 |
Rule
を返します。
Rule#replaceValues()
引数 | 型 |
---|---|
pattern | string | RegExp |
replaced | string | Function |
引数 | 型 | 説明 |
---|---|---|
pattern | string | RegExp | パターンを置換します。 |
options | ValueOptions | |
replaced | string | Function |
Rule
を返します。
Rule#replaceWith()
現在のノードの前にノードを挿入し、現在のノードを削除します。
AtRule: {
mixin: atrule => {
atrule.replaceWith(mixinRules[atrule.params])
}
}
引数 | 型 | 説明 |
---|---|---|
nodes… | (ChildNode | ChildProps | ChildNode[] | ChildProps[])[] | 現在のノードを置き換えるモード。 |
Rule
を返します。
Rule#root()
ノードのツリーの Root インスタンスを検索します。
root.nodes[0].nodes[0].root() === root
Root
を返します。
Rule#selector
ルール全体のセレクターを文字列として表したもの。
const root = postcss.parse('a, b { }')
const rule = root.first
rule.selector //=> 'a, b'
型: string。
Rule#selectors
ルールの個々のセレクターを含む配列。セレクターのグループはカンマで分割されます。
const root = postcss.parse('a, b { }')
const rule = root.first
rule.selector //=> 'a, b'
rule.selectors //=> ['a', 'b']
rule.selectors = ['a', 'strong']
rule.selector //=> 'a, strong'
型: string[]。
Rule#some()
コンテナの子の少なくとも1つに対してコールバックが true
を返す場合は、true
を返します。
const hasPrefix = rule.some(i => i.prop[0] === '-')
引数 | 型 | 説明 |
---|---|---|
condition | Function | イテレータは true または false を返します。 |
boolean
を返します。
Rule#source
ノードの起源に関する情報を表し、ソースマップの生成に必要です。
PostCSSが提供するパブリックAPIを使用して手動で作成されたノードは、source
が未定義になり、ソースマップに表示されません。
このため、プラグイン開発者は、複製されたノードはデフォルトで元のノードと同じソースを持つか、手動で作成されたノードにソースを割り当てるため、ノードを複製することを検討する必要があります。
decl.source.input.from //=> '/home/ai/source.css'
decl.source.start //=> { line: 10, column: 2 }
decl.source.end //=> { line: 10, column: 12 }
// Incorrect method, source not specified!
const prefixed = postcss.decl({
prop: '-moz-' + decl.prop,
value: decl.value
})
// Correct method, source is inherited when duplicating.
const prefixed = decl.clone({
prop: '-moz-' + decl.prop
})
if (atrule.name === 'add-link') {
const rule = postcss.rule({
selector: 'a',
source: atrule.source
})
atrule.parent.insertBefore(atrule, rule)
}
型: Source。
Rule#toJSON()
JSON.stringify()
の循環リンクを修正します。
object
を返します。
Rule#toString()
ノードの型に応じて、ノードをブラウザで読み取り可能なカスケーディングスタイルシート文字列にコンパイルします。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引数 | 型 | 説明 |
---|---|---|
stringifier | Stringifier | Syntax<Document_ | Root> | 文字列生成で使用する構文。 |
string
を返します。
Rule#type
抽象構文木におけるノードの型を表します。
ノードの型は、ノードの識別と型に基づいた操作の実行に役立ちます。
const declaration = new Declaration({
prop: 'color',
value: 'black'
})
declaration.type //=> 'decl'
型: "rule"。
Rule#walk()
コンテナの子孫ノードをトラバースし、各ノードに対してコールバックを呼び出します。
container.each() と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
コンテナの直下の子のみを反復する必要がある場合は、Container#each
を使用します。
root.walk(node => {
// Traverses all descendant nodes.
})
引数 | 型 | 説明 |
---|---|---|
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Rule#walkAtRules()
コンテナの子孫ノードをトラバースし、各 at-rule ノードに対してコールバックを呼び出します。
フィルタを渡すと、名前が一致する at-rule のみに対して反復が行われます。
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
root.walkAtRules(rule => {
if (isOld(rule.name)) rule.remove()
})
let first = false
root.walkAtRules('charset', rule => {
if (!first) {
first = true
} else {
rule.remove()
}
})
引数 | 型 | 説明 |
---|---|---|
nameFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Rule#walkComments()
引数 | 型 |
---|---|
callback | Function |
void | false
を返します。
Rule#walkDecls()
コンテナの子孫ノードをトラバースし、各宣言ノードに対してコールバックを呼び出します。
フィルタを渡すと、プロパティが一致する宣言のみに対して反復が行われます。
root.walkDecls(decl => {
checkPropertySupport(decl.prop)
})
root.walkDecls('border-radius', decl => {
decl.remove()
})
root.walkDecls(/^background/, decl => {
decl.value = takeFirstColorFromGradient(decl.value)
})
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
引数 | 型 | 説明 |
---|---|---|
propFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Rule#walkRules()
コンテナの子孫ノードをトラバースし、各ルールノードに対してコールバックを呼び出します。
フィルタを渡すと、セレクタが一致するルールのみに対して反復が行われます。
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
const selectors = []
root.walkRules(rule => {
selectors.push(rule.selector)
})
console.log(`Your CSS uses ${ selectors.length } selectors`)
引数 | 型 | 説明 |
---|---|---|
selectorFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Rule#warn()
Result#warn のラッパーであり、警告を生成する便利な方法を提供します。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引数 | 型 | 説明 |
---|---|---|
result | Result<Document_ | Root> | 警告を受け取る Result インスタンス。 |
message | string | 警告の説明。 |
options | WarningOptions | 警告のオプション。 |
Warning
を返します。
Stringifier
Stringifier#atrule()
引数 | 型 |
---|---|
node | AtRule_ |
semicolon | boolean |
Stringifier#beforeAfter()
引数 | 型 |
---|---|
node | AnyNode |
detect | "after" | "before" |
string
を返します。
Stringifier#block()
引数 | 型 |
---|---|
node | AnyNode |
start | string |
Stringifier#body()
引数 | 型 |
---|---|
node | Container_<ChildNode> |
Stringifier#builder
型: Builder。
Stringifier#comment()
引数 | 型 |
---|---|
node | Comment_ |
Stringifier#decl()
引数 | 型 |
---|---|
node | Declaration_ |
semicolon | boolean |
Stringifier#document()
引数 | 型 |
---|---|
node | Document_ |
Stringifier#raw()
引数 | 型 |
---|---|
node | AnyNode |
own | string |
detect | string |
string
を返します。
Stringifier#rawBeforeClose()
引数 | 型 |
---|---|
root | Root |
string
を返します。
Stringifier#rawBeforeComment()
引数 | 型 |
---|---|
root | Root |
node | Comment_ |
string
を返します。
Stringifier#rawBeforeDecl()
引数 | 型 |
---|---|
root | Root |
node | Declaration_ |
string
を返します。
Stringifier#rawBeforeOpen()
引数 | 型 |
---|---|
root | Root |
string
を返します。
Stringifier#rawBeforeRule()
引数 | 型 |
---|---|
root | Root |
string
を返します。
Stringifier#rawColon()
引数 | 型 |
---|---|
root | Root |
string
を返します。
Stringifier#rawEmptyBody()
引数 | 型 |
---|---|
root | Root |
string
を返します。
Stringifier#rawIndent()
引数 | 型 |
---|---|
root | Root |
string
を返します。
Stringifier#rawSemicolon()
引数 | 型 |
---|---|
root | Root |
boolean
を返します。
Stringifier#rawValue()
引数 | 型 |
---|---|
node | AnyNode |
prop | string |
string
を返します。
Stringifier#root()
引数 | 型 |
---|---|
node | Root |
Stringifier#rule()
引数 | 型 |
---|---|
node | Rule |
Stringifier#stringify()
引数 | 型 |
---|---|
node | AnyNode |
semicolon | boolean |
Warning
プラグインの警告を表します。Node#warn
を使用して作成できます。
if (decl.important) {
decl.warn(result, 'Avoid !important', { word: '!important' })
}
Warning#column
この警告のソースとなる入力ファイルでの、開始位置(包括的)のカラム。
warning.column //=> 6
型: number。
Warning#endColumn
この警告のソースとなる入力ファイルでの、終了位置(排他的)のカラム。
warning.endColumn //=> 4
型: number。
Warning#endLine
この警告のソースとなる入力ファイルでの、終了位置(排他的)の行。
warning.endLine //=> 6
型: number。
Warning#line
この警告のソースとなる入力ファイルでの、開始位置(包括的)の行。
warning.line //=> 5
型: number。
Warning#node
警告の原因となったCSSノードが含まれています。
warning.node.toString() //=> 'color: white !important'
型: Node。
Warning#plugin
この警告を作成したプラグインの名前。Node#warn
を呼び出すと、このプロパティが自動的に入力されます。
warning.plugin //=> 'postcss-important'
型: string。
Warning#text
警告メッセージ。
warning.text //=> 'Try to avoid !important'
型: string。
Warning#toString()
警告位置とメッセージを返します。
warning.toString() //=> 'postcss-lint:a.css:10:14: Avoid !important'
string
を返します。
Warning#type
Result#messages
から警告をフィルタリングするための型。常に"warning"
と等しくなります。
型: "warning"。
AtRuleProps
AtRuleProps#name
@規則の名前。
型: string。
AtRuleProps#nodes
型: (ChildNode | ChildProps)[]。
AtRuleProps#params
@規則の名前の後に続くパラメータ。
型: string | number。
AtRuleProps#raws
元の入力にあったノード文字列とバイト単位で等しいノード文字列を生成するために使用される情報。
型: AtRuleRaws。
AtRuleProps#source
型: Source。
AtRuleRaws
AtRuleRaws#after
ノードの最後の子からノードの終わりまでの空白記号。
型: string。
AtRuleRaws#afterName
@規則の名前とそのパラメータの間の空白。
型: string。
AtRuleRaws#before
ノードの前の空白記号。宣言の前の*
と_
記号(IEハック)も格納します。
型: string。
AtRuleRaws#between
最後のパラメータとルールに対する{
の間の記号。
型: string。
AtRuleRaws#params
コメント付きのルールのセレクタ。
型: Object。
AtRuleRaws#semicolon
最後の子に(オプションの)セミコロンがある場合はtrue
が含まれます。
型: boolean。
CommentProps
CommentProps#raws
元の入力にあったノード文字列とバイト単位で等しいノード文字列を生成するために使用される情報。
型: CommentRaws。
CommentProps#source
型: Source。
CommentProps#text
コメントの内容。
型: string。
CommentRaws
CommentRaws#before
ノードの前の空白記号。
型: string。
CommentRaws#left
/*
とコメントのテキストの間の空白記号。
型: string。
CommentRaws#right
コメントのテキストの間の空白記号。
型: string。
ContainerProps
ContainerProps#nodes
型: (ChildNode | ChildProps)[]。
ContainerProps#source
型: Source。
DeclarationProps
DeclarationProps#important
宣言に!important
アノテーションがあるかどうか。
型: boolean。
DeclarationProps#prop
宣言の名前。
型: string。
DeclarationProps#raws
元の入力にあったノード文字列とバイト単位で等しいノード文字列を生成するために使用される情報。
型: DeclarationRaws。
DeclarationProps#value
宣言の値。
型: string。
DeclarationRaws
DeclarationRaws#before
ノードの前の空白記号。宣言の前の*
と_
記号(IEハック)も格納します。
型: string。
DeclarationRaws#between
宣言のプロパティと値の間の記号。
型: string。
DeclarationRaws#important
重要なステートメントの内容(!important
だけではない場合)。
型: string。
DeclarationRaws#value
コメント付きの宣言値。
型: Object。
DocumentProps
DocumentProps#nodes
型: Root[]。
DocumentProps#raws
元の入力と同じように、バイト単位で等しいノード文字列を生成するための情報。
すべてのパーサーは独自のプロパティを保存します。
型: Record<string, any>。
DocumentProps#source
型: Source。
FilePosition
FilePosition#column
ソースファイルの包括的な開始位置の列。
型: number。
FilePosition#endColumn
ソースファイルの排他的な終了位置の列。
型: number。
FilePosition#endLine
ソースファイルの排他的な終了位置の行。
型: number。
FilePosition#file
ソースファイルへの絶対パス。
型: string。
FilePosition#line
ソースファイルの包括的な開始位置の行。
型: number。
FilePosition#source
ソースコード。
型: string。
FilePosition#url
ソースファイルのURL。
型: string。
Message
Message#plugin
ソースPostCSSプラグイン名。
型: string。
Message#type
メッセージタイプ。
型: string。
Message
Message#plugin
ソースPostCSSプラグイン名。
型: string。
Message#type
メッセージタイプ。
型: string。
NodeErrorOptions
NodeErrorOptions#endIndex
エラーの原因として強調表示されるノードの文字列内の終了インデックス。
型: number。
NodeErrorOptions#index
エラーの原因として強調表示されるノードの文字列内のインデックス。
型: number。
NodeErrorOptions#plugin
このエラーを作成したプラグインの名前。PostCSSが自動的に設定します。
型: string。
NodeErrorOptions#word
エラーの原因として強調表示されるノードの文字列内の単語。
型: string。
NodeProps
NodeProps#source
型: Source。
OldPlugin
引数 | 型 |
---|---|
opts | T |
引数 | 型 |
---|---|
root | Root |
result | Result<Document_ | Root> |
Transformer
を返します。
OldPlugin#postcss
型: Transformer。
OldPlugin#postcssPlugin
型: string。
OldPlugin#postcssVersion
型: string。
Plugin
Plugin#AtRule
すべてのAtRule
ノードで呼び出されます。
ノードまたは子の変更時に再度呼び出されます。
型: AtRuleProcessor | Object。
Plugin#AtRuleExit
すべての子が処理されると、すべてのAtRule
ノードで呼び出されます。
ノードまたは子の変更時に再度呼び出されます。
型: AtRuleProcessor | Object。
Plugin#Comment
すべてのComment
ノードで呼び出されます。
ノードまたは子の変更時に再度呼び出されます。
型: CommentProcessor。
Plugin#CommentExit
Comment
イベントのリスナーの後、すべてのComment
ノードで呼び出されます。
ノードまたは子の変更時に再度呼び出されます。
型: CommentProcessor。
Plugin#Declaration
Declaration
イベントのリスナーの後、すべてのDeclaration
ノードで呼び出されます。
ノードまたは子の変更時に再度呼び出されます。
型: DeclarationProcessor | Object。
Plugin#DeclarationExit
すべてのDeclaration
ノードで呼び出されます。
ノードまたは子の変更時に再度呼び出されます。
型: DeclarationProcessor | Object。
Plugin#Document
Document
ノードで呼び出されます。
子の変更時に再度呼び出されます。
型: DocumentProcessor。
Plugin#DocumentExit
すべての子が処理されると、Document
ノードで呼び出されます。
子の変更時に再度呼び出されます。
型: DocumentProcessor。
Plugin#Once
Root
ノードで1回呼び出されます。
型: RootProcessor。
Plugin#OnceExit
すべての子が処理されると、Root
ノードで1回呼び出されます。
型: RootProcessor。
Plugin#Root
Root
ノードで呼び出されます。
子の変更時に再度呼び出されます。
型: RootProcessor。
Plugin#RootExit
すべての子が処理されると、Root
ノードで呼び出されます。
子の変更時に再度呼び出されます。
型: RootProcessor。
Plugin#Rule
すべてのRule
ノードで呼び出されます。
ノードまたは子の変更時に再度呼び出されます。
型: RuleProcessor。
Plugin#RuleExit
すべての子が処理されると、すべてのRule
ノードで呼び出されます。
ノードまたは子の変更時に再度呼び出されます。
型: RuleProcessor。
Plugin#postcssPlugin
型: string。
Plugin#prepare
型: Function。
PluginCreator
引数 | 型 |
---|---|
opts | PluginOptions |
PluginCreator#postcss
型: true。
Position
Position#column
ファイル内のソース行。offset
とは対照的に、1から始まります。
型: number。
Position#line
ファイル内のソース列。
型: number。
Position#offset
ファイル内のソースオフセット。0から始まります。
型: number。
ProcessOptions
ProcessOptions#from
CSSソースファイルのパス。ソースマップの生成と構文エラーメッセージで使用されるため、常にfrom
を設定する必要があります。
型: string。
ProcessOptions#map
ソースマップオプション
型: boolean | SourceMapOptions。
ProcessOptions#parser
文字列によってASTを生成する関数。
型: Parser<RootNode> | Syntax<RootNode>。
ProcessOptions#stringifier
ASTによって文字列を生成するクラス。
型: Stringifier | Syntax<RootNode>。
ProcessOptions#syntax
parseとstringifyを持つオブジェクト。
型: Syntax<RootNode>。
ProcessOptions#to
出力CSSファイルを配置するパス。正しいソースマップを生成するために、常にto
を設定する必要があります。
型: string。
Processor
Processor#plugins
このプロセッサーに追加されたプラグインです。
const processor = postcss([autoprefixer, postcssNested])
processor.plugins.length //=> 2
型: (Plugin | TransformCallback | Transformer)[]。
Processor#process()
ソースCSSを解析し、LazyResult
Promiseプロキシを返します。一部のプラグインは非同期である可能性があるため、変換は実行されません。変換はLazyResult
メソッドで適用されます。
processor.process(css, { from: 'a.css', to: 'a.out.css' })
.then(result => {
console.log(result.css)
})
引数 | 型 | 説明 |
---|---|---|
css | string | Root | Result<Document_ | Root> | Object | LazyResult_<Document_ | Root> | 入力CSSを含む文字列、またはBufferのようにtoString() メソッドを持つ任意のオブジェクト。オプションで、Result インスタンスを送信すると、プロセッサーはそこからRoot を取得します。 |
引数 | 型 | 説明 |
---|---|---|
css | string | Root | Result<Document_ | Root> | LazyResult_<Document_ | Root> | Object | 入力CSSを含む文字列、またはBufferのようにtoString() メソッドを持つ任意のオブジェクト。オプションで、Result インスタンスを送信すると、プロセッサーはそこからRoot を取得します。 |
options | ProcessOptions<RootNode> |
NoWorkResult_ | LazyResult_<Document_ | Root>
を返します。
Processor#use()
CSSプロセッサーとして使用するプラグインを追加します。
PostCSSプラグインは4つの形式で指定できます。
Plugin
形式のプラグイン。pluginCreator.postcss = true
を持つプラグインクリエーター関数。PostCSSはこの関数を引数なしで呼び出し、プラグインを取得します。- 関数。PostCSSは、最初の引数としてRoot、2番目の引数として現在の
Result
インスタンスを関数に渡します。 - 別の
Processor
インスタンス。PostCSSはそのインスタンスからこのインスタンスにプラグインをコピーします。
プラグインは、postcss
インスタンスを作成するときに引数として渡すことでも追加できます([postcss(plugins)
]を参照)。
非同期プラグインは、Promise
インスタンスを返す必要があります。
const processor = postcss()
.use(autoprefixer)
.use(postcssNested)
引数 | 型 | 説明 |
---|---|---|
plugin | AcceptedPlugin | PostCSSプラグインまたはプラグインを持つProcessor 。 |
Processor
を返します。
Processor#version
現在のPostCSSバージョン。
if (result.processor.version.split('.')[0] !== '6') {
throw new Error('This plugin works only with PostCSS 6')
}
型: string。
Range
Range#end
終了位置(排他的)。
型: Position。
Range#start
開始位置(包括的)。
型: Position。
RangePosition
RangePosition#column
入力内の列番号。
型: number。
RangePosition#line
入力内の行番号。
型: number。
Result
Result#css
Result#root
を表すCSS文字列。
postcss.parse('a{}').toResult().css //=> "a{}"
型: string。
Result#lastPlugin
最後に実行されたPostCSSプラグイン。
型: Plugin | TransformCallback。
Result#map
source-map
ライブラリからのSourceMapGenerator
クラスのインスタンスで、Result#root
インスタンスへの変更を表します。
result.map.toJSON() //=> { version: 3, file: 'a.css', … }
if (result.map) {
fs.writeFileSync(result.opts.to + '.map', result.map.toString())
}
型: SourceMap。
Result#messages
プラグインからのメッセージ(警告やカスタムメッセージなど)が含まれます。各メッセージには、typeとpluginプロパティが必要です。
AtRule: {
import: (atRule, { result }) {
const importedFile = parseImport(atRule)
result.messages.push({
type: 'dependency',
plugin: 'postcss-import',
file: importedFile,
parent: result.opts.from
})
}
}
型: Message[]。
Result#opts
このResultインスタンスを生成したProcessor#process
またはRoot#toResult
呼び出しからのオプションです。]
root.toResult(opts).opts === opts
型: ResultOptions。
Result#processor
この変換に使用されたProcessorインスタンス。
for (const plugin of result.processor.plugins) {
if (plugin.postcssPlugin === 'postcss-bad') {
throw 'postcss-good is incompatible with postcss-bad'
}
})
型: Processor。
Result#root
すべての変換後のRootノード。
root.toResult().root === root
型: RootNode。
Result#toString()
Result#css
の内容を返します。
result + '' === result.css
string
を返します。
Result#warn()
Warning
のインスタンスを作成し、Result#messages
に追加します。
if (decl.important) {
result.warn('Avoid !important', { node: decl, word: '!important' })
}
引数 | 型 |
---|---|
message | string |
options | WarningOptions |
Warning
を返します。
Result#warnings()
プラグインからの警告を返します。Result#messages
からWarning
インスタンスをフィルタリングします。
result.warnings().forEach(warn => {
console.warn(warn.toString())
})
Warning[]
を返します。
ResultOptions
ResultOptions#from
CSSソースファイルのパス。ソースマップの生成と構文エラーメッセージで使用されるため、常にfrom
を設定する必要があります。
型: string。
ResultOptions#map
ソースマップオプション
型: boolean | SourceMapOptions。
ResultOptions#node
警告のソースであったCSSノード。
型: Node。
ResultOptions#parser
文字列によってASTを生成する関数。
型: Syntax<Document_ | Root> | Parser<Document_ | Root>。
ResultOptions#plugin
この警告を作成したプラグインの名前。Result#warn
は、Plugin#postcssPlugin
値で自動的に入力します。
型: string。
ResultOptions#stringifier
ASTによって文字列を生成するクラス。
型: Stringifier | Syntax<Document_ | Root>。
ResultOptions#syntax
parseとstringifyを持つオブジェクト。
ResultOptions#to
出力CSSファイルを配置するパス。正しいソースマップを生成するために、常にto
を設定する必要があります。
型: string。
Root
Root#after()
現在のノードの親に、現在のノードの後に新しいノードを挿入します。
node.parent.insertAfter(node, add)
のエイリアス。
decl.after('color: black')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Root
を返します。
Root#append()
コンテナの末尾に新しいノードを挿入します。
const decl1 = new Declaration({ prop: 'color', value: 'black' })
const decl2 = new Declaration({ prop: 'background-color', value: 'white' })
rule.append(decl1, decl2)
root.append({ name: 'charset', params: '"UTF-8"' }) // at-rule
root.append({ selector: 'a' }) // rule
rule.append({ prop: 'color', value: 'black' }) // declaration
rule.append({ text: 'Comment' }) // comment
root.append('a {}')
root.first.append('color: black; z-index: 1')
引数 | 型 | 説明 |
---|---|---|
nodes… | (string | string[] | Node | ChildProps | ChildProps[] | Node[])[] | 新しいノード。 |
Root
を返します。
Root#assign()
既存のノードインスタンスにプロパティを割り当てます。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引数 | 型 | 説明 |
---|---|---|
overrides | object | RootProps | ノードをオーバーライドするための新しいプロパティ。 |
Root
を返します。
Root#before()
現在のノードの親に、現在のノードの前に新しいノードを挿入します。
node.parent.insertBefore(node, add)
のエイリアス。
decl.before('content: ""')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Root
を返します。
Root#cleanRaws()
ノードとその子に対するコードスタイルのプロパティをクリアします。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引数 | 型 | 説明 |
---|---|---|
keepBetween | boolean | raws.between シンボルを保持します。 |
Root#clone()
既存のノードのクローンを作成します。これには、すべてのプロパティとその値が含まれます。これには raws
が含まれますが、type
は含まれません。
decl.raws.before //=> "\n "
const cloned = decl.clone({ prop: '-moz-' + decl.prop })
cloned.raws.before //=> "\n "
cloned.toString() //=> -moz-transform: scale(0)
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<RootProps> | クローンでオーバーライドする新しいプロパティ。 |
Root
を返します。
Root#cloneAfter()
ノードをクローンし、結果のクローンされたノードを現在のノードの後に挿入するためのショートカット。
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<RootProps> | クローンでオーバーライドする新しいプロパティ。 |
Root
を返します。
Root#cloneBefore()
ノードをクローンし、結果のクローンされたノードを現在のノードの前に挿入するためのショートカット。
decl.cloneBefore({ prop: '-moz-' + decl.prop })
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<RootProps> | クローンでオーバーライドする新しいプロパティ。 |
Root
を返します。
Root#each()
コンテナの直接の子を反復処理し、子ごとに callback
を呼び出します。
コールバックで false
を返すと、反復処理が中断されます。
このメソッドは、コンテナの直接の子のみを反復処理します。コンテナのすべての子孫ノードを再帰的に反復処理する必要がある場合は、Container#walk
を使用します。
for {}
サイクルまたは Array#forEach
とは異なり、このイテレータは、反復処理中に子ノードの配列をミューテートする場合でも安全です。PostCSS は、ミューテーションに合わせて現在のインデックスを調整します。
const root = postcss.parse('a { color: black; z-index: 1 }')
const rule = root.first
for (const decl of rule.nodes) {
decl.cloneBefore({ prop: '-webkit-' + decl.prop })
// Cycle will be infinite, because cloneBefore moves the current node
// to the next index
}
rule.each(decl => {
decl.cloneBefore({ prop: '-webkit-' + decl.prop })
// Will be executed only for color and z-index
})
引数 | 型 | 説明 |
---|---|---|
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Root#error()
クラス CssSyntaxError
のインスタンスを作成し、このメソッドに渡されたパラメータがエラーインスタンスに割り当てられます。
エラーインスタンスには、エラーの説明、ソース内のノードの元の位置、行番号と列番号を示す説明が含まれます。
前のマップが存在する場合は、ソースの元の位置を取得するために使用されます。
ここでの前のマップは、以前のコンパイルで生成されたソースマップを指します。例: Less、Stylus、Sass。
このメソッドは、エラーインスタンスをスローするのではなく返します。
if (!variables[name]) {
throw decl.error(`Unknown variable ${name}`, { word: name })
// CssSyntaxError: postcss-vars:a.sass:4:3: Unknown variable $black
// color: $black
// a
// ^
// background: white
}
引数 | 型 | 説明 |
---|---|---|
message | string | エラーインスタンスの説明。 |
options | NodeErrorOptions | エラーインスタンスのオプション。 |
CssSyntaxError_
を返します。
Root#every()
コールバックがコンテナのすべての子に対して true
を返す場合は true
を返します。
const noPrefixes = rule.every(i => i.prop[0] !== '-')
引数 | 型 | 説明 |
---|---|---|
condition | Function | イテレータは true または false を返します。 |
boolean
を返します。
Root#index()
Container#nodes
配列内の child
のインデックスを返します。
rule.index( rule.nodes[2] ) //=> 2
引数 | 型 | 説明 |
---|---|---|
child | number | ChildNode | 現在のコンテナの子。 |
number
を返します。
Root#insertAfter()
コンテナ内の古いノードの後に新しいノードを挿入します。
引数 | 型 | 説明 |
---|---|---|
oldNode | number | ChildNode | 子または子のインデックス。 |
newNode | string | string[] | ChildNode | ChildProps | ChildNode[] | ChildProps[] | 新しいノード。 |
Root
を返します。
Root#insertBefore()
コンテナ内の古いノードの前に新しいノードを挿入します。
rule.insertBefore(decl, decl.clone({ prop: '-webkit-' + decl.prop }))
引数 | 型 | 説明 |
---|---|---|
oldNode | number | ChildNode | 子または子のインデックス。 |
newNode | string | string[] | ChildNode | ChildProps | ChildNode[] | ChildProps[] | 新しいノード。 |
Root
を返します。
Root#next()
ノードの親の次の子を返します。現在のノードが最後の子の場合は undefined
を返します。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
ChildNode
を返します。
Root#nodes
コンテナの子を含む配列。
const root = postcss.parse('a { color: black }')
root.nodes.length //=> 1
root.nodes[0].selector //=> 'a'
root.nodes[0].nodes[0].prop //=> 'color'
型: ChildNode[]。
Root#parent
現在のノードの親を表します。
root.nodes[0].parent === root //=> true
型: Document_。
Root#positionBy()
ノード内の単語またはインデックスの位置を取得します。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word"> | オプション。 |
Position
を返します。
Root#positionInside()
文字列のインデックスを行/列に変換します。
引数 | 型 | 説明 |
---|---|---|
index | number | ノードの文字列内の記号の番号。 |
Position
を返します。
Root#prepend()
コンテナの先頭に新しいノードを挿入します。
const decl1 = new Declaration({ prop: 'color', value: 'black' })
const decl2 = new Declaration({ prop: 'background-color', value: 'white' })
rule.prepend(decl1, decl2)
root.append({ name: 'charset', params: '"UTF-8"' }) // at-rule
root.append({ selector: 'a' }) // rule
rule.append({ prop: 'color', value: 'black' }) // declaration
rule.append({ text: 'Comment' }) // comment
root.append('a {}')
root.first.append('color: black; z-index: 1')
引数 | 型 | 説明 |
---|---|---|
nodes… | (string | string[] | Node | ChildProps | ChildProps[] | Node[])[] | 新しいノード。 |
Root
を返します。
Root#prev()
ノードの親の前の子を返します。現在のノードが最初の子である場合は、undefined
を返します。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
ChildNode
を返します。
Root#push()
ノードの末尾に子を追加します。
rule.push(new Declaration({ prop: 'color', value: 'black' }))
引数 | 型 | 説明 |
---|---|---|
child | ChildNode | 新しいノード。 |
Root
を返します。
Root#rangeBy()
ノード内の単語または開始インデックスと終了インデックスの範囲を取得します。開始インデックスは包括的で、終了インデックスは排他的です。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word" | "endIndex"> | オプション。 |
Range
を返します。
Root#raw()
raws
値を返します。ノードにコードスタイルプロパティがない場合(ノードが手動で作成または複製されたため)、PostCSSはツリー内の他のノードを調べてコードスタイルプロパティを自動検出しようとします。
const root = postcss.parse('a { background: white }')
root.nodes[0].append({ prop: 'color', value: 'black' })
root.nodes[0].nodes[1].raws.before //=> undefined
root.nodes[0].nodes[1].raw('before') //=> ' '
引数 | 型 | 説明 |
---|---|---|
prop | string | コードスタイルプロパティの名前。 |
defaultType | string | デフォルト値の名前。値が prop と同じ場合は省略できます。 |
string
を返します。
Root#raws
CSSソースコードに存在する不要な空白と文字を表します。
元の入力と同じように、バイト単位で等しいノード文字列を生成するための情報。
raws オブジェクトのプロパティはパーサーによって決定されます。デフォルトのパーサーは次のプロパティを使用します
before
: ノードの前の空白記号。宣言の前の*
および_
記号も保存します(IEハック)。after
: ノードの最後の子からノードの末尾までの空白記号。between
: 宣言の場合はプロパティと値の間、ルールの場合はセレクタと{
の間、または at-rule の場合は最後のパラメータと{
の間の記号。semicolon
: 最後の子が(オプションの)セミコロンを持っている場合は true が含まれます。afterName
: at-rule 名とそのパラメータの間の空白。left
:/*
とコメントのテキストの間の空白記号。right
: コメントのテキストと */ の間の空白記号。
important
: important ステートメントの内容。単に!important
ではない場合。
PostCSSはセレクタ、宣言値、および at-rule パラメータ内のコメントをフィルタリングしますが、raws に元のコンテンツを保存します。
const root = postcss.parse('a {\n color:black\n}')
root.first.first.raws //=> { before: '\n ', between: ':' }
型: RootRaws。
Root#remove()
ノードをその親から削除し、親プロパティを削除します。
if (decl.prop.match(/^-webkit-/)) {
decl.remove()
}
Root
を返します。
Root#removeAll()
コンテナからすべての子を削除し、それらの親プロパティをクリアします。
rule.removeAll()
rule.nodes.length //=> 0
Root
を返します。
Root#removeChild()
ノードをコンテナから削除し、ノードとその子から親プロパティをクリアします。
rule.nodes.length //=> 5
rule.removeChild(decl)
rule.nodes.length //=> 4
decl.parent //=> undefined
引数 | 型 | 説明 |
---|---|---|
child | number | ChildNode | 子または子のインデックス。 |
Root
を返します。
Root#replaceValues()
引数 | 型 |
---|---|
pattern | string | RegExp |
replaced | string | Function |
引数 | 型 | 説明 |
---|---|---|
pattern | string | RegExp | パターンを置換します。 |
options | ValueOptions | |
replaced | string | Function |
Root
を返します。
Root#replaceWith()
現在のノードの前にノードを挿入し、現在のノードを削除します。
AtRule: {
mixin: atrule => {
atrule.replaceWith(mixinRules[atrule.params])
}
}
引数 | 型 | 説明 |
---|---|---|
nodes… | (ChildNode | ChildProps | ChildNode[] | ChildProps[])[] | 現在のノードを置き換えるモード。 |
Root
を返します。
Root#root()
ノードのツリーの Root インスタンスを検索します。
root.nodes[0].nodes[0].root() === root
Root
を返します。
Root#some()
コンテナの子の少なくとも1つに対してコールバックが true
を返す場合は、true
を返します。
const hasPrefix = rule.some(i => i.prop[0] === '-')
引数 | 型 | 説明 |
---|---|---|
condition | Function | イテレータは true または false を返します。 |
boolean
を返します。
Root#source
ノードの起源に関する情報を表し、ソースマップの生成に必要です。
PostCSSが提供するパブリックAPIを使用して手動で作成されたノードは、source
が未定義になり、ソースマップに表示されません。
このため、プラグイン開発者は、複製されたノードはデフォルトで元のノードと同じソースを持つか、手動で作成されたノードにソースを割り当てるため、ノードを複製することを検討する必要があります。
decl.source.input.from //=> '/home/ai/source.css'
decl.source.start //=> { line: 10, column: 2 }
decl.source.end //=> { line: 10, column: 12 }
// Incorrect method, source not specified!
const prefixed = postcss.decl({
prop: '-moz-' + decl.prop,
value: decl.value
})
// Correct method, source is inherited when duplicating.
const prefixed = decl.clone({
prop: '-moz-' + decl.prop
})
if (atrule.name === 'add-link') {
const rule = postcss.rule({
selector: 'a',
source: atrule.source
})
atrule.parent.insertBefore(atrule, rule)
}
型: Source。
Root#toJSON()
JSON.stringify()
の循環リンクを修正します。
object
を返します。
Root#toResult()
ルートのCSSを表すResult
インスタンスを返します。
const root1 = postcss.parse(css1, { from: 'a.css' })
const root2 = postcss.parse(css2, { from: 'b.css' })
root1.append(root2)
const result = root1.toResult({ to: 'all.css', map: true })
引数 | 型 |
---|---|
options | ProcessOptions<Document_ | Root> |
Result<Document_ | Root>
を返します。
Root#toString()
ノードの型に応じて、ノードをブラウザで読み取り可能なカスケーディングスタイルシート文字列にコンパイルします。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引数 | 型 | 説明 |
---|---|---|
stringifier | Stringifier | Syntax<Document_ | Root> | 文字列生成で使用する構文。 |
string
を返します。
Root#type
抽象構文木におけるノードの型を表します。
ノードの型は、ノードの識別と型に基づいた操作の実行に役立ちます。
const declaration = new Declaration({
prop: 'color',
value: 'black'
})
declaration.type //=> 'decl'
型: "root"。
Root#walk()
コンテナの子孫ノードをトラバースし、各ノードに対してコールバックを呼び出します。
container.each() と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
コンテナの直下の子のみを反復する必要がある場合は、Container#each
を使用します。
root.walk(node => {
// Traverses all descendant nodes.
})
引数 | 型 | 説明 |
---|---|---|
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Root#walkAtRules()
コンテナの子孫ノードをトラバースし、各 at-rule ノードに対してコールバックを呼び出します。
フィルタを渡すと、名前が一致する at-rule のみに対して反復が行われます。
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
root.walkAtRules(rule => {
if (isOld(rule.name)) rule.remove()
})
let first = false
root.walkAtRules('charset', rule => {
if (!first) {
first = true
} else {
rule.remove()
}
})
引数 | 型 | 説明 |
---|---|---|
nameFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Root#walkComments()
引数 | 型 |
---|---|
callback | Function |
void | false
を返します。
Root#walkDecls()
コンテナの子孫ノードをトラバースし、各宣言ノードに対してコールバックを呼び出します。
フィルタを渡すと、プロパティが一致する宣言のみに対して反復が行われます。
root.walkDecls(decl => {
checkPropertySupport(decl.prop)
})
root.walkDecls('border-radius', decl => {
decl.remove()
})
root.walkDecls(/^background/, decl => {
decl.value = takeFirstColorFromGradient(decl.value)
})
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
引数 | 型 | 説明 |
---|---|---|
propFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Root#walkRules()
コンテナの子孫ノードをトラバースし、各ルールノードに対してコールバックを呼び出します。
フィルタを渡すと、セレクタが一致するルールのみに対して反復が行われます。
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
const selectors = []
root.walkRules(rule => {
selectors.push(rule.selector)
})
console.log(`Your CSS uses ${ selectors.length } selectors`)
引数 | 型 | 説明 |
---|---|---|
selectorFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Root#warn()
警告を生成する便利な方法を提供する、warnのラッパーです。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引数 | 型 | 説明 |
---|---|---|
result | Result<Document_ | Root> | 警告を受け取る Result インスタンス。 |
message | string | 警告の説明。 |
options | WarningOptions | 警告のオプション。 |
Warning
を返します。
RootProps
RootProps#nodes
型: (ChildNode | ChildProps)[]。
RootProps#raws
元の入力にあったノード文字列とバイト単位で等しいノード文字列を生成するために使用される情報。
型: RootRaws。
RootProps#source
型: Source。
RootProps
RootProps#nodes
型: (ChildNode | ChildProps)[]。
RootProps#raws
元の入力にあったノード文字列とバイト単位で等しいノード文字列を生成するために使用される情報。
型: RootRaws。
RootProps#source
型: Source。
RootRaws
RootRaws#after
最後のファイルの子からファイルの終わりまでの空白記号。
型: string。
RootRaws#codeAfter
Root
がDocument
内にある場合、Root
の後の非CSSコード。
実験的: このノードの一部の側面は、マイナーまたはパッチバージョンのリリースで変更される可能性があります。
型: string。
RootRaws#codeBefore
Root
がDocument
内にある場合、Root
の前の非CSSコード。
実験的: このノードの一部の側面は、マイナーまたはパッチバージョンのリリースで変更される可能性があります。
型: string。
RootRaws#semicolon
最後の子に(オプションの)セミコロンがあるかどうか。
型: boolean。
Rule
Rule#after()
現在のノードの親に、現在のノードの後に新しいノードを挿入します。
node.parent.insertAfter(node, add)
のエイリアス。
decl.after('color: black')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Rule
を返します。
Rule#append()
コンテナの末尾に新しいノードを挿入します。
const decl1 = new Declaration({ prop: 'color', value: 'black' })
const decl2 = new Declaration({ prop: 'background-color', value: 'white' })
rule.append(decl1, decl2)
root.append({ name: 'charset', params: '"UTF-8"' }) // at-rule
root.append({ selector: 'a' }) // rule
rule.append({ prop: 'color', value: 'black' }) // declaration
rule.append({ text: 'Comment' }) // comment
root.append('a {}')
root.first.append('color: black; z-index: 1')
引数 | 型 | 説明 |
---|---|---|
nodes… | (string | string[] | Node | ChildProps | ChildProps[] | Node[])[] | 新しいノード。 |
Rule
を返します。
Rule#assign()
既存のノードインスタンスにプロパティを割り当てます。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引数 | 型 | 説明 |
---|---|---|
overrides | object | RuleProps | ノードをオーバーライドするための新しいプロパティ。 |
Rule
を返します。
Rule#before()
現在のノードの親に、現在のノードの前に新しいノードを挿入します。
node.parent.insertBefore(node, add)
のエイリアス。
decl.before('content: ""')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Rule
を返します。
Rule#cleanRaws()
ノードとその子に対するコードスタイルのプロパティをクリアします。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引数 | 型 | 説明 |
---|---|---|
keepBetween | boolean | raws.between シンボルを保持します。 |
Rule#clone()
既存のノードのクローンを作成します。これには、すべてのプロパティとその値が含まれます。これには raws
が含まれますが、type
は含まれません。
decl.raws.before //=> "\n "
const cloned = decl.clone({ prop: '-moz-' + decl.prop })
cloned.raws.before //=> "\n "
cloned.toString() //=> -moz-transform: scale(0)
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<RuleProps> | クローンでオーバーライドする新しいプロパティ。 |
Rule
を返します。
Rule#cloneAfter()
ノードをクローンし、結果のクローンされたノードを現在のノードの後に挿入するためのショートカット。
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<RuleProps> | クローンでオーバーライドする新しいプロパティ。 |
Rule
を返します。
Rule#cloneBefore()
ノードをクローンし、結果のクローンされたノードを現在のノードの前に挿入するためのショートカット。
decl.cloneBefore({ prop: '-moz-' + decl.prop })
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<RuleProps> | クローンでオーバーライドする新しいプロパティ。 |
Rule
を返します。
Rule#each()
コンテナの直接の子を反復処理し、子ごとに callback
を呼び出します。
コールバックで false
を返すと、反復処理が中断されます。
このメソッドは、コンテナの直接の子のみを反復処理します。コンテナのすべての子孫ノードを再帰的に反復処理する必要がある場合は、Container#walk
を使用します。
for {}
サイクルまたは Array#forEach
とは異なり、このイテレータは、反復処理中に子ノードの配列をミューテートする場合でも安全です。PostCSS は、ミューテーションに合わせて現在のインデックスを調整します。
const root = postcss.parse('a { color: black; z-index: 1 }')
const rule = root.first
for (const decl of rule.nodes) {
decl.cloneBefore({ prop: '-webkit-' + decl.prop })
// Cycle will be infinite, because cloneBefore moves the current node
// to the next index
}
rule.each(decl => {
decl.cloneBefore({ prop: '-webkit-' + decl.prop })
// Will be executed only for color and z-index
})
引数 | 型 | 説明 |
---|---|---|
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Rule#error()
クラス CssSyntaxError
のインスタンスを作成し、このメソッドに渡されたパラメータがエラーインスタンスに割り当てられます。
エラーインスタンスには、エラーの説明、ソース内のノードの元の位置、行番号と列番号を示す説明が含まれます。
前のマップが存在する場合は、ソースの元の位置を取得するために使用されます。
ここでの前のマップは、以前のコンパイルで生成されたソースマップを指します。例: Less、Stylus、Sass。
このメソッドは、エラーインスタンスをスローするのではなく返します。
if (!variables[name]) {
throw decl.error(`Unknown variable ${name}`, { word: name })
// CssSyntaxError: postcss-vars:a.sass:4:3: Unknown variable $black
// color: $black
// a
// ^
// background: white
}
引数 | 型 | 説明 |
---|---|---|
message | string | エラーインスタンスの説明。 |
options | NodeErrorOptions | エラーインスタンスのオプション。 |
CssSyntaxError_
を返します。
Rule#every()
コールバックがコンテナのすべての子に対して true
を返す場合は true
を返します。
const noPrefixes = rule.every(i => i.prop[0] !== '-')
引数 | 型 | 説明 |
---|---|---|
condition | Function | イテレータは true または false を返します。 |
boolean
を返します。
Rule#index()
Container#nodes
配列内の child
のインデックスを返します。
rule.index( rule.nodes[2] ) //=> 2
引数 | 型 | 説明 |
---|---|---|
child | number | ChildNode | 現在のコンテナの子。 |
number
を返します。
Rule#insertAfter()
コンテナ内の古いノードの後に新しいノードを挿入します。
引数 | 型 | 説明 |
---|---|---|
oldNode | number | ChildNode | 子または子のインデックス。 |
newNode | string | string[] | ChildNode | ChildProps | ChildNode[] | ChildProps[] | 新しいノード。 |
Rule
を返します。
Rule#insertBefore()
コンテナ内の古いノードの前に新しいノードを挿入します。
rule.insertBefore(decl, decl.clone({ prop: '-webkit-' + decl.prop }))
引数 | 型 | 説明 |
---|---|---|
oldNode | number | ChildNode | 子または子のインデックス。 |
newNode | string | string[] | ChildNode | ChildProps | ChildNode[] | ChildProps[] | 新しいノード。 |
Rule
を返します。
Rule#next()
ノードの親の次の子を返します。現在のノードが最後の子の場合は undefined
を返します。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
ChildNode
を返します。
Rule#nodes
コンテナの子を含む配列。
const root = postcss.parse('a { color: black }')
root.nodes.length //=> 1
root.nodes[0].selector //=> 'a'
root.nodes[0].nodes[0].prop //=> 'color'
型: ChildNode[]。
Rule#parent
現在のノードの親を表します。
root.nodes[0].parent === root //=> true
型: Container_<ChildNode>。
Rule#positionBy()
ノード内の単語またはインデックスの位置を取得します。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word"> | オプション。 |
Position
を返します。
Rule#positionInside()
文字列のインデックスを行/列に変換します。
引数 | 型 | 説明 |
---|---|---|
index | number | ノードの文字列内の記号の番号。 |
Position
を返します。
Rule#prepend()
コンテナの先頭に新しいノードを挿入します。
const decl1 = new Declaration({ prop: 'color', value: 'black' })
const decl2 = new Declaration({ prop: 'background-color', value: 'white' })
rule.prepend(decl1, decl2)
root.append({ name: 'charset', params: '"UTF-8"' }) // at-rule
root.append({ selector: 'a' }) // rule
rule.append({ prop: 'color', value: 'black' }) // declaration
rule.append({ text: 'Comment' }) // comment
root.append('a {}')
root.first.append('color: black; z-index: 1')
引数 | 型 | 説明 |
---|---|---|
nodes… | (string | string[] | Node | ChildProps | ChildProps[] | Node[])[] | 新しいノード。 |
Rule
を返します。
Rule#prev()
ノードの親の前の子を返します。現在のノードが最初の子である場合は、undefined
を返します。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
ChildNode
を返します。
Rule#push()
ノードの末尾に子を追加します。
rule.push(new Declaration({ prop: 'color', value: 'black' }))
引数 | 型 | 説明 |
---|---|---|
child | ChildNode | 新しいノード。 |
Rule
を返します。
Rule#rangeBy()
ノード内の単語または開始インデックスと終了インデックスの範囲を取得します。開始インデックスは包括的で、終了インデックスは排他的です。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word" | "endIndex"> | オプション。 |
Range
を返します。
Rule#raw()
raws
値を返します。ノードにコードスタイルプロパティがない場合(ノードが手動で作成または複製されたため)、PostCSSはツリー内の他のノードを調べてコードスタイルプロパティを自動検出しようとします。
const root = postcss.parse('a { background: white }')
root.nodes[0].append({ prop: 'color', value: 'black' })
root.nodes[0].nodes[1].raws.before //=> undefined
root.nodes[0].nodes[1].raw('before') //=> ' '
引数 | 型 | 説明 |
---|---|---|
prop | string | コードスタイルプロパティの名前。 |
defaultType | string | デフォルト値の名前。値が prop と同じ場合は省略できます。 |
string
を返します。
Rule#raws
CSSソースコードに存在する不要な空白と文字を表します。
元の入力と同じように、バイト単位で等しいノード文字列を生成するための情報。
raws オブジェクトのプロパティはパーサーによって決定されます。デフォルトのパーサーは次のプロパティを使用します
before
: ノードの前の空白記号。宣言の前の*
および_
記号も保存します(IEハック)。after
: ノードの最後の子からノードの末尾までの空白記号。between
: 宣言の場合はプロパティと値の間、ルールの場合はセレクタと{
の間、または at-rule の場合は最後のパラメータと{
の間の記号。semicolon
: 最後の子が(オプションの)セミコロンを持っている場合は true が含まれます。afterName
: at-rule 名とそのパラメータの間の空白。left
:/*
とコメントのテキストの間の空白記号。right
: コメントのテキストと */ の間の空白記号。
important
: important ステートメントの内容。単に!important
ではない場合。
PostCSSはセレクタ、宣言値、および at-rule パラメータ内のコメントをフィルタリングしますが、raws に元のコンテンツを保存します。
const root = postcss.parse('a {\n color:black\n}')
root.first.first.raws //=> { before: '\n ', between: ':' }
型: RuleRaws。
Rule#remove()
ノードをその親から削除し、親プロパティを削除します。
if (decl.prop.match(/^-webkit-/)) {
decl.remove()
}
Rule
を返します。
Rule#removeAll()
コンテナからすべての子を削除し、それらの親プロパティをクリアします。
rule.removeAll()
rule.nodes.length //=> 0
Rule
を返します。
Rule#removeChild()
ノードをコンテナから削除し、ノードとその子から親プロパティをクリアします。
rule.nodes.length //=> 5
rule.removeChild(decl)
rule.nodes.length //=> 4
decl.parent //=> undefined
引数 | 型 | 説明 |
---|---|---|
child | number | ChildNode | 子または子のインデックス。 |
Rule
を返します。
Rule#replaceValues()
引数 | 型 |
---|---|
pattern | string | RegExp |
replaced | string | Function |
引数 | 型 | 説明 |
---|---|---|
pattern | string | RegExp | パターンを置換します。 |
options | ValueOptions | |
replaced | string | Function |
Rule
を返します。
Rule#replaceWith()
現在のノードの前にノードを挿入し、現在のノードを削除します。
AtRule: {
mixin: atrule => {
atrule.replaceWith(mixinRules[atrule.params])
}
}
引数 | 型 | 説明 |
---|---|---|
nodes… | (ChildNode | ChildProps | ChildNode[] | ChildProps[])[] | 現在のノードを置き換えるモード。 |
Rule
を返します。
Rule#root()
ノードのツリーの Root インスタンスを検索します。
root.nodes[0].nodes[0].root() === root
Root
を返します。
Rule#selector
ルール全体のセレクターを文字列として表したもの。
const root = postcss.parse('a, b { }')
const rule = root.first
rule.selector //=> 'a, b'
型: string。
Rule#selectors
ルールの個々のセレクターを含む配列。セレクターのグループはカンマで分割されます。
const root = postcss.parse('a, b { }')
const rule = root.first
rule.selector //=> 'a, b'
rule.selectors //=> ['a', 'b']
rule.selectors = ['a', 'strong']
rule.selector //=> 'a, strong'
型: string[]。
Rule#some()
コンテナの子の少なくとも1つに対してコールバックが true
を返す場合は、true
を返します。
const hasPrefix = rule.some(i => i.prop[0] === '-')
引数 | 型 | 説明 |
---|---|---|
condition | Function | イテレータは true または false を返します。 |
boolean
を返します。
Rule#source
ノードの起源に関する情報を表し、ソースマップの生成に必要です。
PostCSSが提供するパブリックAPIを使用して手動で作成されたノードは、source
が未定義になり、ソースマップに表示されません。
このため、プラグイン開発者は、複製されたノードはデフォルトで元のノードと同じソースを持つか、手動で作成されたノードにソースを割り当てるため、ノードを複製することを検討する必要があります。
decl.source.input.from //=> '/home/ai/source.css'
decl.source.start //=> { line: 10, column: 2 }
decl.source.end //=> { line: 10, column: 12 }
// Incorrect method, source not specified!
const prefixed = postcss.decl({
prop: '-moz-' + decl.prop,
value: decl.value
})
// Correct method, source is inherited when duplicating.
const prefixed = decl.clone({
prop: '-moz-' + decl.prop
})
if (atrule.name === 'add-link') {
const rule = postcss.rule({
selector: 'a',
source: atrule.source
})
atrule.parent.insertBefore(atrule, rule)
}
型: Source。
Rule#toJSON()
JSON.stringify()
の循環リンクを修正します。
object
を返します。
Rule#toString()
ノードの型に応じて、ノードをブラウザで読み取り可能なカスケーディングスタイルシート文字列にコンパイルします。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引数 | 型 | 説明 |
---|---|---|
stringifier | Stringifier | Syntax<Document_ | Root> | 文字列生成で使用する構文。 |
string
を返します。
Rule#type
抽象構文木におけるノードの型を表します。
ノードの型は、ノードの識別と型に基づいた操作の実行に役立ちます。
const declaration = new Declaration({
prop: 'color',
value: 'black'
})
declaration.type //=> 'decl'
型: "rule"。
Rule#walk()
コンテナの子孫ノードをトラバースし、各ノードに対してコールバックを呼び出します。
container.each() と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
コンテナの直下の子のみを反復する必要がある場合は、Container#each
を使用します。
root.walk(node => {
// Traverses all descendant nodes.
})
引数 | 型 | 説明 |
---|---|---|
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Rule#walkAtRules()
コンテナの子孫ノードをトラバースし、各 at-rule ノードに対してコールバックを呼び出します。
フィルタを渡すと、名前が一致する at-rule のみに対して反復が行われます。
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
root.walkAtRules(rule => {
if (isOld(rule.name)) rule.remove()
})
let first = false
root.walkAtRules('charset', rule => {
if (!first) {
first = true
} else {
rule.remove()
}
})
引数 | 型 | 説明 |
---|---|---|
nameFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Rule#walkComments()
引数 | 型 |
---|---|
callback | Function |
void | false
を返します。
Rule#walkDecls()
コンテナの子孫ノードをトラバースし、各宣言ノードに対してコールバックを呼び出します。
フィルタを渡すと、プロパティが一致する宣言のみに対して反復が行われます。
root.walkDecls(decl => {
checkPropertySupport(decl.prop)
})
root.walkDecls('border-radius', decl => {
decl.remove()
})
root.walkDecls(/^background/, decl => {
decl.value = takeFirstColorFromGradient(decl.value)
})
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
引数 | 型 | 説明 |
---|---|---|
propFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Rule#walkRules()
コンテナの子孫ノードをトラバースし、各ルールノードに対してコールバックを呼び出します。
フィルタを渡すと、セレクタが一致するルールのみに対して反復が行われます。
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
const selectors = []
root.walkRules(rule => {
selectors.push(rule.selector)
})
console.log(`Your CSS uses ${ selectors.length } selectors`)
引数 | 型 | 説明 |
---|---|---|
selectorFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
Rule#warn()
警告を生成する便利な方法を提供する、warnのラッパーです。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引数 | 型 | 説明 |
---|---|---|
result | Result<Document_ | Root> | 警告を受け取る Result インスタンス。 |
message | string | 警告の説明。 |
options | WarningOptions | 警告のオプション。 |
Warning
を返します。
RuleProps
RuleProps#nodes
型: (ChildNode | ChildProps)[]。
RuleProps#raws
元の入力にあったノード文字列とバイト単位で等しいノード文字列を生成するために使用される情報。
型: RuleRaws。
RuleProps#selector
ルールのセレクタまたはセレクタ。
型: string。
RuleProps#selectors
文字列の配列として表されるルールのセレクタ。
型: string[]。
RuleProps#source
型: Source。
RuleProps
RuleProps#nodes
型: (ChildNode | ChildProps)[]。
RuleProps#raws
元の入力にあったノード文字列とバイト単位で等しいノード文字列を生成するために使用される情報。
型: RuleRaws。
RuleProps#selector
ルールのセレクタまたはセレクタ。
型: string。
RuleProps#selectors
文字列の配列として表されるルールのセレクタ。
型: string[]。
RuleProps#source
型: Source。
RuleRaws
RuleRaws#after
ノードの最後の子からノードの終わりまでの空白記号。
型: string。
RuleRaws#before
ノードの前の空白記号。宣言の前の*
と_
記号(IEハック)も格納します。
型: string。
RuleRaws#between
ルールのセレクタと{
の間の記号。
型: string。
RuleRaws#ownSemicolon
ルール後にセミコロンがある場合はtrue
が含まれます。
型: string。
RuleRaws#selector
コメント付きのルールのセレクタ。
型: Object。
RuleRaws#semicolon
最後の子に(オプションの)セミコロンがある場合はtrue
が含まれます。
型: boolean。
Source
Source#end
ノードのソースコードの包括的な終了位置。
型: Position。
Source#input
ノードの起点となったソースファイル。
型: Input_。
Source#start
ノードのソースコードの包括的な開始位置。
型: Position。
SourceMapOptions
SourceMapOptions#absolute
生成されたソースマップで絶対パスを使用します。
型: boolean。
SourceMapOptions#annotation
PostCSSがCSSにアノテーションコメントを追加する必要があることを示します。デフォルトでは、PostCSSは常にソースマップへのパスを含むコメントを追加します。PostCSSはコメントを含まないCSSファイルにアノテーションを追加しません。
デフォルトでは、PostCSSはソースマップをopts.to + '.map'
として保存し、このパスをアノテーションコメントで使用することを前提としています。アノテーションに文字列値を指定することで、別のパスを設定できます。
inline: true
を設定した場合、アノテーションを無効にすることはできません。
型: string | boolean | Function.
SourceMapOptions#from
マップのsources内のfrom
を上書きします。
型: string。
SourceMapOptions#inline
ソースマップを出力CSSにBase64エンコードされたコメントとして埋め込むかどうかを示します。デフォルトではtrue
です。ただし、以前のすべてのマップが外部マップ(インラインではない)の場合、このオプションを設定しなくても、PostCSSはマップを埋め込みません。
インラインソースマップがある場合、ソースマップはresult.css
のテキストに含まれるため、result.mapプロパティは空になります。
型: boolean。
SourceMapOptions#prev
以前の処理ステップ(例:Sass)からのソースマップの内容。
PostCSSは(ソースCSS内のコメントに基づいて)以前のソースマップを自動的に読み取ろうとしますが、このオプションを使用して手動で識別できます。
必要に応じて、prev: false
を使用して以前のマップを省略できます。
型: string | boolean | object | Function.
SourceMapOptions#sourcesContent
PostCSSがソースマップの元のコンテンツ(例:Sassソース)を設定する必要があるかどうかを示します。デフォルトではtrueです。ただし、以前のすべてのマップにソースコンテンツが含まれていない場合、このオプションを設定しなくても、PostCSSも省略します。
型: boolean。
構文
Syntax#parse
文字列によってASTを生成する関数。
型: Parser<RootNode>.
Syntax#stringify
ASTによって文字列を生成するクラス。
型: Stringifier.
トランスフォーマー
引数 | 型 |
---|---|
root | Root |
result | Result<Document_ | Root> |
void | Promise<void>
を返します。
Transformer#postcssPlugin
型: string。
Transformer#postcssVersion
型: string。
ValueOptions
ValueOptions#fast
値を絞り込み、正規表現検索を高速化するために使用される文字列。
型: string。
ValueOptions#props
プロパティ名の配列。
型: string[]。
Warning
Warning#column
この警告のソースとなる入力ファイルでの、開始位置(包括的)のカラム。
warning.column //=> 6
型: number。
Warning#endColumn
この警告のソースとなる入力ファイルでの、終了位置(排他的)のカラム。
warning.endColumn //=> 4
型: number。
Warning#endLine
この警告のソースとなる入力ファイルでの、終了位置(排他的)の行。
warning.endLine //=> 6
型: number。
Warning#line
この警告のソースとなる入力ファイルでの、開始位置(包括的)の行。
warning.line //=> 5
型: number。
Warning#node
警告の原因となったCSSノードが含まれています。
warning.node.toString() //=> 'color: white !important'
型: Node。
Warning#plugin
この警告を作成したプラグインの名前。Node#warn
を呼び出すと、このプロパティが自動的に入力されます。
warning.plugin //=> 'postcss-important'
型: string。
Warning#text
警告メッセージ。
warning.text //=> 'Try to avoid !important'
型: string。
Warning#toString()
警告位置とメッセージを返します。
warning.toString() //=> 'postcss-lint:a.css:10:14: Avoid !important'
string
を返します。
Warning#type
Result#messages
から警告をフィルタリングするための型。常に"warning"
と等しくなります。
型: "warning"。
WarningOptions
WarningOptions#end
警告の原因となったCSSノード文字列内の終了位置(排他的)。
型: RangePosition.
WarningOptions#endIndex
警告の原因となったCSSノード文字列内の終了インデックス(排他的)。
型: number。
WarningOptions#index
警告の原因となったCSSノード文字列内の開始インデックス(包括的)。
型: number。
WarningOptions#node
警告の原因となったCSSノード。
型: Node。
WarningOptions#plugin
この警告を作成したプラグインの名前。Result#warn
はこのプロパティを自動的に入力します。
型: string。
WarningOptions#start
警告の原因となったCSSノード文字列内の開始位置(包括的)。
型: RangePosition.
WarningOptions#word
警告の原因となったCSSソース内の単語。
型: string。
WarningOptions
WarningOptions#end
警告の原因となったCSSノード文字列内の終了位置(排他的)。
型: RangePosition.
WarningOptions#endIndex
警告の原因となったCSSノード文字列内の終了インデックス(排他的)。
型: number。
WarningOptions#index
警告の原因となったCSSノード文字列内の開始インデックス(包括的)。
型: number。
WarningOptions#node
警告の原因となったCSSノード。
型: Node。
WarningOptions#plugin
この警告を作成したプラグインの名前。Result#warn
はこのプロパティを自動的に入力します。
型: string。
WarningOptions#start
警告の原因となったCSSノード文字列内の開始位置(包括的)。
型: RangePosition.
WarningOptions#word
警告の原因となったCSSソース内の単語。
型: string。
at-rule
at-rule#after()
現在のノードの親に、現在のノードの後に新しいノードを挿入します。
node.parent.insertAfter(node, add)
のエイリアス。
decl.after('color: black')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
AtRule_
を返します。
at-rule#append()
コンテナの末尾に新しいノードを挿入します。
const decl1 = new Declaration({ prop: 'color', value: 'black' })
const decl2 = new Declaration({ prop: 'background-color', value: 'white' })
rule.append(decl1, decl2)
root.append({ name: 'charset', params: '"UTF-8"' }) // at-rule
root.append({ selector: 'a' }) // rule
rule.append({ prop: 'color', value: 'black' }) // declaration
rule.append({ text: 'Comment' }) // comment
root.append('a {}')
root.first.append('color: black; z-index: 1')
引数 | 型 | 説明 |
---|---|---|
nodes… | (string | string[] | Node | ChildProps | ChildProps[] | Node[])[] | 新しいノード。 |
AtRule_
を返します。
at-rule#assign()
既存のノードインスタンスにプロパティを割り当てます。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引数 | 型 | 説明 |
---|---|---|
overrides | object | AtRuleProps | ノードをオーバーライドするための新しいプロパティ。 |
AtRule_
を返します。
at-rule#before()
現在のノードの親に、現在のノードの前に新しいノードを挿入します。
node.parent.insertBefore(node, add)
のエイリアス。
decl.before('content: ""')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
AtRule_
を返します。
at-rule#cleanRaws()
ノードとその子に対するコードスタイルのプロパティをクリアします。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引数 | 型 | 説明 |
---|---|---|
keepBetween | boolean | raws.between シンボルを保持します。 |
at-rule#clone()
既存のノードのクローンを作成します。これには、すべてのプロパティとその値が含まれます。これには raws
が含まれますが、type
は含まれません。
decl.raws.before //=> "\n "
const cloned = decl.clone({ prop: '-moz-' + decl.prop })
cloned.raws.before //=> "\n "
cloned.toString() //=> -moz-transform: scale(0)
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<AtRuleProps> | クローンでオーバーライドする新しいプロパティ。 |
AtRule
を返します。
at-rule#cloneAfter()
ノードをクローンし、結果のクローンされたノードを現在のノードの後に挿入するためのショートカット。
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<AtRuleProps> | クローンでオーバーライドする新しいプロパティ。 |
AtRule
を返します。
at-rule#cloneBefore()
ノードをクローンし、結果のクローンされたノードを現在のノードの前に挿入するためのショートカット。
decl.cloneBefore({ prop: '-moz-' + decl.prop })
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<AtRuleProps> | クローンでオーバーライドする新しいプロパティ。 |
AtRule
を返します。
at-rule#each()
コンテナの直接の子を反復処理し、子ごとに callback
を呼び出します。
コールバックで false
を返すと、反復処理が中断されます。
このメソッドは、コンテナの直接の子のみを反復処理します。コンテナのすべての子孫ノードを再帰的に反復処理する必要がある場合は、Container#walk
を使用します。
for {}
サイクルまたは Array#forEach
とは異なり、このイテレータは、反復処理中に子ノードの配列をミューテートする場合でも安全です。PostCSS は、ミューテーションに合わせて現在のインデックスを調整します。
const root = postcss.parse('a { color: black; z-index: 1 }')
const rule = root.first
for (const decl of rule.nodes) {
decl.cloneBefore({ prop: '-webkit-' + decl.prop })
// Cycle will be infinite, because cloneBefore moves the current node
// to the next index
}
rule.each(decl => {
decl.cloneBefore({ prop: '-webkit-' + decl.prop })
// Will be executed only for color and z-index
})
引数 | 型 | 説明 |
---|---|---|
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
at-rule#error()
クラス CssSyntaxError
のインスタンスを作成し、このメソッドに渡されたパラメータがエラーインスタンスに割り当てられます。
エラーインスタンスには、エラーの説明、ソース内のノードの元の位置、行番号と列番号を示す説明が含まれます。
前のマップが存在する場合は、ソースの元の位置を取得するために使用されます。
ここでの前のマップは、以前のコンパイルで生成されたソースマップを指します。例: Less、Stylus、Sass。
このメソッドは、エラーインスタンスをスローするのではなく返します。
if (!variables[name]) {
throw decl.error(`Unknown variable ${name}`, { word: name })
// CssSyntaxError: postcss-vars:a.sass:4:3: Unknown variable $black
// color: $black
// a
// ^
// background: white
}
引数 | 型 | 説明 |
---|---|---|
message | string | エラーインスタンスの説明。 |
options | NodeErrorOptions | エラーインスタンスのオプション。 |
CssSyntaxError_
を返します。
at-rule#every()
コールバックがコンテナのすべての子に対して true
を返す場合は true
を返します。
const noPrefixes = rule.every(i => i.prop[0] !== '-')
引数 | 型 | 説明 |
---|---|---|
condition | Function | イテレータは true または false を返します。 |
boolean
を返します。
at-rule#index()
Container#nodes
配列内の child
のインデックスを返します。
rule.index( rule.nodes[2] ) //=> 2
引数 | 型 | 説明 |
---|---|---|
child | number | ChildNode | 現在のコンテナの子。 |
number
を返します。
at-rule#insertAfter()
コンテナ内の古いノードの後に新しいノードを挿入します。
引数 | 型 | 説明 |
---|---|---|
oldNode | number | ChildNode | 子または子のインデックス。 |
newNode | string | string[] | ChildNode | ChildProps | ChildNode[] | ChildProps[] | 新しいノード。 |
AtRule_
を返します。
at-rule#insertBefore()
コンテナ内の古いノードの前に新しいノードを挿入します。
rule.insertBefore(decl, decl.clone({ prop: '-webkit-' + decl.prop }))
引数 | 型 | 説明 |
---|---|---|
oldNode | number | ChildNode | 子または子のインデックス。 |
newNode | string | string[] | ChildNode | ChildProps | ChildNode[] | ChildProps[] | 新しいノード。 |
AtRule_
を返します。
at-rule#name
at-rule の名前は、@
の直後に続きます。
const root = postcss.parse('@media print {}')
media.name //=> 'media'
const media = root.first
型: string。
at-rule#next()
ノードの親の次の子を返します。現在のノードが最後の子の場合は undefined
を返します。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
ChildNode
を返します。
at-rule#nodes
コンテナの子を含む配列。
const root = postcss.parse('a { color: black }')
root.nodes.length //=> 1
root.nodes[0].selector //=> 'a'
root.nodes[0].nodes[0].prop //=> 'color'
型: ChildNode[]。
at-rule#params
at-rule のパラメータ。at-rule の名前の後に続き、{}
ブロックの前にある値です。
const root = postcss.parse('@media print, screen {}')
const media = root.first
media.params //=> 'print, screen'
型: string。
at-rule#parent
現在のノードの親を表します。
root.nodes[0].parent === root //=> true
型: Container_<ChildNode>。
at-rule#positionBy()
ノード内の単語またはインデックスの位置を取得します。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word"> | オプション。 |
Position
を返します。
at-rule#positionInside()
文字列のインデックスを行/列に変換します。
引数 | 型 | 説明 |
---|---|---|
index | number | ノードの文字列内の記号の番号。 |
Position
を返します。
at-rule#prepend()
コンテナの先頭に新しいノードを挿入します。
const decl1 = new Declaration({ prop: 'color', value: 'black' })
const decl2 = new Declaration({ prop: 'background-color', value: 'white' })
rule.prepend(decl1, decl2)
root.append({ name: 'charset', params: '"UTF-8"' }) // at-rule
root.append({ selector: 'a' }) // rule
rule.append({ prop: 'color', value: 'black' }) // declaration
rule.append({ text: 'Comment' }) // comment
root.append('a {}')
root.first.append('color: black; z-index: 1')
引数 | 型 | 説明 |
---|---|---|
nodes… | (string | string[] | Node | ChildProps | ChildProps[] | Node[])[] | 新しいノード。 |
AtRule_
を返します。
at-rule#prev()
ノードの親の前の子を返します。現在のノードが最初の子である場合は、undefined
を返します。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
ChildNode
を返します。
at-rule#push()
ノードの末尾に子を追加します。
rule.push(new Declaration({ prop: 'color', value: 'black' }))
引数 | 型 | 説明 |
---|---|---|
child | ChildNode | 新しいノード。 |
AtRule_
を返します。
at-rule#rangeBy()
ノード内の単語または開始インデックスと終了インデックスの範囲を取得します。開始インデックスは包括的で、終了インデックスは排他的です。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word" | "endIndex"> | オプション。 |
Range
を返します。
at-rule#raw()
raws
値を返します。ノードにコードスタイルプロパティがない場合(ノードが手動で作成または複製されたため)、PostCSSはツリー内の他のノードを調べてコードスタイルプロパティを自動検出しようとします。
const root = postcss.parse('a { background: white }')
root.nodes[0].append({ prop: 'color', value: 'black' })
root.nodes[0].nodes[1].raws.before //=> undefined
root.nodes[0].nodes[1].raw('before') //=> ' '
引数 | 型 | 説明 |
---|---|---|
prop | string | コードスタイルプロパティの名前。 |
defaultType | string | デフォルト値の名前。値が prop と同じ場合は省略できます。 |
string
を返します。
at-rule#raws
CSSソースコードに存在する不要な空白と文字を表します。
元の入力と同じように、バイト単位で等しいノード文字列を生成するための情報。
raws オブジェクトのプロパティはパーサーによって決定されます。デフォルトのパーサーは次のプロパティを使用します
before
: ノードの前の空白記号。宣言の前の*
および_
記号も保存します(IEハック)。after
: ノードの最後の子からノードの末尾までの空白記号。between
: 宣言の場合はプロパティと値の間、ルールの場合はセレクタと{
の間、または at-rule の場合は最後のパラメータと{
の間の記号。semicolon
: 最後の子が(オプションの)セミコロンを持っている場合は true が含まれます。afterName
: at-rule 名とそのパラメータの間の空白。left
:/*
とコメントのテキストの間の空白記号。right
: コメントのテキストと */ の間の空白記号。
important
: important ステートメントの内容。単に!important
ではない場合。
PostCSSはセレクタ、宣言値、および at-rule パラメータ内のコメントをフィルタリングしますが、raws に元のコンテンツを保存します。
const root = postcss.parse('a {\n color:black\n}')
root.first.first.raws //=> { before: '\n ', between: ':' }
型: AtRuleRaws。
at-rule#remove()
ノードをその親から削除し、親プロパティを削除します。
if (decl.prop.match(/^-webkit-/)) {
decl.remove()
}
AtRule_
を返します。
at-rule#removeAll()
コンテナからすべての子を削除し、それらの親プロパティをクリアします。
rule.removeAll()
rule.nodes.length //=> 0
AtRule_
を返します。
at-rule#removeChild()
ノードをコンテナから削除し、ノードとその子から親プロパティをクリアします。
rule.nodes.length //=> 5
rule.removeChild(decl)
rule.nodes.length //=> 4
decl.parent //=> undefined
引数 | 型 | 説明 |
---|---|---|
child | number | ChildNode | 子または子のインデックス。 |
AtRule_
を返します。
at-rule#replaceValues()
引数 | 型 |
---|---|
pattern | string | RegExp |
replaced | string | Function |
引数 | 型 | 説明 |
---|---|---|
pattern | string | RegExp | パターンを置換します。 |
options | ValueOptions | |
replaced | string | Function |
AtRule_
を返します。
at-rule#replaceWith()
現在のノードの前にノードを挿入し、現在のノードを削除します。
AtRule: {
mixin: atrule => {
atrule.replaceWith(mixinRules[atrule.params])
}
}
引数 | 型 | 説明 |
---|---|---|
nodes… | (ChildNode | ChildProps | ChildNode[] | ChildProps[])[] | 現在のノードを置き換えるモード。 |
AtRule_
を返します。
at-rule#root()
ノードのツリーの Root インスタンスを検索します。
root.nodes[0].nodes[0].root() === root
Root
を返します。
at-rule#some()
コンテナの子の少なくとも1つに対してコールバックが true
を返す場合は、true
を返します。
const hasPrefix = rule.some(i => i.prop[0] === '-')
引数 | 型 | 説明 |
---|---|---|
condition | Function | イテレータは true または false を返します。 |
boolean
を返します。
at-rule#source
ノードの起源に関する情報を表し、ソースマップの生成に必要です。
PostCSSが提供するパブリックAPIを使用して手動で作成されたノードは、source
が未定義になり、ソースマップに表示されません。
このため、プラグイン開発者は、複製されたノードはデフォルトで元のノードと同じソースを持つか、手動で作成されたノードにソースを割り当てるため、ノードを複製することを検討する必要があります。
decl.source.input.from //=> '/home/ai/source.css'
decl.source.start //=> { line: 10, column: 2 }
decl.source.end //=> { line: 10, column: 12 }
// Incorrect method, source not specified!
const prefixed = postcss.decl({
prop: '-moz-' + decl.prop,
value: decl.value
})
// Correct method, source is inherited when duplicating.
const prefixed = decl.clone({
prop: '-moz-' + decl.prop
})
if (atrule.name === 'add-link') {
const rule = postcss.rule({
selector: 'a',
source: atrule.source
})
atrule.parent.insertBefore(atrule, rule)
}
型: Source。
at-rule#toJSON()
JSON.stringify()
の循環リンクを修正します。
object
を返します。
at-rule#toString()
ノードの型に応じて、ノードをブラウザで読み取り可能なカスケーディングスタイルシート文字列にコンパイルします。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引数 | 型 | 説明 |
---|---|---|
stringifier | Stringifier | Syntax<Document_ | Root> | 文字列生成で使用する構文。 |
string
を返します。
at-rule#type
抽象構文木におけるノードの型を表します。
ノードの型は、ノードの識別と型に基づいた操作の実行に役立ちます。
const declaration = new Declaration({
prop: 'color',
value: 'black'
})
declaration.type //=> 'decl'
型: "atrule"。
at-rule#walk()
コンテナの子孫ノードをトラバースし、各ノードに対してコールバックを呼び出します。
container.each() と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
コンテナの直下の子のみを反復する必要がある場合は、Container#each
を使用します。
root.walk(node => {
// Traverses all descendant nodes.
})
引数 | 型 | 説明 |
---|---|---|
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
at-rule#walkAtRules()
コンテナの子孫ノードをトラバースし、各 at-rule ノードに対してコールバックを呼び出します。
フィルタを渡すと、名前が一致する at-rule のみに対して反復が行われます。
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
root.walkAtRules(rule => {
if (isOld(rule.name)) rule.remove()
})
let first = false
root.walkAtRules('charset', rule => {
if (!first) {
first = true
} else {
rule.remove()
}
})
引数 | 型 | 説明 |
---|---|---|
nameFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
at-rule#walkComments()
引数 | 型 |
---|---|
callback | Function |
void | false
を返します。
at-rule#walkDecls()
コンテナの子孫ノードをトラバースし、各宣言ノードに対してコールバックを呼び出します。
フィルタを渡すと、プロパティが一致する宣言のみに対して反復が行われます。
root.walkDecls(decl => {
checkPropertySupport(decl.prop)
})
root.walkDecls('border-radius', decl => {
decl.remove()
})
root.walkDecls(/^background/, decl => {
decl.value = takeFirstColorFromGradient(decl.value)
})
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
引数 | 型 | 説明 |
---|---|---|
propFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
at-rule#walkRules()
コンテナの子孫ノードをトラバースし、各ルールノードに対してコールバックを呼び出します。
フィルタを渡すと、セレクタが一致するルールのみに対して反復が行われます。
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
const selectors = []
root.walkRules(rule => {
selectors.push(rule.selector)
})
console.log(`Your CSS uses ${ selectors.length } selectors`)
引数 | 型 | 説明 |
---|---|---|
selectorFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
at-rule#warn()
Result#warn のラッパーであり、警告を生成する便利な方法を提供します。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引数 | 型 | 説明 |
---|---|---|
result | Result<Document_ | Root> | 警告を受け取る Result インスタンス。 |
message | string | 警告の説明。 |
options | WarningOptions | 警告のオプション。 |
Warning
を返します。
コメント
comment#after()
現在のノードの親に、現在のノードの後に新しいノードを挿入します。
node.parent.insertAfter(node, add)
のエイリアス。
decl.after('color: black')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Comment_
を返します。
comment#assign()
既存のノードインスタンスにプロパティを割り当てます。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引数 | 型 | 説明 |
---|---|---|
overrides | object | CommentProps | ノードをオーバーライドするための新しいプロパティ。 |
Comment_
を返します。
comment#before()
現在のノードの親に、現在のノードの前に新しいノードを挿入します。
node.parent.insertBefore(node, add)
のエイリアス。
decl.before('content: ""')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Comment_
を返します。
comment#cleanRaws()
ノードとその子に対するコードスタイルのプロパティをクリアします。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引数 | 型 | 説明 |
---|---|---|
keepBetween | boolean | raws.between シンボルを保持します。 |
comment#clone()
既存のノードのクローンを作成します。これには、すべてのプロパティとその値が含まれます。これには raws
が含まれますが、type
は含まれません。
decl.raws.before //=> "\n "
const cloned = decl.clone({ prop: '-moz-' + decl.prop })
cloned.raws.before //=> "\n "
cloned.toString() //=> -moz-transform: scale(0)
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<CommentProps> | クローンでオーバーライドする新しいプロパティ。 |
Comment
を返します。
comment#cloneAfter()
ノードをクローンし、結果のクローンされたノードを現在のノードの後に挿入するためのショートカット。
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<CommentProps> | クローンでオーバーライドする新しいプロパティ。 |
Comment
を返します。
comment#cloneBefore()
ノードをクローンし、結果のクローンされたノードを現在のノードの前に挿入するためのショートカット。
decl.cloneBefore({ prop: '-moz-' + decl.prop })
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<CommentProps> | クローンでオーバーライドする新しいプロパティ。 |
Comment
を返します。
comment#error()
クラス CssSyntaxError
のインスタンスを作成し、このメソッドに渡されたパラメータがエラーインスタンスに割り当てられます。
エラーインスタンスには、エラーの説明、ソース内のノードの元の位置、行番号と列番号を示す説明が含まれます。
前のマップが存在する場合は、ソースの元の位置を取得するために使用されます。
ここでの前のマップは、以前のコンパイルで生成されたソースマップを指します。例: Less、Stylus、Sass。
このメソッドは、エラーインスタンスをスローするのではなく返します。
if (!variables[name]) {
throw decl.error(`Unknown variable ${name}`, { word: name })
// CssSyntaxError: postcss-vars:a.sass:4:3: Unknown variable $black
// color: $black
// a
// ^
// background: white
}
引数 | 型 | 説明 |
---|---|---|
message | string | エラーインスタンスの説明。 |
options | NodeErrorOptions | エラーインスタンスのオプション。 |
CssSyntaxError_
を返します。
comment#next()
ノードの親の次の子を返します。現在のノードが最後の子の場合は undefined
を返します。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
ChildNode
を返します。
comment#parent
現在のノードの親を表します。
root.nodes[0].parent === root //=> true
型: Container_<ChildNode>。
comment#positionBy()
ノード内の単語またはインデックスの位置を取得します。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word"> | オプション。 |
Position
を返します。
comment#positionInside()
文字列のインデックスを行/列に変換します。
引数 | 型 | 説明 |
---|---|---|
index | number | ノードの文字列内の記号の番号。 |
Position
を返します。
comment#prev()
ノードの親の前の子を返します。現在のノードが最初の子である場合は、undefined
を返します。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
ChildNode
を返します。
comment#rangeBy()
ノード内の単語または開始インデックスと終了インデックスの範囲を取得します。開始インデックスは包括的で、終了インデックスは排他的です。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word" | "endIndex"> | オプション。 |
Range
を返します。
comment#raw()
raws
値を返します。ノードにコードスタイルプロパティがない場合(ノードが手動で作成または複製されたため)、PostCSSはツリー内の他のノードを調べてコードスタイルプロパティを自動検出しようとします。
const root = postcss.parse('a { background: white }')
root.nodes[0].append({ prop: 'color', value: 'black' })
root.nodes[0].nodes[1].raws.before //=> undefined
root.nodes[0].nodes[1].raw('before') //=> ' '
引数 | 型 | 説明 |
---|---|---|
prop | string | コードスタイルプロパティの名前。 |
defaultType | string | デフォルト値の名前。値が prop と同じ場合は省略できます。 |
string
を返します。
comment#raws
CSSソースコードに存在する不要な空白と文字を表します。
元の入力と同じように、バイト単位で等しいノード文字列を生成するための情報。
raws オブジェクトのプロパティはパーサーによって決定されます。デフォルトのパーサーは次のプロパティを使用します
before
: ノードの前の空白記号。宣言の前の*
および_
記号も保存します(IEハック)。after
: ノードの最後の子からノードの末尾までの空白記号。between
: 宣言の場合はプロパティと値の間、ルールの場合はセレクタと{
の間、または at-rule の場合は最後のパラメータと{
の間の記号。semicolon
: 最後の子が(オプションの)セミコロンを持っている場合は true が含まれます。afterName
: at-rule 名とそのパラメータの間の空白。left
:/*
とコメントのテキストの間の空白記号。right
: コメントのテキストと */ の間の空白記号。
important
: important ステートメントの内容。単に!important
ではない場合。
PostCSSはセレクタ、宣言値、および at-rule パラメータ内のコメントをフィルタリングしますが、raws に元のコンテンツを保存します。
const root = postcss.parse('a {\n color:black\n}')
root.first.first.raws //=> { before: '\n ', between: ':' }
型: CommentRaws。
comment#remove()
ノードをその親から削除し、親プロパティを削除します。
if (decl.prop.match(/^-webkit-/)) {
decl.remove()
}
Comment_
を返します。
comment#replaceWith()
現在のノードの前にノードを挿入し、現在のノードを削除します。
AtRule: {
mixin: atrule => {
atrule.replaceWith(mixinRules[atrule.params])
}
}
引数 | 型 | 説明 |
---|---|---|
nodes… | (ChildNode | ChildProps | ChildNode[] | ChildProps[])[] | 現在のノードを置き換えるモード。 |
Comment_
を返します。
comment#root()
ノードのツリーの Root インスタンスを検索します。
root.nodes[0].nodes[0].root() === root
Root
を返します。
comment#source
ノードの起源に関する情報を表し、ソースマップの生成に必要です。
PostCSSが提供するパブリックAPIを使用して手動で作成されたノードは、source
が未定義になり、ソースマップに表示されません。
このため、プラグイン開発者は、複製されたノードはデフォルトで元のノードと同じソースを持つか、手動で作成されたノードにソースを割り当てるため、ノードを複製することを検討する必要があります。
decl.source.input.from //=> '/home/ai/source.css'
decl.source.start //=> { line: 10, column: 2 }
decl.source.end //=> { line: 10, column: 12 }
// Incorrect method, source not specified!
const prefixed = postcss.decl({
prop: '-moz-' + decl.prop,
value: decl.value
})
// Correct method, source is inherited when duplicating.
const prefixed = decl.clone({
prop: '-moz-' + decl.prop
})
if (atrule.name === 'add-link') {
const rule = postcss.rule({
selector: 'a',
source: atrule.source
})
atrule.parent.insertBefore(atrule, rule)
}
型: Source。
comment#text
コメントのテキスト。
型: string。
comment#toJSON()
JSON.stringify()
の循環リンクを修正します。
object
を返します。
comment#toString()
ノードの型に応じて、ノードをブラウザで読み取り可能なカスケーディングスタイルシート文字列にコンパイルします。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引数 | 型 | 説明 |
---|---|---|
stringifier | Stringifier | Syntax<Document_ | Root> | 文字列生成で使用する構文。 |
string
を返します。
comment#type
抽象構文木におけるノードの型を表します。
ノードの型は、ノードの識別と型に基づいた操作の実行に役立ちます。
const declaration = new Declaration({
prop: 'color',
value: 'black'
})
declaration.type //=> 'decl'
型: "comment"。
comment#warn()
Result#warn のラッパーであり、警告を生成する便利な方法を提供します。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引数 | 型 | 説明 |
---|---|---|
result | Result<Document_ | Root> | 警告を受け取る Result インスタンス。 |
message | string | 警告の説明。 |
options | WarningOptions | 警告のオプション。 |
Warning
を返します。
コンテナ
container#after()
現在のノードの親に、現在のノードの後に新しいノードを挿入します。
node.parent.insertAfter(node, add)
のエイリアス。
decl.after('color: black')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Container_<Child>
を返します。
container#append()
コンテナの末尾に新しいノードを挿入します。
const decl1 = new Declaration({ prop: 'color', value: 'black' })
const decl2 = new Declaration({ prop: 'background-color', value: 'white' })
rule.append(decl1, decl2)
root.append({ name: 'charset', params: '"UTF-8"' }) // at-rule
root.append({ selector: 'a' }) // rule
rule.append({ prop: 'color', value: 'black' }) // declaration
rule.append({ text: 'Comment' }) // comment
root.append('a {}')
root.first.append('color: black; z-index: 1')
引数 | 型 | 説明 |
---|---|---|
nodes… | (string | string[] | Node | ChildProps | ChildProps[] | Node[])[] | 新しいノード。 |
Container_<Child>
を返します。
container#assign()
既存のノードインスタンスにプロパティを割り当てます。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引数 | 型 | 説明 |
---|---|---|
overrides | object | ContainerProps | ノードをオーバーライドするための新しいプロパティ。 |
Container_<Child>
を返します。
container#before()
現在のノードの親に、現在のノードの前に新しいノードを挿入します。
node.parent.insertBefore(node, add)
のエイリアス。
decl.before('content: ""')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Container_<Child>
を返します。
container#cleanRaws()
ノードとその子に対するコードスタイルのプロパティをクリアします。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引数 | 型 | 説明 |
---|---|---|
keepBetween | boolean | raws.between シンボルを保持します。 |
container#clone()
既存のノードのクローンを作成します。これには、すべてのプロパティとその値が含まれます。これには raws
が含まれますが、type
は含まれません。
decl.raws.before //=> "\n "
const cloned = decl.clone({ prop: '-moz-' + decl.prop })
cloned.raws.before //=> "\n "
cloned.toString() //=> -moz-transform: scale(0)
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<ContainerProps> | クローンでオーバーライドする新しいプロパティ。 |
Container<Child>
を返します。
container#cloneAfter()
ノードをクローンし、結果のクローンされたノードを現在のノードの後に挿入するためのショートカット。
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<ContainerProps> | クローンでオーバーライドする新しいプロパティ。 |
Container<Child>
を返します。
container#cloneBefore()
ノードをクローンし、結果のクローンされたノードを現在のノードの前に挿入するためのショートカット。
decl.cloneBefore({ prop: '-moz-' + decl.prop })
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<ContainerProps> | クローンでオーバーライドする新しいプロパティ。 |
Container<Child>
を返します。
container#each()
コンテナの直接の子を反復処理し、子ごとに callback
を呼び出します。
コールバックで false
を返すと、反復処理が中断されます。
このメソッドは、コンテナの直接の子のみを反復処理します。コンテナのすべての子孫ノードを再帰的に反復処理する必要がある場合は、Container#walk
を使用します。
for {}
サイクルまたは Array#forEach
とは異なり、このイテレータは、反復処理中に子ノードの配列をミューテートする場合でも安全です。PostCSS は、ミューテーションに合わせて現在のインデックスを調整します。
const root = postcss.parse('a { color: black; z-index: 1 }')
const rule = root.first
for (const decl of rule.nodes) {
decl.cloneBefore({ prop: '-webkit-' + decl.prop })
// Cycle will be infinite, because cloneBefore moves the current node
// to the next index
}
rule.each(decl => {
decl.cloneBefore({ prop: '-webkit-' + decl.prop })
// Will be executed only for color and z-index
})
引数 | 型 | 説明 |
---|---|---|
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
container#error()
クラス CssSyntaxError
のインスタンスを作成し、このメソッドに渡されたパラメータがエラーインスタンスに割り当てられます。
エラーインスタンスには、エラーの説明、ソース内のノードの元の位置、行番号と列番号を示す説明が含まれます。
前のマップが存在する場合は、ソースの元の位置を取得するために使用されます。
ここでの前のマップは、以前のコンパイルで生成されたソースマップを指します。例: Less、Stylus、Sass。
このメソッドは、エラーインスタンスをスローするのではなく返します。
if (!variables[name]) {
throw decl.error(`Unknown variable ${name}`, { word: name })
// CssSyntaxError: postcss-vars:a.sass:4:3: Unknown variable $black
// color: $black
// a
// ^
// background: white
}
引数 | 型 | 説明 |
---|---|---|
message | string | エラーインスタンスの説明。 |
options | NodeErrorOptions | エラーインスタンスのオプション。 |
CssSyntaxError_
を返します。
container#every()
コールバックがコンテナのすべての子に対して true
を返す場合は true
を返します。
const noPrefixes = rule.every(i => i.prop[0] !== '-')
引数 | 型 | 説明 |
---|---|---|
condition | Function | イテレータは true または false を返します。 |
boolean
を返します。
container#index()
Container#nodes
配列内の child
のインデックスを返します。
rule.index( rule.nodes[2] ) //=> 2
引数 | 型 | 説明 |
---|---|---|
child | number | Child | 現在のコンテナの子。 |
number
を返します。
container#insertAfter()
コンテナ内の古いノードの後に新しいノードを挿入します。
引数 | 型 | 説明 |
---|---|---|
oldNode | number | Child | 子または子のインデックス。 |
newNode | string | string[] | Child | ChildProps | ChildProps[] | Child[] | 新しいノード。 |
Container_<Child>
を返します。
container#insertBefore()
コンテナ内の古いノードの前に新しいノードを挿入します。
rule.insertBefore(decl, decl.clone({ prop: '-webkit-' + decl.prop }))
引数 | 型 | 説明 |
---|---|---|
oldNode | number | Child | 子または子のインデックス。 |
newNode | string | string[] | Child | ChildProps | ChildProps[] | Child[] | 新しいノード。 |
Container_<Child>
を返します。
container#next()
ノードの親の次の子を返します。現在のノードが最後の子の場合は undefined
を返します。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
ChildNode
を返します。
container#nodes
コンテナの子を含む配列。
const root = postcss.parse('a { color: black }')
root.nodes.length //=> 1
root.nodes[0].selector //=> 'a'
root.nodes[0].nodes[0].prop //=> 'color'
型: Child[]。
container#parent
現在のノードの親を表します。
root.nodes[0].parent === root //=> true
型: Container_<ChildNode> | Document_。
container#positionBy()
ノード内の単語またはインデックスの位置を取得します。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word"> | オプション。 |
Position
を返します。
container#positionInside()
文字列のインデックスを行/列に変換します。
引数 | 型 | 説明 |
---|---|---|
index | number | ノードの文字列内の記号の番号。 |
Position
を返します。
container#prepend()
コンテナの先頭に新しいノードを挿入します。
const decl1 = new Declaration({ prop: 'color', value: 'black' })
const decl2 = new Declaration({ prop: 'background-color', value: 'white' })
rule.prepend(decl1, decl2)
root.append({ name: 'charset', params: '"UTF-8"' }) // at-rule
root.append({ selector: 'a' }) // rule
rule.append({ prop: 'color', value: 'black' }) // declaration
rule.append({ text: 'Comment' }) // comment
root.append('a {}')
root.first.append('color: black; z-index: 1')
引数 | 型 | 説明 |
---|---|---|
nodes… | (string | string[] | Node | ChildProps | ChildProps[] | Node[])[] | 新しいノード。 |
Container_<Child>
を返します。
container#prev()
ノードの親の前の子を返します。現在のノードが最初の子である場合は、undefined
を返します。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
ChildNode
を返します。
container#push()
ノードの末尾に子を追加します。
rule.push(new Declaration({ prop: 'color', value: 'black' }))
引数 | 型 | 説明 |
---|---|---|
child | Child | 新しいノード。 |
Container_<Child>
を返します。
container#rangeBy()
ノード内の単語または開始インデックスと終了インデックスの範囲を取得します。開始インデックスは包括的で、終了インデックスは排他的です。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word" | "endIndex"> | オプション。 |
Range
を返します。
container#raw()
raws
値を返します。ノードにコードスタイルプロパティがない場合(ノードが手動で作成または複製されたため)、PostCSSはツリー内の他のノードを調べてコードスタイルプロパティを自動検出しようとします。
const root = postcss.parse('a { background: white }')
root.nodes[0].append({ prop: 'color', value: 'black' })
root.nodes[0].nodes[1].raws.before //=> undefined
root.nodes[0].nodes[1].raw('before') //=> ' '
引数 | 型 | 説明 |
---|---|---|
prop | string | コードスタイルプロパティの名前。 |
defaultType | string | デフォルト値の名前。値が prop と同じ場合は省略できます。 |
string
を返します。
container#raws
CSSソースコードに存在する不要な空白と文字を表します。
元の入力と同じように、バイト単位で等しいノード文字列を生成するための情報。
raws オブジェクトのプロパティはパーサーによって決定されます。デフォルトのパーサーは次のプロパティを使用します
before
: ノードの前の空白記号。宣言の前の*
および_
記号も保存します(IEハック)。after
: ノードの最後の子からノードの末尾までの空白記号。between
: 宣言の場合はプロパティと値の間、ルールの場合はセレクタと{
の間、または at-rule の場合は最後のパラメータと{
の間の記号。semicolon
: 最後の子が(オプションの)セミコロンを持っている場合は true が含まれます。afterName
: at-rule 名とそのパラメータの間の空白。left
:/*
とコメントのテキストの間の空白記号。right
: コメントのテキストと */ の間の空白記号。
important
: important ステートメントの内容。単に!important
ではない場合。
PostCSSはセレクタ、宣言値、および at-rule パラメータ内のコメントをフィルタリングしますが、raws に元のコンテンツを保存します。
const root = postcss.parse('a {\n color:black\n}')
root.first.first.raws //=> { before: '\n ', between: ':' }
型: any。
container#remove()
ノードをその親から削除し、親プロパティを削除します。
if (decl.prop.match(/^-webkit-/)) {
decl.remove()
}
Container_<Child>
を返します。
container#removeAll()
コンテナからすべての子を削除し、それらの親プロパティをクリアします。
rule.removeAll()
rule.nodes.length //=> 0
Container_<Child>
を返します。
container#removeChild()
ノードをコンテナから削除し、ノードとその子から親プロパティをクリアします。
rule.nodes.length //=> 5
rule.removeChild(decl)
rule.nodes.length //=> 4
decl.parent //=> undefined
引数 | 型 | 説明 |
---|---|---|
child | number | Child | 子または子のインデックス。 |
Container_<Child>
を返します。
container#replaceValues()
引数 | 型 |
---|---|
pattern | string | RegExp |
replaced | string | Function |
引数 | 型 | 説明 |
---|---|---|
pattern | string | RegExp | パターンを置換します。 |
options | ValueOptions | |
replaced | string | Function |
Container_<Child>
を返します。
container#replaceWith()
現在のノードの前にノードを挿入し、現在のノードを削除します。
AtRule: {
mixin: atrule => {
atrule.replaceWith(mixinRules[atrule.params])
}
}
引数 | 型 | 説明 |
---|---|---|
nodes… | (ChildNode | ChildProps | ChildNode[] | ChildProps[])[] | 現在のノードを置き換えるモード。 |
Container_<Child>
を返します。
container#root()
ノードのツリーの Root インスタンスを検索します。
root.nodes[0].nodes[0].root() === root
Root
を返します。
container#some()
コンテナの子の少なくとも1つに対してコールバックが true
を返す場合は、true
を返します。
const hasPrefix = rule.some(i => i.prop[0] === '-')
引数 | 型 | 説明 |
---|---|---|
condition | Function | イテレータは true または false を返します。 |
boolean
を返します。
container#source
ノードの起源に関する情報を表し、ソースマップの生成に必要です。
PostCSSが提供するパブリックAPIを使用して手動で作成されたノードは、source
が未定義になり、ソースマップに表示されません。
このため、プラグイン開発者は、複製されたノードはデフォルトで元のノードと同じソースを持つか、手動で作成されたノードにソースを割り当てるため、ノードを複製することを検討する必要があります。
decl.source.input.from //=> '/home/ai/source.css'
decl.source.start //=> { line: 10, column: 2 }
decl.source.end //=> { line: 10, column: 12 }
// Incorrect method, source not specified!
const prefixed = postcss.decl({
prop: '-moz-' + decl.prop,
value: decl.value
})
// Correct method, source is inherited when duplicating.
const prefixed = decl.clone({
prop: '-moz-' + decl.prop
})
if (atrule.name === 'add-link') {
const rule = postcss.rule({
selector: 'a',
source: atrule.source
})
atrule.parent.insertBefore(atrule, rule)
}
型: Source。
container#toJSON()
JSON.stringify()
の循環リンクを修正します。
object
を返します。
container#toString()
ノードの型に応じて、ノードをブラウザで読み取り可能なカスケーディングスタイルシート文字列にコンパイルします。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引数 | 型 | 説明 |
---|---|---|
stringifier | Stringifier | Syntax<Document_ | Root> | 文字列生成で使用する構文。 |
string
を返します。
container#type
抽象構文木におけるノードの型を表します。
ノードの型は、ノードの識別と型に基づいた操作の実行に役立ちます。
const declaration = new Declaration({
prop: 'color',
value: 'black'
})
declaration.type //=> 'decl'
型: string。
container#walk()
コンテナの子孫ノードをトラバースし、各ノードに対してコールバックを呼び出します。
container.each() と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
コンテナの直下の子のみを反復する必要がある場合は、Container#each
を使用します。
root.walk(node => {
// Traverses all descendant nodes.
})
引数 | 型 | 説明 |
---|---|---|
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
container#walkAtRules()
コンテナの子孫ノードをトラバースし、各 at-rule ノードに対してコールバックを呼び出します。
フィルタを渡すと、名前が一致する at-rule のみに対して反復が行われます。
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
root.walkAtRules(rule => {
if (isOld(rule.name)) rule.remove()
})
let first = false
root.walkAtRules('charset', rule => {
if (!first) {
first = true
} else {
rule.remove()
}
})
引数 | 型 | 説明 |
---|---|---|
nameFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
container#walkComments()
引数 | 型 |
---|---|
callback | Function |
void | false
を返します。
container#walkDecls()
コンテナの子孫ノードをトラバースし、各宣言ノードに対してコールバックを呼び出します。
フィルタを渡すと、プロパティが一致する宣言のみに対して反復が行われます。
root.walkDecls(decl => {
checkPropertySupport(decl.prop)
})
root.walkDecls('border-radius', decl => {
decl.remove()
})
root.walkDecls(/^background/, decl => {
decl.value = takeFirstColorFromGradient(decl.value)
})
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
引数 | 型 | 説明 |
---|---|---|
propFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
container#walkRules()
コンテナの子孫ノードをトラバースし、各ルールノードに対してコールバックを呼び出します。
フィルタを渡すと、セレクタが一致するルールのみに対して反復が行われます。
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
const selectors = []
root.walkRules(rule => {
selectors.push(rule.selector)
})
console.log(`Your CSS uses ${ selectors.length } selectors`)
引数 | 型 | 説明 |
---|---|---|
selectorFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
container#warn()
Result#warn のラッパーであり、警告を生成する便利な方法を提供します。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引数 | 型 | 説明 |
---|---|---|
result | Result<Document_ | Root> | 警告を受け取る Result インスタンス。 |
message | string | 警告の説明。 |
options | WarningOptions | 警告のオプション。 |
Warning
を返します。
css-syntax-error
css-syntax-error#column
エラーのソース列。
error.column //=> 1
error.input.column //=> 4
PostCSS は、入力ソースマップを使用して元の場所を検出します。PostCSS 入力内の位置が必要な場合は、error.input.column
を使用します。
型: number。
css-syntax-error#endColumn
エラーの終了位置のソース列 (排他的)。エラーが範囲に関連する場合に提供されます。
error.endColumn //=> 1
error.input.endColumn //=> 4
PostCSS は、入力ソースマップを使用して元の場所を検出します。PostCSS 入力内の位置が必要な場合は、error.input.endColumn
を使用します。
型: number。
css-syntax-error#endLine
エラーの終了位置のソース行 (排他的)。エラーが範囲に関連する場合に提供されます。
error.endLine //=> 3
error.input.endLine //=> 4
PostCSS は、入力ソースマップを使用して元の場所を検出します。PostCSS 入力内の位置が必要な場合は、error.input.endLine
を使用します。
型: number。
css-syntax-error#file
破損したファイルへの絶対パス。
error.file //=> 'a.sass'
error.input.file //=> 'a.css'
PostCSS は、入力ソースマップを使用して元の場所を検出します。PostCSS 入力内の位置が必要な場合は、error.input.file
を使用します。
型: string。
css-syntax-error#input
入力ファイルに関する PostCSS 内部情報を含む入力オブジェクト。入力に前のツールからのソースマップがある場合、PostCSS は元の (たとえば、Sass) ソースを使用します。このオブジェクトを使用して、PostCSS 入力ソースを取得できます。
error.input.file //=> 'a.css'
error.file //=> 'a.sass'
型: FilePosition。
css-syntax-error#line
エラーのソース行。
error.line //=> 2
error.input.line //=> 4
PostCSS は、入力ソースマップを使用して元の場所を検出します。PostCSS 入力内の位置が必要な場合は、error.input.line
を使用します。
型: number。
css-syntax-error#message
プラグイン、ファイル、行、および列を含む GNU エラー形式の完全なエラーテキスト。
error.message //=> 'a.css:1:1: Unclosed block'
型: string。
css-syntax-error#name
常に 'CssSyntaxError'
に等しくなります。npm に複数の PostCSS バージョンが存在する可能性があるため、常に error instanceof CssSyntaxError
ではなく、error.name === 'CssSyntaxError'
でエラー型を確認する必要があります。
if (error.name === 'CssSyntaxError') {
error //=> CssSyntaxError
}
型: "CssSyntaxError"。
css-syntax-error#plugin
エラーがプラグインから発生した場合のプラグイン名。
error.plugin //=> 'postcss-vars'
型: string。
css-syntax-error#reason
エラーメッセージ。
error.message //=> 'Unclosed block'
型: string。
css-syntax-error#showSourceCode()
エラーの原因となった CSS ソースの数行を返します。
CSS に sourceContent
を含まない入力ソースマップがある場合、このメソッドは空の文字列を返します。
error.showSourceCode() //=> " 4 | }
// 5 | a {
// > 6 | bad
// | ^
// 7 | }
// 8 | b {"
引数 | 型 | 説明 |
---|---|---|
color | boolean | 矢印がターミナルのカラーコードで赤く色付けされるかどうか。デフォルトでは、PostCSS は process.stdout.isTTY と process.env.NODE_DISABLE_COLORS によってカラーサポートを検出します。 |
string
を返します。
css-syntax-error#source
破損したファイルのソースコード。
error.source //=> 'a { b {} }'
error.input.source //=> 'a b { }'
型: string。
css-syntax-error#stack
型: string。
css-syntax-error#toString()
エラー位置、メッセージ、および破損部分のソースコードを返します。
error.toString() //=> "CssSyntaxError: app.css:1:1: Unclosed block
// > 1 | a {
// | ^"
string
を返します。
宣言
declaration#after()
現在のノードの親に、現在のノードの後に新しいノードを挿入します。
node.parent.insertAfter(node, add)
のエイリアス。
decl.after('color: black')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Declaration_
を返します。
declaration#assign()
既存のノードインスタンスにプロパティを割り当てます。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引数 | 型 | 説明 |
---|---|---|
overrides | object | DeclarationProps | ノードをオーバーライドするための新しいプロパティ。 |
Declaration_
を返します。
declaration#before()
現在のノードの親に、現在のノードの前に新しいノードを挿入します。
node.parent.insertBefore(node, add)
のエイリアス。
decl.before('content: ""')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Declaration_
を返します。
declaration#cleanRaws()
ノードとその子に対するコードスタイルのプロパティをクリアします。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引数 | 型 | 説明 |
---|---|---|
keepBetween | boolean | raws.between シンボルを保持します。 |
declaration#clone()
既存のノードのクローンを作成します。これには、すべてのプロパティとその値が含まれます。これには raws
が含まれますが、type
は含まれません。
decl.raws.before //=> "\n "
const cloned = decl.clone({ prop: '-moz-' + decl.prop })
cloned.raws.before //=> "\n "
cloned.toString() //=> -moz-transform: scale(0)
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<DeclarationProps> | クローンでオーバーライドする新しいプロパティ。 |
Declaration
を返します。
declaration#cloneAfter()
ノードをクローンし、結果のクローンされたノードを現在のノードの後に挿入するためのショートカット。
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<DeclarationProps> | クローンでオーバーライドする新しいプロパティ。 |
Declaration
を返します。
declaration#cloneBefore()
ノードをクローンし、結果のクローンされたノードを現在のノードの前に挿入するためのショートカット。
decl.cloneBefore({ prop: '-moz-' + decl.prop })
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<DeclarationProps> | クローンでオーバーライドする新しいプロパティ。 |
Declaration
を返します。
declaration#error()
クラス CssSyntaxError
のインスタンスを作成し、このメソッドに渡されたパラメータがエラーインスタンスに割り当てられます。
エラーインスタンスには、エラーの説明、ソース内のノードの元の位置、行番号と列番号を示す説明が含まれます。
前のマップが存在する場合は、ソースの元の位置を取得するために使用されます。
ここでの前のマップは、以前のコンパイルで生成されたソースマップを指します。例: Less、Stylus、Sass。
このメソッドは、エラーインスタンスをスローするのではなく返します。
if (!variables[name]) {
throw decl.error(`Unknown variable ${name}`, { word: name })
// CssSyntaxError: postcss-vars:a.sass:4:3: Unknown variable $black
// color: $black
// a
// ^
// background: white
}
引数 | 型 | 説明 |
---|---|---|
message | string | エラーインスタンスの説明。 |
options | NodeErrorOptions | エラーインスタンスのオプション。 |
CssSyntaxError_
を返します。
declaration#important
宣言の優先度を表します。
true の場合、CSS 宣言には important 指定子が付きます。
const root = postcss.parse('a { color: black !important; color: red }')
root.first.first.important //=> true
root.first.last.important //=> undefined
型: boolean。
declaration#next()
ノードの親の次の子を返します。現在のノードが最後の子の場合は undefined
を返します。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
ChildNode
を返します。
declaration#parent
現在のノードの親を表します。
root.nodes[0].parent === root //=> true
型: Container_<ChildNode>。
declaration#positionBy()
ノード内の単語またはインデックスの位置を取得します。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word"> | オプション。 |
Position
を返します。
declaration#positionInside()
文字列のインデックスを行/列に変換します。
引数 | 型 | 説明 |
---|---|---|
index | number | ノードの文字列内の記号の番号。 |
Position
を返します。
declaration#prev()
ノードの親の前の子を返します。現在のノードが最初の子である場合は、undefined
を返します。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
ChildNode
を返します。
declaration#prop
CSS 宣言のプロパティ名。
const root = postcss.parse('a { color: black }')
const decl = root.first.first
decl.prop //=> 'color'
型: string。
declaration#rangeBy()
ノード内の単語または開始インデックスと終了インデックスの範囲を取得します。開始インデックスは包括的で、終了インデックスは排他的です。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word" | "endIndex"> | オプション。 |
Range
を返します。
declaration#raw()
raws
値を返します。ノードにコードスタイルプロパティがない場合(ノードが手動で作成または複製されたため)、PostCSSはツリー内の他のノードを調べてコードスタイルプロパティを自動検出しようとします。
const root = postcss.parse('a { background: white }')
root.nodes[0].append({ prop: 'color', value: 'black' })
root.nodes[0].nodes[1].raws.before //=> undefined
root.nodes[0].nodes[1].raw('before') //=> ' '
引数 | 型 | 説明 |
---|---|---|
prop | string | コードスタイルプロパティの名前。 |
defaultType | string | デフォルト値の名前。値が prop と同じ場合は省略できます。 |
string
を返します。
declaration#raws
CSSソースコードに存在する不要な空白と文字を表します。
元の入力と同じように、バイト単位で等しいノード文字列を生成するための情報。
raws オブジェクトのプロパティはパーサーによって決定されます。デフォルトのパーサーは次のプロパティを使用します
before
: ノードの前の空白記号。宣言の前の*
および_
記号も保存します(IEハック)。after
: ノードの最後の子からノードの末尾までの空白記号。between
: 宣言の場合はプロパティと値の間、ルールの場合はセレクタと{
の間、または at-rule の場合は最後のパラメータと{
の間の記号。semicolon
: 最後の子が(オプションの)セミコロンを持っている場合は true が含まれます。afterName
: at-rule 名とそのパラメータの間の空白。left
:/*
とコメントのテキストの間の空白記号。right
: コメントのテキストと */ の間の空白記号。
important
: important ステートメントの内容。単に!important
ではない場合。
PostCSSはセレクタ、宣言値、および at-rule パラメータ内のコメントをフィルタリングしますが、raws に元のコンテンツを保存します。
const root = postcss.parse('a {\n color:black\n}')
root.first.first.raws //=> { before: '\n ', between: ':' }
型: DeclarationRaws。
declaration#remove()
ノードをその親から削除し、親プロパティを削除します。
if (decl.prop.match(/^-webkit-/)) {
decl.remove()
}
Declaration_
を返します。
declaration#replaceWith()
現在のノードの前にノードを挿入し、現在のノードを削除します。
AtRule: {
mixin: atrule => {
atrule.replaceWith(mixinRules[atrule.params])
}
}
引数 | 型 | 説明 |
---|---|---|
nodes… | (ChildNode | ChildProps | ChildNode[] | ChildProps[])[] | 現在のノードを置き換えるモード。 |
Declaration_
を返します。
declaration#root()
ノードのツリーの Root インスタンスを検索します。
root.nodes[0].nodes[0].root() === root
Root
を返します。
declaration#source
ノードの起源に関する情報を表し、ソースマップの生成に必要です。
PostCSSが提供するパブリックAPIを使用して手動で作成されたノードは、source
が未定義になり、ソースマップに表示されません。
このため、プラグイン開発者は、複製されたノードはデフォルトで元のノードと同じソースを持つか、手動で作成されたノードにソースを割り当てるため、ノードを複製することを検討する必要があります。
decl.source.input.from //=> '/home/ai/source.css'
decl.source.start //=> { line: 10, column: 2 }
decl.source.end //=> { line: 10, column: 12 }
// Incorrect method, source not specified!
const prefixed = postcss.decl({
prop: '-moz-' + decl.prop,
value: decl.value
})
// Correct method, source is inherited when duplicating.
const prefixed = decl.clone({
prop: '-moz-' + decl.prop
})
if (atrule.name === 'add-link') {
const rule = postcss.rule({
selector: 'a',
source: atrule.source
})
atrule.parent.insertBefore(atrule, rule)
}
型: Source。
declaration#toJSON()
JSON.stringify()
の循環リンクを修正します。
object
を返します。
declaration#toString()
ノードの型に応じて、ノードをブラウザで読み取り可能なカスケーディングスタイルシート文字列にコンパイルします。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引数 | 型 | 説明 |
---|---|---|
stringifier | Stringifier | Syntax<Document_ | Root> | 文字列生成で使用する構文。 |
string
を返します。
declaration#type
抽象構文木におけるノードの型を表します。
ノードの型は、ノードの識別と型に基づいた操作の実行に役立ちます。
const declaration = new Declaration({
prop: 'color',
value: 'black'
})
declaration.type //=> 'decl'
型: "decl"。
declaration#value
CSS 宣言のプロパティ値。
値文字列内の CSS コメントはすべてフィルタリングされます。ソース値に存在する CSS コメントは、raws
プロパティで利用できます。
新しい value
を割り当てると、ノードを文字列にコンパイルする際に raws
プロパティのコメントは無視されます。
const root = postcss.parse('a { color: black }')
const decl = root.first.first
decl.value //=> 'black'
型: string。
declaration#variable
CSS および SASS/SCSS で変数を宣言するために使用される、宣言が --
または $
で始まる場合に true
を返すゲッターを表します。
const root = postcss.parse(':root { --one: 1 }')
const one = root.first.first
one.variable //=> true
const root = postcss.parse('$one: 1')
const one = root.first
one.variable //=> true
型: boolean。
declaration#warn()
Result#warn のラッパーであり、警告を生成する便利な方法を提供します。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引数 | 型 | 説明 |
---|---|---|
result | Result<Document_ | Root> | 警告を受け取る Result インスタンス。 |
message | string | 警告の説明。 |
options | WarningOptions | 警告のオプション。 |
Warning
を返します。
ドキュメント
document#after()
現在のノードの親に、現在のノードの後に新しいノードを挿入します。
node.parent.insertAfter(node, add)
のエイリアス。
decl.after('color: black')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Document_
を返します。
document#append()
コンテナの末尾に新しいノードを挿入します。
const decl1 = new Declaration({ prop: 'color', value: 'black' })
const decl2 = new Declaration({ prop: 'background-color', value: 'white' })
rule.append(decl1, decl2)
root.append({ name: 'charset', params: '"UTF-8"' }) // at-rule
root.append({ selector: 'a' }) // rule
rule.append({ prop: 'color', value: 'black' }) // declaration
rule.append({ text: 'Comment' }) // comment
root.append('a {}')
root.first.append('color: black; z-index: 1')
引数 | 型 | 説明 |
---|---|---|
nodes… | (string | string[] | Node | ChildProps | ChildProps[] | Node[])[] | 新しいノード。 |
Document_
を返します。
document#assign()
既存のノードインスタンスにプロパティを割り当てます。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引数 | 型 | 説明 |
---|---|---|
overrides | object | DocumentProps | ノードをオーバーライドするための新しいプロパティ。 |
Document_
を返します。
document#before()
現在のノードの親に、現在のノードの前に新しいノードを挿入します。
node.parent.insertBefore(node, add)
のエイリアス。
decl.before('content: ""')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Document_
を返します。
document#cleanRaws()
ノードとその子に対するコードスタイルのプロパティをクリアします。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引数 | 型 | 説明 |
---|---|---|
keepBetween | boolean | raws.between シンボルを保持します。 |
document#clone()
既存のノードのクローンを作成します。これには、すべてのプロパティとその値が含まれます。これには raws
が含まれますが、type
は含まれません。
decl.raws.before //=> "\n "
const cloned = decl.clone({ prop: '-moz-' + decl.prop })
cloned.raws.before //=> "\n "
cloned.toString() //=> -moz-transform: scale(0)
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<DocumentProps> | クローンでオーバーライドする新しいプロパティ。 |
Document
を返します。
document#cloneAfter()
ノードをクローンし、結果のクローンされたノードを現在のノードの後に挿入するためのショートカット。
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<DocumentProps> | クローンでオーバーライドする新しいプロパティ。 |
Document
を返します。
document#cloneBefore()
ノードをクローンし、結果のクローンされたノードを現在のノードの前に挿入するためのショートカット。
decl.cloneBefore({ prop: '-moz-' + decl.prop })
引数 | 型 | 説明 |
---|---|---|
overrides | Partial<DocumentProps> | クローンでオーバーライドする新しいプロパティ。 |
Document
を返します。
document#each()
コンテナの直接の子を反復処理し、子ごとに callback
を呼び出します。
コールバックで false
を返すと、反復処理が中断されます。
このメソッドは、コンテナの直接の子のみを反復処理します。コンテナのすべての子孫ノードを再帰的に反復処理する必要がある場合は、Container#walk
を使用します。
for {}
サイクルまたは Array#forEach
とは異なり、このイテレータは、反復処理中に子ノードの配列をミューテートする場合でも安全です。PostCSS は、ミューテーションに合わせて現在のインデックスを調整します。
const root = postcss.parse('a { color: black; z-index: 1 }')
const rule = root.first
for (const decl of rule.nodes) {
decl.cloneBefore({ prop: '-webkit-' + decl.prop })
// Cycle will be infinite, because cloneBefore moves the current node
// to the next index
}
rule.each(decl => {
decl.cloneBefore({ prop: '-webkit-' + decl.prop })
// Will be executed only for color and z-index
})
引数 | 型 | 説明 |
---|---|---|
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
document#error()
クラス CssSyntaxError
のインスタンスを作成し、このメソッドに渡されたパラメータがエラーインスタンスに割り当てられます。
エラーインスタンスには、エラーの説明、ソース内のノードの元の位置、行番号と列番号を示す説明が含まれます。
前のマップが存在する場合は、ソースの元の位置を取得するために使用されます。
ここでの前のマップは、以前のコンパイルで生成されたソースマップを指します。例: Less、Stylus、Sass。
このメソッドは、エラーインスタンスをスローするのではなく返します。
if (!variables[name]) {
throw decl.error(`Unknown variable ${name}`, { word: name })
// CssSyntaxError: postcss-vars:a.sass:4:3: Unknown variable $black
// color: $black
// a
// ^
// background: white
}
引数 | 型 | 説明 |
---|---|---|
message | string | エラーインスタンスの説明。 |
options | NodeErrorOptions | エラーインスタンスのオプション。 |
CssSyntaxError_
を返します。
document#every()
コールバックがコンテナのすべての子に対して true
を返す場合は true
を返します。
const noPrefixes = rule.every(i => i.prop[0] !== '-')
引数 | 型 | 説明 |
---|---|---|
condition | Function | イテレータは true または false を返します。 |
boolean
を返します。
document#index()
Container#nodes
配列内の child
のインデックスを返します。
rule.index( rule.nodes[2] ) //=> 2
引数 | 型 | 説明 |
---|---|---|
child | number | Root | 現在のコンテナの子。 |
number
を返します。
document#insertAfter()
コンテナ内の古いノードの後に新しいノードを挿入します。
引数 | 型 | 説明 |
---|---|---|
oldNode | number | Root | 子または子のインデックス。 |
newNode | string | string[] | ChildProps | ChildProps[] | Root | Root[] | 新しいノード。 |
Document_
を返します。
document#insertBefore()
コンテナ内の古いノードの前に新しいノードを挿入します。
rule.insertBefore(decl, decl.clone({ prop: '-webkit-' + decl.prop }))
引数 | 型 | 説明 |
---|---|---|
oldNode | number | Root | 子または子のインデックス。 |
newNode | string | string[] | ChildProps | ChildProps[] | Root | Root[] | 新しいノード。 |
Document_
を返します。
document#next()
ノードの親の次の子を返します。現在のノードが最後の子の場合は undefined
を返します。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
ChildNode
を返します。
document#nodes
コンテナの子を含む配列。
const root = postcss.parse('a { color: black }')
root.nodes.length //=> 1
root.nodes[0].selector //=> 'a'
root.nodes[0].nodes[0].prop //=> 'color'
型: Root[]。
document#parent
現在のノードの親を表します。
root.nodes[0].parent === root //=> true
型: undefined。
document#positionBy()
ノード内の単語またはインデックスの位置を取得します。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word"> | オプション。 |
Position
を返します。
document#positionInside()
文字列のインデックスを行/列に変換します。
引数 | 型 | 説明 |
---|---|---|
index | number | ノードの文字列内の記号の番号。 |
Position
を返します。
document#prepend()
コンテナの先頭に新しいノードを挿入します。
const decl1 = new Declaration({ prop: 'color', value: 'black' })
const decl2 = new Declaration({ prop: 'background-color', value: 'white' })
rule.prepend(decl1, decl2)
root.append({ name: 'charset', params: '"UTF-8"' }) // at-rule
root.append({ selector: 'a' }) // rule
rule.append({ prop: 'color', value: 'black' }) // declaration
rule.append({ text: 'Comment' }) // comment
root.append('a {}')
root.first.append('color: black; z-index: 1')
引数 | 型 | 説明 |
---|---|---|
nodes… | (string | string[] | Node | ChildProps | ChildProps[] | Node[])[] | 新しいノード。 |
Document_
を返します。
document#prev()
ノードの親の前の子を返します。現在のノードが最初の子である場合は、undefined
を返します。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
ChildNode
を返します。
document#push()
ノードの末尾に子を追加します。
rule.push(new Declaration({ prop: 'color', value: 'black' }))
引数 | 型 | 説明 |
---|---|---|
child | Root | 新しいノード。 |
Document_
を返します。
document#rangeBy()
ノード内の単語または開始インデックスと終了インデックスの範囲を取得します。開始インデックスは包括的で、終了インデックスは排他的です。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word" | "endIndex"> | オプション。 |
Range
を返します。
document#raw()
raws
値を返します。ノードにコードスタイルプロパティがない場合(ノードが手動で作成または複製されたため)、PostCSSはツリー内の他のノードを調べてコードスタイルプロパティを自動検出しようとします。
const root = postcss.parse('a { background: white }')
root.nodes[0].append({ prop: 'color', value: 'black' })
root.nodes[0].nodes[1].raws.before //=> undefined
root.nodes[0].nodes[1].raw('before') //=> ' '
引数 | 型 | 説明 |
---|---|---|
prop | string | コードスタイルプロパティの名前。 |
defaultType | string | デフォルト値の名前。値が prop と同じ場合は省略できます。 |
string
を返します。
document#raws
CSSソースコードに存在する不要な空白と文字を表します。
元の入力と同じように、バイト単位で等しいノード文字列を生成するための情報。
raws オブジェクトのプロパティはパーサーによって決定されます。デフォルトのパーサーは次のプロパティを使用します
before
: ノードの前の空白記号。宣言の前の*
および_
記号も保存します(IEハック)。after
: ノードの最後の子からノードの末尾までの空白記号。between
: 宣言の場合はプロパティと値の間、ルールの場合はセレクタと{
の間、または at-rule の場合は最後のパラメータと{
の間の記号。semicolon
: 最後の子が(オプションの)セミコロンを持っている場合は true が含まれます。afterName
: at-rule 名とそのパラメータの間の空白。left
:/*
とコメントのテキストの間の空白記号。right
: コメントのテキストと */ の間の空白記号。
important
: important ステートメントの内容。単に!important
ではない場合。
PostCSSはセレクタ、宣言値、および at-rule パラメータ内のコメントをフィルタリングしますが、raws に元のコンテンツを保存します。
const root = postcss.parse('a {\n color:black\n}')
root.first.first.raws //=> { before: '\n ', between: ':' }
型: any。
document#remove()
ノードをその親から削除し、親プロパティを削除します。
if (decl.prop.match(/^-webkit-/)) {
decl.remove()
}
Document_
を返します。
document#removeAll()
コンテナからすべての子を削除し、それらの親プロパティをクリアします。
rule.removeAll()
rule.nodes.length //=> 0
Document_
を返します。
document#removeChild()
ノードをコンテナから削除し、ノードとその子から親プロパティをクリアします。
rule.nodes.length //=> 5
rule.removeChild(decl)
rule.nodes.length //=> 4
decl.parent //=> undefined
引数 | 型 | 説明 |
---|---|---|
child | number | Root | 子または子のインデックス。 |
Document_
を返します。
document#replaceValues()
引数 | 型 |
---|---|
pattern | string | RegExp |
replaced | string | Function |
引数 | 型 | 説明 |
---|---|---|
pattern | string | RegExp | パターンを置換します。 |
options | ValueOptions | |
replaced | string | Function |
Document_
を返します。
document#replaceWith()
現在のノードの前にノードを挿入し、現在のノードを削除します。
AtRule: {
mixin: atrule => {
atrule.replaceWith(mixinRules[atrule.params])
}
}
引数 | 型 | 説明 |
---|---|---|
nodes… | (ChildNode | ChildProps | ChildNode[] | ChildProps[])[] | 現在のノードを置き換えるモード。 |
Document_
を返します。
document#root()
ノードのツリーの Root インスタンスを検索します。
root.nodes[0].nodes[0].root() === root
Root
を返します。
document#some()
コンテナの子の少なくとも1つに対してコールバックが true
を返す場合は、true
を返します。
const hasPrefix = rule.some(i => i.prop[0] === '-')
引数 | 型 | 説明 |
---|---|---|
condition | Function | イテレータは true または false を返します。 |
boolean
を返します。
document#source
ノードの起源に関する情報を表し、ソースマップの生成に必要です。
PostCSSが提供するパブリックAPIを使用して手動で作成されたノードは、source
が未定義になり、ソースマップに表示されません。
このため、プラグイン開発者は、複製されたノードはデフォルトで元のノードと同じソースを持つか、手動で作成されたノードにソースを割り当てるため、ノードを複製することを検討する必要があります。
decl.source.input.from //=> '/home/ai/source.css'
decl.source.start //=> { line: 10, column: 2 }
decl.source.end //=> { line: 10, column: 12 }
// Incorrect method, source not specified!
const prefixed = postcss.decl({
prop: '-moz-' + decl.prop,
value: decl.value
})
// Correct method, source is inherited when duplicating.
const prefixed = decl.clone({
prop: '-moz-' + decl.prop
})
if (atrule.name === 'add-link') {
const rule = postcss.rule({
selector: 'a',
source: atrule.source
})
atrule.parent.insertBefore(atrule, rule)
}
型: Source。
document#toJSON()
JSON.stringify()
の循環リンクを修正します。
object
を返します。
document#toResult()
ドキュメントの CSS ルートを表す Result
インスタンスを返します。
const root1 = postcss.parse(css1, { from: 'a.css' })
const root2 = postcss.parse(css2, { from: 'b.css' })
const document = postcss.document()
document.append(root1)
document.append(root2)
const result = document.toResult({ to: 'all.css', map: true })
引数 | 型 |
---|---|
options | ProcessOptions<Document_ | Root> |
Result<Document_ | Root>
を返します。
document#toString()
ノードの型に応じて、ノードをブラウザで読み取り可能なカスケーディングスタイルシート文字列にコンパイルします。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引数 | 型 | 説明 |
---|---|---|
stringifier | Stringifier | Syntax<Document_ | Root> | 文字列生成で使用する構文。 |
string
を返します。
document#type
抽象構文木におけるノードの型を表します。
ノードの型は、ノードの識別と型に基づいた操作の実行に役立ちます。
const declaration = new Declaration({
prop: 'color',
value: 'black'
})
declaration.type //=> 'decl'
型: "document"。
document#walk()
コンテナの子孫ノードをトラバースし、各ノードに対してコールバックを呼び出します。
container.each() と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
コンテナの直下の子のみを反復する必要がある場合は、Container#each
を使用します。
root.walk(node => {
// Traverses all descendant nodes.
})
引数 | 型 | 説明 |
---|---|---|
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
document#walkAtRules()
コンテナの子孫ノードをトラバースし、各 at-rule ノードに対してコールバックを呼び出します。
フィルタを渡すと、名前が一致する at-rule のみに対して反復が行われます。
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
root.walkAtRules(rule => {
if (isOld(rule.name)) rule.remove()
})
let first = false
root.walkAtRules('charset', rule => {
if (!first) {
first = true
} else {
rule.remove()
}
})
引数 | 型 | 説明 |
---|---|---|
nameFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
document#walkComments()
引数 | 型 |
---|---|
callback | Function |
void | false
を返します。
document#walkDecls()
コンテナの子孫ノードをトラバースし、各宣言ノードに対してコールバックを呼び出します。
フィルタを渡すと、プロパティが一致する宣言のみに対して反復が行われます。
root.walkDecls(decl => {
checkPropertySupport(decl.prop)
})
root.walkDecls('border-radius', decl => {
decl.remove()
})
root.walkDecls(/^background/, decl => {
decl.value = takeFirstColorFromGradient(decl.value)
})
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
引数 | 型 | 説明 |
---|---|---|
propFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
document#walkRules()
コンテナの子孫ノードをトラバースし、各ルールノードに対してコールバックを呼び出します。
フィルタを渡すと、セレクタが一致するルールのみに対して反復が行われます。
Container#each
と同様に、このメソッドは反復中に配列を変更する場合でも安全に使用できます。
const selectors = []
root.walkRules(rule => {
selectors.push(rule.selector)
})
console.log(`Your CSS uses ${ selectors.length } selectors`)
引数 | 型 | 説明 |
---|---|---|
selectorFilter | string | RegExp | |
callback | Function | イテレータは各ノードとインデックスを受け取ります。 |
void | false
を返します。
document#warn()
Result#warn のラッパーであり、警告を生成する便利な方法を提供します。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引数 | 型 | 説明 |
---|---|---|
result | Result<Document_ | Root> | 警告を受け取る Result インスタンス。 |
message | string | 警告の説明。 |
options | WarningOptions | 警告のオプション。 |
Warning
を返します。
fromJSON
引数 | 型 |
---|---|
data | object |
引数 | 型 |
---|---|
data | object[] |
Node
を返します。
input
input#css
入力 CSS ソース。
const input = postcss.parse('a{}', { from: file }).input
input.css //=> "a{}"
型: string。
input#error()
引数 | 型 |
---|---|
message | string |
start | Object | Object |
end | Object | Object |
opts | Object |
引数 | 型 |
---|---|
message | string |
line | number |
column | number |
opts | Object |
引数 | 型 |
---|---|
message | string |
offset | number |
opts | Object |
CssSyntaxError_
を返します。
input#file
from
オプションで定義された CSS ソースファイルへの絶対パス。
const root = postcss.parse(css, { from: 'a.css' })
root.source.input.file //=> '/home/ai/a.css'
型: string。
input#fromOffset()
ソースオフセットを行と列に変換します。
引数 | 型 | 説明 |
---|---|---|
offset | number | ソースオフセット。 |
Object
を返します。
input#hasBOM
ソースコードに Unicode BOM があるかどうかを示すフラグ。
型: boolean。
input#id
CSS ソースの一意の ID。from
オプションが指定されていない場合に作成されます (PostCSS はファイルパスを知らないため)。
const root = postcss.parse(css)
root.source.input.file //=> undefined
root.source.input.id //=> "<input css 8LZeVF>"
型: string。
input#map
PostCSS の前のコンパイルステップ (たとえば、Sass コンパイラから) から渡された入力ソースマップ。
root.source.input.map.consumer().sources //=> ['a.sass']
型: PreviousMap_。
input#origin()
入力ソースマップを読み取り、入力ソース (たとえば、PostCSS に渡される前に CSS にコンパイルされた Sass ファイル) 内のシンボル位置を返します。オプションで、排他的な終了位置を指定できます。
root.source.input.origin(1, 1) //=> { file: 'a.css', line: 3, column: 1 }
root.source.input.origin(1, 1, 1, 4)
//=> { file: 'a.css', line: 3, column: 1, endLine: 3, endColumn: 4 }
引数 | 型 | 説明 |
---|---|---|
line | number | 入力 CSS の包含開始位置の行。 |
column | number | 入力 CSS の包含開始位置の列。 |
endLine | number | 入力 CSS の排他的終了位置の行。 |
endColumn | number | 入力 CSS の排他的終了位置の列。 |
false | FilePosition
を返します。
lazy-result
lazy-result#async()
プラグインを非同期的に実行し、Result
を返します。
Promise<Result<RootNode>>
を返します。
lazy-result#catch
型: Function。
lazy-result#finally
型: Function。
lazy-result#sync()
プラグインを同期的に実行し、Result
を返します。
Result<RootNode>
を返します。
lazy-result#then
型: Function。
lazy-result#toString()
LazyResult#css
プロパティのエイリアス。
lazy + '' === lazy.css
string
を返します。
lazy-result#warnings()
同期プラグインを通じて入力 CSS を処理し、Result#warnings
を呼び出します。
Warning[]
を返します。
no-work-result
no-work-result#async()
プラグインを非同期的に実行し、Result
を返します。
no-work-result#catch
型: Function。
no-work-result#finally
型: Function。
no-work-result#sync()
プラグインを同期的に実行し、Result
を返します。
no-work-result#then
型: Function。
no-work-result#toString()
LazyResult#css
プロパティのエイリアス。
lazy + '' === lazy.css
string
を返します。
no-work-result#warnings()
同期プラグインを通じて入力 CSS を処理し、Result#warnings
を呼び出します。
Warning[]
を返します。
node
node#after()
現在のノードの親に、現在のノードの後に新しいノードを挿入します。
node.parent.insertAfter(node, add)
のエイリアス。
decl.after('color: black')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Node
を返します。
node#assign()
既存のノードインスタンスにプロパティを割り当てます。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引数 | 型 | 説明 |
---|---|---|
overrides | object | ノードをオーバーライドするための新しいプロパティ。 |
Node
を返します。
node#before()
現在のノードの親に、現在のノードの前に新しいノードを挿入します。
node.parent.insertBefore(node, add)
のエイリアス。
decl.before('content: ""')
引数 | 型 | 説明 |
---|---|---|
newNode | string | Node | ChildProps | Node[] | 新しいノード。 |
Node
を返します。
node#cleanRaws()
ノードとその子に対するコードスタイルのプロパティをクリアします。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引数 | 型 | 説明 |
---|---|---|
keepBetween | boolean | raws.between シンボルを保持します。 |
node#clone()
既存のノードのクローンを作成します。これには、すべてのプロパティとその値が含まれます。これには raws
が含まれますが、type
は含まれません。
decl.raws.before //=> "\n "
const cloned = decl.clone({ prop: '-moz-' + decl.prop })
cloned.raws.before //=> "\n "
cloned.toString() //=> -moz-transform: scale(0)
引数 | 型 | 説明 |
---|---|---|
overrides | object | クローンでオーバーライドする新しいプロパティ。 |
Node
を返します。
node#cloneAfter()
ノードをクローンし、結果のクローンされたノードを現在のノードの後に挿入するためのショートカット。
引数 | 型 | 説明 |
---|---|---|
overrides | object | クローンでオーバーライドする新しいプロパティ。 |
Node
を返します。
node#cloneBefore()
ノードをクローンし、結果のクローンされたノードを現在のノードの前に挿入するためのショートカット。
decl.cloneBefore({ prop: '-moz-' + decl.prop })
引数 | 型 | 説明 |
---|---|---|
overrides | object | クローンでオーバーライドする新しいプロパティ。 |
Node
を返します。
node#error()
クラス CssSyntaxError
のインスタンスを作成し、このメソッドに渡されたパラメータがエラーインスタンスに割り当てられます。
エラーインスタンスには、エラーの説明、ソース内のノードの元の位置、行番号と列番号を示す説明が含まれます。
前のマップが存在する場合は、ソースの元の位置を取得するために使用されます。
ここでの前のマップは、以前のコンパイルで生成されたソースマップを指します。例: Less、Stylus、Sass。
このメソッドは、エラーインスタンスをスローするのではなく返します。
if (!variables[name]) {
throw decl.error(`Unknown variable ${name}`, { word: name })
// CssSyntaxError: postcss-vars:a.sass:4:3: Unknown variable $black
// color: $black
// a
// ^
// background: white
}
引数 | 型 | 説明 |
---|---|---|
message | string | エラーインスタンスの説明。 |
options | NodeErrorOptions | エラーインスタンスのオプション。 |
CssSyntaxError_
を返します。
node#next()
ノードの親の次の子を返します。現在のノードが最後の子の場合は undefined
を返します。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
ChildNode
を返します。
node#parent
現在のノードの親を表します。
root.nodes[0].parent === root //=> true
型: Container_<ChildNode> | Document_。
node#positionBy()
ノード内の単語またはインデックスの位置を取得します。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word"> | オプション。 |
Position
を返します。
node#positionInside()
文字列のインデックスを行/列に変換します。
引数 | 型 | 説明 |
---|---|---|
index | number | ノードの文字列内の記号の番号。 |
Position
を返します。
node#prev()
ノードの親の前の子を返します。現在のノードが最初の子である場合は、undefined
を返します。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
ChildNode
を返します。
node#rangeBy()
ノード内の単語または開始インデックスと終了インデックスの範囲を取得します。開始インデックスは包括的で、終了インデックスは排他的です。
引数 | 型 | 説明 |
---|---|---|
opts | Pick<WarningOptions, "index" | "word" | "endIndex"> | オプション。 |
Range
を返します。
node#raw()
raws
値を返します。ノードにコードスタイルプロパティがない場合(ノードが手動で作成または複製されたため)、PostCSSはツリー内の他のノードを調べてコードスタイルプロパティを自動検出しようとします。
const root = postcss.parse('a { background: white }')
root.nodes[0].append({ prop: 'color', value: 'black' })
root.nodes[0].nodes[1].raws.before //=> undefined
root.nodes[0].nodes[1].raw('before') //=> ' '
引数 | 型 | 説明 |
---|---|---|
prop | string | コードスタイルプロパティの名前。 |
defaultType | string | デフォルト値の名前。値が prop と同じ場合は省略できます。 |
string
を返します。
node#raws
CSSソースコードに存在する不要な空白と文字を表します。
元の入力と同じように、バイト単位で等しいノード文字列を生成するための情報。
raws オブジェクトのプロパティはパーサーによって決定されます。デフォルトのパーサーは次のプロパティを使用します
before
: ノードの前の空白記号。宣言の前の*
および_
記号も保存します(IEハック)。after
: ノードの最後の子からノードの末尾までの空白記号。between
: 宣言の場合はプロパティと値の間、ルールの場合はセレクタと{
の間、または at-rule の場合は最後のパラメータと{
の間の記号。semicolon
: 最後の子が(オプションの)セミコロンを持っている場合は true が含まれます。afterName
: at-rule 名とそのパラメータの間の空白。left
:/*
とコメントのテキストの間の空白記号。right
: コメントのテキストと */ の間の空白記号。
important
: important ステートメントの内容。単に!important
ではない場合。
PostCSSはセレクタ、宣言値、および at-rule パラメータ内のコメントをフィルタリングしますが、raws に元のコンテンツを保存します。
const root = postcss.parse('a {\n color:black\n}')
root.first.first.raws //=> { before: '\n ', between: ':' }
型: any。
node#remove()
ノードをその親から削除し、親プロパティを削除します。
if (decl.prop.match(/^-webkit-/)) {
decl.remove()
}
Node
を返します。
node#replaceWith()
現在のノードの前にノードを挿入し、現在のノードを削除します。
AtRule: {
mixin: atrule => {
atrule.replaceWith(mixinRules[atrule.params])
}
}
引数 | 型 | 説明 |
---|---|---|
nodes… | (ChildNode | ChildProps | ChildNode[] | ChildProps[])[] | 現在のノードを置き換えるモード。 |
Node
を返します。
node#root()
ノードのツリーの Root インスタンスを検索します。
root.nodes[0].nodes[0].root() === root
Root
を返します。
node#source
ノードの起源に関する情報を表し、ソースマップの生成に必要です。
PostCSSが提供するパブリックAPIを使用して手動で作成されたノードは、source
が未定義になり、ソースマップに表示されません。
このため、プラグイン開発者は、複製されたノードはデフォルトで元のノードと同じソースを持つか、手動で作成されたノードにソースを割り当てるため、ノードを複製することを検討する必要があります。
decl.source.input.from //=> '/home/ai/source.css'
decl.source.start //=> { line: 10, column: 2 }
decl.source.end //=> { line: 10, column: 12 }
// Incorrect method, source not specified!
const prefixed = postcss.decl({
prop: '-moz-' + decl.prop,
value: decl.value
})
// Correct method, source is inherited when duplicating.
const prefixed = decl.clone({
prop: '-moz-' + decl.prop
})
if (atrule.name === 'add-link') {
const rule = postcss.rule({
selector: 'a',
source: atrule.source
})
atrule.parent.insertBefore(atrule, rule)
}
型: Source。
node#toJSON()
JSON.stringify()
の循環リンクを修正します。
object
を返します。
node#toString()
ノードの型に応じて、ノードをブラウザで読み取り可能なカスケーディングスタイルシート文字列にコンパイルします。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引数 | 型 | 説明 |
---|---|---|
stringifier | Stringifier | Syntax<Document_ | Root> | 文字列生成で使用する構文。 |
string
を返します。
node#type
抽象構文木におけるノードの型を表します。
ノードの型は、ノードの識別と型に基づいた操作の実行に役立ちます。
const declaration = new Declaration({
prop: 'color',
value: 'black'
})
declaration.type //=> 'decl'
型: string。
node#warn()
Result#warn のラッパーであり、警告を生成する便利な方法を提供します。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引数 | 型 | 説明 |
---|---|---|
result | Result<Document_ | Root> | 警告を受け取る Result インスタンス。 |
message | string | 警告の説明。 |
options | WarningOptions | 警告のオプション。 |
Warning
を返します。
previous-map
previous-map#annotation
sourceMappingURL
の内容。
型: string。
previous-map#consumer()
source-map
ライブラリからSourceMapGenerator
クラスのインスタンスを作成し、ソースマップ情報を処理します。
これは遅延メソッドであるため、最初に呼び出されたときにのみオブジェクトを作成し、その後はキャッシュを使用します。
SourceMapConsumer
を返します。
previous-map#file
CSSソースの識別子です。ユーザーがfrom
オプションを設定した場合はInput#file
が含まれ、設定しなかった場合はInput#id
が含まれます。
型: string。
previous-map#inline
ソースマップがdata-uriによって入力CSSにインライン化されたかどうかを示します。
型: boolean。
previous-map#mapFile
ソースマップファイルへのパスです。
型: string。
previous-map#root
ソースマップが別のファイルにある場合の、ソースマップファイルのディレクトリです。
型: string。
previous-map#text
ソースマップファイルの内容です。
型: string。
previous-map#withContent()
ソースマップに、入力ソーステキストを含むsourcesContent
が含まれているかどうかを示します。
boolean
を返します。
stringifier
stringifier#atrule()
引数 | 型 |
---|---|
node | AtRule_ |
semicolon | boolean |
stringifier#beforeAfter()
引数 | 型 |
---|---|
node | AnyNode |
detect | "after" | "before" |
string
を返します。
stringifier#block()
引数 | 型 |
---|---|
node | AnyNode |
start | string |
stringifier#body()
引数 | 型 |
---|---|
node | Container_<ChildNode> |
stringifier#builder
型: Builder。
stringifier#comment()
引数 | 型 |
---|---|
node | Comment_ |
stringifier#decl()
引数 | 型 |
---|---|
node | Declaration_ |
semicolon | boolean |
stringifier#document()
引数 | 型 |
---|---|
node | Document_ |
stringifier#raw()
引数 | 型 |
---|---|
node | AnyNode |
own | string |
detect | string |
string
を返します。
stringifier#rawBeforeClose()
引数 | 型 |
---|---|
root | Root |
string
を返します。
stringifier#rawBeforeComment()
引数 | 型 |
---|---|
root | Root |
node | Comment_ |
string
を返します。
stringifier#rawBeforeDecl()
引数 | 型 |
---|---|
root | Root |
node | Declaration_ |
string
を返します。
stringifier#rawBeforeOpen()
引数 | 型 |
---|---|
root | Root |
string
を返します。
stringifier#rawBeforeRule()
引数 | 型 |
---|---|
root | Root |
string
を返します。
stringifier#rawColon()
引数 | 型 |
---|---|
root | Root |
string
を返します。
stringifier#rawEmptyBody()
引数 | 型 |
---|---|
root | Root |
string
を返します。
stringifier#rawIndent()
引数 | 型 |
---|---|
root | Root |
string
を返します。
stringifier#rawSemicolon()
引数 | 型 |
---|---|
root | Root |
boolean
を返します。
stringifier#rawValue()
引数 | 型 |
---|---|
node | AnyNode |
prop | string |
string
を返します。
stringifier#root()
引数 | 型 |
---|---|
node | Root |
stringifier#rule()
引数 | 型 |
---|---|
node | Rule |
stringifier#stringify()
引数 | 型 |
---|---|
node | AnyNode |
semicolon | boolean |