From f2b557f8af98ba10729a2e79b3f3fdb0516d27c2 Mon Sep 17 00:00:00 2001 From: Ivan Hofer Date: Sun, 7 Aug 2022 20:48:07 +0200 Subject: [PATCH] some more variables --- src/compiler/compile/Component.ts | 4 +++- src/compiler/compile/css/Selector.ts | 4 +++- src/compiler/compile/css/Stylesheet.ts | 4 +++- src/compiler/compile/nodes/Element.ts | 8 ++++++-- .../compile/render_dom/wrappers/Element/Attribute.ts | 4 +++- .../compile/render_dom/wrappers/Element/StyleAttribute.ts | 4 +++- 6 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index 2f8874de7a..87ef087678 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -47,6 +47,8 @@ interface ComponentOptions { preserveWhitespace?: boolean; } +const regex_leading_directory_separator = /^[/\\]/; + export default class Component { stats: Stats; warnings: Warning[]; @@ -136,7 +138,7 @@ export default class Component { (typeof process !== 'undefined' ? compile_options.filename .replace(process.cwd(), '') - .replace(/^[/\\]/, '') + .replace(regex_leading_directory_separator, '') : compile_options.filename); this.locate = getLocator(this.source, { offsetLine: 1 }); diff --git a/src/compiler/compile/css/Selector.ts b/src/compiler/compile/css/Selector.ts index 17302c4abd..1ebacbdd86 100644 --- a/src/compiler/compile/css/Selector.ts +++ b/src/compiler/compile/css/Selector.ts @@ -281,12 +281,14 @@ function apply_selector(blocks: Block[], node: Element, to_encapsulate: Array<{ return true; } +const regex_backslash_and_following_character = /\\(.)/g; + function block_might_apply_to_node(block: Block, node: Element): BlockAppliesToNode { let i = block.selectors.length; while (i--) { const selector = block.selectors[i]; - const name = typeof selector.name === 'string' && selector.name.replace(/\\(.)/g, '$1'); + const name = typeof selector.name === 'string' && selector.name.replace(regex_backslash_and_following_character, '$1'); if (selector.type === 'PseudoClassSelector' && (name === 'host' || name === 'root')) { return BlockAppliesToNode.NotPossible; diff --git a/src/compiler/compile/css/Stylesheet.ts b/src/compiler/compile/css/Stylesheet.ts index 1a9ea7feeb..f983204b13 100644 --- a/src/compiler/compile/css/Stylesheet.ts +++ b/src/compiler/compile/css/Stylesheet.ts @@ -10,8 +10,10 @@ import compiler_warnings from '../compiler_warnings'; import { extract_ignores_above_position } from '../../utils/extract_svelte_ignore'; import { push_array } from '../../utils/push_array'; +const regex_css_browser_prefix = /^-((webkit)|(moz)|(o)|(ms))-/; + function remove_css_prefix(name: string): string { - return name.replace(/^-((webkit)|(moz)|(o)|(ms))-/, ''); + return name.replace(regex_css_browser_prefix, ''); } const is_keyframes_node = (node: CssNode) => diff --git a/src/compiler/compile/nodes/Element.ts b/src/compiler/compile/nodes/Element.ts index 79f2800437..e40f5eb744 100644 --- a/src/compiler/compile/nodes/Element.ts +++ b/src/compiler/compile/nodes/Element.ts @@ -988,7 +988,7 @@ export default class Element extends Node { if (attribute && !attribute.is_true) { attribute.chunks.forEach((chunk, index) => { if (chunk.type === 'Text') { - let data = chunk.data.replace(/[\s\n\t]+/g, ' '); + let data = chunk.data.replace(regex_any_repeated_whitespace, ' '); if (index === 0) { data = data.trimLeft(); } else if (index === attribute.chunks.length - 1) { @@ -1002,12 +1002,16 @@ export default class Element extends Node { } } +const regex_any_repeated_whitespace = /[\s\n\t]+/g; + +const regex_starts_with_vovel = /^[aeiou]/; + function should_have_attribute( node, attributes: string[], name = node.name ) { - const article = /^[aeiou]/.test(attributes[0]) ? 'an' : 'a'; + const article = regex_starts_with_vovel.test(attributes[0]) ? 'an' : 'a'; const sequence = attributes.length > 1 ? attributes.slice(0, -1).join(', ') + ` or ${attributes[attributes.length - 1]}` : attributes[0]; diff --git a/src/compiler/compile/render_dom/wrappers/Element/Attribute.ts b/src/compiler/compile/render_dom/wrappers/Element/Attribute.ts index 21a44ab92a..aa003601c9 100644 --- a/src/compiler/compile/render_dom/wrappers/Element/Attribute.ts +++ b/src/compiler/compile/render_dom/wrappers/Element/Attribute.ts @@ -45,6 +45,8 @@ export class BaseAttributeWrapper { render(_block: Block) {} } +const regex_minus_sign = /-/; + export default class AttributeWrapper extends BaseAttributeWrapper { node: Attribute; parent: ElementWrapper; @@ -115,7 +117,7 @@ export default class AttributeWrapper extends BaseAttributeWrapper { // xlink is a special case... we could maybe extend this to generic // namespaced attributes but I'm not sure that's applicable in // HTML5? - const method = /-/.test(element.node.name) + const method = regex_minus_sign.test(element.node.name) ? '@set_custom_element_data' : name.slice(0, 6) === 'xlink:' ? '@xlink_attr' diff --git a/src/compiler/compile/render_dom/wrappers/Element/StyleAttribute.ts b/src/compiler/compile/render_dom/wrappers/Element/StyleAttribute.ts index ac7f66b6ac..6ffa37fb71 100644 --- a/src/compiler/compile/render_dom/wrappers/Element/StyleAttribute.ts +++ b/src/compiler/compile/render_dom/wrappers/Element/StyleAttribute.ts @@ -69,6 +69,8 @@ export default class StyleAttributeWrapper extends AttributeWrapper { } } +const regex_style_prop_key = /^\s*([\w-]+):\s*/; + function optimize_style(value: Array) { const props: StyleProp[] = []; let chunks = value.slice(); @@ -78,7 +80,7 @@ function optimize_style(value: Array) { if (chunk.type !== 'Text') return null; - const key_match = /^\s*([\w-]+):\s*/.exec(chunk.data); + const key_match = regex_style_prop_key.exec(chunk.data); if (!key_match) return null; const key = key_match[1];