Skip to content

Commit f256450

Browse files
committed
Apply attribute name handling in pretty mode
1 parent 1403064 commit f256450

File tree

4 files changed

+490
-10
lines changed

4 files changed

+490
-10
lines changed

src/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { encodeEntities, styleObjToCss, UNSAFE_NAME } from './util.js';
1+
import {
2+
encodeEntities,
3+
styleObjToCss,
4+
UNSAFE_NAME,
5+
NAMESPACE_REPLACE_REGEX,
6+
HTML_LOWER_CASE,
7+
SVG_CAMEL_CASE
8+
} from './util.js';
29
import { options, h, Fragment } from 'preact';
310
import {
411
CHILDREN,
@@ -454,9 +461,6 @@ function _renderToString(vnode, context, isSvgMode, selectValue, parent) {
454461
return s + '>' + html + '</' + type + '>';
455462
}
456463

457-
const HTML_LOWER_CASE = /^accessK|^auto[A-Z]|^ch|^col|cont|cross|dateT|encT|form[A-Z]|frame|hrefL|inputM|maxL|minL|noV|playsI|readO|rowS|spellC|src[A-Z]|tabI|item[A-Z]/;
458-
const SVG_CAMEL_CASE = /^ac|^ali|arabic|basel|cap|clipPath$|clipRule$|color|dominant|enable|fill|flood|font|glyph[^R]|horiz|image|letter|lighting|marker[^WUH]|overline|panose|pointe|paint|rendering|shape|stop|strikethrough|stroke|text[^L]|transform|underline|unicode|units|^v[^i]|^w|^xH/;
459-
const NAMESPACE_REPLACE_REGEX = /^(xlink|xmlns|xml)(:|[A-Z])/;
460464
const SELF_CLOSING = new Set([
461465
'area',
462466
'base',

src/pretty.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import {
66
getChildren,
77
createComponent,
88
UNSAFE_NAME,
9-
XLINK,
10-
VOID_ELEMENTS
9+
VOID_ELEMENTS,
10+
NAMESPACE_REPLACE_REGEX,
11+
HTML_LOWER_CASE,
12+
SVG_CAMEL_CASE
1113
} from './util.js';
1214
import { COMMIT, DIFF, DIFFED, RENDER, SKIP_EFFECTS } from './constants.js';
1315
import { options, Fragment } from 'preact';
@@ -243,8 +245,21 @@ function _renderToStringPretty(
243245
} else if (name === 'className') {
244246
if (typeof props.class !== 'undefined') continue;
245247
name = 'class';
246-
} else if (isSvgMode && XLINK.test(name)) {
247-
name = name.toLowerCase().replace(/^xlink:?/, 'xlink:');
248+
} else if (name === 'acceptCharset') {
249+
name = 'accept-charset';
250+
} else if (name === 'httpEquiv') {
251+
name = 'http-equiv';
252+
} else if (NAMESPACE_REPLACE_REGEX.test(name)) {
253+
name = name.replace(NAMESPACE_REPLACE_REGEX, '$1:$2').toLowerCase();
254+
} else if (isSvgMode) {
255+
if (SVG_CAMEL_CASE.test(name)) {
256+
name =
257+
name === 'panose1'
258+
? 'panose-1'
259+
: name.replace(/([A-Z])/g, '-$1').toLowerCase();
260+
}
261+
} else if (HTML_LOWER_CASE.test(name)) {
262+
name = name.toLowerCase();
248263
}
249264

250265
if (name === 'htmlFor') {

src/util.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
export const VOID_ELEMENTS = /^(?:area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/;
22
export const UNSAFE_NAME = /[\s\n\\/='"\0<>]/;
3-
export const XLINK = /^xlink:?./;
3+
export const NAMESPACE_REPLACE_REGEX = /^(xlink|xmlns|xml)(:|[A-Z])/;
4+
export const HTML_LOWER_CASE = /^accessK|^auto[A-Z]|^ch|^col|cont|cross|dateT|encT|form[A-Z]|frame|hrefL|inputM|maxL|minL|noV|playsI|readO|rowS|spellC|src[A-Z]|tabI|item[A-Z]/;
5+
export const SVG_CAMEL_CASE = /^ac|^ali|arabic|basel|cap|clipPath$|clipRule$|color|dominant|enable|fill|flood|font|glyph[^R]|horiz|image|letter|lighting|marker[^WUH]|overline|panose|pointe|paint|rendering|shape|stop|strikethrough|stroke|text[^L]|transform|underline|unicode|units|^v[^i]|^w|^xH/;
46

57
// DOM properties that should NOT have "px" added when numeric
68
const ENCODED_ENTITIES = /["&<]/;

0 commit comments

Comments
 (0)