|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
981 bytes
|
| Line | |
|---|
| 1 | // Can unquote attribute detection
|
|---|
| 2 | // Adopted implementation of Mathias Bynens
|
|---|
| 3 | // https://github.com/mathiasbynens/mothereff.in/blob/master/unquoted-attributes/eff.js
|
|---|
| 4 | var escapesRx = /\\([0-9A-Fa-f]{1,6})(\r\n|[ \t\n\f\r])?|\\./g;
|
|---|
| 5 | var blockUnquoteRx = /^(-?\d|--)|[\u0000-\u002c\u002e\u002f\u003A-\u0040\u005B-\u005E\u0060\u007B-\u009f]/;
|
|---|
| 6 |
|
|---|
| 7 | function canUnquote(value) {
|
|---|
| 8 | if (value === '' || value === '-') {
|
|---|
| 9 | return;
|
|---|
| 10 | }
|
|---|
| 11 |
|
|---|
| 12 | // Escapes are valid, so replace them with a valid non-empty string
|
|---|
| 13 | value = value.replace(escapesRx, 'a');
|
|---|
| 14 |
|
|---|
| 15 | return !blockUnquoteRx.test(value);
|
|---|
| 16 | }
|
|---|
| 17 |
|
|---|
| 18 | module.exports = function(node) {
|
|---|
| 19 | var attrValue = node.value;
|
|---|
| 20 |
|
|---|
| 21 | if (!attrValue || attrValue.type !== 'String') {
|
|---|
| 22 | return;
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | var unquotedValue = attrValue.value.replace(/^(.)(.*)\1$/, '$2');
|
|---|
| 26 | if (canUnquote(unquotedValue)) {
|
|---|
| 27 | node.value = {
|
|---|
| 28 | type: 'Identifier',
|
|---|
| 29 | loc: attrValue.loc,
|
|---|
| 30 | name: unquotedValue
|
|---|
| 31 | };
|
|---|
| 32 | }
|
|---|
| 33 | };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.