Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(render/meta): include itemprop and httpequiv in existing element check #16

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/render.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { hasAttributes, isTextNode } from './util';
import { createElement } from './dom';
import type { ChildNode, FunctionalUtilities, VNode } from '@stencil/core';
import { createElement } from './dom';
import { hasAttributes, isTextNode } from './util';

const hasChildren = (node: ChildNode) => Array.isArray(node.vchildren);

Expand All @@ -23,8 +23,22 @@ function title(node: ChildNode, head: HTMLElement, utils: FunctionalUtilities) {
}

function meta(node: ChildNode, head: HTMLElement, utils: FunctionalUtilities) {
const namePropKey = node.vattrs?.property ? 'property' : 'name';
const namePropValue = node.vattrs?.property || node.vattrs?.name;
const namePropKey = (() => {
if (node.vattrs?.property) {
return 'property';
}

if (node.vattrs?.itemprop || node.vattrs?.itemProp) {
return 'itemprop';
}

if (node.vattrs?.httpequiv || node.vattrs?.httpEquiv) {
return 'httpequiv';
}

return 'name';
})();
const namePropValue = node.vattrs?.property || node.vattrs?.name || node.vattrs?.itemprop || node.vattrs?.itemProp || node.vattrs?.httpequiv || node.vattrs?.httpEquiv;

const existingElement = head.querySelector(`meta[${namePropKey}="${namePropValue}"]`);
if (existingElement !== null) {
Expand Down
4 changes: 0 additions & 4 deletions src/stencil-helmet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ const isValidNode = (node: ChildNode) => validTagNames.indexOf(node.vtag as stri
const renderNode = (node: ChildNode, utils: FunctionalUtilities): HTMLElement => RenderTypes[node.vtag as string](node, document.head, utils);

export const Helmet = (_props: any, children: VNode[], utils: FunctionalUtilities) => {
console.log("HELMET2");
console.trace();
// eval('debugger');
if (!headExists) {
return null;
}
Expand All @@ -27,7 +24,6 @@ export const Helmet = (_props: any, children: VNode[], utils: FunctionalUtilitie
}
});

console.log('RENDERING NODES', rendered, rendered.filter(shouldApplyToHead));
// Build an HTMLElement for each provided virtual child
rendered
.filter(shouldApplyToHead)
Expand Down
84 changes: 67 additions & 17 deletions test/render.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ChildNode } from '@stencil/core';

import RenderTypes from '../src/render';


beforeEach(() => {
document.head.innerHTML = '';
});
Expand All @@ -28,26 +28,76 @@ describe('title', () => {
});

describe('meta', () => {
const metaNode: ChildNode = {
vtag: 'meta',
vattrs: {
name: 'foo',
content: 'bar'
},
vchildren: null,
vtext: null
};

it('should return one element without a match', () => {
expect(RenderTypes.meta(metaNode, document.head))
.toBeInstanceOf(HTMLElement);
describe('name attribute', () => {
const metaNode: ChildNode = {
vtag: 'meta',
vattrs: {
name: 'foo',
content: 'bar'
},
vchildren: null,
vtext: null
};

it('should return one element without a match', () => {
expect(RenderTypes.meta(metaNode, document.head))
.toBeInstanceOf(HTMLElement);
});

it('should return two elements with a match', () => {
document.head.innerHTML = `<meta name="foo" content="test"/>`;
expect(RenderTypes.meta(metaNode, document.head))
.toHaveLength(2);
});
});

it('should return two elements with a match', () => {
document.head.innerHTML = `<meta name="foo" content="test"/>`;
expect(RenderTypes.meta(metaNode, document.head))
.toHaveLength(2);
describe('itemprop attribute', () => {
const metaNode: ChildNode = {
vtag: 'meta',
vattrs: {
itemprop: 'foo',
content: 'bar'
},
vchildren: null,
vtext: null
};

it('should return one element without a match', () => {
expect(RenderTypes.meta(metaNode, document.head))
.toBeInstanceOf(HTMLElement);
});

it('should return two elements with a match', () => {
document.head.innerHTML = `<meta itemprop="foo" content="test"/>`;
expect(RenderTypes.meta(metaNode, document.head))
.toHaveLength(2);
});
});

describe('httpequiv attribute', () => {
const metaNode: ChildNode = {
vtag: 'meta',
vattrs: {
httpequiv: 'foo',
content: 'bar'
},
vchildren: null,
vtext: null
};

it('should return one element without a match', () => {
expect(RenderTypes.meta(metaNode, document.head))
.toBeInstanceOf(HTMLElement);
});

it('should return two elements with a match', () => {
document.head.innerHTML = `<meta httpequiv="foo" content="test"/>`;
expect(RenderTypes.meta(metaNode, document.head))
.toHaveLength(2);
});
});

});

describe('link', () => {
Expand Down