From f96ef657416855523cdc9a347139b0b4095b71bf Mon Sep 17 00:00:00 2001 From: xiaoqidun Date: Fri, 9 Oct 2026 23:30:39 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=A0=87=E5=87=86=E6=94=AF=E6=8C=81):=20?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E5=91=BD=E5=90=8D=E7=A9=BA=E9=97=B4=E5=8C=B9?= =?UTF-8?q?=E9=85=8D=E4=B8=8E=E5=85=BC=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ofdgo_editor_action.go | 2 +- ofdgo_editor_annot.go | 6 +++--- ofdgo_editor_annot_content.go | 8 ++++---- ofdgo_editor_attachment.go | 2 +- ofdgo_editor_composite_copy.go | 2 +- ofdgo_editor_metadata.go | 2 +- ofdgo_editor_namespace.go | 7 +++++++ ofdgo_editor_outline.go | 8 ++++---- ofdgo_editor_package.go | 4 ++-- ofdgo_editor_xml.go | 6 +++--- ofdgo_package.go | 11 ++--------- ofdgo_xml.go | 2 +- 12 files changed, 30 insertions(+), 30 deletions(-) diff --git a/ofdgo_editor_action.go b/ofdgo_editor_action.go index 781fdd6..f2c1958 100644 --- a/ofdgo_editor_action.go +++ b/ofdgo_editor_action.go @@ -68,7 +68,7 @@ func (e *Editor) SetDocumentActions(actions []Action) error { context = node } else { for _, child := range root.children { - if packageOFDNode(child, child.name.Local) && slices.Contains([]string{"VPreferences", "Bookmarks", "Annotations", "CustomTags", "Attachments", "Extensions"}, child.name.Local) { + if child.matchesOFD(child.name.Local) && slices.Contains([]string{"VPreferences", "Bookmarks", "Annotations", "CustomTags", "Attachments", "Extensions"}, child.name.Local) { patch.start, patch.end = child.start, child.start break } diff --git a/ofdgo_editor_annot.go b/ofdgo_editor_annot.go index 5f76c26..5093fc4 100644 --- a/ofdgo_editor_annot.go +++ b/ofdgo_editor_annot.go @@ -181,7 +181,7 @@ func (e *Editor) editAnnotations(index int, ids []string, edit func([]byte, *edi found := make(map[string]bool, len(ids)) remaining := 0 for _, ref := range root.children { - if !packageOFDNode(ref, "Page") { + if !ref.matchesOFD("Page") { continue } remaining++ @@ -207,7 +207,7 @@ func (e *Editor) editAnnotations(index int, ids []string, edit func([]byte, *edi var changes []editorXMLPatch count := 0 for _, node := range page.children { - if !packageOFDNode(node, "Annot") { + if !node.matchesOFD("Annot") { continue } count++ @@ -235,7 +235,7 @@ func (e *Editor) editAnnotations(index int, ids []string, edit func([]byte, *edi } sharedFile := false for _, other := range root.children { - if other == ref || !packageOFDNode(other, "Page") || other.child("FileLoc") == nil { + if other == ref || !other.matchesOFD("Page") || other.child("FileLoc") == nil { continue } shared, err := editorPageLocation(reader, nil, name, strings.TrimSpace(editorImportText(data, other.child("FileLoc")))) diff --git a/ofdgo_editor_annot_content.go b/ofdgo_editor_annot_content.go index 996abbe..c7d6052 100644 --- a/ofdgo_editor_annot_content.go +++ b/ofdgo_editor_annot_content.go @@ -176,7 +176,7 @@ func (e *Editor) annotationXML(index int, id string) ([]byte, error) { } var result []byte for _, ref := range root.children { - if !packageOFDNode(ref, "Page") || ref.attr("PageID") != e.pages[index].ID || ref.child("FileLoc") == nil { + if !ref.matchesOFD("Page") || ref.attr("PageID") != e.pages[index].ID || ref.child("FileLoc") == nil { continue } file, err := editorPageLocation(reader, nil, name, strings.TrimSpace(editorImportText(data, ref.child("FileLoc")))) @@ -192,7 +192,7 @@ func (e *Editor) annotationXML(index int, id string) ([]byte, error) { return nil, err } for _, node := range page.children { - if !packageOFDNode(node, "Annot") || node.attr("ID") != id { + if !node.matchesOFD("Annot") || node.attr("ID") != id { continue } if result != nil { @@ -248,7 +248,7 @@ func (e *Editor) appendAnnotations(index int, annotations []byte, generated bool return err } for _, item := range root.children { - if packageOFDNode(item, "Page") && item.attr("PageID") == e.pages[index].ID { + if item.matchesOFD("Page") && item.attr("PageID") == e.pages[index].ID { ref = item loc := item.child("FileLoc") if loc == nil { @@ -285,7 +285,7 @@ func (e *Editor) appendAnnotations(index int, annotations []byte, generated bool file := existing if file != "" { for _, other := range root.children { - if other == ref || !packageOFDNode(other, "Page") || other.child("FileLoc") == nil { + if other == ref || !other.matchesOFD("Page") || other.child("FileLoc") == nil { continue } shared, err := editorPageLocation(reader, nil, name, strings.TrimSpace(editorImportText(data, other.child("FileLoc")))) diff --git a/ofdgo_editor_attachment.go b/ofdgo_editor_attachment.go index 8314a39..a73cd82 100644 --- a/ofdgo_editor_attachment.go +++ b/ofdgo_editor_attachment.go @@ -217,7 +217,7 @@ func (e *Editor) editAttachment(id string, change func([]byte, *editorXML, map[s var found *editorXML if root != nil { for _, child := range root.children { - if packageOFDNode(child, "Attachment") && child.attr("ID") == id { + if child.matchesOFD("Attachment") && child.attr("ID") == id { if found != nil { return fmt.Errorf("duplicate attachment ID: %s", id) } diff --git a/ofdgo_editor_composite_copy.go b/ofdgo_editor_composite_copy.go index e853abb..ee171b2 100644 --- a/ofdgo_editor_composite_copy.go +++ b/ofdgo_editor_composite_copy.go @@ -347,7 +347,7 @@ func (n *editorCompositeNode) preserveActionRegions(matrix Matrix) (bool, error) } var targets []*editorXML for _, action := range actions.children { - if action.name.Local != "Action" || action.name.Space != "" && action.name.Space != actions.name.Space && action.name.Space != ofdNamespace2016 { + if !action.matchesOFD("Action") { continue } if action.attr("Event") == "CLICK" && action.child("Region") == nil { diff --git a/ofdgo_editor_metadata.go b/ofdgo_editor_metadata.go index bb418aa..81c8646 100644 --- a/ofdgo_editor_metadata.go +++ b/ofdgo_editor_metadata.go @@ -123,7 +123,7 @@ func editorCustomDatas(data []byte, info *editorXML, values *CustomDatas) ([]byt var nodes []*editorXML if container != nil { for _, node := range container.children { - if node.name.Local == "CustomData" && (node.name.Space == ofdNamespace2016 || node.name.Space == container.name.Space) { + if node.matchesOFD("CustomData") { nodes = append(nodes, node) byName[node.attr("Name")] = append(byName[node.attr("Name")], node) } diff --git a/ofdgo_editor_namespace.go b/ofdgo_editor_namespace.go index 46c098e..0b04f32 100644 --- a/ofdgo_editor_namespace.go +++ b/ofdgo_editor_namespace.go @@ -20,6 +20,13 @@ import ( "strconv" ) +// matchesOFD 匹配OFD节点,兼容与父节点一致的未知命名空间 +// 入参: name 节点名称 +// 返回: bool 是否匹配 +func (n *editorXML) matchesOFD(name string) bool { + return n.name.Local == name && (classifyOFDNamespace(n.name.Space) != ofdXMLUnknown || n.parent != nil && n.name.Space == n.parent.name.Space) +} + // textXML 按所在节点的命名空间编码文本节点 // 入参: name 节点名称, value 文本 // 返回: []byte XML片段 diff --git a/ofdgo_editor_outline.go b/ofdgo_editor_outline.go index 7b10495..21edc67 100644 --- a/ofdgo_editor_outline.go +++ b/ofdgo_editor_outline.go @@ -119,10 +119,10 @@ func (e *Editor) UpdateOutline(path []int, title string, page int) error { if actions != nil { var removals []editorXMLPatch for _, action := range actions.children { - if action.name.Space != actions.name.Space && action.name.Space != ofdNamespace2016 && action.name.Space != "" { + if !action.matchesOFD("Action") { continue } - if action.name.Local == "Action" && action.child("Goto") != nil { + if action.child("Goto") != nil { removals = append(removals, editorXMLPatch{action.start - actions.open, action.end - actions.open, nil}) } } @@ -315,7 +315,7 @@ func (e *Editor) outlineAction(page int) []byte { func editorOutlineChildren(node *editorXML) []*editorXML { var children []*editorXML for _, child := range node.children { - if child.name.Local == "OutlineElem" && (child.name.Space == node.name.Space || child.name.Space == ofdNamespace2016 || child.name.Space == "") { + if child.matchesOFD("OutlineElem") { children = append(children, child) } } @@ -425,7 +425,7 @@ func (e *Editor) withOutlines(data []byte) ([]byte, error) { walk = func(node *editorXML) { if actions := node.child("Actions"); actions != nil { for _, action := range actions.children { - if action.name.Local != "Action" || action.name.Space != actions.name.Space && action.name.Space != ofdNamespace2016 && action.name.Space != "" { + if !action.matchesOFD("Action") { continue } if target := action.child("Goto"); target != nil { diff --git a/ofdgo_editor_package.go b/ofdgo_editor_package.go index 7ed4e06..da01244 100644 --- a/ofdgo_editor_package.go +++ b/ofdgo_editor_package.go @@ -178,7 +178,7 @@ func (e *Editor) sourcePartsPrepared(progress editorProgress, generated map[stri } originals := make(map[string]*editorXML, len(pages.children)) for _, child := range pages.children { - if packageOFDNode(child, "Page") { + if child.matchesOFD("Page") { id := child.attr("ID") if originals[id] == nil { originals[id] = child @@ -200,7 +200,7 @@ func (e *Editor) sourcePartsPrepared(progress editorProgress, generated map[stri } position := 0 for _, child := range pages.children { - if !packageOFDNode(child, "Page") { + if !child.matchesOFD("Page") { continue } var entry []byte diff --git a/ofdgo_editor_xml.go b/ofdgo_editor_xml.go index 12e8cab..708536f 100644 --- a/ofdgo_editor_xml.go +++ b/ofdgo_editor_xml.go @@ -92,7 +92,7 @@ func (n *editorXML) child(name string) *editorXML { // 返回: *editorXML 节点,不存在时为nil func (n *editorXML) childAt(name string, index int) *editorXML { for _, child := range n.children { - if child.name.Local == name && (child.name.Space == n.name.Space || child.name.Space == ofdNamespace2016 || child.name.Space == "") { + if child.matchesOFD(name) { if index == 0 { return child } @@ -512,7 +512,7 @@ func editorXMLMergeNamespace(data []byte, node *editorXML, oldXML, newXML []byte matching := len(node.children) > 0 && len(node.children) == len(oldNode.children) && len(node.children) == len(newNode.children) if matching { for i, child := range node.children { - if !packageOFDNode(child, oldNode.children[i].name.Local) || child.name.Local != newNode.children[i].name.Local { + if !child.matchesOFD(oldNode.children[i].name.Local) || child.name.Local != newNode.children[i].name.Local { matching = false break } @@ -567,7 +567,7 @@ func editorXMLMergeContent(data []byte, node *editorXML, oldXML []byte, oldNode groups := func(root *editorXML, encoded bool) map[string][]*editorXML { result := make(map[string][]*editorXML) for _, child := range root.children { - if encoded || packageOFDNode(child, child.name.Local) { + if encoded || child.matchesOFD(child.name.Local) { result[child.name.Local] = append(result[child.name.Local], child) } } diff --git a/ofdgo_package.go b/ofdgo_package.go index 0f6c0fd..835b476 100644 --- a/ofdgo_package.go +++ b/ofdgo_package.go @@ -38,13 +38,6 @@ func packagePagePath(directory, id string) string { return path.Join(directory, "Pages", "Page_"+id, "Content.xml") } -// packageOFDNode 识别包索引中的OFD节点,兼容与父节点同命名空间的已知字段 -// 入参: node XML节点, name 节点名称 -// 返回: bool 是否为OFD节点 -func packageOFDNode(node *editorXML, name string) bool { - return node.name.Local == name && (classifyOFDNamespace(node.name.Space) != ofdXMLUnknown || node.parent != nil && node.name.Space == node.parent.name.Space) -} - // packageName 为新增条目分配文档内路径,不覆盖原文件或本次资源 // 入参: relative 文档相对路径 // 返回: string 未占用路径 @@ -125,7 +118,7 @@ func mergePackageResources(reader *Reader, parts map[string][]byte, doc *Documen var content []byte for _, node := range group.children { if target != nil && slices.ContainsFunc(target.children, func(old *editorXML) bool { - return packageOFDNode(old, node.name.Local) && old.attr("ID") == node.attr("ID") + return old.matchesOFD(node.name.Local) && old.attr("ID") == node.attr("ID") }) { continue } @@ -153,7 +146,7 @@ func mergePackageResources(reader *Reader, parts map[string][]byte, doc *Documen } position := root.close for _, child := range root.children { - if packageOFDNode(child, child.name.Local) && slices.Index(order, child.name.Local) > slices.Index(order, group.name.Local) { + if child.matchesOFD(child.name.Local) && slices.Index(order, child.name.Local) > slices.Index(order, group.name.Local) { position = child.start break } diff --git a/ofdgo_xml.go b/ofdgo_xml.go index ba48613..805f1da 100644 --- a/ofdgo_xml.go +++ b/ofdgo_xml.go @@ -28,7 +28,7 @@ const ( ofdXML2016 ) -// ofdXMLNamespace 标识XML节点采用的OFD命名空间 +// ofdXMLNamespace 标识XML命名空间,不代表文档版本或功能支持范围 type ofdXMLNamespace uint8 // classifyOFDNamespace 识别OFD命名空间,未声明与未知版本分别处理