Skip to content

Commit 6875413

Browse files
committed
Extract html-elements.ts to core
1 parent 7734222 commit 6875413

File tree

4 files changed

+35
-51
lines changed

4 files changed

+35
-51
lines changed

javascript/packages/language-server/src/html_tags.ts renamed to javascript/packages/core/src/html-elements.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
export interface HTMLTagInfo {
1+
// https://html.spec.whatwg.org/multipage/indices.html#elements-3
2+
3+
export interface HTMLElementInfo {
24
name: string
35
description: string
46
isVoid: boolean
57
}
68

7-
export const HTML_TAGS: HTMLTagInfo[] = [
9+
export const HTML_ELEMENTS: HTMLElementInfo[] = [
810
{ name: "a", description: "Hyperlink", isVoid: false },
911
{ name: "abbr", description: "Abbreviation", isVoid: false },
12+
{ name: "acronym", description: "Acronym (deprecated)", isVoid: false },
1013
{ name: "address", description: "Contact information", isVoid: false },
1114
{ name: "area", description: "Image map area", isVoid: true },
1215
{ name: "article", description: "Article content", isVoid: false },
@@ -16,6 +19,7 @@ export const HTML_TAGS: HTMLTagInfo[] = [
1619
{ name: "base", description: "Document base URL", isVoid: true },
1720
{ name: "bdi", description: "Bidirectional isolation", isVoid: false },
1821
{ name: "bdo", description: "Bidirectional override", isVoid: false },
22+
{ name: "big", description: "Larger text (deprecated)", isVoid: false },
1923
{ name: "blockquote", description: "Block quotation", isVoid: false },
2024
{ name: "body", description: "Document body", isVoid: false },
2125
{ name: "br", description: "Line break", isVoid: true },
@@ -67,6 +71,7 @@ export const HTML_TAGS: HTMLTagInfo[] = [
6771
{ name: "main", description: "Main content", isVoid: false },
6872
{ name: "map", description: "Image map", isVoid: false },
6973
{ name: "mark", description: "Highlighted text", isVoid: false },
74+
{ name: "math", description: "MathML content", isVoid: false },
7075
{ name: "menu", description: "Menu", isVoid: false },
7176
{ name: "meta", description: "Metadata", isVoid: true },
7277
{ name: "meter", description: "Scalar measurement", isVoid: false },
@@ -78,6 +83,7 @@ export const HTML_TAGS: HTMLTagInfo[] = [
7883
{ name: "option", description: "Select option", isVoid: false },
7984
{ name: "output", description: "Output result", isVoid: false },
8085
{ name: "p", description: "Paragraph", isVoid: false },
86+
{ name: "param", description: "Object parameter (deprecated)", isVoid: true },
8187
{ name: "picture", description: "Responsive image", isVoid: false },
8288
{ name: "pre", description: "Preformatted text", isVoid: false },
8389
{ name: "progress", description: "Progress indicator", isVoid: false },
@@ -91,6 +97,7 @@ export const HTML_TAGS: HTMLTagInfo[] = [
9197
{ name: "search", description: "Search section", isVoid: false },
9298
{ name: "section", description: "Document section", isVoid: false },
9399
{ name: "select", description: "Select control", isVoid: false },
100+
{ name: "selectedcontent", description: "Selected content placeholder", isVoid: false },
94101
{ name: "slot", description: "Web component slot", isVoid: false },
95102
{ name: "small", description: "Small text", isVoid: false },
96103
{ name: "source", description: "Media source", isVoid: true },
@@ -100,6 +107,7 @@ export const HTML_TAGS: HTMLTagInfo[] = [
100107
{ name: "sub", description: "Subscript", isVoid: false },
101108
{ name: "summary", description: "Disclosure summary", isVoid: false },
102109
{ name: "sup", description: "Superscript", isVoid: false },
110+
{ name: "svg", description: "SVG content", isVoid: false },
103111
{ name: "table", description: "Table", isVoid: false },
104112
{ name: "tbody", description: "Table body", isVoid: false },
105113
{ name: "td", description: "Table cell", isVoid: false },
@@ -112,9 +120,28 @@ export const HTML_TAGS: HTMLTagInfo[] = [
112120
{ name: "title", description: "Document title", isVoid: false },
113121
{ name: "tr", description: "Table row", isVoid: false },
114122
{ name: "track", description: "Text track", isVoid: true },
123+
{ name: "tt", description: "Teletype text (deprecated)", isVoid: false },
115124
{ name: "u", description: "Underline", isVoid: false },
116125
{ name: "ul", description: "Unordered list", isVoid: false },
117126
{ name: "var", description: "Variable", isVoid: false },
118127
{ name: "video", description: "Video content", isVoid: false },
119128
{ name: "wbr", description: "Word break opportunity", isVoid: true },
120129
]
130+
131+
export const HTML_ELEMENT_NAMES = new Set(HTML_ELEMENTS.map(element => element.name))
132+
133+
export const HTML_VOID_ELEMENTS = new Set(
134+
HTML_ELEMENTS.filter(element => element.isVoid).map(element => element.name),
135+
)
136+
137+
export function isKnownHTMLElement(tagName: string): boolean {
138+
return HTML_ELEMENT_NAMES.has(tagName.toLowerCase())
139+
}
140+
141+
export function isVoidElement(tagName: string): boolean {
142+
return HTML_VOID_ELEMENTS.has(tagName.toLowerCase())
143+
}
144+
145+
export function isCustomElement(tagName: string): boolean {
146+
return tagName.includes("-")
147+
}

javascript/packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * from "./ast-utils.js"
22
export * from "./html-constants.js"
33
export * from "./html-character-references.js"
4+
export * from "./html-elements.js"
45
export * from "./backend.js"
56
export * from "./diagnostic.js"
67
export * from "./didyoumean.js"

javascript/packages/language-server/src/completion_service.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import {
88
} from "vscode-languageserver/node"
99
import { TextDocument } from "vscode-languageserver-textdocument"
1010

11-
import { Visitor, isERBContentNode, isHTMLOpenTagNode, isHTMLTextNode, HTML_NAMED_CHARACTER_REFERENCES } from "@herb-tools/core"
11+
import { Visitor, isERBContentNode, isHTMLOpenTagNode, isHTMLTextNode, HTML_NAMED_CHARACTER_REFERENCES, HTML_ELEMENTS } from "@herb-tools/core"
1212
import { ParserService } from "./parser_service"
1313
import { nodeToRange, isPositionInRange, rangeSize } from "./range_utils"
14-
import { HTML_TAGS } from "./html_tags"
1514
import { ACTION_VIEW_HELPERS } from "./action_view_helpers"
1615

1716
import type { Node, ERBContentNode, HTMLOpenTagNode, HTMLTextNode } from "@herb-tools/core"
@@ -235,7 +234,7 @@ export class CompletionService {
235234
}
236235

237236
private getTagDotCompletions(prefix: string, hasClosingERBTag: boolean): CompletionList {
238-
const items: CompletionItem[] = HTML_TAGS
237+
const items: CompletionItem[] = HTML_ELEMENTS
239238
.filter(tag => tag.name.startsWith(prefix))
240239
.map((tag, index) => {
241240
const isCommon = COMMON_TAGS.has(tag.name)
@@ -265,7 +264,7 @@ export class CompletionService {
265264
}
266265

267266
private getContentTagCompletions(prefix: string, hasSpaceAfterCursor: boolean): CompletionList {
268-
const items: CompletionItem[] = HTML_TAGS
267+
const items: CompletionItem[] = HTML_ELEMENTS
269268
.filter(tag => tag.name.startsWith(prefix))
270269
.map((tag, index) => {
271270
const isCommon = COMMON_TAGS.has(tag.name)
@@ -286,7 +285,7 @@ export class CompletionService {
286285
}
287286

288287
private getHTMLTagCompletions(prefix: string): CompletionList {
289-
const items: CompletionItem[] = HTML_TAGS
288+
const items: CompletionItem[] = HTML_ELEMENTS
290289
.filter(tag => tag.name.startsWith(prefix))
291290
.map((tag, index) => {
292291
const isCommon = COMMON_TAGS.has(tag.name)

javascript/packages/linter/src/rules/rule-utils.ts

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -182,45 +182,8 @@ export const HTML_BLOCK_ELEMENTS = new Set([
182182
"ol", "p", "pre", "section", "table", "tfoot", "ul", "video"
183183
])
184184

185-
export const HTML_VOID_ELEMENTS = new Set([
186-
"area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta",
187-
"param", "source", "track", "wbr",
188-
])
189-
190185
export { HTML_BOOLEAN_ATTRIBUTES, isBooleanAttribute } from "@herb-tools/core"
191-
192-
export const HTML_KNOWN_ELEMENTS = new Set([
193-
"html", "head", "body",
194-
"base", "link", "meta", "style", "title",
195-
"script", "noscript", "template", "slot", "selectedcontent",
196-
"address", "article", "aside", "footer", "header", "hgroup",
197-
"main", "nav", "section", "search",
198-
"h1", "h2", "h3", "h4", "h5", "h6",
199-
"blockquote", "dd", "details", "dialog", "div", "dl", "dt",
200-
"figcaption", "figure", "hr", "li", "menu", "ol", "p", "pre",
201-
"summary", "ul",
202-
"a", "abbr", "b", "bdi", "bdo", "br", "cite", "code", "data",
203-
"dfn", "em", "i", "kbd", "mark", "q", "rp", "rt", "ruby",
204-
"s", "samp", "small", "span", "strong", "sub", "sup", "time",
205-
"u", "var", "wbr",
206-
"del", "ins",
207-
"area", "audio", "canvas", "embed", "iframe", "img", "map",
208-
"math", "object", "param", "picture", "source", "svg", "track", "video",
209-
"caption", "col", "colgroup", "table", "tbody", "td", "tfoot",
210-
"th", "thead", "tr",
211-
"button", "datalist", "fieldset", "form", "input", "label",
212-
"legend", "meter", "optgroup", "option", "output", "progress",
213-
"select", "textarea",
214-
"acronym", "big", "tt",
215-
])
216-
217-
export function isKnownHTMLElement(tagName: string): boolean {
218-
return HTML_KNOWN_ELEMENTS.has(tagName.toLowerCase())
219-
}
220-
221-
export function isCustomElement(tagName: string): boolean {
222-
return tagName.includes("-")
223-
}
186+
export { HTML_ELEMENTS, HTML_ELEMENT_NAMES, HTML_VOID_ELEMENTS, isKnownHTMLElement, isVoidElement, isCustomElement } from "@herb-tools/core"
224187

225188
export const HEADING_TAGS = new Set(["h1", "h2", "h3", "h4", "h5", "h6"])
226189

@@ -420,12 +383,6 @@ export function isBlockElement(tagName: string): boolean {
420383
return HTML_BLOCK_ELEMENTS.has(tagName.toLowerCase())
421384
}
422385

423-
/**
424-
* Checks if an element is a void element
425-
*/
426-
export function isVoidElement(tagName: string): boolean {
427-
return HTML_VOID_ELEMENTS.has(tagName.toLowerCase())
428-
}
429386

430387
/**
431388
* Attribute visitor that provides granular processing based on both

0 commit comments

Comments
 (0)