Skip to content

Commit

Permalink
Merge branch 'main' into zwsp-readme
Browse files Browse the repository at this point in the history
  • Loading branch information
tushuhei authored Oct 30, 2023
2 parents 4d8bfe7 + dbb8837 commit e2c0463
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion demo/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions java/src/main/java/com/google/budoux/HTMLProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Boolean> elementStack = new Stack<Boolean>();
private final ArrayDeque<Boolean> elementStack = new ArrayDeque<>();

/**
* Constructs a PhraseResolvingNodeVisitor.
Expand Down Expand Up @@ -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("</%s>", node.nodeName()));
}
Expand Down
5 changes: 3 additions & 2 deletions javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
// <p class="budou-this">今日は<b>とても天気</b>です。</p>
parser.applyElement(ele);
parser.applyToElement(ele);
console.log(ele.outerHTML);
// <p class="budou-this" style="word-break: keep-all; overflow-wrap: anywhere;">今日は<b>\u200bとても\u200b天気</b>です。</p>
```

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:

Expand Down
17 changes: 16 additions & 1 deletion javascript/src/html_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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;
}
}
4 changes: 2 additions & 2 deletions javascript/src/tests/test_html_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ describe('HTMLProcessor.splitNodes', () => {
});
});

describe('HTMLProcessingParser.applyElement', () => {
describe('HTMLProcessingParser.applyToElement', () => {
const checkEqual = (
model: {[key: string]: {[key: string]: number}},
inputHTML: string,
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions javascript/src/tests/test_webcomponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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);
Expand Down

0 comments on commit e2c0463

Please sign in to comment.