@lezer/html
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -0,1 +1,7 @@ | ||
## 1.3.0 (2022-12-16) | ||
### New features | ||
`configureNesting` now supports targeting tags other than `style`, `script`, and `textarea`. | ||
## 1.2.0 (2022-11-25) | ||
@@ -2,0 +8,0 @@ |
@@ -7,3 +7,3 @@ import {LRParser} from "@lezer/lr" | ||
export function configureNesting(tags?: readonly { | ||
tag: "script" | "style" | "textarea", | ||
tag: string, | ||
attrs?: (attrs: {[attr: string]: string}) => boolean, | ||
@@ -10,0 +10,0 @@ parser: Parser |
@@ -33,2 +33,3 @@ import { ContextTracker, ExternalTokenizer, LRParser } from '@lezer/lr'; | ||
OpenTag = 35, | ||
CloseTag = 36, | ||
Dialect_noMatch = 0; | ||
@@ -257,5 +258,5 @@ | ||
function getAttrs(element, input) { | ||
function getAttrs(openTag, input) { | ||
let attrs = Object.create(null); | ||
for (let att of element.firstChild.getChildren(Attribute)) { | ||
for (let att of openTag.getChildren(Attribute)) { | ||
let name = att.getChild(AttributeName), value = att.getChild(AttributeValue) || att.getChild(UnquotedAttributeValue); | ||
@@ -268,6 +269,11 @@ if (name) attrs[input.read(name.from, name.to)] = | ||
function findTagName(openTag, input) { | ||
let tagNameNode = openTag.getChild(TagName); | ||
return tagNameNode ? input.read(tagNameNode.from, tagNameNode.to) : " " | ||
} | ||
function maybeNest(node, input, tags) { | ||
let attrs; | ||
for (let tag of tags) { | ||
if (!tag.attrs || tag.attrs(attrs || (attrs = getAttrs(node.node.parent, input)))) | ||
if (!tag.attrs || tag.attrs(attrs || (attrs = getAttrs(node.node.parent.firstChild, input)))) | ||
return {parser: tag.parser} | ||
@@ -279,3 +285,3 @@ } | ||
// tags?: { | ||
// tag: "script" | "style" | "textarea", | ||
// tag: string, | ||
// attrs?: ({[attr: string]: string}) => boolean, | ||
@@ -291,6 +297,5 @@ // parser: Parser | ||
function configureNesting(tags = [], attributes = []) { | ||
let script = [], style = [], textarea = []; | ||
let script = [], style = [], textarea = [], other = []; | ||
for (let tag of tags) { | ||
let array = tag.tag == "script" ? script : tag.tag == "style" ? style : tag.tag == "textarea" ? textarea : null; | ||
if (!array) throw new RangeError("Only script, style, and textarea tags can host nested parsers") | ||
let array = tag.tag == "script" ? script : tag.tag == "style" ? style : tag.tag == "textarea" ? textarea : other; | ||
array.push(tag); | ||
@@ -307,2 +312,12 @@ } | ||
if (id == OpenTag && other.length) { | ||
let n = node.node, tagName = findTagName(n, input), attrs; | ||
for (let tag of other) { | ||
if (tag.tag == tagName && (!tag.attrs || tag.attrs(attrs || (attrs = getAttrs(n, input))))) { | ||
let close = n.parent.lastChild; | ||
return {parser: tag.parser, overlay: [{from: node.to, to: close.type.id == CloseTag ? close.from : n.parent.to}]} | ||
} | ||
} | ||
} | ||
if (attrs && id == Attribute) { | ||
@@ -313,9 +328,3 @@ let n = node.node, nameNode; | ||
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 | ||
} | ||
if (attr.tagName && attr.tagName != findTagName(n.parent, input)) continue | ||
let value = n.lastChild; | ||
@@ -322,0 +331,0 @@ if (value.type.id == AttributeValue) |
{ | ||
"name": "@lezer/html", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "lezer-based HTML grammar", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.cjs", |
@@ -22,3 +22,3 @@ # @lezer/html | ||
**`configureNesting`**`(tags?: {`\ | ||
` tag: "script" | "style" | "textarea",`\ | ||
` tag: string,`\ | ||
` attrs?: (attrs: {[attr: string]: string}) => boolean,`\ | ||
@@ -25,0 +25,0 @@ ` parser: Parser,`\ |
@@ -1,8 +0,9 @@ | ||
import {ScriptText, StyleText, TextareaText, Element, TagName, Attribute, AttributeName, | ||
import {ScriptText, StyleText, TextareaText, | ||
Element, TagName, Attribute, AttributeName, OpenTag, CloseTag, | ||
AttributeValue, UnquotedAttributeValue} from "./parser.terms.js" | ||
import {parseMixed} from "@lezer/common" | ||
function getAttrs(element, input) { | ||
function getAttrs(openTag, input) { | ||
let attrs = Object.create(null) | ||
for (let att of element.firstChild.getChildren(Attribute)) { | ||
for (let att of openTag.getChildren(Attribute)) { | ||
let name = att.getChild(AttributeName), value = att.getChild(AttributeValue) || att.getChild(UnquotedAttributeValue) | ||
@@ -15,6 +16,11 @@ if (name) attrs[input.read(name.from, name.to)] = | ||
function findTagName(openTag, input) { | ||
let tagNameNode = openTag.getChild(TagName) | ||
return tagNameNode ? input.read(tagNameNode.from, tagNameNode.to) : " " | ||
} | ||
function maybeNest(node, input, tags) { | ||
let attrs | ||
for (let tag of tags) { | ||
if (!tag.attrs || tag.attrs(attrs || (attrs = getAttrs(node.node.parent, input)))) | ||
if (!tag.attrs || tag.attrs(attrs || (attrs = getAttrs(node.node.parent.firstChild, input)))) | ||
return {parser: tag.parser} | ||
@@ -26,3 +32,3 @@ } | ||
// tags?: { | ||
// tag: "script" | "style" | "textarea", | ||
// tag: string, | ||
// attrs?: ({[attr: string]: string}) => boolean, | ||
@@ -38,6 +44,5 @@ // parser: Parser | ||
export function configureNesting(tags = [], attributes = []) { | ||
let script = [], style = [], textarea = [] | ||
let script = [], style = [], textarea = [], other = [] | ||
for (let tag of tags) { | ||
let array = tag.tag == "script" ? script : tag.tag == "style" ? style : tag.tag == "textarea" ? textarea : null | ||
if (!array) throw new RangeError("Only script, style, and textarea tags can host nested parsers") | ||
let array = tag.tag == "script" ? script : tag.tag == "style" ? style : tag.tag == "textarea" ? textarea : other | ||
array.push(tag) | ||
@@ -54,2 +59,12 @@ } | ||
if (id == OpenTag && other.length) { | ||
let n = node.node, tagName = findTagName(n, input), attrs | ||
for (let tag of other) { | ||
if (tag.tag == tagName && (!tag.attrs || tag.attrs(attrs || (attrs = getAttrs(n, input))))) { | ||
let close = n.parent.lastChild | ||
return {parser: tag.parser, overlay: [{from: node.to, to: close.type.id == CloseTag ? close.from : n.parent.to}]} | ||
} | ||
} | ||
} | ||
if (attrs && id == Attribute) { | ||
@@ -60,9 +75,3 @@ let n = node.node, nameNode | ||
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 | ||
} | ||
if (attr.tagName && attr.tagName != findTagName(n.parent, input)) continue | ||
let value = n.lastChild | ||
@@ -69,0 +78,0 @@ if (value.type.id == AttributeValue) |
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
96304
1218