-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleanupIds: add support for changing ids in <style> elements (#17)
* Add support for changing ids in <style> elements
- Loading branch information
1 parent
e147d9d
commit 7a3ac84
Showing
35 changed files
with
1,139 additions
and
478 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* @typedef {import('./types.js').XastElement} XastElement | ||
* @typedef {XastElement&{style?:string,declarations?:import('./types.js').CSSDeclarationMap}} XastElementExtended | ||
*/ | ||
|
||
import { _parseStyleDeclarations } from './style-css-tree-tools.js'; | ||
|
||
const REGEX_FN = /url\([^#]/; | ||
|
||
/** | ||
* @param {XastElementExtended} element | ||
* @returns {import('./types.js').CSSDeclarationMap|undefined} | ||
*/ | ||
export function getStyleDeclarations(element) { | ||
const style = element.attributes.style; | ||
if (style === undefined || style === '') { | ||
return; | ||
} | ||
if (element.style !== style) { | ||
element.style = style; | ||
element.declarations = parseStyleDeclarations(style); | ||
} | ||
// Copy cached map in case it is changed by caller. | ||
return new Map(element.declarations); | ||
} | ||
|
||
/** | ||
* @param {string} css | ||
*/ | ||
export function _isStyleComplex(css) { | ||
return REGEX_FN.test(css); | ||
} | ||
|
||
/** | ||
* @param {string|undefined} css | ||
* @returns {Map<string,import('./types.js').CSSPropertyValue>} | ||
*/ | ||
export function parseStyleDeclarations(css) { | ||
/** @type {Map<string,import('./types.js').CSSPropertyValue>} */ | ||
const declarations = new Map(); | ||
if (css === undefined) { | ||
return declarations; | ||
} | ||
|
||
if (_isStyleComplex(css)) { | ||
// There's a function in the declaration; use the less efficient low-level implementation. | ||
return _parseStyleDeclarations(css); | ||
} | ||
|
||
const decList = css.split(';'); | ||
for (const declaration of decList) { | ||
if (declaration) { | ||
const pv = declaration.split(':'); | ||
if (pv.length === 2) { | ||
const dec = pv[1].trim(); | ||
const value = dec.endsWith('!important') | ||
? { value: dec.substring(0, dec.length - 10).trim(), important: true } | ||
: { value: dec, important: false }; | ||
declarations.set(pv[0].trim(), value); | ||
} | ||
} | ||
} | ||
return declarations; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.