prosemirror-model
Advanced tools
Comparing version 1.19.4 to 1.20.0
@@ -0,1 +1,7 @@ | ||
## 1.20.0 (2024-04-08) | ||
### New features | ||
The `ParseRule` type is now a union of `TagParseRule` and `StyleParseRule`, with more specific types being used when appropriate. | ||
## 1.19.4 (2023-12-11) | ||
@@ -2,0 +8,0 @@ |
@@ -508,29 +508,7 @@ import OrderedMap from 'orderedmap'; | ||
/** | ||
A value that describes how to parse a given DOM node or inline | ||
style as a ProseMirror node or mark. | ||
Fields that may be present in both [tag](https://prosemirror.net/docs/ref/#model.TagParseRule) and | ||
[style](https://prosemirror.net/docs/ref/#model.StyleParseRule) parse rules. | ||
*/ | ||
interface ParseRule { | ||
interface GenericParseRule { | ||
/** | ||
A CSS selector describing the kind of DOM elements to match. A | ||
single rule should have _either_ a `tag` or a `style` property. | ||
*/ | ||
tag?: string; | ||
/** | ||
The namespace to match. This should be used with `tag`. | ||
Nodes are only matched when the namespace matches or this property | ||
is null. | ||
*/ | ||
namespace?: string; | ||
/** | ||
A CSS property name to match. When given, this rule matches | ||
inline styles that list that property. May also have the form | ||
`"property=value"`, in which case the rule only matches if the | ||
property's value exactly matches the given value. (For more | ||
complicated filters, use [`getAttrs`](https://prosemirror.net/docs/ref/#model.ParseRule.getAttrs) | ||
and return false to indicate that the match failed.) Rules | ||
matching styles may only produce [marks](https://prosemirror.net/docs/ref/#model.ParseRule.mark), | ||
not nodes. | ||
*/ | ||
style?: string; | ||
/** | ||
Can be used to change the order in which the parse rules in a | ||
@@ -565,12 +543,2 @@ schema are tried. Those with higher priority come first. Rules | ||
/** | ||
The name of the node type to create when this rule matches. Only | ||
valid for rules with a `tag` property, not for style rules. Each | ||
rule should have one of a `node`, `mark`, `clearMark`, or | ||
`ignore` property (except when it appears in a | ||
[node](https://prosemirror.net/docs/ref/#model.NodeSpec.parseDOM) or [mark | ||
spec](https://prosemirror.net/docs/ref/#model.MarkSpec.parseDOM), in which case the `node` or | ||
`mark` property will be derived from its position). | ||
*/ | ||
node?: string; | ||
/** | ||
The name of the mark type to wrap the matched content in. | ||
@@ -580,7 +548,2 @@ */ | ||
/** | ||
[Style](https://prosemirror.net/docs/ref/#model.ParseRule.style) rules can remove marks from the | ||
set of active marks. | ||
*/ | ||
clearMark?: (mark: Mark) => boolean; | ||
/** | ||
When true, ignore content that matches this rule. | ||
@@ -604,3 +567,25 @@ */ | ||
attrs?: Attrs; | ||
} | ||
/** | ||
Parse rule targeting a DOM element. | ||
*/ | ||
interface TagParseRule extends GenericParseRule { | ||
/** | ||
A CSS selector describing the kind of DOM elements to match. | ||
*/ | ||
tag: string; | ||
/** | ||
The namespace to match. Nodes are only matched when the | ||
namespace matches or this property is null. | ||
*/ | ||
namespace?: string; | ||
/** | ||
The name of the node type to create when this rule matches. Each | ||
rule should have either a `node`, `mark`, or `ignore` property | ||
(except when it appears in a [node](https://prosemirror.net/docs/ref/#model.NodeSpec.parseDOM) or | ||
[mark spec](https://prosemirror.net/docs/ref/#model.MarkSpec.parseDOM), in which case the `node` | ||
or `mark` property will be derived from its position). | ||
*/ | ||
node?: string; | ||
/** | ||
A function used to compute the attributes for the node or mark | ||
@@ -611,14 +596,11 @@ created by this rule. Can also be used to describe further | ||
that is interpreted as an empty/default set of attributes. | ||
Called with a DOM Element for `tag` rules, and with a string (the | ||
style's value) for `style` rules. | ||
*/ | ||
getAttrs?: (node: HTMLElement | string) => Attrs | false | null; | ||
getAttrs?: (node: HTMLElement) => Attrs | false | null; | ||
/** | ||
For `tag` rules that produce non-leaf nodes or marks, by default | ||
the content of the DOM element is parsed as content of the mark | ||
or node. If the child nodes are in a descendent node, this may be | ||
a CSS selector string that the parser must use to find the actual | ||
content element, or a function that returns the actual content | ||
element to the parser. | ||
For rules that produce non-leaf nodes, by default the content of | ||
the DOM element is parsed as content of the node. If the child | ||
nodes are in a descendent node, this may be a CSS selector | ||
string that the parser must use to find the actual content | ||
element, or a function that returns the actual content element | ||
to the parser. | ||
*/ | ||
@@ -642,2 +624,36 @@ contentElement?: string | HTMLElement | ((node: DOMNode) => HTMLElement); | ||
/** | ||
A parse rule targeting a style property. | ||
*/ | ||
interface StyleParseRule extends GenericParseRule { | ||
/** | ||
A CSS property name to match. This rule will match inline styles | ||
that list that property. May also have the form | ||
`"property=value"`, in which case the rule only matches if the | ||
property's value exactly matches the given value. (For more | ||
complicated filters, use [`getAttrs`](https://prosemirror.net/docs/ref/#model.ParseRule.getAttrs) | ||
and return false to indicate that the match failed.) Rules | ||
matching styles may only produce [marks](https://prosemirror.net/docs/ref/#model.ParseRule.mark), | ||
not nodes. | ||
*/ | ||
style: string; | ||
/** | ||
Given to make TS see ParseRule as a tagged union @hide | ||
*/ | ||
tag?: undefined; | ||
/** | ||
Style rules can remove marks from the set of active marks. | ||
*/ | ||
clearMark?: (mark: Mark) => boolean; | ||
/** | ||
A function used to compute the attributes for the node or mark | ||
created by this rule. Called with the style's value. | ||
*/ | ||
getAttrs?: (node: string) => Attrs | false | null; | ||
} | ||
/** | ||
A value that describes how to parse a given DOM node or inline | ||
style as a ProseMirror node or mark. | ||
*/ | ||
type ParseRule = TagParseRule | StyleParseRule; | ||
/** | ||
A DOM parser represents a strategy for parsing DOM content into a | ||
@@ -999,3 +1015,3 @@ ProseMirror document conforming to a given schema. Its behavior is | ||
*/ | ||
parseDOM?: readonly ParseRule[]; | ||
parseDOM?: readonly TagParseRule[]; | ||
/** | ||
@@ -1633,2 +1649,2 @@ Defines the default way a node of this type should be serialized | ||
export { type AttributeSpec, type Attrs, ContentMatch, type DOMOutputSpec, DOMParser, DOMSerializer, Fragment, Mark, type MarkSpec, MarkType, Node, NodeRange, type NodeSpec, NodeType, type ParseOptions, type ParseRule, ReplaceError, ResolvedPos, Schema, type SchemaSpec, Slice }; | ||
export { type AttributeSpec, type Attrs, ContentMatch, type DOMOutputSpec, DOMParser, DOMSerializer, Fragment, type GenericParseRule, Mark, type MarkSpec, MarkType, Node, NodeRange, type NodeSpec, NodeType, type ParseOptions, type ParseRule, ReplaceError, ResolvedPos, Schema, type SchemaSpec, Slice, type StyleParseRule, type TagParseRule }; |
{ | ||
"name": "prosemirror-model", | ||
"version": "1.19.4", | ||
"version": "1.20.0", | ||
"description": "ProseMirror's document model", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -48,3 +48,3 @@ import {Fragment} from "./fragment" | ||
/// @internal | ||
ruleFromNode?: (node: DOMNode) => ParseRule | null | ||
ruleFromNode?: (node: DOMNode) => Omit<TagParseRule, "tag"> | null | ||
/// @internal | ||
@@ -54,24 +54,5 @@ topOpen?: boolean | ||
/// A value that describes how to parse a given DOM node or inline | ||
/// style as a ProseMirror node or mark. | ||
export interface ParseRule { | ||
/// A CSS selector describing the kind of DOM elements to match. A | ||
/// single rule should have _either_ a `tag` or a `style` property. | ||
tag?: string | ||
/// The namespace to match. This should be used with `tag`. | ||
/// Nodes are only matched when the namespace matches or this property | ||
/// is null. | ||
namespace?: string | ||
/// A CSS property name to match. When given, this rule matches | ||
/// inline styles that list that property. May also have the form | ||
/// `"property=value"`, in which case the rule only matches if the | ||
/// property's value exactly matches the given value. (For more | ||
/// complicated filters, use [`getAttrs`](#model.ParseRule.getAttrs) | ||
/// and return false to indicate that the match failed.) Rules | ||
/// matching styles may only produce [marks](#model.ParseRule.mark), | ||
/// not nodes. | ||
style?: string | ||
/// Fields that may be present in both [tag](#model.TagParseRule) and | ||
/// [style](#model.StyleParseRule) parse rules. | ||
export interface GenericParseRule { | ||
/// Can be used to change the order in which the parse rules in a | ||
@@ -103,18 +84,5 @@ /// schema are tried. Those with higher priority come first. Rules | ||
/// The name of the node type to create when this rule matches. Only | ||
/// valid for rules with a `tag` property, not for style rules. Each | ||
/// rule should have one of a `node`, `mark`, `clearMark`, or | ||
/// `ignore` property (except when it appears in a | ||
/// [node](#model.NodeSpec.parseDOM) or [mark | ||
/// spec](#model.MarkSpec.parseDOM), in which case the `node` or | ||
/// `mark` property will be derived from its position). | ||
node?: string | ||
/// The name of the mark type to wrap the matched content in. | ||
mark?: string | ||
/// [Style](#model.ParseRule.style) rules can remove marks from the | ||
/// set of active marks. | ||
clearMark?: (mark: Mark) => boolean | ||
/// When true, ignore content that matches this rule. | ||
@@ -134,3 +102,20 @@ ignore?: boolean | ||
attrs?: Attrs | ||
} | ||
/// Parse rule targeting a DOM element. | ||
export interface TagParseRule extends GenericParseRule { | ||
/// A CSS selector describing the kind of DOM elements to match. | ||
tag: string | ||
/// The namespace to match. Nodes are only matched when the | ||
/// namespace matches or this property is null. | ||
namespace?: string | ||
/// The name of the node type to create when this rule matches. Each | ||
/// rule should have either a `node`, `mark`, or `ignore` property | ||
/// (except when it appears in a [node](#model.NodeSpec.parseDOM) or | ||
/// [mark spec](#model.MarkSpec.parseDOM), in which case the `node` | ||
/// or `mark` property will be derived from its position). | ||
node?: string | ||
/// A function used to compute the attributes for the node or mark | ||
@@ -141,13 +126,10 @@ /// created by this rule. Can also be used to describe further | ||
/// that is interpreted as an empty/default set of attributes. | ||
/// | ||
/// Called with a DOM Element for `tag` rules, and with a string (the | ||
/// style's value) for `style` rules. | ||
getAttrs?: (node: HTMLElement | string) => Attrs | false | null | ||
getAttrs?: (node: HTMLElement) => Attrs | false | null | ||
/// For `tag` rules that produce non-leaf nodes or marks, by default | ||
/// the content of the DOM element is parsed as content of the mark | ||
/// or node. If the child nodes are in a descendent node, this may be | ||
/// a CSS selector string that the parser must use to find the actual | ||
/// content element, or a function that returns the actual content | ||
/// element to the parser. | ||
/// For rules that produce non-leaf nodes, by default the content of | ||
/// the DOM element is parsed as content of the node. If the child | ||
/// nodes are in a descendent node, this may be a CSS selector | ||
/// string that the parser must use to find the actual content | ||
/// element, or a function that returns the actual content element | ||
/// to the parser. | ||
contentElement?: string | HTMLElement | ((node: DOMNode) => HTMLElement) | ||
@@ -168,2 +150,32 @@ | ||
/// A parse rule targeting a style property. | ||
export interface StyleParseRule extends GenericParseRule { | ||
/// A CSS property name to match. This rule will match inline styles | ||
/// that list that property. May also have the form | ||
/// `"property=value"`, in which case the rule only matches if the | ||
/// property's value exactly matches the given value. (For more | ||
/// complicated filters, use [`getAttrs`](#model.ParseRule.getAttrs) | ||
/// and return false to indicate that the match failed.) Rules | ||
/// matching styles may only produce [marks](#model.ParseRule.mark), | ||
/// not nodes. | ||
style: string | ||
/// Given to make TS see ParseRule as a tagged union @hide | ||
tag?: undefined | ||
/// Style rules can remove marks from the set of active marks. | ||
clearMark?: (mark: Mark) => boolean | ||
/// A function used to compute the attributes for the node or mark | ||
/// created by this rule. Called with the style's value. | ||
getAttrs?: (node: string) => Attrs | false | null | ||
} | ||
/// A value that describes how to parse a given DOM node or inline | ||
/// style as a ProseMirror node or mark. | ||
export type ParseRule = TagParseRule | StyleParseRule | ||
function isTagRule(rule: ParseRule): rule is TagParseRule { return (rule as TagParseRule).tag != null } | ||
function isStyleRule(rule: ParseRule): rule is StyleParseRule { return (rule as StyleParseRule).style != null } | ||
/// A DOM parser represents a strategy for parsing DOM content into a | ||
@@ -174,5 +186,5 @@ /// ProseMirror document conforming to a given schema. Its behavior is | ||
/// @internal | ||
tags: ParseRule[] = [] | ||
tags: TagParseRule[] = [] | ||
/// @internal | ||
styles: ParseRule[] = [] | ||
styles: StyleParseRule[] = [] | ||
/// @internal | ||
@@ -191,4 +203,4 @@ normalizeLists: boolean | ||
rules.forEach(rule => { | ||
if (rule.tag) this.tags.push(rule) | ||
else if (rule.style) this.styles.push(rule) | ||
if (isTagRule(rule)) this.tags.push(rule) | ||
else if (isStyleRule(rule)) this.styles.push(rule) | ||
}) | ||
@@ -224,3 +236,3 @@ | ||
/// @internal | ||
matchTag(dom: DOMNode, context: ParseContext, after?: ParseRule) { | ||
matchTag(dom: DOMNode, context: ParseContext, after?: TagParseRule) { | ||
for (let i = after ? this.tags.indexOf(after) + 1 : 0; i < this.tags.length; i++) { | ||
@@ -242,3 +254,3 @@ let rule = this.tags[i] | ||
/// @internal | ||
matchStyle(prop: string, value: string, context: ParseContext, after?: ParseRule) { | ||
matchStyle(prop: string, value: string, context: ParseContext, after?: StyleParseRule) { | ||
for (let i = after ? this.styles.indexOf(after) + 1 : 0; i < this.styles.length; i++) { | ||
@@ -278,4 +290,4 @@ let rule = this.styles[i], style = rule.style! | ||
if (rules) rules.forEach(rule => { | ||
insert(rule = copy(rule)) | ||
if (!(rule.mark || rule.ignore || rule.clearMark)) | ||
insert(rule = copy(rule) as ParseRule) | ||
if (!(rule.mark || rule.ignore || (rule as StyleParseRule).clearMark)) | ||
rule.mark = name | ||
@@ -287,4 +299,4 @@ }) | ||
if (rules) rules.forEach(rule => { | ||
insert(rule = copy(rule)) | ||
if (!(rule.node || rule.ignore || rule.mark)) | ||
insert(rule = copy(rule) as TagParseRule) | ||
if (!((rule as TagParseRule).node || rule.ignore || rule.mark)) | ||
rule.node = name | ||
@@ -493,4 +505,4 @@ }) | ||
// none is found, the element's content nodes are added directly. | ||
addElement(dom: HTMLElement, matchAfter?: ParseRule) { | ||
let name = dom.nodeName.toLowerCase(), ruleID: ParseRule | undefined | ||
addElement(dom: HTMLElement, matchAfter?: TagParseRule) { | ||
let name = dom.nodeName.toLowerCase(), ruleID: TagParseRule | undefined | ||
if (listTags.hasOwnProperty(name) && this.parser.normalizeLists) normalizeList(dom) | ||
@@ -523,3 +535,3 @@ let rule = (this.options.ruleFromNode && this.options.ruleFromNode(dom)) || | ||
this.withStyleRules(dom, () => { | ||
this.addElementByRule(dom, rule!, rule!.consuming === false ? ruleID : undefined) | ||
this.addElementByRule(dom, rule as TagParseRule, rule!.consuming === false ? ruleID : undefined) | ||
}) | ||
@@ -569,3 +581,3 @@ } | ||
// the node's content is wrapped, and return true. | ||
addElementByRule(dom: HTMLElement, rule: ParseRule, continueAfter?: ParseRule) { | ||
addElementByRule(dom: HTMLElement, rule: TagParseRule, continueAfter?: TagParseRule) { | ||
let sync, nodeType, mark | ||
@@ -572,0 +584,0 @@ if (rule.node) { |
@@ -10,3 +10,3 @@ export {Node} from "./node" | ||
export {DOMParser, ParseRule, ParseOptions} from "./from_dom" | ||
export {DOMParser, GenericParseRule, TagParseRule, StyleParseRule, ParseRule, ParseOptions} from "./from_dom" | ||
export {DOMSerializer, DOMOutputSpec} from "./to_dom" |
@@ -56,2 +56,5 @@ This module defines ProseMirror's content model, the data structures | ||
@ParseOptions | ||
@GenericParseRule | ||
@TagParseRule | ||
@StyleParseRule | ||
@ParseRule | ||
@@ -58,0 +61,0 @@ |
@@ -8,3 +8,3 @@ import OrderedMap from "orderedmap" | ||
import {DOMOutputSpec} from "./to_dom" | ||
import {ParseRule} from "./from_dom" | ||
import {ParseRule, TagParseRule} from "./from_dom" | ||
@@ -433,3 +433,3 @@ /// An object holding the attributes of a node. | ||
/// parsing rules in your schema. | ||
parseDOM?: readonly ParseRule[] | ||
parseDOM?: readonly TagParseRule[] | ||
@@ -436,0 +436,0 @@ /// Defines the default way a node of this type should be serialized |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
509885
10924