@lezer/html
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -0,1 +1,7 @@ | ||
## 1.2.0 (2022-11-25) | ||
### New features | ||
`configureNesting` now takes a second argument that can be used to specify that the value of some attributes should be parsed with an external parser. | ||
## 1.1.1 (2022-11-18) | ||
@@ -2,0 +8,0 @@ |
@@ -6,6 +6,10 @@ import {LRParser} from "@lezer/lr" | ||
export function configureNesting(tags: readonly { | ||
export function configureNesting(tags?: readonly { | ||
tag: "script" | "style" | "textarea", | ||
attrs?: (attrs: {[attr: string]: string}) => boolean, | ||
parser: Parser | ||
}[], attributes?: { | ||
name: string, | ||
tagName?: string, | ||
parser: Parser | ||
}[]): ParseWrapper |
@@ -24,2 +24,7 @@ import { ContextTracker, ExternalTokenizer, LRParser } from '@lezer/lr'; | ||
Element = 18, | ||
TagName = 20, | ||
Attribute = 21, | ||
AttributeName = 22, | ||
AttributeValue = 24, | ||
UnquotedAttributeValue = 25, | ||
ScriptText = 27, | ||
@@ -254,6 +259,6 @@ StyleText = 30, | ||
let attrs = Object.create(null); | ||
for (let att of element.firstChild.getChildren("Attribute")) { | ||
let name = att.getChild("AttributeName"), value = att.getChild("AttributeValue") || att.getChild("UnquotedAttributeValue"); | ||
for (let att of element.firstChild.getChildren(Attribute)) { | ||
let name = att.getChild(AttributeName), value = att.getChild(AttributeValue) || att.getChild(UnquotedAttributeValue); | ||
if (name) attrs[input.read(name.from, name.to)] = | ||
!value ? "" : value.name == "AttributeValue" ? input.read(value.from + 1, value.to - 1) : input.read(value.from, value.to); | ||
!value ? "" : value.type.id == AttributeValue ? input.read(value.from + 1, value.to - 1) : input.read(value.from, value.to); | ||
} | ||
@@ -272,3 +277,3 @@ return attrs | ||
// tags: { | ||
// tags?: { | ||
// tag: "script" | "style" | "textarea", | ||
@@ -278,4 +283,9 @@ // attrs?: ({[attr: string]: string}) => boolean, | ||
// }[] | ||
// attributes?: { | ||
// name: string, | ||
// tagName?: string, | ||
// parser: Parser | ||
// }[] | ||
function configureNesting(tags) { | ||
function configureNesting(tags = [], attributes = []) { | ||
let script = [], style = [], textarea = []; | ||
@@ -287,2 +297,5 @@ for (let tag of tags) { | ||
} | ||
let attrs = attributes.length ? Object.create(null) : null; | ||
for (let attr of attributes) (attrs[attr.name] || (attrs[attr.name] = [])).push(attr); | ||
return parseMixed((node, input) => { | ||
@@ -293,2 +306,23 @@ let id = node.type.id; | ||
if (id == TextareaText) return maybeNest(node, input, textarea) | ||
if (attrs && id == Attribute) { | ||
let n = node.node, nameNode; | ||
if (nameNode = n.firstChild) { | ||
let matches = attrs[input.read(nameNode.from, nameNode.to)]; | ||
if (matches) for (let attr of matches) { | ||
if (attr.tagName) { | ||
if (!tagName) { | ||
let tagNameNode = n.parent.getChild(TagName); | ||
tagName = tagNameNode ? input.read(tagNameNode.from, tagNameNode.to) : " "; | ||
} | ||
if (attrTagName != tagName) continue | ||
} | ||
let value = n.lastChild; | ||
if (value.type.id == AttributeValue) | ||
return {parser: attr.parser, overlay: [{from: value.from + 1, to: value.to - 1}]} | ||
else if (value.type.id == UnquotedAttributeValue) | ||
return {parser: attr.parser, overlay: [{from: value.from, to: value.to}]} | ||
} | ||
} | ||
} | ||
return null | ||
@@ -295,0 +329,0 @@ }) |
{ | ||
"name": "@lezer/html", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "lezer-based HTML grammar", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.cjs", |
@@ -21,14 +21,18 @@ # @lezer/html | ||
**`configureNesting`**`(tags: {`\ | ||
**`configureNesting`**`(tags?: {`\ | ||
` tag: "script" | "style" | "textarea",`\ | ||
` attrs?: (attrs: {[attr: string]: string}) => boolean,`\ | ||
` parser: {startParse: (input: Input, startPos: number, context: ParseContext) => PartialParse},`\ | ||
`}[]): {[name: string]: NestedParser}` | ||
` parser: Parser,`\ | ||
`}[], attributes?: {`\ | ||
` name: string,`\ | ||
` tagName?: string,`\ | ||
` parser: Parser,`\ | ||
`}[]): ParseWrapper` | ||
Create a nested parser config object which overrides the way the | ||
content of some tags is parsed. Each override is an object with a | ||
`tag` property holding the (lower case) tag name to override, and an | ||
optional `attrs` predicate that, if given, has to return true for the | ||
tag's attributes for this override to apply. | ||
content of some tags or attributes is parsed. Each tag override is an | ||
object with a `tag` property holding the (lower case) tag name to | ||
override, and an optional `attrs` predicate that, if given, has to | ||
return true for the tag's attributes for this override to apply. | ||
The `parser` property describes the way the tag's content is parsed. |
@@ -1,2 +0,3 @@ | ||
import {ScriptText, StyleText, TextareaText, Element} from "./parser.terms.js" | ||
import {ScriptText, StyleText, TextareaText, Element, TagName, Attribute, AttributeName, | ||
AttributeValue, UnquotedAttributeValue} from "./parser.terms.js" | ||
import {parseMixed} from "@lezer/common" | ||
@@ -6,6 +7,6 @@ | ||
let attrs = Object.create(null) | ||
for (let att of element.firstChild.getChildren("Attribute")) { | ||
let name = att.getChild("AttributeName"), value = att.getChild("AttributeValue") || att.getChild("UnquotedAttributeValue") | ||
for (let att of element.firstChild.getChildren(Attribute)) { | ||
let name = att.getChild(AttributeName), value = att.getChild(AttributeValue) || att.getChild(UnquotedAttributeValue) | ||
if (name) attrs[input.read(name.from, name.to)] = | ||
!value ? "" : value.name == "AttributeValue" ? input.read(value.from + 1, value.to - 1) : input.read(value.from, value.to) | ||
!value ? "" : value.type.id == AttributeValue ? input.read(value.from + 1, value.to - 1) : input.read(value.from, value.to) | ||
} | ||
@@ -24,3 +25,3 @@ return attrs | ||
// tags: { | ||
// tags?: { | ||
// tag: "script" | "style" | "textarea", | ||
@@ -30,4 +31,9 @@ // attrs?: ({[attr: string]: string}) => boolean, | ||
// }[] | ||
// attributes?: { | ||
// name: string, | ||
// tagName?: string, | ||
// parser: Parser | ||
// }[] | ||
export function configureNesting(tags) { | ||
export function configureNesting(tags = [], attributes = []) { | ||
let script = [], style = [], textarea = [] | ||
@@ -39,2 +45,5 @@ for (let tag of tags) { | ||
} | ||
let attrs = attributes.length ? Object.create(null) : null | ||
for (let attr of attributes) (attrs[attr.name] || (attrs[attr.name] = [])).push(attr) | ||
return parseMixed((node, input) => { | ||
@@ -45,4 +54,25 @@ let id = node.type.id | ||
if (id == TextareaText) return maybeNest(node, input, textarea) | ||
if (attrs && id == Attribute) { | ||
let n = node.node, nameNode | ||
if (nameNode = n.firstChild) { | ||
let matches = attrs[input.read(nameNode.from, nameNode.to)] | ||
if (matches) for (let attr of matches) { | ||
if (attr.tagName) { | ||
if (!tagName) { | ||
let tagNameNode = n.parent.getChild(TagName) | ||
tagName = tagNameNode ? input.read(tagNameNode.from, tagNameNode.to) : " " | ||
} | ||
if (attrTagName != tagName) continue | ||
} | ||
let value = n.lastChild | ||
if (value.type.id == AttributeValue) | ||
return {parser: attr.parser, overlay: [{from: value.from + 1, to: value.to - 1}]} | ||
else if (value.type.id == UnquotedAttributeValue) | ||
return {parser: attr.parser, overlay: [{from: value.from, to: value.to}]} | ||
} | ||
} | ||
} | ||
return null | ||
}) | ||
} |
Sorry, the diff of this file is not supported yet
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
95316
21
1197
38