postcss
建立新的 處理器
執行個體,將 外掛程式
套用為 CSS 處理器。
let postcss = require('postcss')
postcss(plugins).process(css, { from, to }).then(result => {
console.log(result.css)
})
引數 | 類型 | 說明 |
---|---|---|
外掛程式 | AcceptedPlugin[] | PostCSS 外掛程式。 |
引數 | 類型 | 說明 |
---|---|---|
外掛程式… | AcceptedPlugin[] | PostCSS 外掛程式。 |
傳回 處理器
。
postcss.AcceptedPlugin
類型:物件 | OldPlugin<any> | 外掛程式 | PluginCreator<any> | 處理器 | TransformCallback。
postcss.Builder
引數 | 類型 |
---|---|
部分 | 字串 |
節點 | AnyNode |
類型 | "end" | "start" |
postcss.Helpers
類型:物件 & Postcss。
postcss.JSONHydrator
引數 | 類型 |
---|---|
資料 | 物件 |
引數 | 類型 |
---|---|
資料 | 物件陣列 |
傳回 節點
。
postcss.OldPlugin
引數 | 類型 |
---|---|
選項 | T |
引數 | 類型 |
---|---|
根節點 | Root |
結果 | 結果<Document_ | 根> |
傳回 轉換器
。
postcss.Parser
引數 | 類型 |
---|---|
css | 字串 | 物件 |
選項 | Pick<ProcessOptions<Document_ | 根>, "map" | "from"> |
傳回 RootNode
。
postcss.PluginCreator
引數 | 類型 |
---|---|
選項 | PluginOptions |
postcss.Postcss
類型:postcss 的 typeof。
postcss.SourceMap
類型:SourceMapGenerator 和物件。
postcss.Stringifier
引數 | 類型 |
---|---|
節點 | AnyNode |
建構器 | Builder |
postcss.TransformCallback
引數 | 類型 |
---|---|
根節點 | Root |
結果 | 結果<Document_ | 根> |
傳回 void | Promise<void>
。
postcss.Transformer
引數 | 類型 |
---|---|
根節點 | Root |
結果 | 結果<Document_ | 根> |
傳回 void | Promise<void>
。
postcss.atRule
建立新的 AtRule
節點。
引數 | 類型 | 說明 |
---|---|---|
預設值 | AtRuleProps | 新節點的屬性。 |
傳回 AtRule_
。
postcss.comment
建立新的 Comment
節點。
引數 | 類型 | 說明 |
---|---|---|
預設值 | CommentProps | 新節點的屬性。 |
傳回 Comment_
。
postcss.decl
建立新的 Declaration
節點。
引數 | 類型 | 說明 |
---|---|---|
預設值 | DeclarationProps | 新節點的屬性。 |
傳回 Declaration_
。
postcss.document
建立新的 Document
節點。
引數 | 類型 | 說明 |
---|---|---|
預設值 | 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)
引數 | 類型 |
---|---|
資料 | 物件 |
引數 | 類型 |
---|---|
資料 | 物件陣列 |
傳回 節點
。
postcss.list
類型:List。
postcss.parse
剖析原始碼 css 並傳回新的 Root
或 Document
節點,其中包含原始碼 CSS 節點。
// 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 | 字串 | 物件 |
選項 | Pick<ProcessOptions<Document_ | 根>, "map" | "from"> |
傳回 Root
。
postcss.root
建立新的 Root
節點。
引數 | 類型 | 說明 |
---|---|---|
預設值 | RootProps | 新節點的屬性。 |
傳回 Root
。
postcss.rule
建立新的 Rule
節點。
引數 | 類型 |
---|---|
預設值 | RuleProps |
傳回 Rule
。
postcss.stringify
預設函式用於將節點樹轉換為 CSS 字串。
引數 | 類型 |
---|---|
節點 | AnyNode |
建構器 | 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 | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 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… | (字串 | 字串[] | 節點 | 子節點屬性 | 子節點屬性[] | 節點[])[] | 新節點。 |
傳回 AtRule
。
AtRule#assign()
將屬性指定給現有的節點實例。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引數 | 類型 | 說明 |
---|---|---|
overrides | 物件 | AtRuleProps | 用於覆寫節點的新屬性。 |
傳回 AtRule
。
AtRule#before()
將新節點插入至目前的節點之前,並加入至目前的節點父節點中。
僅為 node.parent.insertBefore(node, add)
的別名。
decl.before('content: ""')
引數 | 類型 | 說明 |
---|---|---|
newNode | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 AtRule
。
AtRule#cleanRaws()
清除節點及其子節點的程式碼樣式屬性。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引數 | 類型 | 說明 |
---|---|---|
keepBetween | 布林值 | 保留 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
。
在 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 | 函數 | 迭代器接收每個節點和索引。 |
傳回 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
}
引數 | 類型 | 說明 |
---|---|---|
訊息 | 字串 | 錯誤執行個體的描述。 |
options | NodeErrorOptions | 錯誤執行個體的選項。 |
傳回 CssSyntaxError_
。
AtRule#every()
如果 callback 對容器的所有子節點傳回 true
,則傳回 true
。
const noPrefixes = rule.every(i => i.prop[0] !== '-')
引數 | 類型 | 說明 |
---|---|---|
condition | 函數 | 迭代器傳回 true 或 false。 |
傳回 boolean
。
AtRule#index()
傳回 child
在 Container#nodes
陣列中的索引。
rule.index( rule.nodes[2] ) //=> 2
引數 | 類型 | 說明 |
---|---|---|
child | 數字 | ChildNode | 目前容器的子節點。 |
傳回 數字
。
AtRule#insertAfter()
在容器中將新節點插入舊節點之後。
引數 | 類型 | 說明 |
---|---|---|
oldNode | 數字 | ChildNode | 子節點或子節點索引。 |
newNode | 字串 | 字串陣列 | 子節點 | 子節點屬性 | 子節點 陣列 | 子節點屬性 陣列 | 新節點。 |
傳回 AtRule
。
AtRule#insertBefore()
在容器中插入新的節點,位於舊節點之前。
rule.insertBefore(decl, decl.clone({ prop: '-webkit-' + decl.prop }))
引數 | 類型 | 說明 |
---|---|---|
oldNode | 數字 | ChildNode | 子節點或子節點索引。 |
newNode | 字串 | 字串陣列 | 子節點 | 子節點屬性 | 子節點 陣列 | 子節點屬性 陣列 | 新節點。 |
傳回 AtRule
。
AtRule#name
at 規則的名稱緊接在 @
之後。
const root = postcss.parse('@media print {}')
media.name //=> 'media'
const media = root.first
類型:字串。
AtRule#next()
傳回節點父層的下一層子節點。如果目前節點為最後一個子節點,則傳回 undefined
。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
傳回 子節點。
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'
類型:子節點[].
AtRule#params
at 規則的參數,即 at 規則名稱之後但位於任何 {}
區塊之前的那些值。
const root = postcss.parse('@media print, screen {}')
const media = root.first
media.params //=> 'print, screen'
類型:字串。
AtRule#parent
代表目前節點的父層。
root.nodes[0].parent === root //=> true
類型:Container_<子節點>。
AtRule#positionBy()
取得節點內部字詞或索引的位置。
引數 | 類型 | 說明 |
---|---|---|
選項 | Pick<警告選項, "index" | "word"> | 選項。 |
傳回 位置。
AtRule#positionInside()
將字串索引轉換為行/欄。
引數 | 類型 | 說明 |
---|---|---|
index | 數字 | 節點字串中的符號編號。 |
傳回 位置。
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… | (字串 | 字串[] | 節點 | 子節點屬性 | 子節點屬性[] | 節點[])[] | 新節點。 |
傳回 AtRule
。
AtRule#prev()
傳回節點父層的前一個子節點。如果目前節點為第一個子節點,則傳回 undefined
。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
傳回 子節點。
AtRule#push()
將子節點新增至節點尾端。
rule.push(new Declaration({ prop: 'color', value: 'black' }))
引數 | 類型 | 說明 |
---|---|---|
child | 子節點 | 新節點。 |
傳回 AtRule
。
AtRule#rangeBy()
取得節點內單字或開始和結束索引的範圍。開始索引包含在內;結束索引則不包含在內。
引數 | 類型 | 說明 |
---|---|---|
選項 | 選取<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') //=> ' '
引數 | 類型 | 說明 |
---|---|---|
屬性 | 字串 | 程式碼樣式屬性的名稱。 |
defaultType | 字串 | 預設值的名稱,如果值與 prop 相同,則可以省略。 |
傳回 字串
。
AtRule#raws
它代表 css 原始程式碼中不必要的空白和字元。
產生與原始輸入中位元組對位元組相等的節點字串資訊。
raws 物件的屬性由剖析器決定,預設剖析器使用下列屬性
before
:節點前的空白符號。它也儲存宣告前的*
和_
符號(IE hack)。after
:節點最後一個子節點到節點結尾的空白符號。between
:宣告中屬性和值、規則中選擇器和{
或 at 規則中最後一個參數和{
之間的符號。semicolon
:如果最後一個子節點有(可選的)分號,則包含 true。afterName
:at 規則名稱和其參數之間的空白。left
:/*
和註解文字之間的空白符號。right
:註解文字和 */ 之間的空白符號。
important
:重要聲明的內容,如果它不只是!important
。
PostCSS 會過濾掉選擇器、宣告值和 at 規則參數中的註解,但會將原始內容儲存在 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 | 數字 | ChildNode | 子節點或子節點索引。 |
傳回 AtRule
。
AtRule#replaceValues()
引數 | 類型 |
---|---|
模式 | 字串 | RegExp |
替換 | 字串 | 函式 |
引數 | 類型 | 說明 |
---|---|---|
模式 | 字串 | RegExp | 替換模式。 |
options | ValueOptions | |
替換 | 字串 | 函式 |
傳回 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()
如果回呼函式對容器的其中一個(至少一個)子節點傳回 true
,則傳回 true
。
const hasPrefix = rule.some(i => i.prop[0] === '-')
引數 | 類型 | 說明 |
---|---|---|
condition | 函數 | 迭代器傳回 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)
}
類型:來源。
AtRule#toJSON()
修正 JSON.stringify()
上的循環連結。
傳回 物件
。
AtRule#toString()
它會根據類型將節點編譯成瀏覽器可讀的層疊樣式表字串。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引數 | 類型 | 說明 |
---|---|---|
字串化器 | 字串化器 | 語法<Document_ | 根> | 字串產生中要使用的語法。 |
傳回 字串
。
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 | 函數 | 迭代器接收每個節點和索引。 |
傳回 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 | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
AtRule#walkComments()
引數 | 類型 |
---|---|
callback | 函數 |
傳回 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 | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
AtRule#walkRules()
遍歷容器的後代節點,針對每個規則節點呼叫回呼。
如果您傳遞篩選器,則迭代只會發生在具有匹配選擇器的規則上。
與 Container#each
一樣,如果您在反覆運算期間變異陣列,則可以使用這個方法。
const selectors = []
root.walkRules(rule => {
selectors.push(rule.selector)
})
console.log(`Your CSS uses ${ selectors.length } selectors`)
引數 | 類型 | 說明 |
---|---|---|
selectorFilter | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
AtRule#warn()
它是 Result#warn 的包裝器,提供產生警告的便利方式。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引數 | 類型 | 說明 |
---|---|---|
結果 | 結果<Document_ | 根> | 將接收警告的Result 實例。 |
訊息 | 字串 | 警告的說明。 |
options | WarningOptions | 警告的選項。 |
傳回Warning
。
Comment
它表示處理 CSS 註解 的類別
Once (root, { Comment }) {
const note = new Comment({ text: 'Note: …' })
root.append(note)
}
請記住,選擇器、at 規則參數或宣告值中的 CSS 註解會儲存在上面說明的 raws
屬性中。
Comment#after()
將新節點插入至目前的節點之後,並加入至目前的節點父節點中。
僅為 node.parent.insertAfter(node, add)
的別名。
decl.after('color: black')
引數 | 類型 | 說明 |
---|---|---|
newNode | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回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 | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回Comment
。
Comment#cleanRaws()
清除節點及其子節點的程式碼樣式屬性。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引數 | 類型 | 說明 |
---|---|---|
keepBetween | 布林值 | 保留 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
}
引數 | 類型 | 說明 |
---|---|---|
訊息 | 字串 | 錯誤執行個體的描述。 |
options | NodeErrorOptions | 錯誤執行個體的選項。 |
傳回 CssSyntaxError_
。
Comment#next()
傳回節點父層的下一層子節點。如果目前節點為最後一個子節點,則傳回 undefined
。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
傳回 子節點。
Comment#parent
代表目前節點的父層。
root.nodes[0].parent === root //=> true
類型:Container_<子節點>。
Comment#positionBy()
取得節點內部字詞或索引的位置。
引數 | 類型 | 說明 |
---|---|---|
選項 | Pick<警告選項, "index" | "word"> | 選項。 |
傳回 位置。
Comment#positionInside()
將字串索引轉換為行/欄。
引數 | 類型 | 說明 |
---|---|---|
index | 數字 | 節點字串中的符號編號。 |
傳回 位置。
Comment#prev()
傳回節點父層的前一個子節點。如果目前節點為第一個子節點,則傳回 undefined
。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
傳回 子節點。
Comment#rangeBy()
取得節點內單字或開始和結束索引的範圍。開始索引包含在內;結束索引則不包含在內。
引數 | 類型 | 說明 |
---|---|---|
選項 | 選取<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') //=> ' '
引數 | 類型 | 說明 |
---|---|---|
屬性 | 字串 | 程式碼樣式屬性的名稱。 |
defaultType | 字串 | 預設值的名稱,如果值與 prop 相同,則可以省略。 |
傳回 字串
。
Comment#raws
它代表 css 原始程式碼中不必要的空白和字元。
產生與原始輸入中位元組對位元組相等的節點字串資訊。
raws 物件的屬性由剖析器決定,預設剖析器使用下列屬性
before
:節點前的空白符號。它也儲存宣告前的*
和_
符號(IE hack)。after
:節點最後一個子節點到節點結尾的空白符號。between
:宣告中屬性和值、規則中選擇器和{
或 at 規則中最後一個參數和{
之間的符號。semicolon
:如果最後一個子節點有(可選的)分號,則包含 true。afterName
:at 規則名稱和其參數之間的空白。left
:/*
和註解文字之間的空白符號。right
:註解文字和 */ 之間的空白符號。
important
:重要聲明的內容,如果它不只是!important
。
PostCSS 會過濾掉選擇器、宣告值和 at 規則參數中的註解,但會將原始內容儲存在 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)
}
類型:來源。
Comment#text
註解的文字。
類型:字串。
Comment#toJSON()
修正 JSON.stringify()
上的循環連結。
傳回 物件
。
Comment#toString()
它會根據類型將節點編譯成瀏覽器可讀的層疊樣式表字串。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引數 | 類型 | 說明 |
---|---|---|
字串化器 | 字串化器 | 語法<Document_ | 根> | 字串產生中要使用的語法。 |
傳回 字串
。
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')
}
}
引數 | 類型 | 說明 |
---|---|---|
結果 | 結果<Document_ | 根> | 將接收警告的Result 實例。 |
訊息 | 字串 | 警告的說明。 |
options | WarningOptions | 警告的選項。 |
傳回Warning
。
容器
Root
、AtRule
和 Rule
容器節點繼承了一些常見方法,以協助處理其子節點。
請注意,所有容器都可以儲存任何內容。如果您在規則內撰寫規則,PostCSS 將會解析它。
Container#after()
將新節點插入至目前的節點之後,並加入至目前的節點父節點中。
僅為 node.parent.insertAfter(node, add)
的別名。
decl.after('color: black')
引數 | 類型 | 說明 |
---|---|---|
newNode | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 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… | (字串 | 字串[] | 節點 | 子節點屬性 | 子節點屬性[] | 節點[])[] | 新節點。 |
傳回 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 | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 Container<Child>
。
Container#cleanRaws()
清除節點及其子節點的程式碼樣式屬性。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引數 | 類型 | 說明 |
---|---|---|
keepBetween | 布林值 | 保留 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
。
在 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 | 函數 | 迭代器接收每個節點和索引。 |
傳回 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
}
引數 | 類型 | 說明 |
---|---|---|
訊息 | 字串 | 錯誤執行個體的描述。 |
options | NodeErrorOptions | 錯誤執行個體的選項。 |
傳回 CssSyntaxError_
。
Container#every()
如果 callback 對容器的所有子節點傳回 true
,則傳回 true
。
const noPrefixes = rule.every(i => i.prop[0] !== '-')
引數 | 類型 | 說明 |
---|---|---|
condition | 函數 | 迭代器傳回 true 或 false。 |
傳回 boolean
。
Container#index()
傳回 child
在 Container#nodes
陣列中的索引。
rule.index( rule.nodes[2] ) //=> 2
引數 | 類型 | 說明 |
---|---|---|
child | number | Child | 目前容器的子節點。 |
傳回 數字
。
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()
}
}
傳回 子節點。
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()
取得節點內部字詞或索引的位置。
引數 | 類型 | 說明 |
---|---|---|
選項 | Pick<警告選項, "index" | "word"> | 選項。 |
傳回 位置。
Container#positionInside()
將字串索引轉換為行/欄。
引數 | 類型 | 說明 |
---|---|---|
index | 數字 | 節點字串中的符號編號。 |
傳回 位置。
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… | (字串 | 字串[] | 節點 | 子節點屬性 | 子節點屬性[] | 節點[])[] | 新節點。 |
傳回 Container<Child>
。
Container#prev()
傳回節點父層的前一個子節點。如果目前節點為第一個子節點,則傳回 undefined
。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
傳回 子節點。
Container#push()
將子節點新增至節點尾端。
rule.push(new Declaration({ prop: 'color', value: 'black' }))
引數 | 類型 | 說明 |
---|---|---|
child | Child | 新節點。 |
傳回 Container<Child>
。
Container#rangeBy()
取得節點內單字或開始和結束索引的範圍。開始索引包含在內;結束索引則不包含在內。
引數 | 類型 | 說明 |
---|---|---|
選項 | 選取<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') //=> ' '
引數 | 類型 | 說明 |
---|---|---|
屬性 | 字串 | 程式碼樣式屬性的名稱。 |
defaultType | 字串 | 預設值的名稱,如果值與 prop 相同,則可以省略。 |
傳回 字串
。
Container#raws
它代表 css 原始程式碼中不必要的空白和字元。
產生與原始輸入中位元組對位元組相等的節點字串資訊。
raws 物件的屬性由剖析器決定,預設剖析器使用下列屬性
before
:節點前的空白符號。它也儲存宣告前的*
和_
符號(IE hack)。after
:節點最後一個子節點到節點結尾的空白符號。between
:宣告中屬性和值、規則中選擇器和{
或 at 規則中最後一個參數和{
之間的符號。semicolon
:如果最後一個子節點有(可選的)分號,則包含 true。afterName
:at 規則名稱和其參數之間的空白。left
:/*
和註解文字之間的空白符號。right
:註解文字和 */ 之間的空白符號。
important
:重要聲明的內容,如果它不只是!important
。
PostCSS 會過濾掉選擇器、宣告值和 at 規則參數中的註解,但會將原始內容儲存在 raws 中。
const root = postcss.parse('a {\n color:black\n}')
root.first.first.raws //=> { before: '\n ', between: ':' }
類型:任何。
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()
引數 | 類型 |
---|---|
模式 | 字串 | RegExp |
替換 | 字串 | 函式 |
引數 | 類型 | 說明 |
---|---|---|
模式 | 字串 | RegExp | 替換模式。 |
options | ValueOptions | |
替換 | 字串 | 函式 |
傳回 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()
如果回呼函式對容器的其中一個(至少一個)子節點傳回 true
,則傳回 true
。
const hasPrefix = rule.some(i => i.prop[0] === '-')
引數 | 類型 | 說明 |
---|---|---|
condition | 函數 | 迭代器傳回 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)
}
類型:來源。
Container#toJSON()
修正 JSON.stringify()
上的循環連結。
傳回 物件
。
Container#toString()
它會根據類型將節點編譯成瀏覽器可讀的層疊樣式表字串。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引數 | 類型 | 說明 |
---|---|---|
字串化器 | 字串化器 | 語法<Document_ | 根> | 字串產生中要使用的語法。 |
傳回 字串
。
Container#type
它表示抽象語法樹中節點的類型。
節點類型有助於識別節點,並根據其類型執行操作。
const declaration = new Declaration({
prop: 'color',
value: 'black'
})
declaration.type //=> 'decl'
類型:字串。
Container#walk()
遍歷容器的後代節點,並針對每個節點呼叫回呼函式。
與 container.each() 一樣,如果您在反覆運算期間變異陣列,則可以使用這個方法。
如果您只需要反覆運算容器的直接子節點,請使用 Container#each
。
root.walk(node => {
// Traverses all descendant nodes.
})
引數 | 類型 | 說明 |
---|---|---|
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 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 | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
Container#walkComments()
引數 | 類型 |
---|---|
callback | 函數 |
傳回 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 | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
Container#walkRules()
遍歷容器的後代節點,針對每個規則節點呼叫回呼。
如果您傳遞篩選器,則迭代只會發生在具有匹配選擇器的規則上。
與 Container#each
一樣,如果您在反覆運算期間變異陣列,則可以使用這個方法。
const selectors = []
root.walkRules(rule => {
selectors.push(rule.selector)
})
console.log(`Your CSS uses ${ selectors.length } selectors`)
引數 | 類型 | 說明 |
---|---|---|
selectorFilter | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
Container#warn()
它是 Result#warn 的包裝器,提供產生警告的便利方式。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引數 | 類型 | 說明 |
---|---|---|
結果 | 結果<Document_ | 根> | 將接收警告的Result 實例。 |
訊息 | 字串 | 警告的說明。 |
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
。
類型:數字。
CssSyntaxError#endColumn
錯誤結束的來源欄位,不包含。如果錯誤與範圍有關,則提供。
error.endColumn //=> 1
error.input.endColumn //=> 4
PostCSS 會使用輸入來源地圖來偵測原始位置。如果您需要 PostCSS 輸入中的位置,請使用 error.input.endColumn
。
類型:數字。
CssSyntaxError#endLine
錯誤結束的來源行,不包含。如果錯誤與範圍有關,則提供。
error.endLine //=> 3
error.input.endLine //=> 4
PostCSS 會使用輸入來源地圖來偵測原始位置。如果您需要 PostCSS 輸入中的位置,請使用 error.input.endLine
。
類型:數字。
CssSyntaxError#file
損壞檔案的絕對路徑。
error.file //=> 'a.sass'
error.input.file //=> 'a.css'
PostCSS 會使用輸入來源地圖來偵測原始位置。如果您需要 PostCSS 輸入中的位置,請使用 error.input.file
。
類型:字串。
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
。
類型:數字。
CssSyntaxError#message
使用外掛程式、檔案、行和欄的 GNU 錯誤格式的完整錯誤文字。
error.message //=> 'a.css:1:1: Unclosed block'
類型:字串。
CssSyntaxError#name
總是等於 'CssSyntaxError'
。您應該總是透過 error.name === 'CssSyntaxError'
而不是 error instanceof CssSyntaxError
來檢查錯誤類型,因為 npm 可能有多個 PostCSS 版本。
if (error.name === 'CssSyntaxError') {
error //=> CssSyntaxError
}
類型:"CssSyntaxError"。
CssSyntaxError#plugin
如果錯誤來自於外掛,則為外掛名稱。
error.plugin //=> 'postcss-vars'
類型:字串。
CssSyntaxError#reason
錯誤訊息。
error.message //=> 'Unclosed block'
類型:字串。
CssSyntaxError#showSourceCode()
傳回導致錯誤的幾行 CSS 原始碼。
如果 CSS 有一個輸入來源地圖,但沒有 sourceContent
,此方法將傳回一個空字串。
error.showSourceCode() //=> " 4 | }
// 5 | a {
// > 6 | bad
// | ^
// 7 | }
// 8 | b {"
引數 | 類型 | 說明 |
---|---|---|
color | 布林值 | 箭頭是否會被終端機色彩碼標示為紅色。預設情況下,PostCSS 會透過 process.stdout.isTTY 和 process.env.NODE_DISABLE_COLORS 偵測色彩支援。 |
傳回 字串
。
CssSyntaxError#source
有問題檔案的原始碼。
error.source //=> 'a { b {} }'
error.input.source //=> 'a b { }'
類型:字串。
CssSyntaxError#stack
類型:字串。
CssSyntaxError#toString()
傳回錯誤位置、訊息和有問題部分的原始碼。
error.toString() //=> "CssSyntaxError: app.css:1:1: Unclosed block
// > 1 | a {
// | ^"
傳回 字串
。
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 | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 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 | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 Declaration
。
Declaration#cleanRaws()
清除節點及其子節點的程式碼樣式屬性。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引數 | 類型 | 說明 |
---|---|---|
keepBetween | 布林值 | 保留 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
}
引數 | 類型 | 說明 |
---|---|---|
訊息 | 字串 | 錯誤執行個體的描述。 |
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
類型:布林值。
Declaration#next()
傳回節點父層的下一層子節點。如果目前節點為最後一個子節點,則傳回 undefined
。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
傳回 子節點。
Declaration#parent
代表目前節點的父層。
root.nodes[0].parent === root //=> true
類型:Container_<子節點>。
宣告#positionBy()
取得節點內部字詞或索引的位置。
引數 | 類型 | 說明 |
---|---|---|
選項 | Pick<警告選項, "index" | "word"> | 選項。 |
傳回 位置。
宣告#positionInside()
將字串索引轉換為行/欄。
引數 | 類型 | 說明 |
---|---|---|
index | 數字 | 節點字串中的符號編號。 |
傳回 位置。
宣告#prev()
傳回節點父層的前一個子節點。如果目前節點為第一個子節點,則傳回 undefined
。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
傳回 子節點。
宣告#prop
CSS 宣告的屬性名稱。
const root = postcss.parse('a { color: black }')
const decl = root.first.first
decl.prop //=> 'color'
類型:字串。
宣告#rangeBy()
取得節點內單字或開始和結束索引的範圍。開始索引包含在內;結束索引則不包含在內。
引數 | 類型 | 說明 |
---|---|---|
選項 | 選取<WarningOptions, "index" | "word" | "endIndex"> | 選項。 |
傳回 Range
。
宣告#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') //=> ' '
引數 | 類型 | 說明 |
---|---|---|
屬性 | 字串 | 程式碼樣式屬性的名稱。 |
defaultType | 字串 | 預設值的名稱,如果值與 prop 相同,則可以省略。 |
傳回 字串
。
宣告#raws
它代表 css 原始程式碼中不必要的空白和字元。
產生與原始輸入中位元組對位元組相等的節點字串資訊。
raws 物件的屬性由剖析器決定,預設剖析器使用下列屬性
before
:節點前的空白符號。它也儲存宣告前的*
和_
符號(IE hack)。after
:節點最後一個子節點到節點結尾的空白符號。between
:宣告中屬性和值、規則中選擇器和{
或 at 規則中最後一個參數和{
之間的符號。semicolon
:如果最後一個子節點有(可選的)分號,則包含 true。afterName
:at 規則名稱和其參數之間的空白。left
:/*
和註解文字之間的空白符號。right
:註解文字和 */ 之間的空白符號。
important
:重要聲明的內容,如果它不只是!important
。
PostCSS 會過濾掉選擇器、宣告值和 at 規則參數中的註解,但會將原始內容儲存在 raws 中。
const root = postcss.parse('a {\n color:black\n}')
root.first.first.raws //=> { before: '\n ', between: ':' }
類型:DeclarationRaws。
宣告#remove()
它會從父節點移除節點並刪除其父屬性。
if (decl.prop.match(/^-webkit-/)) {
decl.remove()
}
傳回 Declaration
。
宣告#replaceWith()
在目前節點之前插入節點並移除目前節點。
AtRule: {
mixin: atrule => {
atrule.replaceWith(mixinRules[atrule.params])
}
}
引數 | 類型 | 說明 |
---|---|---|
nodes… | (ChildNode | ChildProps | ChildNode[] | ChildProps[])[] | 取代目前節點的模式。 |
傳回 Declaration
。
宣告#root()
尋找節點樹的 Root 實例。
root.nodes[0].nodes[0].root() === root
傳回 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)
}
類型:來源。
宣告#toJSON()
修正 JSON.stringify()
上的循環連結。
傳回 物件
。
宣告#toString()
它會根據類型將節點編譯成瀏覽器可讀的層疊樣式表字串。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引數 | 類型 | 說明 |
---|---|---|
字串化器 | 字串化器 | 語法<Document_ | 根> | 字串產生中要使用的語法。 |
傳回 字串
。
宣告#type
它表示抽象語法樹中節點的類型。
節點類型有助於識別節點,並根據其類型執行操作。
const declaration = new Declaration({
prop: 'color',
value: 'black'
})
declaration.type //=> 'decl'
類型:「decl」。
宣告#value
CSS 宣告的屬性值。
值字串中的任何 CSS 註解都會被濾除。原始值中存在的 CSS 註解會在 raws
屬性中提供。
指定新的 value
會在將節點編譯成字串時忽略 raws
屬性中的註解。
const root = postcss.parse('a { color: black }')
const decl = root.first.first
decl.value //=> 'black'
類型:字串。
宣告#variable
它表示一個 getter,如果宣告以 --
或 $
開頭,則會傳回 true
,這些符號用於在 CSS 和 SASS/SCSS 中宣告變數。
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
類型:布林值。
宣告#warn()
它是 Result#warn 的包裝器,提供產生警告的便利方式。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引數 | 類型 | 說明 |
---|---|---|
結果 | 結果<Document_ | 根> | 將接收警告的Result 實例。 |
訊息 | 字串 | 警告的說明。 |
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
文件#after()
將新節點插入至目前的節點之後,並加入至目前的節點父節點中。
僅為 node.parent.insertAfter(node, add)
的別名。
decl.after('color: black')
引數 | 類型 | 說明 |
---|---|---|
newNode | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 文件
。
文件#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… | (字串 | 字串[] | 節點 | 子節點屬性 | 子節點屬性[] | 節點[])[] | 新節點。 |
傳回 文件
。
文件#assign()
將屬性指定給現有的節點實例。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引數 | 類型 | 說明 |
---|---|---|
overrides | 物件 | DocumentProps | 用於覆寫節點的新屬性。 |
傳回 文件
。
文件#before()
將新節點插入至目前的節點之前,並加入至目前的節點父節點中。
僅為 node.parent.insertBefore(node, add)
的別名。
decl.before('content: ""')
引數 | 類型 | 說明 |
---|---|---|
newNode | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 文件
。
文件#cleanRaws()
清除節點及其子節點的程式碼樣式屬性。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引數 | 類型 | 說明 |
---|---|---|
keepBetween | 布林值 | 保留 raws.between 符號。 |
文件#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#cloneAfter()
複製節點的捷徑,並將複製後的節點插入至目前的節點之後。
引數 | 類型 | 說明 |
---|---|---|
overrides | Partial<DocumentProps> | 在複製中覆寫的新屬性。 |
傳回 文件
。
Document#cloneBefore()
複製節點的捷徑,並將複製後的節點插入至目前的節點之前。
decl.cloneBefore({ prop: '-moz-' + decl.prop })
引數 | 類型 | 說明 |
---|---|---|
overrides | Partial<DocumentProps> | 在複製中覆寫的新屬性。 |
傳回 文件
。
Document#each()
遍歷容器的直接子節點,並為每個子節點呼叫 callback
。
在 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 | 函數 | 迭代器接收每個節點和索引。 |
傳回 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
}
引數 | 類型 | 說明 |
---|---|---|
訊息 | 字串 | 錯誤執行個體的描述。 |
options | NodeErrorOptions | 錯誤執行個體的選項。 |
傳回 CssSyntaxError_
。
Document#every()
如果 callback 對容器的所有子節點傳回 true
,則傳回 true
。
const noPrefixes = rule.every(i => i.prop[0] !== '-')
引數 | 類型 | 說明 |
---|---|---|
condition | 函數 | 迭代器傳回 true 或 false。 |
傳回 boolean
。
Document#index()
傳回 child
在 Container#nodes
陣列中的索引。
rule.index( rule.nodes[2] ) //=> 2
引數 | 類型 | 說明 |
---|---|---|
child | 數字 | Root | 目前容器的子節點。 |
傳回 數字
。
Document#insertAfter()
在容器中將新節點插入舊節點之後。
引數 | 類型 | 說明 |
---|---|---|
oldNode | 數字 | Root | 子節點或子節點索引。 |
newNode | 字串 | 字串陣列 | ChildProps | ChildProps 陣列 | Root | Root 陣列 | 新節點。 |
傳回 文件
。
Document#insertBefore()
在容器中插入新的節點,位於舊節點之前。
rule.insertBefore(decl, decl.clone({ prop: '-webkit-' + decl.prop }))
引數 | 類型 | 說明 |
---|---|---|
oldNode | 數字 | Root | 子節點或子節點索引。 |
newNode | 字串 | 字串陣列 | ChildProps | ChildProps 陣列 | Root | Root 陣列 | 新節點。 |
傳回 文件
。
Document#next()
傳回節點父層的下一層子節點。如果目前節點為最後一個子節點,則傳回 undefined
。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
傳回 子節點。
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
類型:未定義。
Document#positionBy()
取得節點內部字詞或索引的位置。
引數 | 類型 | 說明 |
---|---|---|
選項 | Pick<警告選項, "index" | "word"> | 選項。 |
傳回 位置。
Document#positionInside()
將字串索引轉換為行/欄。
引數 | 類型 | 說明 |
---|---|---|
index | 數字 | 節點字串中的符號編號。 |
傳回 位置。
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… | (字串 | 字串[] | 節點 | 子節點屬性 | 子節點屬性[] | 節點[])[] | 新節點。 |
傳回 文件
。
Document#prev()
傳回節點父層的前一個子節點。如果目前節點為第一個子節點,則傳回 undefined
。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
傳回 子節點。
Document#push()
將子節點新增至節點尾端。
rule.push(new Declaration({ prop: 'color', value: 'black' }))
引數 | 類型 | 說明 |
---|---|---|
child | Root | 新節點。 |
傳回 文件
。
Document#rangeBy()
取得節點內單字或開始和結束索引的範圍。開始索引包含在內;結束索引則不包含在內。
引數 | 類型 | 說明 |
---|---|---|
選項 | 選取<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') //=> ' '
引數 | 類型 | 說明 |
---|---|---|
屬性 | 字串 | 程式碼樣式屬性的名稱。 |
defaultType | 字串 | 預設值的名稱,如果值與 prop 相同,則可以省略。 |
傳回 字串
。
Document#raws
它代表 css 原始程式碼中不必要的空白和字元。
產生與原始輸入中位元組對位元組相等的節點字串資訊。
raws 物件的屬性由剖析器決定,預設剖析器使用下列屬性
before
:節點前的空白符號。它也儲存宣告前的*
和_
符號(IE hack)。after
:節點最後一個子節點到節點結尾的空白符號。between
:宣告中屬性和值、規則中選擇器和{
或 at 規則中最後一個參數和{
之間的符號。semicolon
:如果最後一個子節點有(可選的)分號,則包含 true。afterName
:at 規則名稱和其參數之間的空白。left
:/*
和註解文字之間的空白符號。right
:註解文字和 */ 之間的空白符號。
important
:重要聲明的內容,如果它不只是!important
。
PostCSS 會過濾掉選擇器、宣告值和 at 規則參數中的註解,但會將原始內容儲存在 raws 中。
const root = postcss.parse('a {\n color:black\n}')
root.first.first.raws //=> { before: '\n ', between: ':' }
類型:任何。
Document#remove()
它會從父節點移除節點並刪除其父屬性。
if (decl.prop.match(/^-webkit-/)) {
decl.remove()
}
傳回 文件
。
Document#removeAll()
從容器中移除所有子節點並清除其父屬性。
rule.removeAll()
rule.nodes.length //=> 0
傳回 文件
。
Document#removeChild()
從容器中移除節點並清除節點及其子節點的父屬性。
rule.nodes.length //=> 5
rule.removeChild(decl)
rule.nodes.length //=> 4
decl.parent //=> undefined
引數 | 類型 | 說明 |
---|---|---|
child | 數字 | Root | 子節點或子節點索引。 |
傳回 文件
。
Document#replaceValues()
引數 | 類型 |
---|---|
模式 | 字串 | RegExp |
替換 | 字串 | 函式 |
引數 | 類型 | 說明 |
---|---|---|
模式 | 字串 | RegExp | 替換模式。 |
options | ValueOptions | |
替換 | 字串 | 函式 |
傳回 文件
。
Document#replaceWith()
在目前節點之前插入節點並移除目前節點。
AtRule: {
mixin: atrule => {
atrule.replaceWith(mixinRules[atrule.params])
}
}
引數 | 類型 | 說明 |
---|---|---|
nodes… | (ChildNode | ChildProps | ChildNode[] | ChildProps[])[] | 取代目前節點的模式。 |
傳回 文件
。
Document#root()
尋找節點樹的 Root 實例。
root.nodes[0].nodes[0].root() === root
傳回 Root
。
Document#some()
如果回呼函式對容器的其中一個(至少一個)子節點傳回 true
,則傳回 true
。
const hasPrefix = rule.some(i => i.prop[0] === '-')
引數 | 類型 | 說明 |
---|---|---|
condition | 函數 | 迭代器傳回 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)
}
類型:來源。
Document#toJSON()
修正 JSON.stringify()
上的循環連結。
傳回 物件
。
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> |
Document#toString()
它會根據類型將節點編譯成瀏覽器可讀的層疊樣式表字串。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引數 | 類型 | 說明 |
---|---|---|
字串化器 | 字串化器 | 語法<Document_ | 根> | 字串產生中要使用的語法。 |
傳回 字串
。
Document#type
它表示抽象語法樹中節點的類型。
節點類型有助於識別節點,並根據其類型執行操作。
const declaration = new Declaration({
prop: 'color',
value: 'black'
})
declaration.type //=> 'decl'
類型:"document".
文件#walk()
遍歷容器的後代節點,並針對每個節點呼叫回呼函式。
與 container.each() 一樣,如果您在反覆運算期間變異陣列,則可以使用這個方法。
如果您只需要反覆運算容器的直接子節點,請使用 Container#each
。
root.walk(node => {
// Traverses all descendant nodes.
})
引數 | 類型 | 說明 |
---|---|---|
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
文件#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 | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
文件#walkComments()
引數 | 類型 |
---|---|
callback | 函數 |
傳回 void | false
。
文件#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 | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
文件#walkRules()
遍歷容器的後代節點,針對每個規則節點呼叫回呼。
如果您傳遞篩選器,則迭代只會發生在具有匹配選擇器的規則上。
與 Container#each
一樣,如果您在反覆運算期間變異陣列,則可以使用這個方法。
const selectors = []
root.walkRules(rule => {
selectors.push(rule.selector)
})
console.log(`Your CSS uses ${ selectors.length } selectors`)
引數 | 類型 | 說明 |
---|---|---|
selectorFilter | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
文件#warn()
它是 Result#warn 的包裝器,提供產生警告的便利方式。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引數 | 類型 | 說明 |
---|---|---|
結果 | 結果<Document_ | 根> | 將接收警告的Result 實例。 |
訊息 | 字串 | 警告的說明。 |
options | WarningOptions | 警告的選項。 |
傳回Warning
。
Input
表示來源 CSS。
const root = postcss.parse(css, { from: file })
const input = root.source.input
輸入#css
輸入 CSS 來源。
const input = postcss.parse('a{}', { from: file }).input
input.css //=> "a{}"
類型:字串。
輸入#error()
引數 | 類型 |
---|---|
訊息 | 字串 |
開始 | 物件 | 物件 |
結束 | 物件 | 物件 |
選項 | 物件 |
引數 | 類型 |
---|---|
訊息 | 字串 |
列 | 數字 |
欄 | 數字 |
選項 | 物件 |
引數 | 類型 |
---|---|
訊息 | 字串 |
偏移 | 數字 |
選項 | 物件 |
傳回 CssSyntaxError_
。
輸入#檔案
使用 from
選項定義的 CSS 來源檔案的絕對路徑。
const root = postcss.parse(css, { from: 'a.css' })
root.source.input.file //=> '/home/ai/a.css'
類型:字串。
輸入#fromOffset()
將來源偏移轉換為行和欄。
引數 | 類型 | 說明 |
---|---|---|
偏移 | 數字 | 來源偏移。 |
傳回 物件
。
輸入#hasBOM
旗標用於指示來源程式碼是否有 Unicode BOM。
類型:布林值。
輸入#id
CSS 來源的唯一 ID。如果未提供 from
選項,將會建立此 ID(因為 PostCSS 不知道檔案路徑)。
const root = postcss.parse(css)
root.source.input.file //=> undefined
root.source.input.id //=> "<input css 8LZeVF>"
類型:字串。
輸入#map
從 PostCSS 之前的編譯步驟傳遞的輸入來源地圖(例如,來自 Sass 編譯器)。
root.source.input.map.consumer().sources //=> ['a.sass']
類型:PreviousMap_。
輸入#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 }
引數 | 類型 | 說明 |
---|---|---|
列 | 數字 | 輸入 CSS 中包含開始位置的行。 |
欄 | 數字 | 輸入 CSS 中包含開始位置的欄。 |
結束列 | 數字 | 輸入 CSS 中不包含結束位置的行。 |
結束欄 | 數字 | 輸入 CSS 中不包含結束位置的欄。 |
傳回 false | 檔案位置
。
LazyResult
PostCSS 轉換結果的 Promise 代理。
LazyResult
執行個體由 Processor#process
傳回。
const lazy = postcss([autoprefixer]).process(css)
LazyResult#async()
以非同步方式執行外掛程式,並傳回 Result
。
傳回 Promise<Result<RootNode>>
。
LazyResult#catch
類型:函式。
LazyResult#finally
類型:函式。
LazyResult#sync()
以同步方式執行外掛程式,並傳回 Result
。
傳回 Result<RootNode>
。
LazyResult#then
類型:函式。
LazyResult#toString()
LazyResult#css
屬性的別名。
lazy + '' === lazy.css
傳回 字串
。
LazyResult#warnings()
透過同步外掛程式處理輸入的 CSS,並呼叫 Result#warnings
。
傳回 Warning[]
。
NoWorkResult
PostCSS 轉換結果的 Promise 代理。此 LazyResult 執行個體不會剖析 CSS,除非存取 NoWorkResult#root
或 Result#root
。請參閱以下範例,以取得詳細資料。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
類型:函式。
NoWorkResult#finally
類型:函式。
NoWorkResult#sync()
以同步方式執行外掛程式,並傳回 Result
。
NoWorkResult#then
類型:函式。
NoWorkResult#toString()
LazyResult#css
屬性的別名。
lazy + '' === lazy.css
傳回 字串
。
NoWorkResult#warnings()
透過同步外掛程式處理輸入的 CSS,並呼叫 Result#warnings
。
傳回 Warning[]
。
節點
Node#after()
將新節點插入至目前的節點之後,並加入至目前的節點父節點中。
僅為 node.parent.insertAfter(node, add)
的別名。
decl.after('color: black')
引數 | 類型 | 說明 |
---|---|---|
newNode | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 節點
。
Node#assign()
將屬性指定給現有的節點實例。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引數 | 類型 | 說明 |
---|---|---|
overrides | 物件 | 用於覆寫節點的新屬性。 |
傳回 節點
。
節點#before()
將新節點插入至目前的節點之前,並加入至目前的節點父節點中。
僅為 node.parent.insertBefore(node, add)
的別名。
decl.before('content: ""')
引數 | 類型 | 說明 |
---|---|---|
newNode | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 節點
。
節點#cleanRaws()
清除節點及其子節點的程式碼樣式屬性。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引數 | 類型 | 說明 |
---|---|---|
keepBetween | 布林值 | 保留 raws.between 符號。 |
節點#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 | 物件 | 在複製中覆寫的新屬性。 |
傳回 節點
。
節點#cloneAfter()
複製節點的捷徑,並將複製後的節點插入至目前的節點之後。
引數 | 類型 | 說明 |
---|---|---|
overrides | 物件 | 在複製中覆寫的新屬性。 |
傳回 節點
。
節點#cloneBefore()
複製節點的捷徑,並將複製後的節點插入至目前的節點之前。
decl.cloneBefore({ prop: '-moz-' + decl.prop })
引數 | 類型 | 說明 |
---|---|---|
overrides | 物件 | 在複製中覆寫的新屬性。 |
傳回 節點
。
節點#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
}
引數 | 類型 | 說明 |
---|---|---|
訊息 | 字串 | 錯誤執行個體的描述。 |
options | NodeErrorOptions | 錯誤執行個體的選項。 |
傳回 CssSyntaxError_
。
節點#next()
傳回節點父層的下一層子節點。如果目前節點為最後一個子節點,則傳回 undefined
。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
傳回 子節點。
節點#parent
代表目前節點的父層。
root.nodes[0].parent === root //=> true
類型:Container_<ChildNode> | Document_。
節點#positionBy()
取得節點內部字詞或索引的位置。
引數 | 類型 | 說明 |
---|---|---|
選項 | Pick<警告選項, "index" | "word"> | 選項。 |
傳回 位置。
節點#positionInside()
將字串索引轉換為行/欄。
引數 | 類型 | 說明 |
---|---|---|
index | 數字 | 節點字串中的符號編號。 |
傳回 位置。
節點#prev()
傳回節點父層的前一個子節點。如果目前節點為第一個子節點,則傳回 undefined
。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
傳回 子節點。
節點#rangeBy()
取得節點內單字或開始和結束索引的範圍。開始索引包含在內;結束索引則不包含在內。
引數 | 類型 | 說明 |
---|---|---|
選項 | 選取<WarningOptions, "index" | "word" | "endIndex"> | 選項。 |
傳回 Range
。
節點#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') //=> ' '
引數 | 類型 | 說明 |
---|---|---|
屬性 | 字串 | 程式碼樣式屬性的名稱。 |
defaultType | 字串 | 預設值的名稱,如果值與 prop 相同,則可以省略。 |
傳回 字串
。
節點#raws
它代表 css 原始程式碼中不必要的空白和字元。
產生與原始輸入中位元組對位元組相等的節點字串資訊。
raws 物件的屬性由剖析器決定,預設剖析器使用下列屬性
before
:節點前的空白符號。它也儲存宣告前的*
和_
符號(IE hack)。after
:節點最後一個子節點到節點結尾的空白符號。between
:宣告中屬性和值、規則中選擇器和{
或 at 規則中最後一個參數和{
之間的符號。semicolon
:如果最後一個子節點有(可選的)分號,則包含 true。afterName
:at 規則名稱和其參數之間的空白。left
:/*
和註解文字之間的空白符號。right
:註解文字和 */ 之間的空白符號。
important
:重要聲明的內容,如果它不只是!important
。
PostCSS 會過濾掉選擇器、宣告值和 at 規則參數中的註解,但會將原始內容儲存在 raws 中。
const root = postcss.parse('a {\n color:black\n}')
root.first.first.raws //=> { before: '\n ', between: ':' }
類型:任何。
節點#remove()
它會從父節點移除節點並刪除其父屬性。
if (decl.prop.match(/^-webkit-/)) {
decl.remove()
}
傳回 節點
。
節點#replaceWith()
在目前節點之前插入節點並移除目前節點。
AtRule: {
mixin: atrule => {
atrule.replaceWith(mixinRules[atrule.params])
}
}
引數 | 類型 | 說明 |
---|---|---|
nodes… | (ChildNode | ChildProps | ChildNode[] | ChildProps[])[] | 取代目前節點的模式。 |
傳回 節點
。
節點#root()
尋找節點樹的 Root 實例。
root.nodes[0].nodes[0].root() === root
傳回 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)
}
類型:來源。
節點#toJSON()
修正 JSON.stringify()
上的循環連結。
傳回 物件
。
節點#toString()
它會根據類型將節點編譯成瀏覽器可讀的層疊樣式表字串。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引數 | 類型 | 說明 |
---|---|---|
字串化器 | 字串化器 | 語法<Document_ | 根> | 字串產生中要使用的語法。 |
傳回 字串
。
節點#type
它表示抽象語法樹中節點的類型。
節點類型有助於識別節點,並根據其類型執行操作。
const declaration = new Declaration({
prop: 'color',
value: 'black'
})
declaration.type //=> 'decl'
類型:字串。
節點#warn()
它是 Result#warn 的包裝器,提供產生警告的便利方式。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引數 | 類型 | 說明 |
---|---|---|
結果 | 結果<Document_ | 根> | 將接收警告的Result 實例。 |
訊息 | 字串 | 警告的說明。 |
options | WarningOptions | 警告的選項。 |
傳回Warning
。
PreviousMap
來自輸入 CSS 的原始碼資訊。例如,Sass 編譯器後的原始碼資訊。
此類別會自動在輸入 CSS 或輸入檔案附近的檔案系統中尋找原始碼資訊(根據 from
選項)。
const root = parse(css, { from: 'a.sass.css' })
root.input.map //=> PreviousMap
PreviousMap#annotation
sourceMappingURL
內容。
類型:字串。
PreviousMap#consumer()
從 source-map
函式庫建立 SourceMapGenerator
類別的執行個體,以處理原始碼資訊。
這是延遲方法,因此它只會在第一次呼叫時建立物件,然後會使用快取。
傳回 SourceMapConsumer
。
PreviousMap#file
CSS 原始碼識別碼。如果使用者設定 from
選項,則包含 輸入#檔案
;如果使用者未設定,則包含 輸入#id
。
類型:字串。
PreviousMap#inline
原始碼資訊是否透過 data-uri 內嵌到輸入 CSS。
類型:布林值。
PreviousMap#mapFile
來源地圖檔案路徑。
類型:字串。
PreviousMap#root
如果來源地圖在獨立檔案中,則為來源地圖檔案的目錄。
類型:字串。
PreviousMap#text
來源地圖檔案內容。
類型:字串。
PreviousMap#withContent()
來源地圖是否包含具有輸入來源文字的 sourcesContent
。
傳回 boolean
。
處理器
包含用於處理 CSS 的外掛程式。建立一個 Processor
實例,初始化其外掛程式,然後在多個 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 | 字串 | Root | Result<Document_ | Root> | 物件 | LazyResult_<Document_ | Root> | 包含輸入 CSS 的字串或任何具有 toString() 方法的物件,例如 Buffer。選擇性地傳送 Result 實例,處理器將從中取得 Root 。 |
引數 | 類型 | 說明 |
---|---|---|
css | 字串 | Root | Result<Document_ | Root> | LazyResult_<Document_ | Root> | 物件 | 包含輸入 CSS 的字串或任何具有 toString() 方法的物件,例如 Buffer。選擇性地傳送 Result 實例,處理器將從中取得 Root 。 |
options | ProcessOptions<RootNode> |
傳回 NoWorkResult_ | LazyResult_<Document_ | Root>
。
Processor#use()
新增外掛程式以用作 CSS 處理器。
PostCSS 外掛程式可以有 4 種格式
Plugin
格式的外掛程式。- 具有
pluginCreator.postcss = true
的外掛程式建立函式。PostCSS 會在沒有引數的情況下呼叫這個函式以取得外掛程式。 - 函式。PostCSS 會將函式傳遞 Root 作為第一個引數,並將目前的
Result
實例作為第二個引數。 - 另一個
Processor
實例。PostCSS 將從該實例複製外掛程式到這個實例。
建立 postcss
實例時,也可以透過傳遞外掛程式作為參數來新增外掛程式(請參閱 [postcss(plugins)
])。
非同步外掛程式應傳回 Promise
實例。
const processor = postcss()
.use(autoprefixer)
.use(postcssNested)
引數 | 類型 | 說明 |
---|---|---|
外掛程式 | AcceptedPlugin | PostCSS 外掛程式或包含外掛程式的 Processor 。 |
傳回 處理器
。
Processor#version
目前的 PostCSS 版本。
if (result.processor.version.split('.')[0] !== '6') {
throw new Error('This plugin works only with PostCSS 6')
}
類型:字串。
Result
提供 PostCSS 轉換的結果。
LazyResult#then
或 Root#toResult
方法會傳回 Result 實例。
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{}"
類型:字串。
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
包含來自外掛程式的訊息(例如警告或自訂訊息)。每個訊息都應有類型和外掛程式屬性。
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.toResult().root === root
類型:RootNode。
Result#toString()
傳回 Result#css
內容。
result + '' === result.css
傳回 字串
。
Result#warn()
建立 Warning
的執行個體,並將其新增至 Result#messages
。
if (decl.important) {
result.warn('Avoid !important', { node: decl, word: '!important' })
}
引數 | 類型 |
---|---|
訊息 | 字串 |
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 | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 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… | (字串 | 字串[] | 節點 | 子節點屬性 | 子節點屬性[] | 節點[])[] | 新節點。 |
傳回 Root
。
Root#assign()
將屬性指定給現有的節點實例。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引數 | 類型 | 說明 |
---|---|---|
overrides | 物件 | RootProps | 用於覆寫節點的新屬性。 |
傳回 Root
。
Root#before()
將新節點插入至目前的節點之前,並加入至目前的節點父節點中。
僅為 node.parent.insertBefore(node, add)
的別名。
decl.before('content: ""')
引數 | 類型 | 說明 |
---|---|---|
newNode | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 Root
。
Root#cleanRaws()
清除節點及其子節點的程式碼樣式屬性。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引數 | 類型 | 說明 |
---|---|---|
keepBetween | 布林值 | 保留 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
。
在 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 | 函數 | 迭代器接收每個節點和索引。 |
傳回 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
}
引數 | 類型 | 說明 |
---|---|---|
訊息 | 字串 | 錯誤執行個體的描述。 |
options | NodeErrorOptions | 錯誤執行個體的選項。 |
傳回 CssSyntaxError_
。
Root#every()
如果 callback 對容器的所有子節點傳回 true
,則傳回 true
。
const noPrefixes = rule.every(i => i.prop[0] !== '-')
引數 | 類型 | 說明 |
---|---|---|
condition | 函數 | 迭代器傳回 true 或 false。 |
傳回 boolean
。
Root#index()
傳回 child
在 Container#nodes
陣列中的索引。
rule.index( rule.nodes[2] ) //=> 2
引數 | 類型 | 說明 |
---|---|---|
child | 數字 | ChildNode | 目前容器的子節點。 |
傳回 數字
。
Root#insertAfter()
在容器中將新節點插入舊節點之後。
引數 | 類型 | 說明 |
---|---|---|
oldNode | 數字 | ChildNode | 子節點或子節點索引。 |
newNode | 字串 | 字串陣列 | 子節點 | 子節點屬性 | 子節點 陣列 | 子節點屬性 陣列 | 新節點。 |
傳回 Root
。
Root#insertBefore()
在容器中插入新的節點,位於舊節點之前。
rule.insertBefore(decl, decl.clone({ prop: '-webkit-' + decl.prop }))
引數 | 類型 | 說明 |
---|---|---|
oldNode | 數字 | ChildNode | 子節點或子節點索引。 |
newNode | 字串 | 字串陣列 | 子節點 | 子節點屬性 | 子節點 陣列 | 子節點屬性 陣列 | 新節點。 |
傳回 Root
。
Root#next()
傳回節點父層的下一層子節點。如果目前節點為最後一個子節點,則傳回 undefined
。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
傳回 子節點。
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'
類型:子節點[].
Root#parent
代表目前節點的父層。
root.nodes[0].parent === root //=> true
類型:Document_。
Root#positionBy()
取得節點內部字詞或索引的位置。
引數 | 類型 | 說明 |
---|---|---|
選項 | Pick<警告選項, "index" | "word"> | 選項。 |
傳回 位置。
Root#positionInside()
將字串索引轉換為行/欄。
引數 | 類型 | 說明 |
---|---|---|
index | 數字 | 節點字串中的符號編號。 |
傳回 位置。
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… | (字串 | 字串[] | 節點 | 子節點屬性 | 子節點屬性[] | 節點[])[] | 新節點。 |
傳回 Root
。
Root#prev()
傳回節點父層的前一個子節點。如果目前節點為第一個子節點,則傳回 undefined
。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
傳回 子節點。
Root#push()
將子節點新增至節點尾端。
rule.push(new Declaration({ prop: 'color', value: 'black' }))
引數 | 類型 | 說明 |
---|---|---|
child | 子節點 | 新節點。 |
傳回 Root
。
Root#rangeBy()
取得節點內單字或開始和結束索引的範圍。開始索引包含在內;結束索引則不包含在內。
引數 | 類型 | 說明 |
---|---|---|
選項 | 選取<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') //=> ' '
引數 | 類型 | 說明 |
---|---|---|
屬性 | 字串 | 程式碼樣式屬性的名稱。 |
defaultType | 字串 | 預設值的名稱,如果值與 prop 相同,則可以省略。 |
傳回 字串
。
Root#raws
它代表 css 原始程式碼中不必要的空白和字元。
產生與原始輸入中位元組對位元組相等的節點字串資訊。
raws 物件的屬性由剖析器決定,預設剖析器使用下列屬性
before
:節點前的空白符號。它也儲存宣告前的*
和_
符號(IE hack)。after
:節點最後一個子節點到節點結尾的空白符號。between
:宣告中屬性和值、規則中選擇器和{
或 at 規則中最後一個參數和{
之間的符號。semicolon
:如果最後一個子節點有(可選的)分號,則包含 true。afterName
:at 規則名稱和其參數之間的空白。left
:/*
和註解文字之間的空白符號。right
:註解文字和 */ 之間的空白符號。
important
:重要聲明的內容,如果它不只是!important
。
PostCSS 會過濾掉選擇器、宣告值和 at 規則參數中的註解,但會將原始內容儲存在 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 | 數字 | ChildNode | 子節點或子節點索引。 |
傳回 Root
。
Root#replaceValues()
引數 | 類型 |
---|---|
模式 | 字串 | RegExp |
替換 | 字串 | 函式 |
引數 | 類型 | 說明 |
---|---|---|
模式 | 字串 | RegExp | 替換模式。 |
options | ValueOptions | |
替換 | 字串 | 函式 |
傳回 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()
如果回呼函式對容器的其中一個(至少一個)子節點傳回 true
,則傳回 true
。
const hasPrefix = rule.some(i => i.prop[0] === '-')
引數 | 類型 | 說明 |
---|---|---|
condition | 函數 | 迭代器傳回 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)
}
類型:來源。
Root#toJSON()
修正 JSON.stringify()
上的循環連結。
傳回 物件
。
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> |
Root#toString()
它會根據類型將節點編譯成瀏覽器可讀的層疊樣式表字串。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引數 | 類型 | 說明 |
---|---|---|
字串化器 | 字串化器 | 語法<Document_ | 根> | 字串產生中要使用的語法。 |
傳回 字串
。
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 | 函數 | 迭代器接收每個節點和索引。 |
傳回 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 | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
Root#walkComments()
引數 | 類型 |
---|---|
callback | 函數 |
傳回 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 | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
Root#walkRules()
遍歷容器的後代節點,針對每個規則節點呼叫回呼。
如果您傳遞篩選器,則迭代只會發生在具有匹配選擇器的規則上。
與 Container#each
一樣,如果您在反覆運算期間變異陣列,則可以使用這個方法。
const selectors = []
root.walkRules(rule => {
selectors.push(rule.selector)
})
console.log(`Your CSS uses ${ selectors.length } selectors`)
引數 | 類型 | 說明 |
---|---|---|
selectorFilter | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
Root#warn()
它是 Result#warn 的包裝器,提供產生警告的便利方式。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引數 | 類型 | 說明 |
---|---|---|
結果 | 結果<Document_ | 根> | 將接收警告的Result 實例。 |
訊息 | 字串 | 警告的說明。 |
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 | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 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… | (字串 | 字串[] | 節點 | 子節點屬性 | 子節點屬性[] | 節點[])[] | 新節點。 |
傳回 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 | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 Rule
。
Rule#cleanRaws()
清除節點及其子節點的程式碼樣式屬性。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引數 | 類型 | 說明 |
---|---|---|
keepBetween | 布林值 | 保留 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
。
在 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 | 函數 | 迭代器接收每個節點和索引。 |
傳回 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
}
引數 | 類型 | 說明 |
---|---|---|
訊息 | 字串 | 錯誤執行個體的描述。 |
options | NodeErrorOptions | 錯誤執行個體的選項。 |
傳回 CssSyntaxError_
。
Rule#every()
如果 callback 對容器的所有子節點傳回 true
,則傳回 true
。
const noPrefixes = rule.every(i => i.prop[0] !== '-')
引數 | 類型 | 說明 |
---|---|---|
condition | 函數 | 迭代器傳回 true 或 false。 |
傳回 boolean
。
Rule#index()
傳回 child
在 Container#nodes
陣列中的索引。
rule.index( rule.nodes[2] ) //=> 2
引數 | 類型 | 說明 |
---|---|---|
child | 數字 | ChildNode | 目前容器的子節點。 |
傳回 數字
。
Rule#insertAfter()
在容器中將新節點插入舊節點之後。
引數 | 類型 | 說明 |
---|---|---|
oldNode | 數字 | ChildNode | 子節點或子節點索引。 |
newNode | 字串 | 字串陣列 | 子節點 | 子節點屬性 | 子節點 陣列 | 子節點屬性 陣列 | 新節點。 |
傳回 Rule
。
Rule#insertBefore()
在容器中插入新的節點,位於舊節點之前。
rule.insertBefore(decl, decl.clone({ prop: '-webkit-' + decl.prop }))
引數 | 類型 | 說明 |
---|---|---|
oldNode | 數字 | ChildNode | 子節點或子節點索引。 |
newNode | 字串 | 字串陣列 | 子節點 | 子節點屬性 | 子節點 陣列 | 子節點屬性 陣列 | 新節點。 |
傳回 Rule
。
Rule#next()
傳回節點父層的下一層子節點。如果目前節點為最後一個子節點,則傳回 undefined
。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
傳回 子節點。
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'
類型:子節點[].
Rule#parent
代表目前節點的父層。
root.nodes[0].parent === root //=> true
類型:Container_<子節點>。
Rule#positionBy()
取得節點內部字詞或索引的位置。
引數 | 類型 | 說明 |
---|---|---|
選項 | Pick<警告選項, "index" | "word"> | 選項。 |
傳回 位置。
Rule#positionInside()
將字串索引轉換為行/欄。
引數 | 類型 | 說明 |
---|---|---|
index | 數字 | 節點字串中的符號編號。 |
傳回 位置。
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… | (字串 | 字串[] | 節點 | 子節點屬性 | 子節點屬性[] | 節點[])[] | 新節點。 |
傳回 Rule
。
Rule#prev()
傳回節點父層的前一個子節點。如果目前節點為第一個子節點,則傳回 undefined
。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
傳回 子節點。
Rule#push()
將子節點新增至節點尾端。
rule.push(new Declaration({ prop: 'color', value: 'black' }))
引數 | 類型 | 說明 |
---|---|---|
child | 子節點 | 新節點。 |
傳回 Rule
。
Rule#rangeBy()
取得節點內單字或開始和結束索引的範圍。開始索引包含在內;結束索引則不包含在內。
引數 | 類型 | 說明 |
---|---|---|
選項 | 選取<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') //=> ' '
引數 | 類型 | 說明 |
---|---|---|
屬性 | 字串 | 程式碼樣式屬性的名稱。 |
defaultType | 字串 | 預設值的名稱,如果值與 prop 相同,則可以省略。 |
傳回 字串
。
Rule#raws
它代表 css 原始程式碼中不必要的空白和字元。
產生與原始輸入中位元組對位元組相等的節點字串資訊。
raws 物件的屬性由剖析器決定,預設剖析器使用下列屬性
before
:節點前的空白符號。它也儲存宣告前的*
和_
符號(IE hack)。after
:節點最後一個子節點到節點結尾的空白符號。between
:宣告中屬性和值、規則中選擇器和{
或 at 規則中最後一個參數和{
之間的符號。semicolon
:如果最後一個子節點有(可選的)分號,則包含 true。afterName
:at 規則名稱和其參數之間的空白。left
:/*
和註解文字之間的空白符號。right
:註解文字和 */ 之間的空白符號。
important
:重要聲明的內容,如果它不只是!important
。
PostCSS 會過濾掉選擇器、宣告值和 at 規則參數中的註解,但會將原始內容儲存在 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 | 數字 | ChildNode | 子節點或子節點索引。 |
傳回 Rule
。
Rule#replaceValues()
引數 | 類型 |
---|---|
模式 | 字串 | RegExp |
替換 | 字串 | 函式 |
引數 | 類型 | 說明 |
---|---|---|
模式 | 字串 | RegExp | 替換模式。 |
options | ValueOptions | |
替換 | 字串 | 函式 |
傳回 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'
類型:字串。
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()
如果回呼函式對容器的其中一個(至少一個)子節點傳回 true
,則傳回 true
。
const hasPrefix = rule.some(i => i.prop[0] === '-')
引數 | 類型 | 說明 |
---|---|---|
condition | 函數 | 迭代器傳回 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)
}
類型:來源。
Rule#toJSON()
修正 JSON.stringify()
上的循環連結。
傳回 物件
。
Rule#toString()
它會根據類型將節點編譯成瀏覽器可讀的層疊樣式表字串。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引數 | 類型 | 說明 |
---|---|---|
字串化器 | 字串化器 | 語法<Document_ | 根> | 字串產生中要使用的語法。 |
傳回 字串
。
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 | 函數 | 迭代器接收每個節點和索引。 |
傳回 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 | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
Rule#walkComments()
引數 | 類型 |
---|---|
callback | 函數 |
傳回 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 | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
Rule#walkRules()
遍歷容器的後代節點,針對每個規則節點呼叫回呼。
如果您傳遞篩選器,則迭代只會發生在具有匹配選擇器的規則上。
與 Container#each
一樣,如果您在反覆運算期間變異陣列,則可以使用這個方法。
const selectors = []
root.walkRules(rule => {
selectors.push(rule.selector)
})
console.log(`Your CSS uses ${ selectors.length } selectors`)
引數 | 類型 | 說明 |
---|---|---|
selectorFilter | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
Rule#warn()
它是 Result#warn 的包裝器,提供產生警告的便利方式。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引數 | 類型 | 說明 |
---|---|---|
結果 | 結果<Document_ | 根> | 將接收警告的Result 實例。 |
訊息 | 字串 | 警告的說明。 |
options | WarningOptions | 警告的選項。 |
傳回Warning
。
字串化器
Stringifier#atrule()
引數 | 類型 |
---|---|
節點 | AtRule_ |
semicolon | 布林值 |
Stringifier#beforeAfter()
引數 | 類型 |
---|---|
節點 | AnyNode |
detect | "after" | "before" |
傳回 字串
。
Stringifier#block()
引數 | 類型 |
---|---|
節點 | AnyNode |
開始 | 字串 |
Stringifier#body()
引數 | 類型 |
---|---|
節點 | Container_<ChildNode> |
Stringifier#builder
類型:Builder。
Stringifier#comment()
引數 | 類型 |
---|---|
節點 | Comment_ |
Stringifier#decl()
引數 | 類型 |
---|---|
節點 | Declaration_ |
semicolon | 布林值 |
Stringifier#document()
引數 | 類型 |
---|---|
節點 | Document_ |
Stringifier#raw()
引數 | 類型 |
---|---|
節點 | AnyNode |
own | 字串 |
detect | 字串 |
傳回 字串
。
Stringifier#rawBeforeClose()
引數 | 類型 |
---|---|
根節點 | Root |
傳回 字串
。
Stringifier#rawBeforeComment()
引數 | 類型 |
---|---|
根節點 | Root |
節點 | Comment_ |
傳回 字串
。
Stringifier#rawBeforeDecl()
引數 | 類型 |
---|---|
根節點 | Root |
節點 | Declaration_ |
傳回 字串
。
Stringifier#rawBeforeOpen()
引數 | 類型 |
---|---|
根節點 | Root |
傳回 字串
。
Stringifier#rawBeforeRule()
引數 | 類型 |
---|---|
根節點 | Root |
傳回 字串
。
Stringifier#rawColon()
引數 | 類型 |
---|---|
根節點 | Root |
傳回 字串
。
Stringifier#rawEmptyBody()
引數 | 類型 |
---|---|
根節點 | Root |
傳回 字串
。
Stringifier#rawIndent()
引數 | 類型 |
---|---|
根節點 | Root |
傳回 字串
。
Stringifier#rawSemicolon()
引數 | 類型 |
---|---|
根節點 | Root |
傳回 boolean
。
Stringifier#rawValue()
引數 | 類型 |
---|---|
節點 | AnyNode |
屬性 | 字串 |
傳回 字串
。
Stringifier#root()
引數 | 類型 |
---|---|
節點 | Root |
Stringifier#rule()
引數 | 類型 |
---|---|
節點 | Rule |
Stringifier#stringify()
引數 | 類型 |
---|---|
節點 | AnyNode |
semicolon | 布林值 |
警告
表示外掛的警告。它可以使用 Node#warn
建立。
if (decl.important) {
decl.warn(result, 'Avoid !important', { word: '!important' })
}
Warning#column
此警告來源的輸入檔案中包含開始位置的欄位。
warning.column //=> 6
類型:數字。
Warning#endColumn
此警告來源的輸入檔案中包含結束位置的欄位。
warning.endColumn //=> 4
類型:數字。
Warning#endLine
此警告來源的輸入檔案中包含結束位置的行數。
warning.endLine //=> 6
類型:數字。
Warning#line
此警告來源的輸入檔案中包含開始位置的行數。
warning.line //=> 5
類型:數字。
Warning#node
包含導致警告的 CSS 節點。
warning.node.toString() //=> 'color: white !important'
類型:Node。
Warning#plugin
建立此警告的外掛名稱。當您呼叫 Node#warn
時,它會自動填入此屬性。
warning.plugin //=> 'postcss-important'
類型:字串。
Warning#text
警告訊息。
warning.text //=> 'Try to avoid !important'
類型:字串。
Warning#toString()
傳回警告位置和訊息。
warning.toString() //=> 'postcss-lint:a.css:10:14: Avoid !important'
傳回 字串
。
Warning#type
從 Result#messages
篩選警告的類型。永遠等於 "warning"
。
類型:"warning"。
AtRuleProps
AtRuleProps#name
at 規則的名稱。
類型:字串。
AtRuleProps#nodes
類型:(ChildNode | ChildProps)[].
AtRuleProps#params
at-rule 名稱後面的參數。
類型:字串 | 數字。
AtRuleProps#raws
用於產生位元組對位元組相等的節點字串資訊,如同原始輸入。
類型:AtRuleRaws。
AtRuleProps#source
類型:來源。
AtRuleRaws
AtRuleRaws#after
節點最後一個子節點之後到節點結尾的空白符號。
類型:字串。
AtRuleRaws#afterName
at-rule 名稱和其參數之間的空白。
類型:字串。
AtRuleRaws#before
節點之前的空白符號。它也儲存宣告之前的 *
和 _
符號(IE hack)。
類型:字串。
AtRuleRaws#between
規則中最後一個參數和 {
之間的符號。
類型:字串。
AtRuleRaws#params
包含註解的規則選擇器。
類型:物件。
AtRuleRaws#semicolon
如果最後一個子節點有(可選的)分號,則包含 true
。
類型:布林值。
CommentProps
CommentProps#raws
用於產生位元組對位元組相等的節點字串資訊,如同原始輸入。
類型:CommentRaws。
CommentProps#source
類型:來源。
CommentProps#text
註解內容。
類型:字串。
CommentRaws
CommentRaws#before
節點之前的空白符號。
類型:字串。
CommentRaws#left
/*
和註解文字之間的空白符號。
類型:字串。
CommentRaws#right
註解文字之間的空白符號。
類型:字串。
ContainerProps
ContainerProps#nodes
類型:(ChildNode | ChildProps)[].
ContainerProps#source
類型:來源。
DeclarationProps
DeclarationProps#important
宣告是否有 !important
註解。
類型:布林值。
DeclarationProps#prop
宣告的名稱。
類型:字串。
DeclarationProps#raws
用於產生位元組對位元組相等的節點字串資訊,如同原始輸入。
類型:DeclarationRaws。
DeclarationProps#value
宣告的值。
類型:字串。
DeclarationRaws
DeclarationRaws#before
節點之前的空白符號。它也儲存宣告之前的 *
和 _
符號(IE hack)。
類型:字串。
DeclarationRaws#between
宣告中屬性和值之間的符號。
類型:字串。
DeclarationRaws#important
重要聲明的內容,如果它不只是 !important
。
類型:字串。
DeclarationRaws#value
包含註解的宣告值。
類型:物件。
DocumentProps
DocumentProps#nodes
類型:Root[].
DocumentProps#raws
產生與原始輸入中位元組對位元組相等的節點字串資訊。
每個剖析器都會儲存自己的屬性。
類型:Record<string, any>。
DocumentProps#source
類型:來源。
FilePosition
FilePosition#column
來源檔案中包含起始位置的欄位。
類型:數字。
FilePosition#endColumn
來源檔案中獨佔結束位置的欄位。
類型:數字。
FilePosition#endLine
來源檔案中獨佔結束位置的行數。
類型:數字。
FilePosition#file
來源檔案的絕對路徑。
類型:字串。
FilePosition#line
來源檔案中包含起始位置的行數。
類型:數字。
FilePosition#source
原始碼。
類型:字串。
FilePosition#url
來源檔案的 URL。
類型:字串。
Message
Message#plugin
來源 PostCSS 外掛程式名稱。
類型:字串。
Message#type
訊息類型。
類型:字串。
Message
Message#plugin
來源 PostCSS 外掛程式名稱。
類型:字串。
Message#type
訊息類型。
類型:字串。
NodeErrorOptions
NodeErrorOptions#endIndex
節點字串中應標示為錯誤來源的結束索引。
類型:數字。
NodeErrorOptions#index
節點字串中應標示為錯誤來源的索引。
類型:數字。
NodeErrorOptions#plugin
建立此錯誤的插件名稱。PostCSS 會自動設定。
類型:字串。
NodeErrorOptions#字詞
節點字串中的字詞,應標示為錯誤來源。
類型:字串。
NodeProps
NodeProps#來源
類型:來源。
OldPlugin
引數 | 類型 |
---|---|
選項 | T |
引數 | 類型 |
---|---|
根節點 | Root |
結果 | 結果<Document_ | 根> |
傳回 轉換器
。
OldPlugin#postcss
類型:Transformer。
OldPlugin#postcssPlugin
類型:字串。
OldPlugin#postcssVersion
類型:字串。
插件
Plugin#AtRule
將會呼叫所有AtRule
節點。
將會再次呼叫節點或子節點變更。
類型:AtRuleProcessor | 物件。
Plugin#AtRuleExit
將會呼叫所有AtRule
節點,當所有子節點都已處理完畢時。
將會再次呼叫節點或子節點變更。
類型:AtRuleProcessor | 物件。
Plugin#Comment
將會呼叫所有Comment
節點。
將會再次呼叫節點或子節點變更。
類型:CommentProcessor。
Plugin#CommentExit
將會呼叫所有Comment
節點,在Comment
事件的監聽程式之後。
將會再次呼叫節點或子節點變更。
類型:CommentProcessor。
Plugin#Declaration
將會呼叫所有Declaration
節點,在Declaration
事件的監聽程式之後。
將會再次呼叫節點或子節點變更。
類型:DeclarationProcessor | 物件。
Plugin#DeclarationExit
將會呼叫所有Declaration
節點。
將會再次呼叫節點或子節點變更。
類型:DeclarationProcessor | 物件。
Plugin#Document
將會呼叫Document
節點。
將會再次呼叫子節點變更。
類型:DocumentProcessor。
Plugin#DocumentExit
將會呼叫Document
節點,當所有子節點都已處理完畢時。
將會再次呼叫子節點變更。
類型:DocumentProcessor。
Plugin#Once
將會呼叫Root
節點一次。
類型:RootProcessor。
Plugin#OnceExit
當所有子節點都已處理後,將在 Root
節點上呼叫一次。
類型:RootProcessor。
Plugin#Root
將在 Root
節點上呼叫。
將會再次呼叫子節點變更。
類型:RootProcessor。
Plugin#RootExit
當所有子節點都已處理後,將在 Root
節點上呼叫。
將會再次呼叫子節點變更。
類型:RootProcessor。
Plugin#Rule
將在所有 Rule
節點上呼叫。
將會再次呼叫節點或子節點變更。
類型:RuleProcessor。
Plugin#RuleExit
當所有子節點都已處理後,將在所有 Rule
節點上呼叫。
將會再次呼叫節點或子節點變更。
類型:RuleProcessor。
Plugin#postcssPlugin
類型:字串。
Plugin#prepare
類型:函式。
PluginCreator
引數 | 類型 |
---|---|
選項 | PluginOptions |
PluginCreator#postcss
類型:true。
位置
Position#column
檔案中的原始程式碼行。與 offset
相反,它從 1 開始。
類型:數字。
Position#line
檔案中的原始程式碼欄位。
類型:數字。
Position#offset
檔案中的原始程式碼偏移量。它從 0 開始。
類型:數字。
ProcessOptions
ProcessOptions#from
CSS 原始程式碼檔案的路徑。您應該總是設定 from
,因為它用於原始程式碼對應和語法錯誤訊息。
類型:字串。
ProcessOptions#map
原始程式碼對應選項
類型:布林值 | SourceMapOptions。
ProcessOptions#parser
透過字串產生 AST 的函式。
類型:Parser<RootNode> | Syntax<RootNode>。
ProcessOptions#stringifier
透過 AST 產生字串的類別。
類型:Stringifier | Syntax<RootNode>。
ProcessOptions#syntax
包含剖析和字串化的物件。
類型:Syntax<RootNode>。
ProcessOptions#to
您將放置輸出 CSS 檔案的路徑。您應該總是設定 to
以產生正確的原始程式碼對應。
類型:字串。
處理器
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 | 字串 | Root | Result<Document_ | Root> | 物件 | LazyResult_<Document_ | Root> | 包含輸入 CSS 的字串或任何具有 toString() 方法的物件,例如 Buffer。選擇性地傳送 Result 實例,處理器將從中取得 Root 。 |
引數 | 類型 | 說明 |
---|---|---|
css | 字串 | Root | Result<Document_ | Root> | LazyResult_<Document_ | Root> | 物件 | 包含輸入 CSS 的字串或任何具有 toString() 方法的物件,例如 Buffer。選擇性地傳送 Result 實例,處理器將從中取得 Root 。 |
options | ProcessOptions<RootNode> |
傳回 NoWorkResult_ | LazyResult_<Document_ | Root>
。
Processor#use()
新增外掛程式以用作 CSS 處理器。
PostCSS 外掛程式可以有 4 種格式
Plugin
格式的外掛程式。- 具有
pluginCreator.postcss = true
的外掛程式建立函式。PostCSS 會在沒有引數的情況下呼叫這個函式以取得外掛程式。 - 函式。PostCSS 會將函式傳遞 Root 作為第一個引數,並將目前的
Result
實例作為第二個引數。 - 另一個
Processor
實例。PostCSS 將從該實例複製外掛程式到這個實例。
建立 postcss
實例時,也可以透過傳遞外掛程式作為參數來新增外掛程式(請參閱 [postcss(plugins)
])。
非同步外掛程式應傳回 Promise
實例。
const processor = postcss()
.use(autoprefixer)
.use(postcssNested)
引數 | 類型 | 說明 |
---|---|---|
外掛程式 | AcceptedPlugin | PostCSS 外掛程式或包含外掛程式的 Processor 。 |
傳回 處理器
。
Processor#version
目前的 PostCSS 版本。
if (result.processor.version.split('.')[0] !== '6') {
throw new Error('This plugin works only with PostCSS 6')
}
類型:字串。
範圍
Range#end
結束位置(不含)。
類型:位置。
Range#start
開始位置(含)。
類型:位置。
RangePosition
RangePosition#column
輸入中的欄位號碼。
類型:數字。
RangePosition#line
輸入中的行號。
類型:數字。
Result
Result#css
代表 Result#root
的 CSS 字串。
postcss.parse('a{}').toResult().css //=> "a{}"
類型:字串。
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
包含來自外掛程式的訊息(例如警告或自訂訊息)。每個訊息都應有類型和外掛程式屬性。
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.toResult().root === root
類型:RootNode。
Result#toString()
傳回 Result#css
內容。
result + '' === result.css
傳回 字串
。
Result#warn()
建立 Warning
的執行個體,並將其新增至 Result#messages
。
if (decl.important) {
result.warn('Avoid !important', { node: decl, word: '!important' })
}
引數 | 類型 |
---|---|
訊息 | 字串 |
options | WarningOptions |
傳回Warning
。
Result#warnings()
傳回外掛程式中的警告。從 Result#messages
中篩選出 Warning
執行個體。
result.warnings().forEach(warn => {
console.warn(warn.toString())
})
傳回 Warning[]
。
ResultOptions
ResultOptions#from
CSS 原始程式碼檔案的路徑。您應該總是設定 from
,因為它用於原始程式碼對應和語法錯誤訊息。
類型:字串。
ResultOptions#map
原始程式碼對應選項
類型:布林值 | SourceMapOptions。
ResultOptions#node
產生警告的 CSS 節點。
類型:Node。
ResultOptions#parser
透過字串產生 AST 的函式。
類型:語法<Document_ | 根> | 解析器<Document_ | 根>。
ResultOptions#plugin
建立此警告的擴充功能名稱。Result#warn
會自動填入 擴充功能#postcssPlugin
值。
類型:字串。
ResultOptions#stringifier
透過 AST 產生字串的類別。
ResultOptions#syntax
包含剖析和字串化的物件。
ResultOptions#to
您將放置輸出 CSS 檔案的路徑。您應該總是設定 to
以產生正確的原始程式碼對應。
類型:字串。
Root
Root#after()
將新節點插入至目前的節點之後,並加入至目前的節點父節點中。
僅為 node.parent.insertAfter(node, add)
的別名。
decl.after('color: black')
引數 | 類型 | 說明 |
---|---|---|
newNode | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 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… | (字串 | 字串[] | 節點 | 子節點屬性 | 子節點屬性[] | 節點[])[] | 新節點。 |
傳回 Root
。
Root#assign()
將屬性指定給現有的節點實例。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引數 | 類型 | 說明 |
---|---|---|
overrides | 物件 | RootProps | 用於覆寫節點的新屬性。 |
傳回 Root
。
Root#before()
將新節點插入至目前的節點之前,並加入至目前的節點父節點中。
僅為 node.parent.insertBefore(node, add)
的別名。
decl.before('content: ""')
引數 | 類型 | 說明 |
---|---|---|
newNode | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 Root
。
Root#cleanRaws()
清除節點及其子節點的程式碼樣式屬性。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引數 | 類型 | 說明 |
---|---|---|
keepBetween | 布林值 | 保留 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
。
在 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 | 函數 | 迭代器接收每個節點和索引。 |
傳回 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
}
引數 | 類型 | 說明 |
---|---|---|
訊息 | 字串 | 錯誤執行個體的描述。 |
options | NodeErrorOptions | 錯誤執行個體的選項。 |
傳回 CssSyntaxError_
。
Root#every()
如果 callback 對容器的所有子節點傳回 true
,則傳回 true
。
const noPrefixes = rule.every(i => i.prop[0] !== '-')
引數 | 類型 | 說明 |
---|---|---|
condition | 函數 | 迭代器傳回 true 或 false。 |
傳回 boolean
。
Root#index()
傳回 child
在 Container#nodes
陣列中的索引。
rule.index( rule.nodes[2] ) //=> 2
引數 | 類型 | 說明 |
---|---|---|
child | 數字 | ChildNode | 目前容器的子節點。 |
傳回 數字
。
Root#insertAfter()
在容器中將新節點插入舊節點之後。
引數 | 類型 | 說明 |
---|---|---|
oldNode | 數字 | ChildNode | 子節點或子節點索引。 |
newNode | 字串 | 字串陣列 | 子節點 | 子節點屬性 | 子節點 陣列 | 子節點屬性 陣列 | 新節點。 |
傳回 Root
。
Root#insertBefore()
在容器中插入新的節點,位於舊節點之前。
rule.insertBefore(decl, decl.clone({ prop: '-webkit-' + decl.prop }))
引數 | 類型 | 說明 |
---|---|---|
oldNode | 數字 | ChildNode | 子節點或子節點索引。 |
newNode | 字串 | 字串陣列 | 子節點 | 子節點屬性 | 子節點 陣列 | 子節點屬性 陣列 | 新節點。 |
傳回 Root
。
Root#next()
傳回節點父層的下一層子節點。如果目前節點為最後一個子節點,則傳回 undefined
。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
傳回 子節點。
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'
類型:子節點[].
Root#parent
代表目前節點的父層。
root.nodes[0].parent === root //=> true
類型:Document_。
Root#positionBy()
取得節點內部字詞或索引的位置。
引數 | 類型 | 說明 |
---|---|---|
選項 | Pick<警告選項, "index" | "word"> | 選項。 |
傳回 位置。
Root#positionInside()
將字串索引轉換為行/欄。
引數 | 類型 | 說明 |
---|---|---|
index | 數字 | 節點字串中的符號編號。 |
傳回 位置。
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… | (字串 | 字串[] | 節點 | 子節點屬性 | 子節點屬性[] | 節點[])[] | 新節點。 |
傳回 Root
。
Root#prev()
傳回節點父層的前一個子節點。如果目前節點為第一個子節點,則傳回 undefined
。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
傳回 子節點。
Root#push()
將子節點新增至節點尾端。
rule.push(new Declaration({ prop: 'color', value: 'black' }))
引數 | 類型 | 說明 |
---|---|---|
child | 子節點 | 新節點。 |
傳回 Root
。
Root#rangeBy()
取得節點內單字或開始和結束索引的範圍。開始索引包含在內;結束索引則不包含在內。
引數 | 類型 | 說明 |
---|---|---|
選項 | 選取<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') //=> ' '
引數 | 類型 | 說明 |
---|---|---|
屬性 | 字串 | 程式碼樣式屬性的名稱。 |
defaultType | 字串 | 預設值的名稱,如果值與 prop 相同,則可以省略。 |
傳回 字串
。
Root#raws
它代表 css 原始程式碼中不必要的空白和字元。
產生與原始輸入中位元組對位元組相等的節點字串資訊。
raws 物件的屬性由剖析器決定,預設剖析器使用下列屬性
before
:節點前的空白符號。它也儲存宣告前的*
和_
符號(IE hack)。after
:節點最後一個子節點到節點結尾的空白符號。between
:宣告中屬性和值、規則中選擇器和{
或 at 規則中最後一個參數和{
之間的符號。semicolon
:如果最後一個子節點有(可選的)分號,則包含 true。afterName
:at 規則名稱和其參數之間的空白。left
:/*
和註解文字之間的空白符號。right
:註解文字和 */ 之間的空白符號。
important
:重要聲明的內容,如果它不只是!important
。
PostCSS 會過濾掉選擇器、宣告值和 at 規則參數中的註解,但會將原始內容儲存在 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 | 數字 | ChildNode | 子節點或子節點索引。 |
傳回 Root
。
Root#replaceValues()
引數 | 類型 |
---|---|
模式 | 字串 | RegExp |
替換 | 字串 | 函式 |
引數 | 類型 | 說明 |
---|---|---|
模式 | 字串 | RegExp | 替換模式。 |
options | ValueOptions | |
替換 | 字串 | 函式 |
傳回 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()
如果回呼函式對容器的其中一個(至少一個)子節點傳回 true
,則傳回 true
。
const hasPrefix = rule.some(i => i.prop[0] === '-')
引數 | 類型 | 說明 |
---|---|---|
condition | 函數 | 迭代器傳回 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)
}
類型:來源。
Root#toJSON()
修正 JSON.stringify()
上的循環連結。
傳回 物件
。
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> |
Root#toString()
它會根據類型將節點編譯成瀏覽器可讀的層疊樣式表字串。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引數 | 類型 | 說明 |
---|---|---|
字串化器 | 字串化器 | 語法<Document_ | 根> | 字串產生中要使用的語法。 |
傳回 字串
。
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 | 函數 | 迭代器接收每個節點和索引。 |
傳回 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 | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
Root#walkComments()
引數 | 類型 |
---|---|
callback | 函數 |
傳回 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 | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
Root#walkRules()
遍歷容器的後代節點,針對每個規則節點呼叫回呼。
如果您傳遞篩選器,則迭代只會發生在具有匹配選擇器的規則上。
與 Container#each
一樣,如果您在反覆運算期間變異陣列,則可以使用這個方法。
const selectors = []
root.walkRules(rule => {
selectors.push(rule.selector)
})
console.log(`Your CSS uses ${ selectors.length } selectors`)
引數 | 類型 | 說明 |
---|---|---|
selectorFilter | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
Root#warn()
它是 warn 的包裝器,提供產生警告的便利方式。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引數 | 類型 | 說明 |
---|---|---|
結果 | 結果<Document_ | 根> | 將接收警告的Result 實例。 |
訊息 | 字串 | 警告的說明。 |
options | WarningOptions | 警告的選項。 |
傳回Warning
。
RootProps
RootProps#nodes
類型:(ChildNode | ChildProps)[].
RootProps#raws
用於產生位元組對位元組相等的節點字串資訊,如同原始輸入。
類型:RootRaws。
RootProps#source
類型:來源。
RootProps
RootProps#nodes
類型:(ChildNode | ChildProps)[].
RootProps#raws
用於產生位元組對位元組相等的節點字串資訊,如同原始輸入。
類型:RootRaws。
RootProps#source
類型:來源。
RootRaws
RootRaws#after
最後一個子節點到檔案結尾的空白符號。
類型:字串。
RootRaws#codeAfter
實驗性:此節點的某些面向可能會在次要或修補版本中變更。
類型:字串。
RootRaws#codeBefore
實驗性:此節點的某些面向可能會在次要或修補版本中變更。
類型:字串。
RootRaws#分號
最後一個子元素是否有一個(可選)分號。
類型:布林值。
Rule
Rule#after()
將新節點插入至目前的節點之後,並加入至目前的節點父節點中。
僅為 node.parent.insertAfter(node, add)
的別名。
decl.after('color: black')
引數 | 類型 | 說明 |
---|---|---|
newNode | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 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… | (字串 | 字串[] | 節點 | 子節點屬性 | 子節點屬性[] | 節點[])[] | 新節點。 |
傳回 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 | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 Rule
。
Rule#cleanRaws()
清除節點及其子節點的程式碼樣式屬性。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引數 | 類型 | 說明 |
---|---|---|
keepBetween | 布林值 | 保留 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
。
在 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 | 函數 | 迭代器接收每個節點和索引。 |
傳回 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
}
引數 | 類型 | 說明 |
---|---|---|
訊息 | 字串 | 錯誤執行個體的描述。 |
options | NodeErrorOptions | 錯誤執行個體的選項。 |
傳回 CssSyntaxError_
。
Rule#every()
如果 callback 對容器的所有子節點傳回 true
,則傳回 true
。
const noPrefixes = rule.every(i => i.prop[0] !== '-')
引數 | 類型 | 說明 |
---|---|---|
condition | 函數 | 迭代器傳回 true 或 false。 |
傳回 boolean
。
Rule#index()
傳回 child
在 Container#nodes
陣列中的索引。
rule.index( rule.nodes[2] ) //=> 2
引數 | 類型 | 說明 |
---|---|---|
child | 數字 | ChildNode | 目前容器的子節點。 |
傳回 數字
。
Rule#insertAfter()
在容器中將新節點插入舊節點之後。
引數 | 類型 | 說明 |
---|---|---|
oldNode | 數字 | ChildNode | 子節點或子節點索引。 |
newNode | 字串 | 字串陣列 | 子節點 | 子節點屬性 | 子節點 陣列 | 子節點屬性 陣列 | 新節點。 |
傳回 Rule
。
Rule#insertBefore()
在容器中插入新的節點,位於舊節點之前。
rule.insertBefore(decl, decl.clone({ prop: '-webkit-' + decl.prop }))
引數 | 類型 | 說明 |
---|---|---|
oldNode | 數字 | ChildNode | 子節點或子節點索引。 |
newNode | 字串 | 字串陣列 | 子節點 | 子節點屬性 | 子節點 陣列 | 子節點屬性 陣列 | 新節點。 |
傳回 Rule
。
Rule#next()
傳回節點父層的下一層子節點。如果目前節點為最後一個子節點,則傳回 undefined
。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
傳回 子節點。
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'
類型:子節點[].
Rule#parent
代表目前節點的父層。
root.nodes[0].parent === root //=> true
類型:Container_<子節點>。
Rule#positionBy()
取得節點內部字詞或索引的位置。
引數 | 類型 | 說明 |
---|---|---|
選項 | Pick<警告選項, "index" | "word"> | 選項。 |
傳回 位置。
Rule#positionInside()
將字串索引轉換為行/欄。
引數 | 類型 | 說明 |
---|---|---|
index | 數字 | 節點字串中的符號編號。 |
傳回 位置。
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… | (字串 | 字串[] | 節點 | 子節點屬性 | 子節點屬性[] | 節點[])[] | 新節點。 |
傳回 Rule
。
Rule#prev()
傳回節點父層的前一個子節點。如果目前節點為第一個子節點,則傳回 undefined
。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
傳回 子節點。
Rule#push()
將子節點新增至節點尾端。
rule.push(new Declaration({ prop: 'color', value: 'black' }))
引數 | 類型 | 說明 |
---|---|---|
child | 子節點 | 新節點。 |
傳回 Rule
。
Rule#rangeBy()
取得節點內單字或開始和結束索引的範圍。開始索引包含在內;結束索引則不包含在內。
引數 | 類型 | 說明 |
---|---|---|
選項 | 選取<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') //=> ' '
引數 | 類型 | 說明 |
---|---|---|
屬性 | 字串 | 程式碼樣式屬性的名稱。 |
defaultType | 字串 | 預設值的名稱,如果值與 prop 相同,則可以省略。 |
傳回 字串
。
Rule#raws
它代表 css 原始程式碼中不必要的空白和字元。
產生與原始輸入中位元組對位元組相等的節點字串資訊。
raws 物件的屬性由剖析器決定,預設剖析器使用下列屬性
before
:節點前的空白符號。它也儲存宣告前的*
和_
符號(IE hack)。after
:節點最後一個子節點到節點結尾的空白符號。between
:宣告中屬性和值、規則中選擇器和{
或 at 規則中最後一個參數和{
之間的符號。semicolon
:如果最後一個子節點有(可選的)分號,則包含 true。afterName
:at 規則名稱和其參數之間的空白。left
:/*
和註解文字之間的空白符號。right
:註解文字和 */ 之間的空白符號。
important
:重要聲明的內容,如果它不只是!important
。
PostCSS 會過濾掉選擇器、宣告值和 at 規則參數中的註解,但會將原始內容儲存在 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 | 數字 | ChildNode | 子節點或子節點索引。 |
傳回 Rule
。
Rule#replaceValues()
引數 | 類型 |
---|---|
模式 | 字串 | RegExp |
替換 | 字串 | 函式 |
引數 | 類型 | 說明 |
---|---|---|
模式 | 字串 | RegExp | 替換模式。 |
options | ValueOptions | |
替換 | 字串 | 函式 |
傳回 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'
類型:字串。
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()
如果回呼函式對容器的其中一個(至少一個)子節點傳回 true
,則傳回 true
。
const hasPrefix = rule.some(i => i.prop[0] === '-')
引數 | 類型 | 說明 |
---|---|---|
condition | 函數 | 迭代器傳回 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)
}
類型:來源。
Rule#toJSON()
修正 JSON.stringify()
上的循環連結。
傳回 物件
。
Rule#toString()
它會根據類型將節點編譯成瀏覽器可讀的層疊樣式表字串。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引數 | 類型 | 說明 |
---|---|---|
字串化器 | 字串化器 | 語法<Document_ | 根> | 字串產生中要使用的語法。 |
傳回 字串
。
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 | 函數 | 迭代器接收每個節點和索引。 |
傳回 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 | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
Rule#walkComments()
引數 | 類型 |
---|---|
callback | 函數 |
傳回 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 | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
Rule#walkRules()
遍歷容器的後代節點,針對每個規則節點呼叫回呼。
如果您傳遞篩選器,則迭代只會發生在具有匹配選擇器的規則上。
與 Container#each
一樣,如果您在反覆運算期間變異陣列,則可以使用這個方法。
const selectors = []
root.walkRules(rule => {
selectors.push(rule.selector)
})
console.log(`Your CSS uses ${ selectors.length } selectors`)
引數 | 類型 | 說明 |
---|---|---|
selectorFilter | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
Rule#warn()
它是 warn 的包裝器,提供產生警告的便利方式。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引數 | 類型 | 說明 |
---|---|---|
結果 | 結果<Document_ | 根> | 將接收警告的Result 實例。 |
訊息 | 字串 | 警告的說明。 |
options | WarningOptions | 警告的選項。 |
傳回Warning
。
RuleProps
RuleProps#節點
類型:(ChildNode | ChildProps)[].
RuleProps#原始
用於產生位元組對位元組相等的節點字串資訊,如同原始輸入。
類型:RuleRaws。
RuleProps#選擇器
規則的選擇器或選擇器。
類型:字串。
RuleProps#選擇器
規則的選擇器,表示為字串陣列。
類型:string[]。
RuleProps#來源
類型:來源。
RuleProps
RuleProps#節點
類型:(ChildNode | ChildProps)[].
RuleProps#原始
用於產生位元組對位元組相等的節點字串資訊,如同原始輸入。
類型:RuleRaws。
RuleProps#選擇器
規則的選擇器或選擇器。
類型:字串。
RuleProps#選擇器
規則的選擇器,表示為字串陣列。
類型:string[]。
RuleProps#來源
類型:來源。
RuleRaws
RuleRaws#後
節點最後一個子節點之後到節點結尾的空白符號。
類型:字串。
RuleRaws#前
節點之前的空白符號。它也儲存宣告之前的 *
和 _
符號(IE hack)。
類型:字串。
RuleRaws#中間
規則選擇器和 {
之間的符號。
類型:字串。
RuleRaws#自身分號
如果規則後有分號,則包含 true
。
類型:字串。
RuleRaws#選擇器
包含註解的規則選擇器。
類型:物件。
RuleRaws#分號
如果最後一個子節點有(可選的)分號,則包含 true
。
類型:布林值。
來源
Source#結束
節點原始碼的包含結束位置。
類型:位置。
Source#輸入
節點來源的原始檔案。
類型:Input_。
Source#開始
節點原始碼的包含開始位置。
類型:位置。
SourceMapOptions
SourceMapOptions#絕對
在產生的原始碼地圖中使用絕對路徑。
類型:布林值。
SourceMapOptions#註解
表示 PostCSS 應將註解註解新增至 CSS。預設情況下,PostCSS 始終會新增一個包含原始碼地圖路徑的註解。PostCSS 不會將註解新增至不包含任何註解的 CSS 檔案。
預設情況下,PostCSS 會假設您想要將原始碼地圖儲存為 opts.to + '.map'
,並會在註解中使用此路徑。可以透過提供註解的字串值來設定不同的路徑。
如果您已設定 inline: true
,則無法停用註解。
類型:字串 | 布林 | 函式。
SourceMapOptions#from
覆寫地圖來源中的 from
。
類型:字串。
SourceMapOptions#inline
表示應將原始碼地圖嵌入輸出 CSS 中,作為 Base64 編碼的註解。預設為 true
。但如果所有先前的映射都是外部的,而不是內嵌的,則即使您未設定此選項,PostCSS 也不會嵌入映射。
如果您有內嵌原始碼地圖,則 result.map 屬性將為空,因為原始碼地圖將包含在 result.css
的文字中。
類型:布林值。
SourceMapOptions#prev
來自先前處理步驟(例如 Sass)的原始碼地圖內容。
PostCSS 會嘗試自動讀取先前的原始碼地圖(根據原始碼 CSS 中的註解),但您可以使用此選項手動識別它。
如果需要,您可以省略先前的映射,並使用 prev: false
。
類型:字串 | 布林 | 物件 | 函式。
SourceMapOptions#sourcesContent
表示 PostCSS 應設定原始碼地圖的原始內容(例如 Sass 來源)。預設為 true。但如果所有先前的映射都不包含來源內容,則即使您未設定此選項,PostCSS 也會將其省略。
類型:布林值。
語法
Syntax#parse
透過字串產生 AST 的函式。
類型:Parser<RootNode>。
Syntax#stringify
透過 AST 產生字串的類別。
類型:Stringifier。
轉換器
引數 | 類型 |
---|---|
根節點 | Root |
結果 | 結果<Document_ | 根> |
傳回 void | Promise<void>
。
Transformer#postcssPlugin
類型:字串。
Transformer#postcssVersion
類型:字串。
ValueOptions
ValueOptions#fast
用於縮小值並加速正規表示式搜尋的字串。
類型:字串。
ValueOptions#props
屬性名稱陣列。
類型:string[]。
警告
Warning#column
此警告來源的輸入檔案中包含開始位置的欄位。
warning.column //=> 6
類型:數字。
Warning#endColumn
此警告來源的輸入檔案中包含結束位置的欄位。
warning.endColumn //=> 4
類型:數字。
Warning#endLine
此警告來源的輸入檔案中包含結束位置的行數。
warning.endLine //=> 6
類型:數字。
Warning#line
此警告來源的輸入檔案中包含開始位置的行數。
warning.line //=> 5
類型:數字。
Warning#node
包含導致警告的 CSS 節點。
warning.node.toString() //=> 'color: white !important'
類型:Node。
Warning#plugin
建立此警告的外掛名稱。當您呼叫 Node#warn
時,它會自動填入此屬性。
warning.plugin //=> 'postcss-important'
類型:字串。
Warning#text
警告訊息。
warning.text //=> 'Try to avoid !important'
類型:字串。
Warning#toString()
傳回警告位置和訊息。
warning.toString() //=> 'postcss-lint:a.css:10:14: Avoid !important'
傳回 字串
。
Warning#type
從 Result#messages
篩選警告的類型。永遠等於 "warning"
。
類型:"warning"。
WarningOptions
WarningOptions#end
導致警告的 CSS 節點字串中,結束位置(不包含)。
類型:RangePosition。
WarningOptions#endIndex
導致警告的 CSS 節點字串中,結束索引(不包含)。
類型:數字。
WarningOptions#index
導致警告的 CSS 節點字串中,開始索引(包含)。
類型:數字。
WarningOptions#node
導致警告的 CSS 節點。
類型:Node。
WarningOptions#plugin
建立此警告的擴充功能名稱。Result#warn
會自動填入此屬性。
類型:字串。
WarningOptions#start
導致警告的 CSS 節點字串中,開始位置(包含)。
類型:RangePosition。
WarningOptions#word
導致警告的 CSS 來源中的字詞。
類型:字串。
WarningOptions
WarningOptions#end
導致警告的 CSS 節點字串中,結束位置(不包含)。
類型:RangePosition。
WarningOptions#endIndex
導致警告的 CSS 節點字串中,結束索引(不包含)。
類型:數字。
WarningOptions#index
導致警告的 CSS 節點字串中,開始索引(包含)。
類型:數字。
WarningOptions#node
導致警告的 CSS 節點。
類型:Node。
WarningOptions#plugin
建立此警告的擴充功能名稱。Result#warn
會自動填入此屬性。
類型:字串。
WarningOptions#start
導致警告的 CSS 節點字串中,開始位置(包含)。
類型:RangePosition。
WarningOptions#word
導致警告的 CSS 來源中的字詞。
類型:字串。
at-rule
at-rule#after()
將新節點插入至目前的節點之後,並加入至目前的節點父節點中。
僅為 node.parent.insertAfter(node, add)
的別名。
decl.after('color: black')
引數 | 類型 | 說明 |
---|---|---|
newNode | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 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… | (字串 | 字串[] | 節點 | 子節點屬性 | 子節點屬性[] | 節點[])[] | 新節點。 |
傳回 AtRule_
。
at-rule#assign()
將屬性指定給現有的節點實例。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引數 | 類型 | 說明 |
---|---|---|
overrides | 物件 | AtRuleProps | 用於覆寫節點的新屬性。 |
傳回 AtRule_
。
at-rule#before()
將新節點插入至目前的節點之前,並加入至目前的節點父節點中。
僅為 node.parent.insertBefore(node, add)
的別名。
decl.before('content: ""')
引數 | 類型 | 說明 |
---|---|---|
newNode | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 AtRule_
。
at-rule#cleanRaws()
清除節點及其子節點的程式碼樣式屬性。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引數 | 類型 | 說明 |
---|---|---|
keepBetween | 布林值 | 保留 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
。
在 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 | 函數 | 迭代器接收每個節點和索引。 |
傳回 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
}
引數 | 類型 | 說明 |
---|---|---|
訊息 | 字串 | 錯誤執行個體的描述。 |
options | NodeErrorOptions | 錯誤執行個體的選項。 |
傳回 CssSyntaxError_
。
at-rule#every()
如果 callback 對容器的所有子節點傳回 true
,則傳回 true
。
const noPrefixes = rule.every(i => i.prop[0] !== '-')
引數 | 類型 | 說明 |
---|---|---|
condition | 函數 | 迭代器傳回 true 或 false。 |
傳回 boolean
。
at-rule#index()
傳回 child
在 Container#nodes
陣列中的索引。
rule.index( rule.nodes[2] ) //=> 2
引數 | 類型 | 說明 |
---|---|---|
child | 數字 | ChildNode | 目前容器的子節點。 |
傳回 數字
。
at-rule#insertAfter()
在容器中將新節點插入舊節點之後。
引數 | 類型 | 說明 |
---|---|---|
oldNode | 數字 | ChildNode | 子節點或子節點索引。 |
newNode | 字串 | 字串陣列 | 子節點 | 子節點屬性 | 子節點 陣列 | 子節點屬性 陣列 | 新節點。 |
傳回 AtRule_
。
at-rule#insertBefore()
在容器中插入新的節點,位於舊節點之前。
rule.insertBefore(decl, decl.clone({ prop: '-webkit-' + decl.prop }))
引數 | 類型 | 說明 |
---|---|---|
oldNode | 數字 | ChildNode | 子節點或子節點索引。 |
newNode | 字串 | 字串陣列 | 子節點 | 子節點屬性 | 子節點 陣列 | 子節點屬性 陣列 | 新節點。 |
傳回 AtRule_
。
at-rule#name
at 規則的名稱緊接在 @
之後。
const root = postcss.parse('@media print {}')
media.name //=> 'media'
const media = root.first
類型:字串。
at-rule#next()
傳回節點父層的下一層子節點。如果目前節點為最後一個子節點,則傳回 undefined
。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
傳回 子節點。
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'
類型:子節點[].
at-rule#params
at 規則的參數,即 at 規則名稱之後但位於任何 {}
區塊之前的那些值。
const root = postcss.parse('@media print, screen {}')
const media = root.first
media.params //=> 'print, screen'
類型:字串。
at-rule#parent
代表目前節點的父層。
root.nodes[0].parent === root //=> true
類型:Container_<子節點>。
at-rule#positionBy()
取得節點內部字詞或索引的位置。
引數 | 類型 | 說明 |
---|---|---|
選項 | Pick<警告選項, "index" | "word"> | 選項。 |
傳回 位置。
at-rule#positionInside()
將字串索引轉換為行/欄。
引數 | 類型 | 說明 |
---|---|---|
index | 數字 | 節點字串中的符號編號。 |
傳回 位置。
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… | (字串 | 字串[] | 節點 | 子節點屬性 | 子節點屬性[] | 節點[])[] | 新節點。 |
傳回 AtRule_
。
at-rule#prev()
傳回節點父層的前一個子節點。如果目前節點為第一個子節點,則傳回 undefined
。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
傳回 子節點。
at-rule#push()
將子節點新增至節點尾端。
rule.push(new Declaration({ prop: 'color', value: 'black' }))
引數 | 類型 | 說明 |
---|---|---|
child | 子節點 | 新節點。 |
傳回 AtRule_
。
at-rule#rangeBy()
取得節點內單字或開始和結束索引的範圍。開始索引包含在內;結束索引則不包含在內。
引數 | 類型 | 說明 |
---|---|---|
選項 | 選取<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') //=> ' '
引數 | 類型 | 說明 |
---|---|---|
屬性 | 字串 | 程式碼樣式屬性的名稱。 |
defaultType | 字串 | 預設值的名稱,如果值與 prop 相同,則可以省略。 |
傳回 字串
。
at-rule#raws
它代表 css 原始程式碼中不必要的空白和字元。
產生與原始輸入中位元組對位元組相等的節點字串資訊。
raws 物件的屬性由剖析器決定,預設剖析器使用下列屬性
before
:節點前的空白符號。它也儲存宣告前的*
和_
符號(IE hack)。after
:節點最後一個子節點到節點結尾的空白符號。between
:宣告中屬性和值、規則中選擇器和{
或 at 規則中最後一個參數和{
之間的符號。semicolon
:如果最後一個子節點有(可選的)分號,則包含 true。afterName
:at 規則名稱和其參數之間的空白。left
:/*
和註解文字之間的空白符號。right
:註解文字和 */ 之間的空白符號。
important
:重要聲明的內容,如果它不只是!important
。
PostCSS 會過濾掉選擇器、宣告值和 at 規則參數中的註解,但會將原始內容儲存在 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 | 數字 | ChildNode | 子節點或子節點索引。 |
傳回 AtRule_
。
at-rule#replaceValues()
引數 | 類型 |
---|---|
模式 | 字串 | RegExp |
替換 | 字串 | 函式 |
引數 | 類型 | 說明 |
---|---|---|
模式 | 字串 | RegExp | 替換模式。 |
options | ValueOptions | |
替換 | 字串 | 函式 |
傳回 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()
如果回呼函式對容器的其中一個(至少一個)子節點傳回 true
,則傳回 true
。
const hasPrefix = rule.some(i => i.prop[0] === '-')
引數 | 類型 | 說明 |
---|---|---|
condition | 函數 | 迭代器傳回 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)
}
類型:來源。
at-rule#toJSON()
修正 JSON.stringify()
上的循環連結。
傳回 物件
。
at-rule#toString()
它會根據類型將節點編譯成瀏覽器可讀的層疊樣式表字串。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引數 | 類型 | 說明 |
---|---|---|
字串化器 | 字串化器 | 語法<Document_ | 根> | 字串產生中要使用的語法。 |
傳回 字串
。
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 | 函數 | 迭代器接收每個節點和索引。 |
傳回 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 | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
at-rule#walkComments()
引數 | 類型 |
---|---|
callback | 函數 |
傳回 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 | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 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 | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
at-rule#warn()
它是 Result#warn 的包裝器,提供產生警告的便利方式。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引數 | 類型 | 說明 |
---|---|---|
結果 | 結果<Document_ | 根> | 將接收警告的Result 實例。 |
訊息 | 字串 | 警告的說明。 |
options | WarningOptions | 警告的選項。 |
傳回Warning
。
註解
註解#after()
將新節點插入至目前的節點之後,並加入至目前的節點父節點中。
僅為 node.parent.insertAfter(node, add)
的別名。
decl.after('color: black')
引數 | 類型 | 說明 |
---|---|---|
newNode | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 Comment_
。
註解#assign()
將屬性指定給現有的節點實例。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引數 | 類型 | 說明 |
---|---|---|
overrides | object | CommentProps | 用於覆寫節點的新屬性。 |
傳回 Comment_
。
註解#before()
將新節點插入至目前的節點之前,並加入至目前的節點父節點中。
僅為 node.parent.insertBefore(node, add)
的別名。
decl.before('content: ""')
引數 | 類型 | 說明 |
---|---|---|
newNode | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 Comment_
。
註解#cleanRaws()
清除節點及其子節點的程式碼樣式屬性。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引數 | 類型 | 說明 |
---|---|---|
keepBetween | 布林值 | 保留 raws.between 符號。 |
註解#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
。
註解#cloneAfter()
複製節點的捷徑,並將複製後的節點插入至目前的節點之後。
引數 | 類型 | 說明 |
---|---|---|
overrides | Partial<CommentProps> | 在複製中覆寫的新屬性。 |
傳回Comment
。
註解#cloneBefore()
複製節點的捷徑,並將複製後的節點插入至目前的節點之前。
decl.cloneBefore({ prop: '-moz-' + decl.prop })
引數 | 類型 | 說明 |
---|---|---|
overrides | Partial<CommentProps> | 在複製中覆寫的新屬性。 |
傳回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
}
引數 | 類型 | 說明 |
---|---|---|
訊息 | 字串 | 錯誤執行個體的描述。 |
options | NodeErrorOptions | 錯誤執行個體的選項。 |
傳回 CssSyntaxError_
。
註解#next()
傳回節點父層的下一層子節點。如果目前節點為最後一個子節點,則傳回 undefined
。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
傳回 子節點。
註解#parent
代表目前節點的父層。
root.nodes[0].parent === root //=> true
類型:Container_<子節點>。
註解#positionBy()
取得節點內部字詞或索引的位置。
引數 | 類型 | 說明 |
---|---|---|
選項 | Pick<警告選項, "index" | "word"> | 選項。 |
傳回 位置。
註解#positionInside()
將字串索引轉換為行/欄。
引數 | 類型 | 說明 |
---|---|---|
index | 數字 | 節點字串中的符號編號。 |
傳回 位置。
註解#prev()
傳回節點父層的前一個子節點。如果目前節點為第一個子節點,則傳回 undefined
。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
傳回 子節點。
註解#rangeBy()
取得節點內單字或開始和結束索引的範圍。開始索引包含在內;結束索引則不包含在內。
引數 | 類型 | 說明 |
---|---|---|
選項 | 選取<WarningOptions, "index" | "word" | "endIndex"> | 選項。 |
傳回 Range
。
註解#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') //=> ' '
引數 | 類型 | 說明 |
---|---|---|
屬性 | 字串 | 程式碼樣式屬性的名稱。 |
defaultType | 字串 | 預設值的名稱,如果值與 prop 相同,則可以省略。 |
傳回 字串
。
註解#raws
它代表 css 原始程式碼中不必要的空白和字元。
產生與原始輸入中位元組對位元組相等的節點字串資訊。
raws 物件的屬性由剖析器決定,預設剖析器使用下列屬性
before
:節點前的空白符號。它也儲存宣告前的*
和_
符號(IE hack)。after
:節點最後一個子節點到節點結尾的空白符號。between
:宣告中屬性和值、規則中選擇器和{
或 at 規則中最後一個參數和{
之間的符號。semicolon
:如果最後一個子節點有(可選的)分號,則包含 true。afterName
:at 規則名稱和其參數之間的空白。left
:/*
和註解文字之間的空白符號。right
:註解文字和 */ 之間的空白符號。
important
:重要聲明的內容,如果它不只是!important
。
PostCSS 會過濾掉選擇器、宣告值和 at 規則參數中的註解,但會將原始內容儲存在 raws 中。
const root = postcss.parse('a {\n color:black\n}')
root.first.first.raws //=> { before: '\n ', between: ':' }
類型:CommentRaws。
註解#remove()
它會從父節點移除節點並刪除其父屬性。
if (decl.prop.match(/^-webkit-/)) {
decl.remove()
}
傳回 Comment_
。
註解#replaceWith()
在目前節點之前插入節點並移除目前節點。
AtRule: {
mixin: atrule => {
atrule.replaceWith(mixinRules[atrule.params])
}
}
引數 | 類型 | 說明 |
---|---|---|
nodes… | (ChildNode | ChildProps | ChildNode[] | ChildProps[])[] | 取代目前節點的模式。 |
傳回 Comment_
。
註解#root()
尋找節點樹的 Root 實例。
root.nodes[0].nodes[0].root() === root
傳回 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)
}
類型:來源。
註解#text
註解的文字。
類型:字串。
註解#toJSON()
修正 JSON.stringify()
上的循環連結。
傳回 物件
。
註解#toString()
它會根據類型將節點編譯成瀏覽器可讀的層疊樣式表字串。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引數 | 類型 | 說明 |
---|---|---|
字串化器 | 字串化器 | 語法<Document_ | 根> | 字串產生中要使用的語法。 |
傳回 字串
。
註解#type
它表示抽象語法樹中節點的類型。
節點類型有助於識別節點,並根據其類型執行操作。
const declaration = new Declaration({
prop: 'color',
value: 'black'
})
declaration.type //=> 'decl'
類型:「comment」。
註解#warn()
它是 Result#warn 的包裝器,提供產生警告的便利方式。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引數 | 類型 | 說明 |
---|---|---|
結果 | 結果<Document_ | 根> | 將接收警告的Result 實例。 |
訊息 | 字串 | 警告的說明。 |
options | WarningOptions | 警告的選項。 |
傳回Warning
。
容器
容器#after()
將新節點插入至目前的節點之後,並加入至目前的節點父節點中。
僅為 node.parent.insertAfter(node, add)
的別名。
decl.after('color: black')
引數 | 類型 | 說明 |
---|---|---|
newNode | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 Container_<Child>
。
容器#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… | (字串 | 字串[] | 節點 | 子節點屬性 | 子節點屬性[] | 節點[])[] | 新節點。 |
傳回 Container_<Child>
。
容器#assign()
將屬性指定給現有的節點實例。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引數 | 類型 | 說明 |
---|---|---|
overrides | object | ContainerProps | 用於覆寫節點的新屬性。 |
傳回 Container_<Child>
。
容器#before()
將新節點插入至目前的節點之前,並加入至目前的節點父節點中。
僅為 node.parent.insertBefore(node, add)
的別名。
decl.before('content: ""')
引數 | 類型 | 說明 |
---|---|---|
newNode | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 Container_<Child>
。
容器#cleanRaws()
清除節點及其子節點的程式碼樣式屬性。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引數 | 類型 | 說明 |
---|---|---|
keepBetween | 布林值 | 保留 raws.between 符號。 |
容器#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>
。
容器#cloneAfter()
複製節點的捷徑,並將複製後的節點插入至目前的節點之後。
引數 | 類型 | 說明 |
---|---|---|
overrides | Partial<ContainerProps> | 在複製中覆寫的新屬性。 |
傳回 Container<Child>
。
容器#cloneBefore()
複製節點的捷徑,並將複製後的節點插入至目前的節點之前。
decl.cloneBefore({ prop: '-moz-' + decl.prop })
引數 | 類型 | 說明 |
---|---|---|
overrides | Partial<ContainerProps> | 在複製中覆寫的新屬性。 |
傳回 Container<Child>
。
容器#each()
遍歷容器的直接子節點,並為每個子節點呼叫 callback
。
在 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 | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
容器#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
}
引數 | 類型 | 說明 |
---|---|---|
訊息 | 字串 | 錯誤執行個體的描述。 |
options | NodeErrorOptions | 錯誤執行個體的選項。 |
傳回 CssSyntaxError_
。
容器#every()
如果 callback 對容器的所有子節點傳回 true
,則傳回 true
。
const noPrefixes = rule.every(i => i.prop[0] !== '-')
引數 | 類型 | 說明 |
---|---|---|
condition | 函數 | 迭代器傳回 true 或 false。 |
傳回 boolean
。
容器#index()
傳回 child
在 Container#nodes
陣列中的索引。
rule.index( rule.nodes[2] ) //=> 2
引數 | 類型 | 說明 |
---|---|---|
child | number | Child | 目前容器的子節點。 |
傳回 數字
。
容器#insertAfter()
在容器中將新節點插入舊節點之後。
引數 | 類型 | 說明 |
---|---|---|
oldNode | number | Child | 子節點或子節點索引。 |
newNode | string | string[] | Child | ChildProps | ChildProps[] | Child[] | 新節點。 |
傳回 Container_<Child>
。
容器#insertBefore()
在容器中插入新的節點,位於舊節點之前。
rule.insertBefore(decl, decl.clone({ prop: '-webkit-' + decl.prop }))
引數 | 類型 | 說明 |
---|---|---|
oldNode | number | Child | 子節點或子節點索引。 |
newNode | string | string[] | Child | ChildProps | ChildProps[] | Child[] | 新節點。 |
傳回 Container_<Child>
。
容器#next()
傳回節點父層的下一層子節點。如果目前節點為最後一個子節點,則傳回 undefined
。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
傳回 子節點。
容器#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[]。
容器#parent
代表目前節點的父層。
root.nodes[0].parent === root //=> true
類型:Container_<ChildNode> | Document_。
容器#positionBy()
取得節點內部字詞或索引的位置。
引數 | 類型 | 說明 |
---|---|---|
選項 | Pick<警告選項, "index" | "word"> | 選項。 |
傳回 位置。
容器#positionInside()
將字串索引轉換為行/欄。
引數 | 類型 | 說明 |
---|---|---|
index | 數字 | 節點字串中的符號編號。 |
傳回 位置。
容器#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… | (字串 | 字串[] | 節點 | 子節點屬性 | 子節點屬性[] | 節點[])[] | 新節點。 |
傳回 Container_<Child>
。
容器#prev()
傳回節點父層的前一個子節點。如果目前節點為第一個子節點,則傳回 undefined
。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
傳回 子節點。
容器#push()
將子節點新增至節點尾端。
rule.push(new Declaration({ prop: 'color', value: 'black' }))
引數 | 類型 | 說明 |
---|---|---|
child | Child | 新節點。 |
傳回 Container_<Child>
。
容器#rangeBy()
取得節點內單字或開始和結束索引的範圍。開始索引包含在內;結束索引則不包含在內。
引數 | 類型 | 說明 |
---|---|---|
選項 | 選取<WarningOptions, "index" | "word" | "endIndex"> | 選項。 |
傳回 Range
。
容器#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') //=> ' '
引數 | 類型 | 說明 |
---|---|---|
屬性 | 字串 | 程式碼樣式屬性的名稱。 |
defaultType | 字串 | 預設值的名稱,如果值與 prop 相同,則可以省略。 |
傳回 字串
。
容器#raws
它代表 css 原始程式碼中不必要的空白和字元。
產生與原始輸入中位元組對位元組相等的節點字串資訊。
raws 物件的屬性由剖析器決定,預設剖析器使用下列屬性
before
:節點前的空白符號。它也儲存宣告前的*
和_
符號(IE hack)。after
:節點最後一個子節點到節點結尾的空白符號。between
:宣告中屬性和值、規則中選擇器和{
或 at 規則中最後一個參數和{
之間的符號。semicolon
:如果最後一個子節點有(可選的)分號,則包含 true。afterName
:at 規則名稱和其參數之間的空白。left
:/*
和註解文字之間的空白符號。right
:註解文字和 */ 之間的空白符號。
important
:重要聲明的內容,如果它不只是!important
。
PostCSS 會過濾掉選擇器、宣告值和 at 規則參數中的註解,但會將原始內容儲存在 raws 中。
const root = postcss.parse('a {\n color:black\n}')
root.first.first.raws //=> { before: '\n ', between: ':' }
類型:任何。
容器#remove()
它會從父節點移除節點並刪除其父屬性。
if (decl.prop.match(/^-webkit-/)) {
decl.remove()
}
傳回 Container_<Child>
。
容器#removeAll()
從容器中移除所有子節點並清除其父屬性。
rule.removeAll()
rule.nodes.length //=> 0
傳回 Container_<Child>
。
容器#removeChild()
從容器中移除節點並清除節點及其子節點的父屬性。
rule.nodes.length //=> 5
rule.removeChild(decl)
rule.nodes.length //=> 4
decl.parent //=> undefined
引數 | 類型 | 說明 |
---|---|---|
child | number | Child | 子節點或子節點索引。 |
傳回 Container_<Child>
。
容器#replaceValues()
引數 | 類型 |
---|---|
模式 | 字串 | RegExp |
替換 | 字串 | 函式 |
引數 | 類型 | 說明 |
---|---|---|
模式 | 字串 | RegExp | 替換模式。 |
options | ValueOptions | |
替換 | 字串 | 函式 |
傳回 Container_<Child>
。
容器#replaceWith()
在目前節點之前插入節點並移除目前節點。
AtRule: {
mixin: atrule => {
atrule.replaceWith(mixinRules[atrule.params])
}
}
引數 | 類型 | 說明 |
---|---|---|
nodes… | (ChildNode | ChildProps | ChildNode[] | ChildProps[])[] | 取代目前節點的模式。 |
傳回 Container_<Child>
。
容器#root()
尋找節點樹的 Root 實例。
root.nodes[0].nodes[0].root() === root
傳回 Root
。
容器#some()
如果回呼函式對容器的其中一個(至少一個)子節點傳回 true
,則傳回 true
。
const hasPrefix = rule.some(i => i.prop[0] === '-')
引數 | 類型 | 說明 |
---|---|---|
condition | 函數 | 迭代器傳回 true 或 false。 |
傳回 boolean
。
容器#來源
它表示與節點來源相關的資訊,且對於產生原始碼對應表是必要的。
使用 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)
}
類型:來源。
容器#toJSON()
修正 JSON.stringify()
上的循環連結。
傳回 物件
。
容器#toString()
它會根據類型將節點編譯成瀏覽器可讀的層疊樣式表字串。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引數 | 類型 | 說明 |
---|---|---|
字串化器 | 字串化器 | 語法<Document_ | 根> | 字串產生中要使用的語法。 |
傳回 字串
。
容器#類型
它表示抽象語法樹中節點的類型。
節點類型有助於識別節點,並根據其類型執行操作。
const declaration = new Declaration({
prop: 'color',
value: 'black'
})
declaration.type //=> 'decl'
類型:字串。
容器#walk()
遍歷容器的後代節點,並針對每個節點呼叫回呼函式。
與 container.each() 一樣,如果您在反覆運算期間變異陣列,則可以使用這個方法。
如果您只需要反覆運算容器的直接子節點,請使用 Container#each
。
root.walk(node => {
// Traverses all descendant nodes.
})
引數 | 類型 | 說明 |
---|---|---|
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
容器#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 | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
容器#walkComments()
引數 | 類型 |
---|---|
callback | 函數 |
傳回 void | false
。
容器#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 | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
容器#walkRules()
遍歷容器的後代節點,針對每個規則節點呼叫回呼。
如果您傳遞篩選器,則迭代只會發生在具有匹配選擇器的規則上。
與 Container#each
一樣,如果您在反覆運算期間變異陣列,則可以使用這個方法。
const selectors = []
root.walkRules(rule => {
selectors.push(rule.selector)
})
console.log(`Your CSS uses ${ selectors.length } selectors`)
引數 | 類型 | 說明 |
---|---|---|
selectorFilter | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
容器#warn()
它是 Result#warn 的包裝器,提供產生警告的便利方式。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引數 | 類型 | 說明 |
---|---|---|
結果 | 結果<Document_ | 根> | 將接收警告的Result 實例。 |
訊息 | 字串 | 警告的說明。 |
options | WarningOptions | 警告的選項。 |
傳回Warning
。
css 語法錯誤
css 語法錯誤#欄
錯誤的來源欄位。
error.column //=> 1
error.input.column //=> 4
PostCSS 會使用輸入來源地圖來偵測原始位置。如果您需要 PostCSS 輸入中的位置,請使用 error.input.column
。
類型:數字。
css 語法錯誤#結束欄
錯誤結束的來源欄位,不包含。如果錯誤與範圍有關,則提供。
error.endColumn //=> 1
error.input.endColumn //=> 4
PostCSS 會使用輸入來源地圖來偵測原始位置。如果您需要 PostCSS 輸入中的位置,請使用 error.input.endColumn
。
類型:數字。
css 語法錯誤#結束行
錯誤結束的來源行,不包含。如果錯誤與範圍有關,則提供。
error.endLine //=> 3
error.input.endLine //=> 4
PostCSS 會使用輸入來源地圖來偵測原始位置。如果您需要 PostCSS 輸入中的位置,請使用 error.input.endLine
。
類型:數字。
css 語法錯誤#檔案
損壞檔案的絕對路徑。
error.file //=> 'a.sass'
error.input.file //=> 'a.css'
PostCSS 會使用輸入來源地圖來偵測原始位置。如果您需要 PostCSS 輸入中的位置,請使用 error.input.file
。
類型:字串。
css 語法錯誤#輸入
輸入物件,包含 PostCSS 關於輸入檔案的內部資訊。如果輸入有來自先前工具的來源地圖,PostCSS 會使用原始 (例如 Sass) 來源。您可以使用這個物件來取得 PostCSS 輸入來源。
error.input.file //=> 'a.css'
error.file //=> 'a.sass'
類型:FilePosition。
css 語法錯誤#行
錯誤的來源行。
error.line //=> 2
error.input.line //=> 4
PostCSS 會使用輸入來源地圖來偵測原始位置。如果您需要 PostCSS 輸入中的位置,請使用 error.input.line
。
類型:數字。
css 語法錯誤#訊息
使用外掛程式、檔案、行和欄的 GNU 錯誤格式的完整錯誤文字。
error.message //=> 'a.css:1:1: Unclosed block'
類型:字串。
css 語法錯誤#名稱
總是等於 'CssSyntaxError'
。您應該總是透過 error.name === 'CssSyntaxError'
而不是 error instanceof CssSyntaxError
來檢查錯誤類型,因為 npm 可能有多個 PostCSS 版本。
if (error.name === 'CssSyntaxError') {
error //=> CssSyntaxError
}
類型:"CssSyntaxError"。
css 語法錯誤#外掛程式
如果錯誤來自於外掛,則為外掛名稱。
error.plugin //=> 'postcss-vars'
類型:字串。
css 語法錯誤#原因
錯誤訊息。
error.message //=> 'Unclosed block'
類型:字串。
css 語法錯誤#showSourceCode()
傳回導致錯誤的幾行 CSS 原始碼。
如果 CSS 有一個輸入來源地圖,但沒有 sourceContent
,此方法將傳回一個空字串。
error.showSourceCode() //=> " 4 | }
// 5 | a {
// > 6 | bad
// | ^
// 7 | }
// 8 | b {"
引數 | 類型 | 說明 |
---|---|---|
color | 布林值 | 箭頭是否會被終端機色彩碼標示為紅色。預設情況下,PostCSS 會透過 process.stdout.isTTY 和 process.env.NODE_DISABLE_COLORS 偵測色彩支援。 |
傳回 字串
。
css 語法錯誤#來源
有問題檔案的原始碼。
error.source //=> 'a { b {} }'
error.input.source //=> 'a b { }'
類型:字串。
css 語法錯誤#堆疊
類型:字串。
css 語法錯誤#toString()
傳回錯誤位置、訊息和有問題部分的原始碼。
error.toString() //=> "CssSyntaxError: app.css:1:1: Unclosed block
// > 1 | a {
// | ^"
傳回 字串
。
宣告
宣告#after()
將新節點插入至目前的節點之後,並加入至目前的節點父節點中。
僅為 node.parent.insertAfter(node, add)
的別名。
decl.after('color: black')
引數 | 類型 | 說明 |
---|---|---|
newNode | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 Declaration_
。
宣告#assign()
將屬性指定給現有的節點實例。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引數 | 類型 | 說明 |
---|---|---|
overrides | object | DeclarationProps | 用於覆寫節點的新屬性。 |
傳回 Declaration_
。
宣告#before()
將新節點插入至目前的節點之前,並加入至目前的節點父節點中。
僅為 node.parent.insertBefore(node, add)
的別名。
decl.before('content: ""')
引數 | 類型 | 說明 |
---|---|---|
newNode | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 Declaration_
。
宣告#cleanRaws()
清除節點及其子節點的程式碼樣式屬性。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引數 | 類型 | 說明 |
---|---|---|
keepBetween | 布林值 | 保留 raws.between 符號。 |
宣告#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
。
宣告#cloneAfter()
複製節點的捷徑,並將複製後的節點插入至目前的節點之後。
引數 | 類型 | 說明 |
---|---|---|
overrides | Partial<DeclarationProps> | 在複製中覆寫的新屬性。 |
傳回 Declaration
。
宣告#cloneBefore()
複製節點的捷徑,並將複製後的節點插入至目前的節點之前。
decl.cloneBefore({ prop: '-moz-' + decl.prop })
引數 | 類型 | 說明 |
---|---|---|
overrides | Partial<DeclarationProps> | 在複製中覆寫的新屬性。 |
傳回 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
}
引數 | 類型 | 說明 |
---|---|---|
訊息 | 字串 | 錯誤執行個體的描述。 |
options | NodeErrorOptions | 錯誤執行個體的選項。 |
傳回 CssSyntaxError_
。
宣告#important
它代表宣告的特殊性。
如果為 true,CSS 宣告將會有 important 規範。
const root = postcss.parse('a { color: black !important; color: red }')
root.first.first.important //=> true
root.first.last.important //=> undefined
類型:布林值。
宣告#next()
傳回節點父層的下一層子節點。如果目前節點為最後一個子節點,則傳回 undefined
。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
傳回 子節點。
宣告#parent
代表目前節點的父層。
root.nodes[0].parent === root //=> true
類型:Container_<子節點>。
宣告#positionBy()
取得節點內部字詞或索引的位置。
引數 | 類型 | 說明 |
---|---|---|
選項 | Pick<警告選項, "index" | "word"> | 選項。 |
傳回 位置。
宣告#positionInside()
將字串索引轉換為行/欄。
引數 | 類型 | 說明 |
---|---|---|
index | 數字 | 節點字串中的符號編號。 |
傳回 位置。
宣告#prev()
傳回節點父層的前一個子節點。如果目前節點為第一個子節點,則傳回 undefined
。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
傳回 子節點。
宣告#prop
CSS 宣告的屬性名稱。
const root = postcss.parse('a { color: black }')
const decl = root.first.first
decl.prop //=> 'color'
類型:字串。
宣告#rangeBy()
取得節點內單字或開始和結束索引的範圍。開始索引包含在內;結束索引則不包含在內。
引數 | 類型 | 說明 |
---|---|---|
選項 | 選取<WarningOptions, "index" | "word" | "endIndex"> | 選項。 |
傳回 Range
。
宣告#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') //=> ' '
引數 | 類型 | 說明 |
---|---|---|
屬性 | 字串 | 程式碼樣式屬性的名稱。 |
defaultType | 字串 | 預設值的名稱,如果值與 prop 相同,則可以省略。 |
傳回 字串
。
宣告#raws
它代表 css 原始程式碼中不必要的空白和字元。
產生與原始輸入中位元組對位元組相等的節點字串資訊。
raws 物件的屬性由剖析器決定,預設剖析器使用下列屬性
before
:節點前的空白符號。它也儲存宣告前的*
和_
符號(IE hack)。after
:節點最後一個子節點到節點結尾的空白符號。between
:宣告中屬性和值、規則中選擇器和{
或 at 規則中最後一個參數和{
之間的符號。semicolon
:如果最後一個子節點有(可選的)分號,則包含 true。afterName
:at 規則名稱和其參數之間的空白。left
:/*
和註解文字之間的空白符號。right
:註解文字和 */ 之間的空白符號。
important
:重要聲明的內容,如果它不只是!important
。
PostCSS 會過濾掉選擇器、宣告值和 at 規則參數中的註解,但會將原始內容儲存在 raws 中。
const root = postcss.parse('a {\n color:black\n}')
root.first.first.raws //=> { before: '\n ', between: ':' }
類型:DeclarationRaws。
宣告#remove()
它會從父節點移除節點並刪除其父屬性。
if (decl.prop.match(/^-webkit-/)) {
decl.remove()
}
傳回 Declaration_
。
宣告#replaceWith()
在目前節點之前插入節點並移除目前節點。
AtRule: {
mixin: atrule => {
atrule.replaceWith(mixinRules[atrule.params])
}
}
引數 | 類型 | 說明 |
---|---|---|
nodes… | (ChildNode | ChildProps | ChildNode[] | ChildProps[])[] | 取代目前節點的模式。 |
傳回 Declaration_
。
宣告#root()
尋找節點樹的 Root 實例。
root.nodes[0].nodes[0].root() === root
傳回 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)
}
類型:來源。
宣告#toJSON()
修正 JSON.stringify()
上的循環連結。
傳回 物件
。
宣告#toString()
它會根據類型將節點編譯成瀏覽器可讀的層疊樣式表字串。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引數 | 類型 | 說明 |
---|---|---|
字串化器 | 字串化器 | 語法<Document_ | 根> | 字串產生中要使用的語法。 |
傳回 字串
。
宣告#type
它表示抽象語法樹中節點的類型。
節點類型有助於識別節點,並根據其類型執行操作。
const declaration = new Declaration({
prop: 'color',
value: 'black'
})
declaration.type //=> 'decl'
類型:「decl」。
宣告#value
CSS 宣告的屬性值。
值字串中的任何 CSS 註解都會被濾除。原始值中存在的 CSS 註解會在 raws
屬性中提供。
指定新的 value
會在將節點編譯成字串時忽略 raws
屬性中的註解。
const root = postcss.parse('a { color: black }')
const decl = root.first.first
decl.value //=> 'black'
類型:字串。
宣告#variable
它表示一個 getter,如果宣告以 --
或 $
開頭,則會傳回 true
,這些符號用於在 CSS 和 SASS/SCSS 中宣告變數。
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
類型:布林值。
宣告#warn()
它是 Result#warn 的包裝器,提供產生警告的便利方式。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引數 | 類型 | 說明 |
---|---|---|
結果 | 結果<Document_ | 根> | 將接收警告的Result 實例。 |
訊息 | 字串 | 警告的說明。 |
options | WarningOptions | 警告的選項。 |
傳回Warning
。
文件
文件#after()
將新節點插入至目前的節點之後,並加入至目前的節點父節點中。
僅為 node.parent.insertAfter(node, add)
的別名。
decl.after('color: black')
引數 | 類型 | 說明 |
---|---|---|
newNode | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 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… | (字串 | 字串[] | 節點 | 子節點屬性 | 子節點屬性[] | 節點[])[] | 新節點。 |
傳回 Document_
。
文件#assign()
將屬性指定給現有的節點實例。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引數 | 類型 | 說明 |
---|---|---|
overrides | 物件 | DocumentProps | 用於覆寫節點的新屬性。 |
傳回 Document_
。
文件#before()
將新節點插入至目前的節點之前,並加入至目前的節點父節點中。
僅為 node.parent.insertBefore(node, add)
的別名。
decl.before('content: ""')
引數 | 類型 | 說明 |
---|---|---|
newNode | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 Document_
。
文件#cleanRaws()
清除節點及其子節點的程式碼樣式屬性。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引數 | 類型 | 說明 |
---|---|---|
keepBetween | 布林值 | 保留 raws.between 符號。 |
文件#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> | 在複製中覆寫的新屬性。 |
傳回 文件
。
文件#cloneAfter()
複製節點的捷徑,並將複製後的節點插入至目前的節點之後。
引數 | 類型 | 說明 |
---|---|---|
overrides | Partial<DocumentProps> | 在複製中覆寫的新屬性。 |
傳回 文件
。
文件#cloneBefore()
複製節點的捷徑,並將複製後的節點插入至目前的節點之前。
decl.cloneBefore({ prop: '-moz-' + decl.prop })
引數 | 類型 | 說明 |
---|---|---|
overrides | Partial<DocumentProps> | 在複製中覆寫的新屬性。 |
傳回 文件
。
文件#each()
遍歷容器的直接子節點,並為每個子節點呼叫 callback
。
在 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 | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
文件#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
}
引數 | 類型 | 說明 |
---|---|---|
訊息 | 字串 | 錯誤執行個體的描述。 |
options | NodeErrorOptions | 錯誤執行個體的選項。 |
傳回 CssSyntaxError_
。
文件#every()
如果 callback 對容器的所有子節點傳回 true
,則傳回 true
。
const noPrefixes = rule.every(i => i.prop[0] !== '-')
引數 | 類型 | 說明 |
---|---|---|
condition | 函數 | 迭代器傳回 true 或 false。 |
傳回 boolean
。
文件#index()
傳回 child
在 Container#nodes
陣列中的索引。
rule.index( rule.nodes[2] ) //=> 2
引數 | 類型 | 說明 |
---|---|---|
child | 數字 | Root | 目前容器的子節點。 |
傳回 數字
。
文件#insertAfter()
在容器中將新節點插入舊節點之後。
引數 | 類型 | 說明 |
---|---|---|
oldNode | 數字 | Root | 子節點或子節點索引。 |
newNode | 字串 | 字串陣列 | ChildProps | ChildProps 陣列 | Root | Root 陣列 | 新節點。 |
傳回 Document_
。
文件#insertBefore()
在容器中插入新的節點,位於舊節點之前。
rule.insertBefore(decl, decl.clone({ prop: '-webkit-' + decl.prop }))
引數 | 類型 | 說明 |
---|---|---|
oldNode | 數字 | Root | 子節點或子節點索引。 |
newNode | 字串 | 字串陣列 | ChildProps | ChildProps 陣列 | Root | Root 陣列 | 新節點。 |
傳回 Document_
。
文件#next()
傳回節點父層的下一層子節點。如果目前節點為最後一個子節點,則傳回 undefined
。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
傳回 子節點。
文件#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[].
文件#parent
代表目前節點的父層。
root.nodes[0].parent === root //=> true
類型:未定義。
文件#positionBy()
取得節點內部字詞或索引的位置。
引數 | 類型 | 說明 |
---|---|---|
選項 | Pick<警告選項, "index" | "word"> | 選項。 |
傳回 位置。
文件#positionInside()
將字串索引轉換為行/欄。
引數 | 類型 | 說明 |
---|---|---|
index | 數字 | 節點字串中的符號編號。 |
傳回 位置。
文件#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… | (字串 | 字串[] | 節點 | 子節點屬性 | 子節點屬性[] | 節點[])[] | 新節點。 |
傳回 Document_
。
文件#prev()
傳回節點父層的前一個子節點。如果目前節點為第一個子節點,則傳回 undefined
。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
傳回 子節點。
文件#push()
將子節點新增至節點尾端。
rule.push(new Declaration({ prop: 'color', value: 'black' }))
引數 | 類型 | 說明 |
---|---|---|
child | Root | 新節點。 |
傳回 Document_
。
文件#rangeBy()
取得節點內單字或開始和結束索引的範圍。開始索引包含在內;結束索引則不包含在內。
引數 | 類型 | 說明 |
---|---|---|
選項 | 選取<WarningOptions, "index" | "word" | "endIndex"> | 選項。 |
傳回 Range
。
文件#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') //=> ' '
引數 | 類型 | 說明 |
---|---|---|
屬性 | 字串 | 程式碼樣式屬性的名稱。 |
defaultType | 字串 | 預設值的名稱,如果值與 prop 相同,則可以省略。 |
傳回 字串
。
文件#raws
它代表 css 原始程式碼中不必要的空白和字元。
產生與原始輸入中位元組對位元組相等的節點字串資訊。
raws 物件的屬性由剖析器決定,預設剖析器使用下列屬性
before
:節點前的空白符號。它也儲存宣告前的*
和_
符號(IE hack)。after
:節點最後一個子節點到節點結尾的空白符號。between
:宣告中屬性和值、規則中選擇器和{
或 at 規則中最後一個參數和{
之間的符號。semicolon
:如果最後一個子節點有(可選的)分號,則包含 true。afterName
:at 規則名稱和其參數之間的空白。left
:/*
和註解文字之間的空白符號。right
:註解文字和 */ 之間的空白符號。
important
:重要聲明的內容,如果它不只是!important
。
PostCSS 會過濾掉選擇器、宣告值和 at 規則參數中的註解,但會將原始內容儲存在 raws 中。
const root = postcss.parse('a {\n color:black\n}')
root.first.first.raws //=> { before: '\n ', between: ':' }
類型:任何。
文件#remove()
它會從父節點移除節點並刪除其父屬性。
if (decl.prop.match(/^-webkit-/)) {
decl.remove()
}
傳回 Document_
。
文件#removeAll()
從容器中移除所有子節點並清除其父屬性。
rule.removeAll()
rule.nodes.length //=> 0
傳回 Document_
。
文件#removeChild()
從容器中移除節點並清除節點及其子節點的父屬性。
rule.nodes.length //=> 5
rule.removeChild(decl)
rule.nodes.length //=> 4
decl.parent //=> undefined
引數 | 類型 | 說明 |
---|---|---|
child | 數字 | Root | 子節點或子節點索引。 |
傳回 Document_
。
文件#replaceValues()
引數 | 類型 |
---|---|
模式 | 字串 | RegExp |
替換 | 字串 | 函式 |
引數 | 類型 | 說明 |
---|---|---|
模式 | 字串 | RegExp | 替換模式。 |
options | ValueOptions | |
替換 | 字串 | 函式 |
傳回 Document_
。
文件#replaceWith()
在目前節點之前插入節點並移除目前節點。
AtRule: {
mixin: atrule => {
atrule.replaceWith(mixinRules[atrule.params])
}
}
引數 | 類型 | 說明 |
---|---|---|
nodes… | (ChildNode | ChildProps | ChildNode[] | ChildProps[])[] | 取代目前節點的模式。 |
傳回 Document_
。
文件#root()
尋找節點樹的 Root 實例。
root.nodes[0].nodes[0].root() === root
傳回 Root
。
文件#some()
如果回呼函式對容器的其中一個(至少一個)子節點傳回 true
,則傳回 true
。
const hasPrefix = rule.some(i => i.prop[0] === '-')
引數 | 類型 | 說明 |
---|---|---|
condition | 函數 | 迭代器傳回 true 或 false。 |
傳回 boolean
。
文件#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)
}
類型:來源。
文件#toJSON()
修正 JSON.stringify()
上的循環連結。
傳回 物件
。
文件#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> |
文件#toString()
它會根據類型將節點編譯成瀏覽器可讀的層疊樣式表字串。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引數 | 類型 | 說明 |
---|---|---|
字串化器 | 字串化器 | 語法<Document_ | 根> | 字串產生中要使用的語法。 |
傳回 字串
。
文件#type
它表示抽象語法樹中節點的類型。
節點類型有助於識別節點,並根據其類型執行操作。
const declaration = new Declaration({
prop: 'color',
value: 'black'
})
declaration.type //=> 'decl'
類型:"document".
文件#walk()
遍歷容器的後代節點,並針對每個節點呼叫回呼函式。
與 container.each() 一樣,如果您在反覆運算期間變異陣列,則可以使用這個方法。
如果您只需要反覆運算容器的直接子節點,請使用 Container#each
。
root.walk(node => {
// Traverses all descendant nodes.
})
引數 | 類型 | 說明 |
---|---|---|
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
文件#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 | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
文件#walkComments()
引數 | 類型 |
---|---|
callback | 函數 |
傳回 void | false
。
文件#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 | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
文件#walkRules()
遍歷容器的後代節點,針對每個規則節點呼叫回呼。
如果您傳遞篩選器,則迭代只會發生在具有匹配選擇器的規則上。
與 Container#each
一樣,如果您在反覆運算期間變異陣列,則可以使用這個方法。
const selectors = []
root.walkRules(rule => {
selectors.push(rule.selector)
})
console.log(`Your CSS uses ${ selectors.length } selectors`)
引數 | 類型 | 說明 |
---|---|---|
selectorFilter | 字串 | RegExp | |
callback | 函數 | 迭代器接收每個節點和索引。 |
傳回 void | false
。
文件#warn()
它是 Result#warn 的包裝器,提供產生警告的便利方式。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引數 | 類型 | 說明 |
---|---|---|
結果 | 結果<Document_ | 根> | 將接收警告的Result 實例。 |
訊息 | 字串 | 警告的說明。 |
options | WarningOptions | 警告的選項。 |
傳回Warning
。
fromJSON
引數 | 類型 |
---|---|
資料 | 物件 |
引數 | 類型 |
---|---|
資料 | 物件陣列 |
傳回 節點
。
輸入
輸入#css
輸入 CSS 來源。
const input = postcss.parse('a{}', { from: file }).input
input.css //=> "a{}"
類型:字串。
輸入#error()
引數 | 類型 |
---|---|
訊息 | 字串 |
開始 | 物件 | 物件 |
結束 | 物件 | 物件 |
選項 | 物件 |
引數 | 類型 |
---|---|
訊息 | 字串 |
列 | 數字 |
欄 | 數字 |
選項 | 物件 |
引數 | 類型 |
---|---|
訊息 | 字串 |
偏移 | 數字 |
選項 | 物件 |
傳回 CssSyntaxError_
。
輸入#file
使用 from
選項定義的 CSS 來源檔案的絕對路徑。
const root = postcss.parse(css, { from: 'a.css' })
root.source.input.file //=> '/home/ai/a.css'
類型:字串。
輸入#fromOffset()
將來源偏移轉換為行和欄。
引數 | 類型 | 說明 |
---|---|---|
偏移 | 數字 | 來源偏移。 |
傳回 物件
。
輸入#hasBOM
旗標用於指示來源程式碼是否有 Unicode BOM。
類型:布林值。
輸入#id
CSS 來源的唯一 ID。如果未提供 from
選項,將會建立此 ID(因為 PostCSS 不知道檔案路徑)。
const root = postcss.parse(css)
root.source.input.file //=> undefined
root.source.input.id //=> "<input css 8LZeVF>"
類型:字串。
輸入#map
從 PostCSS 之前的編譯步驟傳遞的輸入來源地圖(例如,來自 Sass 編譯器)。
root.source.input.map.consumer().sources //=> ['a.sass']
類型:PreviousMap_。
輸入#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 }
引數 | 類型 | 說明 |
---|---|---|
列 | 數字 | 輸入 CSS 中包含開始位置的行。 |
欄 | 數字 | 輸入 CSS 中包含開始位置的欄。 |
結束列 | 數字 | 輸入 CSS 中不包含結束位置的行。 |
結束欄 | 數字 | 輸入 CSS 中不包含結束位置的欄。 |
傳回 false | 檔案位置
。
lazy-result
lazy-result#async()
以非同步方式執行外掛程式,並傳回 Result
。
傳回 Promise<Result<RootNode>>
。
lazy-result#catch
類型:函式。
lazy-result#finally
類型:函式。
lazy-result#sync()
以同步方式執行外掛程式,並傳回 Result
。
傳回 Result<RootNode>
。
lazy-result#then
類型:函式。
lazy-result#toString()
LazyResult#css
屬性的別名。
lazy + '' === lazy.css
傳回 字串
。
lazy-result#warnings()
透過同步外掛程式處理輸入的 CSS,並呼叫 Result#warnings
。
傳回 Warning[]
。
no-work-result
no-work-result#async()
以非同步方式執行外掛程式,並傳回 Result
。
no-work-result#catch
類型:函式。
no-work-result#finally
類型:函式。
no-work-result#sync()
以同步方式執行外掛程式,並傳回 Result
。
no-work-result#then
類型:函式。
no-work-result#toString()
LazyResult#css
屬性的別名。
lazy + '' === lazy.css
傳回 字串
。
no-work-result#warnings()
透過同步外掛程式處理輸入的 CSS,並呼叫 Result#warnings
。
傳回 Warning[]
。
節點
節點#after()
將新節點插入至目前的節點之後,並加入至目前的節點父節點中。
僅為 node.parent.insertAfter(node, add)
的別名。
decl.after('color: black')
引數 | 類型 | 說明 |
---|---|---|
newNode | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 節點
。
節點#assign()
將屬性指定給現有的節點實例。
decl.assign({ prop: 'word-wrap', value: 'break-word' })
引數 | 類型 | 說明 |
---|---|---|
overrides | 物件 | 用於覆寫節點的新屬性。 |
傳回 節點
。
節點#before()
將新節點插入至目前的節點之前,並加入至目前的節點父節點中。
僅為 node.parent.insertBefore(node, add)
的別名。
decl.before('content: ""')
引數 | 類型 | 說明 |
---|---|---|
newNode | 字串 | 節點 | 子節點屬性 | 節點[] | 新節點。 |
傳回 節點
。
節點#cleanRaws()
清除節點及其子節點的程式碼樣式屬性。
node.raws.before //=> ' '
node.cleanRaws()
node.raws.before //=> undefined
引數 | 類型 | 說明 |
---|---|---|
keepBetween | 布林值 | 保留 raws.between 符號。 |
節點#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 | 物件 | 在複製中覆寫的新屬性。 |
傳回 節點
。
節點#cloneAfter()
複製節點的捷徑,並將複製後的節點插入至目前的節點之後。
引數 | 類型 | 說明 |
---|---|---|
overrides | 物件 | 在複製中覆寫的新屬性。 |
傳回 節點
。
節點#cloneBefore()
複製節點的捷徑,並將複製後的節點插入至目前的節點之前。
decl.cloneBefore({ prop: '-moz-' + decl.prop })
引數 | 類型 | 說明 |
---|---|---|
overrides | 物件 | 在複製中覆寫的新屬性。 |
傳回 節點
。
節點#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
}
引數 | 類型 | 說明 |
---|---|---|
訊息 | 字串 | 錯誤執行個體的描述。 |
options | NodeErrorOptions | 錯誤執行個體的選項。 |
傳回 CssSyntaxError_
。
節點#next()
傳回節點父層的下一層子節點。如果目前節點為最後一個子節點,則傳回 undefined
。
if (comment.text === 'delete next') {
const next = comment.next()
if (next) {
next.remove()
}
}
傳回 子節點。
節點#parent
代表目前節點的父層。
root.nodes[0].parent === root //=> true
類型:Container_<ChildNode> | Document_。
節點#positionBy()
取得節點內部字詞或索引的位置。
引數 | 類型 | 說明 |
---|---|---|
選項 | Pick<警告選項, "index" | "word"> | 選項。 |
傳回 位置。
節點#positionInside()
將字串索引轉換為行/欄。
引數 | 類型 | 說明 |
---|---|---|
index | 數字 | 節點字串中的符號編號。 |
傳回 位置。
節點#prev()
傳回節點父層的前一個子節點。如果目前節點為第一個子節點,則傳回 undefined
。
const annotation = decl.prev()
if (annotation.type === 'comment') {
readAnnotation(annotation.text)
}
傳回 子節點。
節點#rangeBy()
取得節點內單字或開始和結束索引的範圍。開始索引包含在內;結束索引則不包含在內。
引數 | 類型 | 說明 |
---|---|---|
選項 | 選取<WarningOptions, "index" | "word" | "endIndex"> | 選項。 |
傳回 Range
。
節點#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') //=> ' '
引數 | 類型 | 說明 |
---|---|---|
屬性 | 字串 | 程式碼樣式屬性的名稱。 |
defaultType | 字串 | 預設值的名稱,如果值與 prop 相同,則可以省略。 |
傳回 字串
。
節點#raws
它代表 css 原始程式碼中不必要的空白和字元。
產生與原始輸入中位元組對位元組相等的節點字串資訊。
raws 物件的屬性由剖析器決定,預設剖析器使用下列屬性
before
:節點前的空白符號。它也儲存宣告前的*
和_
符號(IE hack)。after
:節點最後一個子節點到節點結尾的空白符號。between
:宣告中屬性和值、規則中選擇器和{
或 at 規則中最後一個參數和{
之間的符號。semicolon
:如果最後一個子節點有(可選的)分號,則包含 true。afterName
:at 規則名稱和其參數之間的空白。left
:/*
和註解文字之間的空白符號。right
:註解文字和 */ 之間的空白符號。
important
:重要聲明的內容,如果它不只是!important
。
PostCSS 會過濾掉選擇器、宣告值和 at 規則參數中的註解,但會將原始內容儲存在 raws 中。
const root = postcss.parse('a {\n color:black\n}')
root.first.first.raws //=> { before: '\n ', between: ':' }
類型:任何。
節點#remove()
它會從父節點移除節點並刪除其父屬性。
if (decl.prop.match(/^-webkit-/)) {
decl.remove()
}
傳回 節點
。
節點#replaceWith()
在目前節點之前插入節點並移除目前節點。
AtRule: {
mixin: atrule => {
atrule.replaceWith(mixinRules[atrule.params])
}
}
引數 | 類型 | 說明 |
---|---|---|
nodes… | (ChildNode | ChildProps | ChildNode[] | ChildProps[])[] | 取代目前節點的模式。 |
傳回 節點
。
節點#root()
尋找節點樹的 Root 實例。
root.nodes[0].nodes[0].root() === root
傳回 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)
}
類型:來源。
節點#toJSON()
修正 JSON.stringify()
上的循環連結。
傳回 物件
。
節點#toString()
它會根據類型將節點編譯成瀏覽器可讀的層疊樣式表字串。
new Rule({ selector: 'a' }).toString() //=> "a {}"
引數 | 類型 | 說明 |
---|---|---|
字串化器 | 字串化器 | 語法<Document_ | 根> | 字串產生中要使用的語法。 |
傳回 字串
。
節點#type
它表示抽象語法樹中節點的類型。
節點類型有助於識別節點,並根據其類型執行操作。
const declaration = new Declaration({
prop: 'color',
value: 'black'
})
declaration.type //=> 'decl'
類型:字串。
節點#warn()
它是 Result#warn 的包裝器,提供產生警告的便利方式。
Declaration: {
bad: (decl, { result }) => {
decl.warn(result, 'Deprecated property: bad')
}
}
引數 | 類型 | 說明 |
---|---|---|
結果 | 結果<Document_ | 根> | 將接收警告的Result 實例。 |
訊息 | 字串 | 警告的說明。 |
options | WarningOptions | 警告的選項。 |
傳回Warning
。
previous-map
previous-map#annotation
sourceMappingURL
內容。
類型:字串。
previous-map#consumer()
從 source-map
函式庫建立 SourceMapGenerator
類別的執行個體,以處理原始碼資訊。
這是延遲方法,因此它只會在第一次呼叫時建立物件,然後會使用快取。
傳回 SourceMapConsumer
。
previous-map#file
CSS 原始碼識別碼。如果使用者設定 from
選項,則包含 輸入#檔案
;如果使用者未設定,則包含 輸入#id
。
類型:字串。
previous-map#inline
原始碼資訊是否透過 data-uri 內嵌到輸入 CSS。
類型:布林值。
previous-map#mapFile
來源地圖檔案路徑。
類型:字串。
previous-map#root
如果來源地圖在獨立檔案中,則為來源地圖檔案的目錄。
類型:字串。
previous-map#text
來源地圖檔案內容。
類型:字串。
previous-map#withContent()
來源地圖是否包含具有輸入來源文字的 sourcesContent
。
傳回 boolean
。
字串化器
字串化器#atrule()
引數 | 類型 |
---|---|
節點 | AtRule_ |
semicolon | 布林值 |
字串化器#beforeAfter()
引數 | 類型 |
---|---|
節點 | AnyNode |
detect | "after" | "before" |
傳回 字串
。
字串化器#block()
引數 | 類型 |
---|---|
節點 | AnyNode |
開始 | 字串 |
字串化器#body()
引數 | 類型 |
---|---|
節點 | Container_<ChildNode> |
字串化器#builder
類型:Builder。
字串化器#comment()
引數 | 類型 |
---|---|
節點 | Comment_ |
字串化器#decl()
引數 | 類型 |
---|---|
節點 | Declaration_ |
semicolon | 布林值 |
字串化器#document()
引數 | 類型 |
---|---|
節點 | Document_ |
字串化器#raw()
引數 | 類型 |
---|---|
節點 | AnyNode |
own | 字串 |
detect | 字串 |
傳回 字串
。
字串化器#rawBeforeClose()
引數 | 類型 |
---|---|
根節點 | Root |
傳回 字串
。
字串化器#rawBeforeComment()
引數 | 類型 |
---|---|
根節點 | Root |
節點 | Comment_ |
傳回 字串
。
字串化器#rawBeforeDecl()
引數 | 類型 |
---|---|
根節點 | Root |
節點 | Declaration_ |
傳回 字串
。
字串化器#rawBeforeOpen()
引數 | 類型 |
---|---|
根節點 | Root |
傳回 字串
。
字串化器#rawBeforeRule()
引數 | 類型 |
---|---|
根節點 | Root |
傳回 字串
。
字串化器#rawColon()
引數 | 類型 |
---|---|
根節點 | Root |
傳回 字串
。
字串化器#rawEmptyBody()
引數 | 類型 |
---|---|
根節點 | Root |
傳回 字串
。
字串化器#rawIndent()
引數 | 類型 |
---|---|
根節點 | Root |
傳回 字串
。
字串化器#rawSemicolon()
引數 | 類型 |
---|---|
根節點 | Root |
傳回 boolean
。
字串化器#rawValue()
引數 | 類型 |
---|---|
節點 | AnyNode |
屬性 | 字串 |
傳回 字串
。
字串化器#root()
引數 | 類型 |
---|---|
節點 | Root |
字串化器#rule()
引數 | 類型 |
---|---|
節點 | Rule |
字串化器#stringify()
引數 | 類型 |
---|---|
節點 | AnyNode |
semicolon | 布林值 |