Skip to content

Commit

Permalink
perf: static-optimize <a>/<area> href (#4354)
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanlawson authored Jul 8, 2024
1 parent 48d6948 commit 7ee59c7
Show file tree
Hide file tree
Showing 69 changed files with 3,103 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { createElement } from 'lwc';
import HrefStatic from 'x/hrefStatic';
import HrefDynamic from 'x/hrefDynamic';
import HrefDangling from 'x/hrefDangling';
import HrefBooleanTrue from 'x/hrefBooleanTrue';
import HrefBooleanTrueNoId from 'x/hrefBooleanTrueNoId';

function testHref(type, create) {
describe(`${type} href attribute values`, () => {
Expand Down Expand Up @@ -60,11 +62,37 @@ it('should not transform fragment-only urls when the template has no ids', () =>
const elm = createElement('x-href-dangling', { is: HrefDangling });
document.body.appendChild(elm);
expect(elm.shadowRoot.querySelector('a').getAttribute('href')).toBe('#foo');
expect(elm.shadowRoot.querySelector('area').getAttribute('href')).toBe('#bar');
});
// Enable this test when we transform all href values with fragment-only urls
// https://github.com/salesforce/lwc/issues/1150
// TODO [#1150]: Always mangle frag-id href attribute values
xit('should transform fragment-only urls even when the template has no ids', () => {
const elm = createElement('x-href-dangling', { is: HrefDangling });
document.body.appendChild(elm);
expect(elm.shadowRoot.querySelector('a').getAttribute('href')).not.toBe('#foo');
expect(elm.shadowRoot.querySelector('area').getAttribute('href')).not.toBe('#bar');
});

describe('boolean true', () => {
const scenarios = [
{
name: 'no id',
Ctor: HrefBooleanTrueNoId,
tagName: 'x-href-boolean-true-no-id',
},
{ name: 'with id', Ctor: HrefBooleanTrue, tagName: 'x-href-boolean-true' },
];

scenarios.forEach(({ name, Ctor, tagName }) => {
describe(name, () => {
// For the "boolean true" case (e.g. `<a href>` or `<area href>`), no transformation is applied
it('does not transform href', () => {
const elm = createElement(tagName, { is: Ctor });
document.body.appendChild(elm);

expect(elm.shadowRoot.querySelector('a').getAttribute('href')).toBe('');
expect(elm.shadowRoot.querySelector('area').getAttribute('href')).toBe('');
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<template>
<div>
<a href></a>
</div>

<div>
<map>
<area href/>
</map>
</div>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { LightningElement } from 'lwc';

export default class extends LightningElement {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<template>
<div id="foo"></div>

<div>
<a href data-id={fragmentUrl}></a>
</div>

<div>
<map>
<area href />
</map>
</div>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { LightningElement } from 'lwc';

export default class extends LightningElement {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<template>
<!-- "dangling" because there exists no element in this template with the id "foo" -->
<a href="#foo">foo</a>
</template>
<map>
<area href="#bar" />
</map>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<template>
<a href={narita}>KIX</a>
<map>
<area href={haneda}/>
<area href={chubu}/>
</map>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
{
"root": {
"type": "Root",
"location": {
"startLine": 1,
"startColumn": 1,
"endLine": 7,
"endColumn": 12,
"start": 0,
"end": 131,
"startTag": {
"startLine": 1,
"startColumn": 1,
"endLine": 1,
"endColumn": 11,
"start": 0,
"end": 10
},
"endTag": {
"startLine": 7,
"startColumn": 1,
"endLine": 7,
"endColumn": 12,
"start": 120,
"end": 131
}
},
"directives": [],
"children": [
{
"type": "Element",
"name": "a",
"namespace": "http://www.w3.org/1999/xhtml",
"location": {
"startLine": 2,
"startColumn": 5,
"endLine": 2,
"endColumn": 29,
"start": 15,
"end": 39,
"startTag": {
"startLine": 2,
"startColumn": 5,
"endLine": 2,
"endColumn": 22,
"start": 15,
"end": 32
},
"endTag": {
"startLine": 2,
"startColumn": 25,
"endLine": 2,
"endColumn": 29,
"start": 35,
"end": 39
}
},
"attributes": [
{
"type": "Attribute",
"name": "href",
"value": {
"type": "Identifier",
"start": 1,
"end": 7,
"name": "narita",
"location": {
"startLine": 2,
"startColumn": 8,
"endLine": 2,
"endColumn": 21,
"start": 18,
"end": 31
}
},
"location": {
"startLine": 2,
"startColumn": 8,
"endLine": 2,
"endColumn": 21,
"start": 18,
"end": 31
}
}
],
"properties": [],
"directives": [],
"listeners": [],
"children": [
{
"type": "Text",
"raw": "KIX",
"value": {
"type": "Literal",
"value": "KIX"
},
"location": {
"startLine": 2,
"startColumn": 22,
"endLine": 2,
"endColumn": 25,
"start": 32,
"end": 35
}
}
]
},
{
"type": "Element",
"name": "map",
"namespace": "http://www.w3.org/1999/xhtml",
"location": {
"startLine": 3,
"startColumn": 5,
"endLine": 6,
"endColumn": 11,
"start": 44,
"end": 119,
"startTag": {
"startLine": 3,
"startColumn": 5,
"endLine": 3,
"endColumn": 10,
"start": 44,
"end": 49
},
"endTag": {
"startLine": 6,
"startColumn": 5,
"endLine": 6,
"endColumn": 11,
"start": 113,
"end": 119
}
},
"attributes": [],
"properties": [],
"directives": [],
"listeners": [],
"children": [
{
"type": "Element",
"name": "area",
"namespace": "http://www.w3.org/1999/xhtml",
"location": {
"startLine": 4,
"startColumn": 9,
"endLine": 4,
"endColumn": 30,
"start": 58,
"end": 79,
"startTag": {
"startLine": 4,
"startColumn": 9,
"endLine": 4,
"endColumn": 30,
"start": 58,
"end": 79
}
},
"attributes": [
{
"type": "Attribute",
"name": "href",
"value": {
"type": "Identifier",
"start": 1,
"end": 7,
"name": "haneda",
"location": {
"startLine": 4,
"startColumn": 15,
"endLine": 4,
"endColumn": 29,
"start": 64,
"end": 78
}
},
"location": {
"startLine": 4,
"startColumn": 15,
"endLine": 4,
"endColumn": 29,
"start": 64,
"end": 78
}
}
],
"properties": [],
"directives": [],
"listeners": [],
"children": []
},
{
"type": "Element",
"name": "area",
"namespace": "http://www.w3.org/1999/xhtml",
"location": {
"startLine": 5,
"startColumn": 9,
"endLine": 5,
"endColumn": 29,
"start": 88,
"end": 108,
"startTag": {
"startLine": 5,
"startColumn": 9,
"endLine": 5,
"endColumn": 29,
"start": 88,
"end": 108
}
},
"attributes": [
{
"type": "Attribute",
"name": "href",
"value": {
"type": "Identifier",
"start": 1,
"end": 6,
"name": "chubu",
"location": {
"startLine": 5,
"startColumn": 15,
"endLine": 5,
"endColumn": 28,
"start": 94,
"end": 107
}
},
"location": {
"startLine": 5,
"startColumn": 15,
"endLine": 5,
"endColumn": 28,
"start": 94,
"end": 107
}
}
],
"properties": [],
"directives": [],
"listeners": [],
"children": []
}
]
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"enableStaticContentOptimization": false
}
Loading

0 comments on commit 7ee59c7

Please sign in to comment.