Small and fast HTML matcher
Finds matching opening and closing tag pair for given location in HTML/XML source:
import match from '@emmetio/html-matcher';
const content = '<div><a href="http://emmet.io">Example</a></div>';
const tag = match(content, 35);
console.log(tag.name);
console.log(tag.open);
console.log(tag.end);
console.log(tag.attributes);
By default, matcher works in HTML, which means if it finds tag name which is known to be empty (for example, <img>
) it will not search for it’s closing part. However, such behavior might be unexpected for XML syntaxes where all tags should be either self-closed or provide closing part. In this case, you should pass xml: true
option to properly handle XML mode:
import match from '@emmetio/html-matcher';
const content = '<div><img>Caption</img></div>';
const html = match(content, 8);
const xml = match(content, 8, { xml: true });
console.log(html.name);
console.log(html.open);
console.log(html.close);
console.log(xml.name);
console.log(xml.open);
console.log(xml.close);
Special tags
In HTML, some tags has special meaning. For example, a <script>
tag: its contents should be completely ignored until we find closing </script>
tag. But, if <script>
tag contains unknown type
attribute value, we should consider it as a regular tag. By default, matcher understands script
and style
tags as “special” but you can override them with special
option:
import match from '@emmetio/html-matcher';
match('...', 10, { special: { 'foo-bar': null } });
The special
option is an object where key is a tag name and value is an array of type
attribute values which, if present in tag, will make it special. If array is not provided, all instances of tag with given name will be considered as special.