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

add embeddedAttrJsExtractor #60

Merged
merged 14 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
39 changes: 39 additions & 0 deletions src/html/extractors/factories/embeddedAttributeJs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Attribute } from 'parse5';
import { JsParser } from '../../../js/parser';
import { Validate } from '../../../utils/validate';
import { Element, IHtmlExtractorFunction, Node } from '../../parser';

export type AttributePredicate = (attribute: Attribute) => boolean;

export function embeddedAttributeJsExtractor(jsParser: JsParser, filter: null | string | AttributePredicate): IHtmlExtractorFunction {
youthlin marked this conversation as resolved.
Show resolved Hide resolved
Validate.required.argument({ jsParser });
let test: AttributePredicate = attr => true;
if (typeof filter === 'string') {
const namePattern = filter;
test = (attr: Attribute) => {
if (attr.name.match(namePattern)) { return true; }
youthlin marked this conversation as resolved.
Show resolved Hide resolved
return false;
};
} else if (typeof filter === 'function') {
test = filter;
}

return (node: Node, fileName: string, _, lineNumberStart) => {
if (typeof (node as Element).tagName !== 'string') {
return;
}

const element = node as Element;

element.attrs.filter(test).forEach((attr) => {
const startLine = element.sourceCodeLocation?.attrs[attr.name]?.startLine;
if (startLine) {
lineNumberStart = lineNumberStart + startLine - 1;
}
jsParser.parseString(attr.value, fileName, {
lineNumberStart
});
});

};
}
4 changes: 3 additions & 1 deletion src/html/extractors/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { elementContentExtractor } from './factories/elementContent';
import { elementAttributeExtractor } from './factories/elementAttribute';
import { elementContentExtractor } from './factories/elementContent';
import { embeddedAttributeJsExtractor } from './factories/embeddedAttributeJs';
import { embeddedJsExtractor } from './factories/embeddedJs';

export abstract class HtmlExtractors {
public static elementContent: typeof elementContentExtractor = elementContentExtractor;
public static elementAttribute: typeof elementAttributeExtractor = elementAttributeExtractor;
public static embeddedJs: typeof embeddedJsExtractor = embeddedJsExtractor;
public static embeddedAttributeJs: typeof embeddedAttributeJsExtractor = embeddedAttributeJsExtractor;
}
24 changes: 24 additions & 0 deletions tests/e2e/fixtures/html/linenumberStart.expected.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
msgid ""
msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"

#: tests/e2e/fixtures/html/linenumberStart.html:13
msgid "%{n} apple"
msgid_plural "%{n} apples"
msgstr[0] ""
msgstr[1] ""

#: tests/e2e/fixtures/html/linenumberStart.html:16
msgid "msg 1"
msgstr ""

#: tests/e2e/fixtures/html/linenumberStart.html:18
msgid "msg 2"
msgstr ""

#: tests/e2e/fixtures/html/linenumberStart.html:12
msgctxt "title"
msgid "%{n} apple"
msgid_plural "%{n} apples"
msgstr[0] ""
msgstr[1] ""
10 changes: 10 additions & 0 deletions tests/e2e/fixtures/html/linenumberStart.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<template>
<span :title="_xn('title', '%{n} apple', '%{n} apples', n, {n:''+n})" data-name="__('whatever')">
{{ _n( '%{n} apple', '%{n} apples', n, {n:''+n}) }}
</span>
<div>
{{ __('msg 1') }}
<br>
{{ __('msg 2') }}
</div>
</template>
19 changes: 18 additions & 1 deletion tests/e2e/html.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GettextExtractor, HtmlExtractors, JsExtractors } from '../../dist';
import * as fs from 'fs';
import { GettextExtractor, HtmlExtractors, JsExtractors } from '../../dist';

describe('HTML E2E', () => {

Expand Down Expand Up @@ -59,4 +59,21 @@ describe('HTML E2E', () => {

expect(extractor.getPotString()).toBe(fs.readFileSync(__dirname + '/fixtures/html/embeddedJs.expected.pot').toString());
});

test('line number start 11', () => {
const extractor = new GettextExtractor();
const jsParser = extractor.createJsParser([
JsExtractors.callExpression('__', { arguments: { text: 0, } }),
JsExtractors.callExpression('_n', { arguments: { text: 0, textPlural: 1 } }),
JsExtractors.callExpression('_xn', { arguments: { context: 0, text: 1, textPlural: 2 } }),
]);
const htmlParser = extractor.createHtmlParser([
HtmlExtractors.embeddedAttributeJs(jsParser, '^:'),
HtmlExtractors.embeddedJs('*', jsParser),
youthlin marked this conversation as resolved.
Show resolved Hide resolved
]);
htmlParser.parseFile('tests/e2e/fixtures/html/linenumberStart.html', { lineNumberStart: 11 });
expect(extractor.getPotString())
.toBe(fs.readFileSync(__dirname + '/fixtures/html/linenumberStart.expected.pot').toString())
});

});
47 changes: 47 additions & 0 deletions tests/html/extractors/factories/embeddedAttributeJs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { embeddedAttributeJsExtractor } from '../../../../src/html/extractors/factories/embeddedAttributeJs';
import { HtmlParser } from '../../../../src/html/parser';
import { JsParser } from '../../../../src/js/parser';

describe('HTML: Attribute Value as Embedded JS Extractor', () => {

describe('calling js parser', () => {

let htmlParser: HtmlParser,
jsParserMock: JsParser;

beforeEach(() => {
jsParserMock = <any>{
parseString: jest.fn()
};

htmlParser = new HtmlParser(undefined!, [
embeddedAttributeJsExtractor(jsParserMock, ':title')
]);
});

test('single line', () => {
htmlParser.parseString(`<span :title="__('msg id')" title="__('another')">content</span>`, 'foo.html');
expect(jsParserMock.parseString).toHaveBeenCalledWith(`__('msg id')`, 'foo.html', {
lineNumberStart: 1
});
});

test('with lineNumberStart option', () => {
htmlParser.parseString(`<span :title="__('msg id')" :class="abc">content</span>`, 'foo.html', { lineNumberStart: 10 });
expect(jsParserMock.parseString).toHaveBeenCalledWith(`__('msg id')`, 'foo.html', {
lineNumberStart: 10
});
});

youthlin marked this conversation as resolved.
Show resolved Hide resolved
});

describe('argument validation', () => {

test('selector: (none)', () => {
youthlin marked this conversation as resolved.
Show resolved Hide resolved
expect(() => {
(<any>embeddedAttributeJsExtractor)();
}).toThrowError(`Missing argument 'jsParser'`);
});

});
});