如何使用Nokogiri获取倒数第二个脚本结束标记

问题描述 投票:0回答:2

我需要使用Nokogiri获取倒数第二个脚本结束标记。

示例代码:

<head>
    <script src="first.js"></script>
    <script src="second.js"></script>
    <!-- How to place some scripts here? -->
    <script>
      // init load
    </script>
</head>

我尝试过这样的代码doc.css('/html/head/script')[-2]。但是,它将代码放置在标签内。

ruby nokogiri
2个回答
0
投票

有一条CSS规则可“排除”某些元素:not。与first-childlast-child结合使用,您可以在head标签中省略第一个和最后一个脚本:

require 'nokogiri'

doc = <<-DOC
<head>
    <script src="Build/UnityLoader.js"></script>
    <!-- How to place some scripts here? -->
    <script src="second"></script>
    <script src="third"></script>
    <script>
      // init load
    </script>
</head>
DOC

p Nokogiri.parse(doc).css('head script:not(:first-child):not(:last-child)')
# [
#   #<Nokogiri::XML::Element:0x3fc9d142f4d4 name="script" attributes=[#<Nokogiri::XML::Attr:0x3fc9d142f358 name="src" value="second">]>,
#   #<Nokogiri::XML::Element:0x3fc9d142e73c name="script" attributes=[#<Nokogiri::XML::Attr:0x3fc9d142e250 name="src" value="third">]>
# ]

如果您需要在文档中插入元素,则可以使用atadd_next_sibling的组合。例如:

require 'nokogiri'

doc = <<-DOC
<head>
    <script src="Build/UnityLoader.js"></script>
    <!-- How to place some scripts here? -->
    <script>
      // init load
    </script>
</head>
DOC

parsed = Nokogiri.parse(doc)
parsed.at('head script').add_next_sibling('<script src="second"></script>')
p parsed.to_html

# <head>
#   <script src="Build/UnityLoader.js"></script>
#   <script src="second"></script>
#   <!-- How to place some scripts here? -->
#   <script>
#     // init load
#   </script>
# </head>

0
投票

尚不清楚您想要什么,因为您没有给我们带来预期的结果,但这似乎就像您在说的那样:

require 'nokogiri'

doc = Nokogiri::HTML(<<EOT)
<html>
    <head>
        <script src="first.js"></script>
        <script src="second.js"></script>
        <!-- How to place some scripts here? -->
        <script>
          // init load
        </script>
    </head>
</html>
EOT

doc.css('script')[-2].add_next_sibling("\n<script src='new_script.js'></script>")

将导致:

doc.to_html
# => "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n" +
#    "<html>\n" +
#    "    <head>\n" +
#    "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" +
#    "        <script src=\"first.js\"></script>\n" +
#    "        <script src=\"second.js\"></script>\n" +
#    "<script src=\"new_script.js\"></script>\n" +
#    "        <!-- How to place some scripts here? -->\n" +
#    "        <script>\n" +
#    "          // init load\n" +
#    "        </script>\n" +
#    "    </head>\n" +
#    "</html>\n"

Nokogiri的XML::Node documentation充满了有用的方法。我建议您多次阅读。

Nokogiri不知道关闭标签。解析之后,它知道存在一个对象,并且该对象在层次结构中具有同级,因此我们可以搜索这些对象,然后在这种情况下,插入一个新节点。如果您要求它输出HTML,则基于HTML的规则,它将提供结束标记,即使它们最初不在此也没有。

© www.soinside.com 2019 - 2024. All rights reserved.