Skip to content

Commit

Permalink
Directly query style props used in parse rules
Browse files Browse the repository at this point in the history
... rather than iterating style items, which may show only normalized
versions of the properties.

FIX: Fix an issue where parse rules for CSS properties that were
shorthands for a number of more detailed properties weren't matching
properly.

Closes ProseMirror/prosemirror#1473
  • Loading branch information
marijnh committed Jun 26, 2024
1 parent 68c3cd5 commit 1f0c6ed
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/from_dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ export class DOMParser {
/// @internal
styles: StyleParseRule[] = []
/// @internal
matchedStyles: readonly string[]
/// @internal
normalizeLists: boolean

/// Create a parser that targets the given schema, using the given
Expand All @@ -192,9 +194,15 @@ export class DOMParser {
/// uses, in order of precedence.
readonly rules: readonly ParseRule[]
) {
let matchedStyles: string[] = this.matchedStyles = []
rules.forEach(rule => {
if (isTagRule(rule)) this.tags.push(rule)
else if (isStyleRule(rule)) this.styles.push(rule)
if (isTagRule(rule)) {
this.tags.push(rule)
} else if (isStyleRule(rule)) {
let prop = /[^=]*/.exec(rule.style)![0]
if (matchedStyles.indexOf(prop) < 0) matchedStyles.push(prop)
this.styles.push(rule)
}
})

// Only normalize list elements when lists in the schema can't directly contain themselves
Expand Down Expand Up @@ -542,10 +550,15 @@ class ParseContext {
// had a rule with `ignore` set.
readStyles(styles: CSSStyleDeclaration) {
let add = Mark.none, remove = Mark.none
for (let i = 0, l = styles.length; i < l; i++) {
let name = styles.item(i)
for (let after: StyleParseRule | undefined = undefined;;) {
let rule = this.parser.matchStyle(name, styles.getPropertyValue(name), this, after)
// Because many properties will only show up in 'normalized' form
// in `style.item` (i.e. text-decoration becomes
// text-decoration-line, text-decoration-color, etc), we directly
// query the styles mentioned in our rules instead of iterating
// over the items.
if (styles.length) for (let i = 0; i < this.parser.matchedStyles.length; i++) {
let name = this.parser.matchedStyles[i], value = styles.getPropertyValue(name)
if (value) for (let after: StyleParseRule | undefined = undefined;;) {
let rule = this.parser.matchStyle(name, value, this, after)
if (!rule) break
if (rule.ignore) return null
if (rule.clearMark) {
Expand Down

0 comments on commit 1f0c6ed

Please sign in to comment.