diff --git a/demo/src/app.ts b/demo/src/app.ts index 16c53a5f..293713f1 100644 --- a/demo/src/app.ts +++ b/demo/src/app.ts @@ -54,7 +54,7 @@ const run = () => { worker.postMessage({'sentence': outputContainerElement.textContent, 'model': model}); const parser = parsers.get(model); if (!parser) return; - parser.applyElement(outputContainerElement); + parser.applyToElement(outputContainerElement); outputContainerElement.style.fontSize = `${fontSizeElement.value}rem`; const renderWithBR = brCheckElement.checked; if (renderWithBR) { diff --git a/java/src/main/java/com/google/budoux/HTMLProcessor.java b/java/src/main/java/com/google/budoux/HTMLProcessor.java index f35f8e4d..5797967d 100644 --- a/java/src/main/java/com/google/budoux/HTMLProcessor.java +++ b/java/src/main/java/com/google/budoux/HTMLProcessor.java @@ -24,12 +24,12 @@ import java.io.InputStreamReader; import java.io.Reader; import java.nio.charset.StandardCharsets; +import java.util.ArrayDeque; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Set; -import java.util.Stack; import java.util.stream.Collectors; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; @@ -63,7 +63,7 @@ private static class PhraseResolvingNodeVisitor implements NodeVisitor { private final StringBuilder output = new StringBuilder(); private Integer scanIndex = 0; private boolean toSkip = false; - private Stack elementStack = new Stack(); + private final ArrayDeque elementStack = new ArrayDeque<>(); /** * Constructs a PhraseResolvingNodeVisitor. @@ -126,7 +126,7 @@ public void tail(Node node, int depth) { if (node.nodeName().equals("body") || node instanceof TextNode) { return; } - assert node instanceof Element; + // assume node instanceof Element; toSkip = elementStack.pop(); output.append(String.format("", node.nodeName())); } diff --git a/javascript/README.md b/javascript/README.md index 18937493..77d974c5 100644 --- a/javascript/README.md +++ b/javascript/README.md @@ -72,13 +72,14 @@ You can also feed an HTML element to the parser to apply the process. const ele = document.querySelector('p.budou-this'); console.log(ele.outerHTML); //

今日はとても天気です。

-parser.applyElement(ele); +parser.applyToElement(ele); console.log(ele.outerHTML); //

今日は\u200bとても\u200b天気です。

``` -Internally, the `applyElement` calls the [`HTMLProcessor`]'s `applyToElement` +Internally, the `applyToElement` calls the [`HTMLProcessor`]'s `applyToElement` function with the zero-width space as the separator. + You can use the [`HTMLProcessor`] class directly if desired. For example: diff --git a/javascript/src/html_processor.ts b/javascript/src/html_processor.ts index 289341da..b990a648 100644 --- a/javascript/src/html_processor.ts +++ b/javascript/src/html_processor.ts @@ -603,10 +603,25 @@ export class HTMLProcessingParser extends Parser { } /** + * @deprecated Use `applyToElement` instead. `applyElement` will be removed + * in v0.7.0 to align the function name with `HTMLProcessor`'s API. + * * Applies markups for semantic line breaks to the given HTML element. * @param parentElement The input element. */ applyElement(parentElement: HTMLElement) { + console.warn( + '`applyElement` is deprecated. Please use `applyToElement` instead. ' + + '`applyElement` will be removed in v0.7.0.' + ); + this.applyToElement(parentElement); + } + + /** + * Applies markups for semantic line breaks to the given HTML element. + * @param parentElement The input element. + */ + applyToElement(parentElement: HTMLElement) { this.htmlProcessor.applyToElement(parentElement); } @@ -624,7 +639,7 @@ export class HTMLProcessingParser extends Parser { wrapper.append(...doc.body.childNodes); doc.body.append(wrapper); } - this.applyElement(doc.body.childNodes[0] as HTMLElement); + this.applyToElement(doc.body.childNodes[0] as HTMLElement); return doc.body.innerHTML; } } diff --git a/javascript/src/tests/test_html_processor.ts b/javascript/src/tests/test_html_processor.ts index 78302c4e..e2464a23 100644 --- a/javascript/src/tests/test_html_processor.ts +++ b/javascript/src/tests/test_html_processor.ts @@ -316,7 +316,7 @@ describe('HTMLProcessor.splitNodes', () => { }); }); -describe('HTMLProcessingParser.applyElement', () => { +describe('HTMLProcessingParser.applyToElement', () => { const checkEqual = ( model: {[key: string]: {[key: string]: number}}, inputHTML: string, @@ -325,7 +325,7 @@ describe('HTMLProcessingParser.applyElement', () => { const inputDOM = parseFromString(inputHTML); const inputDocument = inputDOM.querySelector('p') as HTMLElement; const parser = new HTMLProcessingParser(model); - parser.applyElement(inputDocument); + parser.applyToElement(inputDocument); const expectedDocument = parseFromString(expectedHTML); const expectedElement = expectedDocument.querySelector('p') as HTMLElement; expect(inputDocument.isEqualNode(expectedElement)).toBeTrue(); diff --git a/javascript/src/tests/test_webcomponents.ts b/javascript/src/tests/test_webcomponents.ts index 8177f8d0..1b81a218 100644 --- a/javascript/src/tests/test_webcomponents.ts +++ b/javascript/src/tests/test_webcomponents.ts @@ -33,7 +33,7 @@ describe('Web Components', () => { const mirroredElement = window.document.createElement('span'); mirroredElement.textContent = inputText; - parser.applyElement(mirroredElement); + parser.applyToElement(mirroredElement); expect(budouxElement.innerHTML).toBe(mirroredElement.outerHTML); }); @@ -46,7 +46,7 @@ describe('Web Components', () => { const inputText = '明日はどうなるかな。'; const mirroredElement = window.document.createElement('span'); mirroredElement.textContent = inputText; - parser.applyElement(mirroredElement); + parser.applyToElement(mirroredElement); const observer = new window.MutationObserver(() => { expect(budouxElement.innerHTML).toBe(mirroredElement.outerHTML);